This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Creating a working traffic light!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Creating a working traffic light!!

Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
Hello. I am new to JAVA and I am really confused by this problem that I am trying to work on..I am supposed to write a program using BlueJ to create a stoplight with a black rectangle as its base and three individual lamps inside, a red, yellow and green lamp that each turn on and off and when they turn off and another color turns on...they must return to being black. I have no idea how to go about this...all I know is that these methods should be used..can someone please help me get on the right track? I am supposed to create a Lamp class and a TrafficLight class...and the MyApp class is as below:
import OOPS.SP.*;
public class MyApp extends OOPS.SP.Frame
{
private Simulation _light;
public MyApp()
{

// initialize instance variables
_light = new Simulation();
_light.runSimulation(30);
}

/**
* Method to run the program
*/
public static void main( String[] argv)
{
new MyApp();
}
}
And this is the Simulation class...
public class Simulation
{
// instance variables
private TrafficLight _light1;
private TrafficLight _light2;
/**
* Constructor for objects of class Simulation
*/
public Simulation()
{

int startCycleTime, timeLightStaysOn;
// initialize instance variables
_light1=new TrafficLight(50,50);
_light2=new TrafficLight(250,50);
_light1.setCycleTimes(7,2,5);
_light2.setCycleTimes(10,3,7);
}
/**
* An method to run the simulation for the specified time
*
* @param time amount of time to run simulation
*/
public void runSimulation(int time)
{
int startCycleTime, timeLightStaysOn;
for (int currTime=1; currTime<time; currTime++)
{
startCycleTime = _light1.getWhenLastChanged();
timeLightStaysOn = _light1.getTimeOn();
if ((currTime-startCycleTime) >= timeLightStaysOn)
_light1.cycle(currTime);
startCycleTime = _light2.getWhenLastChanged();
timeLightStaysOn = _light2.getTimeOn();
if ((currTime-startCycleTime) >= timeLightStaysOn)
_light2.cycle(currTime);
}
}
}
Now I am supposed to create a Lamp class with the following in it...
Class Lamp
Instance variables:
�The color of the light bulb, or glass cover of the light bulb. It should be of type java.awt.Color.
�The circle that represents the light bulb. It will be black when turned off and the specified color when turned on.
�An int to specify how long the light should stay on. Note: for realism the time the red lamp is on should be the sum of the times for the yellow and the green lamps.
Methods:
�Lamp() � A default constructor which places the lamp at the center or the frame. The lamp is red, and the lamp is set to off. The time on is set to 1.
�Lamp(int x, int y) � A constructor, which sets the lamp to an arbitrary color, instantiates the circle, specifies that the lamp is off, and that the time on is 1, and the next lamp to itself, or 0. The lamp is placed at position (x,y), where x,y defines the position of the upper left corner of a square which circumscribes the circle.
�Lamp(java.awt.Color, int x, int y) - A constructor, with a parameter for the color which stores the color of the lamp, and creates a lamp which is on and of that color, the time is again set to 1, and the next lamp is set to itself or 0.
�void turnOn() - A method to turn the lamp on.
�void turnOff() - A method to turn the lamp off.
�void setTimeOn(int) - A method to set the value of the time the light is on, again it will take one parameter, which is the time to be used.
�int getTimeOn() - A method to return the time the light is to be on
And then the trafficLight class with the following...
Class TrafficLight
The traffic light consists of a black box that holds three lamps (from top to bottom: red, yellow, green).


Instance variables:
The black box for the traffic light.
The three instance variables of type Lamp, one for the red light, one for the yellow light, and one for the green light.
The time the light last changed, which is an int.
The lamp that is on, which can be an int, a java.awt.Color, or a Lamp.
Methods:
�TrafficLight() - The default constructor, which will construct the light with its three lamps in their correct position based on the lamp being in the center of the screen. It will also set the red light on, and the time the light last changed to 1.
�TrafficLight(int x, int y) - The default constructor, which will construct the light with its three lamps in their correct position based on the upper left corner of the lamp being at position (x,y) on the center of the screen. It will also set the red light on, and the time the light last changed to 1.
�void cycle(int) � This method will cycle the lights, by turning the current light which is on to off, and turning the next light that should be turned on to on. Note: It will enforce the cycle pattern: red ->green; green -> yellow, and yellow -> red. It will also store the int into the time when the light last changed.
�int getWhenLastChanged() � This method will return the time when the light last changed.
�int getTimeOn() � This method will return the time the light which is currently on is supposed to stay on.
�void setCycleTimes(int red, int yellow, int green) � This method will set the instance variables for the time the lamp is to be on, in each instance of the lamps, to the appropriate values. The time on for the red lamp will be set red. The time on for the yellow lamp will be set to yellow, and the time on for the green lamp will be set to green.

Please help me understand how to implement these all together and how to create a class based on the definitions above. I am not asking for the answer necessarily, I just need a frame to work from...please help me...
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Cross-posted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a working traffic light!!
 
Similar Threads
Creating a working traffic light!
Images don't show up with first click
Reg Assertions
Creating a working trafficlight!
javadoc format colors