Question
I m trying to complete this code bu I am stuck. please help is needed. .Add the instance variable state, suitably commented, to your LevelCrossingController
I m trying to complete this code bu I am stuck. please help is needed.
- .Add the instance variable state, suitably commented, to your LevelCrossingController class.
- ii.Now write a public constructor for the class LevelCrossingController that takes three Light references as arguments, sets the given instance variables topLeft, topRight and bottom to reference these arguments (in that order), sets their positions using the provided private method setPositions(), sets the value of state to 0 and sets the value of trainComing to false.
It is a good idea to test your constructor at this point. Note there is provided test code in the README.TXT file in the project.
-
c.
- i.Write a public instance method changeState() which takes no argument and returns no value. If trainComing is true then it should change the value of state as follows:
state | new state |
---|---|
0 | 1 |
1 | 2 |
2 | 3 |
3 | 2 |
Otherwise it should change state to 0.
-
- ii.Write a public instance method colourAllLights() which takes no argument and returns no value. This method uses the value of state to determine what colour each of the three lights should be set to and sets their colours as described in part (b).
- iii.Add a line to the constructor to initialise the colours of all the lights. Make sure you make appropriate re-use of a method youve written.
- ii.Write a public instance method colourAllLights() which takes no argument and returns no value. This method uses the value of state to determine what colour each of the three lights should be set to and sets their colours as described in part (b).
-
d.The number of times the red lights should flash on the crossing will be entered by the user but should be greater than a minimum number MIN_REPEATS which we will set to an arbitrary value.
e.g. if the user enters 4, then the sequence would be:
(i) bottom orange;
(ii) bottom black, topLeft red, topRight black;
(iii) bottom black, topLeft black, topRight red;
(iv) bottom black, topLeft red, topRight black;
(v) bottom black, topLeft black, topRight red;
(vi) all black
- i.Add a public class constant MIN_REPEATS of type int and initialise this to 4. (The number 4 has been chosen as a low number to enable easy testing).
- ii.Look at the provided public class method findNumRepeats(). The method should ask the user to enter the number of times the red lights should flash, until a valid input is received, and then return this number. The lights should flash a minimum of MIN_REPEATS times. Some lines are currently commented out uncomment them now.
This method sets the value of a local variable repeats to input supplied by the user and then returns this number. It rather simplistically returns zero if the user presses Cancel, but when the user does provide input there are two distinct kinds of things that can go wrong.
1. The input might be something that cannot be parsed as an integer. For example, the user might input "two" or "2.0". If so, the parseInt() method will throw an exception and if nothing is done about it the program will simply stop running.
To handle this case more gracefully, the findNumRepeats() method can be modified to make use of a try-catch block. If an exception is thrown, the catch statement should give appropriate feedback to the user.
2. An entirely different situation is where the input can be parsed as an integer but this is smaller than the permitted lower limit of MIN_REPEATS. In this case no exception will be thrown, but we need to detect that the input was out of range and provide appropriate feedback to the user.
Add suitable code to findNumRepeats() that handles both cases described above.
- i.Add a public class constant MIN_REPEATS of type int and initialise this to 4. (The number 4 has been chosen as a low number to enable easy testing).
- e.Now some automation of the system is required.
Write a public instance method doTrainApproaching() which takes no arguments and returns no value. The method should first print "Train approaching" and set the value of trainComing to true. It should then reuse appropriate methods to start the light sequence so the bottom light is orange and print "Barrier lowered" before setting an int variable numRepeats to a valid value set by the user to control the number of times the top lights should change from red to black or black to red.
Your method should then alternately colour the top lights so they appear on and off, with the bottom light off. On the final iteration, trainComing should be set to false so all lights will be switched off and then "Barrier raised" should be printed.
public class LevelCrossingController { private Light topLeft; private Light topRight; private Light bottom; private boolean trainComing; /** * @return trainComing */ public boolean getTrainComing() { return trainComing; }
/** * @param trainComing * setter for trainComing */ public void setTrainComing(boolean trainComing) { this.trainComing = trainComing; } /** * Sets the positions of the lights. */ private void setPositions() { this.bottom.setXPos(100); this.bottom.setYPos(200); this.topLeft.setXPos(0); this.topLeft.setYPos(100); this.topRight.setXPos(200); this.topRight.setYPos(100); } /** * Find out how many times red lights should flash at the crossing. * Simulates length of train at crossing. */ public static int findNumRepeats() { int repeats = 0; // String timesAsString = // OUDialog.request("How many times should the red lights" // + " flash? (" // + LevelCrossingController.MIN_REPEATS // + " or over times)"); // // if (timesAsString != null) // { // repeats = Integer.parseInt(timesAsString); // } return repeats; } /** * Causes execution to pause for a number of milliseconds. */ public static void delay(int time) { try { Thread.sleep(time); } catch (Exception e) { System.out.println(e); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started