2010-04-07 2 views
0

Si quelqu'un peut me diriger dans la bonne direction pour ce code pour mon affectation, je l'apprécierais vraiment.Classe publique Aide DiscoLight

J'ai collé tout le code que j'ai besoin de compléter mais j'ai besoin d'aide avec la méthode publique vide changeColour(Circle aCircle) qui est censée permettre de changer la couleur du cercle aléatoirement, si 0 vient la lumière du cercle devrait changer au rouge, 1 pour le vert et 2 pour le violet.

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

} 
+0

Vous pouvez utiliser des blocs de code pour rendre votre message plus facile à lire ... indent chaque ligne avec du code avec 4 espaces et vous aurez plus de chances d'obtenir une réponse :) – ChrisR

+0

Vous avez épelé la couleur fausse. – Gandalf

+1

Gandalf, c'est comme ça que nous l'épelons de ce côté de l'étang ... – James

Répondre

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

Hey merci beaucoup pour vos conseils – luvthug

+0

Salut Gandalf, pour une partie suivante, je l'ai fait ce qui suit public void croître (int size) { en (int i = 0; i luvthug

+0

Quelle est l'erreur exacte? – Gandalf

1

Vous avez oublié de passer un paramètre lors de l'appel getRandomInt() dans la première ligne ci-dessous //student to write code here. Votre compilateur devrait déjà le signaler, cependant.

Le commentaire de documentation ci-dessus la méthode getRandomInt() vous indique ce qu'il attend en tant que paramètre, et ce que vous pouvez attendre que la valeur de retour. De plus, si vous voulez avoir la même chance que la lampe soit rouge, verte ou violette, vous devriez pouvoir le faire avec une seule invocation de getRandomInt(). Conservez la valeur dans une variable et utiliser une instruction interrupteur pour allumer la lumière correcte:

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; 
} 
+0

Hé merci beaucoup pour vos conseils – luvthug

0

la méthode getRandomInt retourne un int, de sorte que vous ne pouvez pas utiliser equals à comparer. utiliser:

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

et vous avez juste besoin de l'appeler une seule fois. stockez l'entier aléatoire dans une variable et comparez sa valeur avec celles que vous voulez.

+0

Hé merci beaucoup pour vos conseils – luvthug

0

vous appelez getRandomInt à plusieurs reprises, chaque fois qu'une nouvelle valeur (aléatoire) est revenu. vous devriez l'appeler une fois au début de la méthode, puis vérifier si elle est 0,1 ou 2.

Cordialement Guillaume

+0

merci vous pour votre aide et des conseils, je ne parviennent à terminer tout le code – luvthug

Questions connexes