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 but its worth a shot.... Thanks in advance i.Write a private instance method cycleState() which takes no ar...
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...
------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T> implements QueueADT<T> { private int count; private LinearNode<T> head; private LinearNode<T> tail; public LinkedQueue() { count = 0; head = null; tail = null; } @Override public void enqueue(T element) { LinearNode<T> node = new LinearNode<T> (element); if(isEmpty()) head = node; else ...
C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...