tags:

views:

154

answers:

5

Hi All,

If some one can point me in the right direction for this code for my assigment I would really appreciate it.

I have pasted the whole code that I need to complete but I need help with the following method public void changeColour(Circle aCircle) which is meant to allow to change the colour of the circle randomly, if 0 comes the light of the circle should change to red, 1 for green and 2 for purple.

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

   /**
    * Default constructor for objects of class DiscoLight
    */
   public DiscoLight()
   {
      super();
      this.randomNumberGenerator = new Random();           
   }


   /**
    * 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);
   }


   /** 
    * student to write code and comment here for setLight(Circle) for Q4(i)
    */
   public void setLight(Circle aCircle)
   {
       this.light = aCircle;

   }


   /**
    * student to write code and comment here for getLight() for Q4(i)
    */
   public Circle getLight()
   {
       return this.light;
   }

   /** 
    * Sets the argument to have a diameter of 50, an xPos 
    * of 122, a yPos of 162 and the colour GREEN.
    * The method then sets the receiver's instance variable
    * light, to the argument aCircle.
    */ 
   public void addLight(Circle aCircle)
   {
      //Student to write code here, Q4(ii)
      this.light = aCircle;
      this.light.setDiameter(50);
      this.light.setXPos(122);
      this.light.setYPos(162);
      this.light.setColour(OUColour.GREEN);
   }


   /** 
    * Randomly sets the colour of the instance variable 
    * light to red, green, or purple.
    */  
   public void changeColour(Circle aCircle)
   {
       //student to write code here, Q4(iii)
       if (getRandomInt() == 0)
       {
           this.light.setColour(OUColour.RED);
       }
       if (this.getRandomInt().equals(1))
       {
           this.light.setColour(OUColour.GREEN);
       }
       else
       if (this.getRandomInt().equals(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)
   {   
       //student to write code here, Q4(iv) 
   }


   /** 
    * 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)
   {   
       //student to write code here, Q4(v)
   }


   /** 
    * 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)
   {
      //student to write code here, Q4(vi)
   }


   /** 
    * 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()
   {  
    //student to write code here, Q4(vii)
   }  


   /**
    * 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);
      } 
   }

}
+1  A: 

You have forgotten to pass a parameter when calling getRandomInt() in the very first line below //student to write code here. Your compiler should already point this out, though.

The documentation comment above the getRandomInt() method tells you what it expects as parameter, and what you can expect as return value.

Also, if you want equal chances that the lamp is red, green or purple, you should be able to do that with a single invocation of getRandomInt(). Store the value in a variable, and use a switch statement to turn on the correct light:

int randomValue = getRandomInt(/* I am not telling you what to put here */);

switch (randomValue) {
    case 0: light.setColour(OUColour.RED); break;
    case 1: light.setColour(OUColour.GREEN); break;
    case 2: light.setColour(OUColour.PURPLE); break;
}
Jørn Schou-Rode
Hey thank you very much for your guidance
luvthug
A: 

the method getRandomInt returns an int, so you can't use equals to compare. use:

if (getRandomInt(3) == 1) {
  ...
}

and you just need to call it once. store the random integer in a variable and compare its value with the ones you want.

cd1
Hey thank you very much for your guidance
luvthug
A: 

Hello,

you're calling getRandomInt several times, with each time a new (random) value returned. you should call it once at the begin of the method, and then check if it is 0,1 or 2.

Regards Guillaume

PATRY
thank you for your help and advice I have manage to finish the whole code
luvthug
A: 
 /** 
    * Randomly sets the colour of the instance variable 
    * light to red, green, or purple.
    */  
   public void changeColour(Circle aCircle)
   {
       int i = getRandomInt(3);
       if (i == 0)
       {
           this.light.setColour(OUColour.RED);
       }
       else if (i == 1)
       {
           this.light.setColour(OUColour.GREEN);
       }
       else
       {
            this.light.setColour(OUColour.PURPLE);
       }
   }
Gandalf
Hey thank you very much for your guidance
luvthug
Hi Gandalf,for next part I have done the following public void grow(int size) { while (int i = 0; i < size; i++) { this.aCircle.aDiameter(i) = aDiameter + 2; this.aCircle.xPos(i) = xPos - 1; this.aCircle.yPos(i) = yPos - 1; } } but it keeps giving '.class' exception error.do you know what I am doing wrong here.
luvthug
What's the exact error?
Gandalf
'.class' exception when i try to compile it in program called BlueJ
luvthug
hey mate if you get a chance can yuo have a look at this its the same code as yesterday I have tried different things of still can't get it to work public void grow(int size) { while (int i = 0; i < size.length; i++) { this.aCircle.aDiameter(i) = aDiameter + 2; this.aCircle.xPos(i) = xPos - 1; this.aCircle.yPos(i) = yPos - 1; } }
luvthug
[public void grow(int size) { while (int i = 0; i < size.length; i++) { this.aCircle.aDiameter(i) = aDiameter + 2; this.aCircle.xPos(i) = xPos - 1; this.aCircle.yPos(i) = yPos - 1; } }]
luvthug
thank you for your help and advice I have manage to finish the whole code
luvthug
A: 

Do you want us to do your homework ? xD

This is easy you can do it yourself and it's better to learn :)

Sebastien Lorber
thank you for your help and advice I have manage to finish the rest of the code and by the way I didn't want you to do my home work just poing me inn th right direction, I didn't just ask becuase I was not bothered I was at this for few days was obivisly missing something simple. You have experince thats y you find it easy.
luvthug