need help!!!!

Create a TrafficLight that models a traffic signal for a simple four-way intersection.

In this kind of intersection, there are really only two directions of traffic flow, which for convenience I'll abbreviate as ns and ew for north-south and east-west. There's nothing fancy here; either ns traffic is moving or ew is moving, or the signal is in the failure state and is blinking red in all directions.

Here's the outline of the new class, which I called FourWayTwoSignal for lack of any better ideas:

?Data members: two Traffic Light objects for the two ns and ew directions.

?Default constructor: Both lights start off red and non-blinking.

?toString: Have the string representation of this new object use a format like: <ns:red, ew:green> where for each direction it shows either the color of the light or the word blink.

?fail: Both lights go to blinking red.

?reset: Both lights go to non-blinking red.

?cycle: The light cycling is a little more complicated here, but can be broken down like this:

o If both lights are blinking red, they stay that way (only a reset operation can fix this).

o If both lights are non-blinking red (the initial or reset state), start the cycle going by cycling the ns light to green.

o If one light is red and the other isn't (this is the normal operating state), cycle the non-red light to its next color. If this causes that light to change to red, then also cycle the other light so that the other traffic direction now sees green.

******

1. Both blink

2. Both red

3. One red, the other green or yellow

And note that combination 2 should only appear when the light is first created or just after a reset; as soon as you invoke the cycle operation, combination 2 changes to some variation of combination 3.

this is the tester for the main

publicclass FWTSTest{

publicstaticvoid main(String[] args){

FourWayTwoSignal fwts =new FourWayTwoSignal();

System.out.println(fwts);

System.out.println();

for (int i = 1; i <= 14; i++){

fwts.cycle();

System.out.println(fwts);

}

System.out.println();

fwts.fail();

System.out.println(fwts);

for (int i = 1; i <= 4; i++){

fwts.cycle();

System.out.println(fwts);

}

System.out.println();

fwts.reset();

System.out.println(fwts);

for (int i = 1; i <= 4; i++){

fwts.cycle();

System.out.println(fwts);

}

}

}

i have created basicly what i need for one traffic light but i need help tweeking it so it can work with a four way one. there would basicly be one going ns (north south) and ew (east west)

here is what i have so far

publicclass TrafficLight{

privateint color = 2;

privateboolean Blink =false;

publicint getColor(){

return color;

}

public String getColorName(){

if(color == 0)

return"green";

elseif(color == 1)

return"yellow";

elseif(color == 2)

return"red";

else

returnnull;

}

publicvoid cycleLight(){

if(Blink ==true)

color = color;

elseif(color == 2)

color = 0;

else

color = color + 1;

}

publicboolean isBlinking(){

return Blink;

}

publicvoid reset(){

color = 2;

Blink =false;

}

publicvoid fail(){

color = 2;

Blink =true;

}

public String toString(){

return"<"+getColorName()+(isBlinking()?", blinking":"")+">";

}

}

I cant get mine to work the way i want it to can some one help me...

[code]

publicclass FourWayTwoSignal{

private TrafficLight ns =new TrafficLight();

private TrafficLight ew =new TrafficLight();

public FourWayTwoSignal(){

ns.reset();

ew.reset();

}

public String toString(){

return"<ns:" + ns.getColorName()+", " +"><ew:" + ew.getColorName() +">";

}

publicvoid fail(){

ns.fail();

ew.fail();

}

publicvoid reset(){

ns.reset();

ew.reset();

}

publicvoid cycle(){

if((ns.getColorName() =="red")&&(ew.getColorName() =="red")&&(ns.isBlinking() ==false)&&(ew.isBlinking() ==false)){

ns.cycleLight();

if(ns.getColorName() != ew.getColorName()){

if(ns.getColorName()!="red"){

ns.cycleLight();

}

if(ew.getColorName() !="red"){

ew.cycleLight();

}

}

}

}

}

mine prints out

<ns:red, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:red, ><ew:red>

<ns:red, ><ew:red>

<ns:red, ><ew:red>

<ns:red, ><ew:red>

<ns:red, ><ew:red>

<ns:red, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

<ns:yellow, ><ew:red>

i dont get why it does that

[9698 byte] By [BGagnon05a] at [2007-10-2 17:11:42]
# 1

Don't get why it does what? What do you expect it to do differently?

If you don't understand why your code behaves the way it does, it means one or more of your assumptions is wrong. Rather than guessing, or asking others to guess, you should use a debugger, or put in print statements at key steps to test your assumptions about which code is being executed and which values are being set.

jverda at 2007-7-13 18:26:52 > top of Java-index,Java Essentials,New To Java...
# 2

And next time please bother to put a bit of thought into your subject line. It's supposed to convey useful information about the nature of your problem. Everybody who posts here does so because e "needs help," and the exclamation points are just annoying. They do not provide any useful information.

jverda at 2007-7-13 18:26:52 > top of Java-index,Java Essentials,New To Java...