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