Hello everybody, could someone please urgent help me with this assignment question?
I just need some tips on why my prompt dialog is not working correctly.
The script is below after the assignment question...

Finish the coding of the class LightController by completing the public instance method runLight(). This method should first prompt the user to enter the number of times the disco light is to grow and shrink. Then the method should prompt the user for the size increase which they want the diameter of the disco light to grow by. The size increase provided by the user should be an even number although it is not required that your method should check this. The method then causes the circle referenced by light to perform the required number of growing and shrinking cycles.

import ou.*;  
    import java.util.*;  

    /** 
    * Class LightController 
    * This class uses the Circle class, and the Shapes window  
    * to simulate a disco light, that grows and shrinks and  
    * changes colour.  
    * @author M250 Module Team 
    * @version 1.0 
    */  
    public class LightController  
  {  

    /* instance variables */  
    private Circle light;   // simulates a circular disco light in the Shapes window  
    private Random randomNumberGenerator;  

    /** 
    * Default constructor for objects of class LightController. 
    */  
    public LightController()  
    {  
      super();  
      this.randomNumberGenerator = new Random();   
      light = new Circle();  
      light.setColour(OUColour.GREEN);  
      light.setDiameter(50);  
      light.setXPos(122);  
      light.setYPos(162);  
     }  

     /** 
     * Returns a randomly generated int between 0 (inclusive)  
     * and number (exclusive). For example if number is 6, 
     * the method will return one of 0, 1, 2, 3, 4, or 5. 
     */  
     public int getRandomInt(int number)  
     {  
      return this.randomNumberGenerator.nextInt(number);  
     }  

    /** 
     * Returns the instance variable, light. 
     */  
     public Circle getLight()  
     {  
       return this.light;  
    }  

     /**  
     * Randomly sets the colour of the instance variable  
     * light to red, green, or purple. 
     */    
     public void changeColour()  
     {  
        this.getRandomInt(3);  
        int l = getRandomInt(3);  
        if (l == 0)  
        {  
           this.light.setColour(OUColour.RED);  
        }  
        else if (l == 1)  
        {  
           this.light.setColour(OUColour.GREEN);  
        }  
        else if (l == 2)  
        {  
            this.light.setColour(OUColour.PURPLE);  
        }  
        }  

   /** 
    * Grows the diameter of the circle referenced by the 
    * receiver's instance variable light, to the argument size.  
    * The diameter is incremented in steps of 2,
    * the xPos and yPos are decremented in steps of 1 until the
    * diameter reaches the value given by size. 
    * Between each step there is a random colour change.  The message 
    * delay(anInt) is used to slow down the graphical interface, as required.
    */   
   public void grow(int size)
   {   
       int curDiameter = this.light.getDiameter();
       while(curDiameter < size)
       {
          this.light.setDiameter(curDiameter + 2);
          this.light.setXPos(this.light.getXPos() - 1);
          this.light.setYPos(this.light.getYPos() - 1);
          this.changeColour();
          curDiameter += 2;
          this.delay(100);
       }
   }

   /** 
    * Shrinks the diameter of the circle referenced by the 
    * receiver's instance variable light, to the argument size.  
    * The diameter is decremented in steps of 2,
    * the xPos and yPos are incremented in steps of 1 until the
    * diameter reaches the value given by size. 
    * Between each step there is a random colour change.  The message 
    * delay(anInt) is used to slow down the graphical interface, as required.
    */     
   public void shrink(int size)
   {   
       int curDiameter = this.light.getDiameter();
       while(curDiameter > size)
       {
          this.light.setDiameter(curDiameter - 2);
          this.light.setXPos(this.light.getXPos() + 1);
          this.light.setYPos(this.light.getYPos() + 1);
          this.changeColour();
          curDiameter -=2;
          this.delay(100);
       }
   }

   /** 
    * Expands the diameter of the light by the amount given by
    * sizeIncrease (changing colour as it grows).
    * 
    * The method then contracts the light until it reaches its
    * original size (changing colour as it shrinks).
    */     
   public void lightCycle(int sizeIncrease)
   {
      int oldDiameter = this.light.getDiameter();
      this.grow(sizeIncrease);
      this.shrink(oldDiameter);
   }

   /** 
    * Prompts the user for number of growing and shrinking
    * cycles. Then prompts the user for the number of units
    * by which to increase the diameter of light.
    * Method then performs the requested growing and 
    * shrinking cycles.
    */     
   public void runLight(int numCycles,int sizePerCycle)
   {  
      for (int doneCycles = 0; doneCycles <= numCycles; doneCycles++)
      {
         this.lightCycle(sizePerCycle);
      }
   }


   /**
    * Causes execution to pause by time number of milliseconds.
    */
   private void delay(int time)
   {
      try
      {
         Thread.sleep(time); 
      }
      catch (Exception e)
      {
         System.out.println(e);
      } 
   }  
}

Recommended Answers

All 6 Replies

what prompt dialog ?
what should it do, and what is it (not) doing ?

Hi Sorry I know this looks very confusing.
I am trying to find out how to create a dialog box using the information given below.
At first it must ask for a number promt then followed by a size incease prompt.
Then all this must make my circle perform the requests in my graphical display.

/** 
    * Prompts the user for number of growing and shrinking
    * cycles. Then prompts the user for the number of units
    * by which to increase the diameter of light.
    * Method then performs the requested growing and 
    * shrinking cycles.
    */     
   public void runLight(int numCycles,int sizePerCycle)
   {  
      for (int doneCycles = 0; doneCycles <= numCycles; doneCycles++)
      {
         this.lightCycle(sizePerCycle);
      }
   }

This Oracle tutorial tells you how to create a dialog that prompts the user and gets the user's input.

Thank you for your reply, I have looked through this and can't really see which section would help me, any pointers? Im really struggling.

I have a code which is lc.runlight();

this is to used in the code pane to execute the action, but nothing happens all i get is the following error message...

Compilation failed (19/01/2015 18:29:49)
Error: line 1 - method runLight in class LightController cannot be applied to given types;
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length

Any help would be greatly appreciated.

I have a code which is lc.runlight();

It has no parameters. public void runLight(int numCycles,int sizePerCycle) requires two integers.

I have looked through this and can't really see which section would help me

Start at the top, read until you're at the bottom. It's a good tutorial telling you exactly how to create different Dialogs, including the one you need for your assignment.

robert.swaine: seeing that the title of that tutorial is (and I quote) "How to Make Dialogs" the entire thing would help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.