2010-04-07 47 views
0

如果有人能指出我爲這個代碼的正確方向爲我的assigment我真的很感激。公共課DiscoLight幫助

我已經粘貼了需要完成的整個代碼,但我需要以下方法的幫助:public void changeColour(Circle aCircle)這意味着允許隨機更改圓的顏色,如果爲0,圓的亮度應該改變紅色,1爲綠色,2爲紫色。

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

您可能需要使用一些代碼塊,使您的文章更容易閱讀...用4個空格縮進每行代碼,你將更有可能得到答案:) – ChrisR 2010-04-07 14:29:03

+0

你拼寫錯誤。 – Gandalf 2010-04-07 14:33:07

+1

甘道夫,這就是我們如何拼寫它在池塘的這一邊... – James 2010-04-07 14:37:46

回答

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

嘿非常感謝你對你的指導 – luvthug 2010-04-07 14:59:04

+0

嗨甘道夫, 爲下一部分,我做了以下 公共無效成長(INT大小) { 而(int i = 0; i luvthug 2010-04-07 18:16:48

+0

確切的錯誤是什麼? – Gandalf 2010-04-07 18:59:45

1

你已經忘記調用下面//student to write code here的第一行getRandomInt()時傳遞參數。不過,你的編譯器應該已經指出了這一點。

getRandomInt()方法上方的文檔註釋告訴您它期望什麼作爲參數,以及您可以期待什麼作爲返回值。另外,如果您希望燈具爲紅色,綠色或紫色的機會相等,則您應該可以通過單一調用getRandomInt()來完成此操作。在一個變量中存儲的值,使用switch語句來打開正確的光:

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

非常感謝您的指導 – luvthug 2010-04-07 15:00:05

0

getRandomInt返回int的方法,所以你不能使用equals比較。使用:

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

你只需要調用一次。將隨機整數存儲在變量中,並將其值與您想要的值進行比較。

+0

嘿非常感謝您的指導 – luvthug 2010-04-07 14:59:40

0

你幾次調用getRandomInt,每次都返回一個新的(隨機)值。 你應該在開始方法的調用它一次,然後檢查它是否是0,1或2

問候 紀堯姆

+0

謝謝您的幫助和建議,我已經設法完成整個代碼 – luvthug 2010-04-08 15:49:38