aspose file tools*
The moose likes Java in General and the fly likes Creating a working trafficlight! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating a working trafficlight!" Watch "Creating a working trafficlight!" New topic
Author

Creating a working trafficlight!

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...
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
I'm sorry I didn't know I couldn't post in more than one forum to get help...its just that I'm really worried about this and I can't figure it out...I just wanted someone to help me...please....I'm sorry I didn't know the rules of this site, I guess...
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Kelsey
You are asking quite a lot in your post. As a result many people here are not going to want to tackle it all. Also since this is a homework assignment, for obvious reasons, no one is going to do the all work for you. I know you said I am not asking for the answer necessarily, I just need a frame to work from, but we need you to provide something that you have done and we can help guide you with problems you run into.
Why don�t we start with one thing and go from there... Let�s start with the Lamp class since the TrafficLight class needs do have instance variable of type Lamp.
Try creating a skeleton of the class with the information/requirments you have, post it and I or someone else can help you fine-tune it. For example, lets say I was told to create a Line class that has two instance variables of type Point and a default constructor of Line(), a constructor which takes the two points, a method to set & get each point, and getLength() method to get the length of the line. A skeleton of the class would look like this:

Try reading through your requirements for the Lamp class and create a skeleton and then post it for review. If you run into problems, post what you have so far and what problems you are encountering and I or someone else can help you out.
Regards,
Mark
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Of course the traffic light itself is relatively simple...
Where it gets interesting is in the interaction between all the traffic lights on an intersection.
Make sure the correct ones are triggered in the correct sequence with some interval to make sure no collisions occur.
Then add some traffic...


42
Ben Dover
Ranch Hand

Joined: Jan 30, 2004
Posts: 91
It sounds like your problem is not the traffic lights, but your general Java / OOD knowledge. Focus on learning these basic principles and then come to the problem with a fresher perspective.
 
jQuery in Action, 2nd edition
 
subject: Creating a working trafficlight!