CODE
public class Main
{
public String name; public int startX; public int endX; public int startY; public int endY; //road vars
public String vname; public double speed; public double xlocation; public double ylocation; //vehicle vars
public String set; public boolean ared; public boolean aredamber; public boolean aamber; public boolean agreen; //light vars
public boolean bred; public boolean bredamber; public boolean bamber; public boolean bgreen;
public static void main(String[] args)
{
Road h1 = new Road ("H1", 0, 600, 50, 100);
Road h2 = new Road ("H2", 0, 600, 150, 200);
Road h3 = new Road ("H3", 0, 600, 250, 300);
Road v1 = new Road ("V1", 50, 100, 0, 400);
Road v2 = new Road ("V2", 150, 200, 0, 400);
Road v3 = new Road ("V3", 250, 300, 0, 400);
Vehicle c1 = new Car ("Car 1", 5, 0, 52); //test car designed to travel west to east along h1
Vehicle c2 = new Car ("Car 2", 5, 52, 0); //test car north to south v1
Vehicle b1 = new Bike ("Bike 1", 15, 20, 51);
Lights j11 = new Lights ("Set 1", false, false, false, true, false, false, false, false); //lights at junction 11
j11.setStateAgreen(false);
j11.setStateBgreen(false);
int i=0;
int k=0;
while (c1.getXLocation() <600)
if (j11.getStateAgreen() ==false && ((Vehicle)c1).getXLocation()<=50 & ((Vehicle)c1).getXLocation()>=47)
{c1.xlocation+= 0; i++;
System.out.println("Car 1 has stopped at lights at X position " + c1.xlocation);
break;
}
else {
c1.ylocation+=((Vehicle)c1).getSpeed(); i++;
}
// while (c2.getYLocation() <450)
// if (j11.getStateBgreen() ==false && ((Vehicle)c2).getYLocation()<=50 & ((Vehicle)c2).getYLocation()>=47)
// {c2.ylocation+= 0; k++;
// System.out.println("Car 2 has stopped at lights at y position " + c2.ylocation);
// break;
// }
// else {
// c2.ylocation+=((Vehicle)c2).getSpeed(); k++;
// }
}
}
The aim of the above code is to move a vehicle along a road. If the traffic lights are not set to green, the vehcile must stop at them. This seems to workfor c2, but not c1. Cannot understand as code is the same.
Ideally I want to add several of these but as soon as I take the commenting //'s out the program just seems to get "stuck" running with no break or output.
Any ideas what I ca do or what is wrong?