This is the code so far:
public class TrafficSystem
{
// given instance variables for the four lights
private TrafficLight north;
private TrafficLight south;
private TrafficLight east;
private TrafficLight west;
public void colourLight(TrafficLight aLight, OUColour aColour) //
as aLight is a TrafficLight variable and aColour is a OUColour
varaiable
{
aLight.setColour(aColour);
}
private int state; //This is the state variable
public TrafficSystem (TrafficLight north,TrafficLight
south,TrafficLight east,TrafficLight west) //This is the
constructor which takes the four TrafficLight references for each
direction
{
this.north=north;
this.south=south;
this.east=east;
this.west=west;
this.state=0; //sets state to 0
this.setPositions(); //calls the setPositions() to initiate the
positions
}
private void setPositions() // provided method
{
north.setXPos(100);
north.setYPos(0);
south.setXPos(100);
south.setYPos(200);
west.setXPos(0);
west.setYPos(100);
east.setXPos(200);
east.setYPos(100);
}
/**
* Provided method without try catch or the checking for 0-4
state.
* Extra functionality is to be added to deal with non-integer
* or out of range inputs
*/
public void manualOverride()
{
// if (!go)
// {
int newState = Integer.parseInt(OUDialog.request("Please give the
state you want to change to - between 0 and 4 inclusive"));
// state = newState;
// colourAllLights();
// }
}
/**
* causes execution to pause by time number of milliseconds
*/
public void delay(int time)
{
try
{
Thread.sleep(time);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Trafficlight class you wanted:
public class TrafficLight
{
private Circle light;
/**
* Constructor for objects of class TrafficLight
*/
public TrafficLight(Circle aCircle)
{
light = aCircle;
light.setColour(OUColour.RED);
light.setDiameter(50);
}
/**
* setColour just sets the colour of the light as told to
* 0 = red, 1 = green, 2 = orange
*/
public void setColour(OUColour colour)
{
light.setColour(colour);
}
/**
* setter for x position
*/
public void setXPos(int xPos)
{
light.setXPos(xPos);
}
/**
* setter for y position
*/
public void setYPos(int yPos)
{
light.setYPos(yPos);
}
and for the different states at the junctions i need:
cycling state |
North and south |
East and west |
1 |
Green |
Red |
2 |
Orange |
Red |
3 |
Red |
Green |
4 |
Red |
Orange |
1 |
Green |
Red |
I have added code for all the 3 questions mentioned in the question. I have mentioned all the details in the comments.
Please find the code below:
TrafficSystem.java public class TrafficSystem { // given instance variables for the four lights private TrafficLight north; private TrafficLight south; private TrafficLight east; private TrafficLight west; //method cycleState to change the state private void cycleState(){ //if state is less then 4 then go to the next state if(state<4){ state++; } //else reset it to the 1 else{ state = 1; } } private void colourAllLights(){ //based on the value of the state switch case would be executed switch(state){ case 0: north.setColour(OUColour.RED); south.setColour(OUColour.RED); east.setColour(OUColour.RED); west.setColour(OUColour.RED); break; case 1: north.setColour(OUColour.GREEN); south.setColour(OUColour.GREEN); east.setColour(OUColour.RED); west.setColour(OUColour.RED); break; case 2: north.setColour(OUColour.ORANGE); south.setColour(OUColour.ORANGE); east.setColour(OUColour.RED); west.setColour(OUColour.RED); break; case 3: north.setColour(OUColour.RED); south.setColour(OUColour.RED); east.setColour(OUColour.GREEN); west.setColour(OUColour.GREEN); break; case 4: north.setColour(OUColour.RED); south.setColour(OUColour.RED); east.setColour(OUColour.ORANGE); west.setColour(OUColour.ORANGE); break; } } public void colourLight(TrafficLight aLight, OUColour aColour) // as aLight is a TrafficLight variable and aColour is a OUColour varaiable { aLight.setColour(aColour); } private int state; //This is the state variable public TrafficSystem (TrafficLight north,TrafficLight south,TrafficLight east,TrafficLight west) //This is the constructor which takes the four TrafficLight references for each direction { this.north=north; this.south=south; this.east=east; this.west=west; this.state=0; //sets state to 0 //lies to set initially all the lights to RED this.north.setColour(OUColour.RED); this.south.setColour(OUColour.RED); this.east.setColour(OUColour.RED); this.west.setColour(OUColour.RED); this.setPositions(); //calls the setPositions() to initiate the positions } private void setPositions() // provided method { north.setXPos(100); north.setYPos(0); south.setXPos(100); south.setYPos(200); west.setXPos(0); west.setYPos(100); east.setXPos(200); east.setYPos(100); } /** * Provided method without try catch or the checking for 0-4 state. * Extra functionality is to be added to deal with non-integer * or out of range inputs */ public void manualOverride() { // if (!go) // { int newState = Integer.parseInt(OUDialog.request("Please give the state you want to change to - between 0 and 4 inclusive")); // state = newState; // colourAllLights(); // } } /** * causes execution to pause by time number of milliseconds */ public void delay(int time) { try { Thread.sleep(time); } catch (Exception e) { System.out.println(e); } } } I am assuming that you have your own class library where OUColour is implemented.
If anyone could give me some guidance for these questions. I know i'm chancing my arm...
If anyone could give me some guidance for these questions. I know i'm chancing my arm but its worth a shot.... Thanks in advance i.Write a private instance method cycleState() which takes no argument and returns no value. This method cycles the value of state, from 1 to 2, from 2 to 3, from 3 to 4 and from 4 back to 1 again. (Note that the value of 0 for state is not part of this cycle). ii.Write a...
Java. i.Write a public instance method alignBody() that takes no arguments and returns no value. It should set the xPos and yPos of the body to the xPos and yPos of the StickFigure. ii.Write a public instance method alignLeg() that takes no argument and returns no value. It should set the xPos and yPos of the leg so it is centred immediately below the body of the StickFigure. public class StickFigure { /*Instance variables*/ private int xPos;//The horizontal position...
Please help with my car traffic simulator! Code that I already have below, I do not know how to start it off! public class IntersectionSimulation { private final static int EAST_WEST_GREEN_TIME = 30 ; private final static int[] NORTH_SOUTH_GREEN_TIMES = { 20, 24, 30, 42 } ; private final static int[] CAR_INTERSECTION_RATES = { 3, 5, 10 } ; private final static int[] CAR_QUEUEING_RATES = { 5, 10, 30 } ; private final static int[] EXPERIMENT_DURATIONS = { 3*60, 5*60,...
C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...
It's been too long since I have done any Java programming. Can anyone help me with this one? Thank you 6.5 Write the declaration for a class named C that declares: (1) a private int instance variable named mX; (2) a private int class variable named mY initialized to 0; (3) a private int class constant named A which is equivalent to 100; (4) a public int class constant named B which is equivalent to 200; (5) public accessor and...
Hello, I ran into some issues with my code and wanted to ask if anyone can help me fix them. Thank you in advance! Question: You and your team of software developers are experiencing problems saving the state of a program between different times that the program is run. An object oriented program state can be maintained by saving the state of the objects in the program. Also, you can transfer the state of an object between different computer systems...
Why can't the 'length' be resolved? import java.util.*; enum Commands { UNKNOWN, QUIT, LOOK, NORTH, SOUTH, EAST, WEST } public class Main { public static void main(String[] args) { System.out.println("Welcome to Zork!"); SpawnPlayer(); Scanner scanner = new Scanner(System.in); Commands command = Commands.UNKNOWN; while (command != Commands.QUIT) { System.out.println(rooms[]locationRow[locationColumn]); System.out.print(">"); String inputString = scanner.nextLine(); command = ToCommand(inputString); switch (command) { case QUIT: System.out.println("Thank you for playing!"); break; case LOOK: System.out.println("A rubber mat saying 'Welcome to Zork!' lies by the door."); break;...
fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life Before you begin, DOWNLOAD the files “Lab8_Bicycle.java” and “Lab8_BicycleApp.java” from Canvas to your network drive (not the local hard drive or desktop). Once you have the files saved, RENAME THE FILES Bicycle.java and BicycleApp.java then open each file. 1) Look at the Bicycle class. Two of the many data properties we could use to define a bike are its color and the gear it...
[Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what is wrong with my code. Here'e the problem: Write a class SemiCircle that represents the northern half of a circle in 2D space. A SemiCircle has center coordinates and a radius. Define a constructor: public SemiCircle(int centerX, int centerY, int theRadius) Implement the following methods: public boolean contains(int otherX, int otherY) returns true if the point given by the coordinates is inside the SemiCircle....
A Java Problem. In this problem you are going write a class to model a traffic light that can cycle through colors, red, green, yellow. To do this, you will have to maintain the state of the traffic light. Maintaining state is one of the design pattern discussed in this week's lesson. The state of the TrafficLight indicates which light is lit. It changes every time the TrafficLight cycles. Specification TrafficLight will have a constructor that takes the x and...