Question: public class LevelCrossingController { private Light topLeft; private Light topRight; private Light bottom; private boolean trainComing; /** * @return trainComing */ public boolean getTrainComing() {
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); } } }
- Launch BlueJ and open the project TMA03_Q1 which was provided in the download for this TMA, then immediately save it as TMA03_Q1_Sol.
Open the LevelCrossingController and Light classes before proceeding further and familiarise yourself with their contents. Note the delay() method in LevelCrossingController which is the same method you met in TMA02 Q1.
The LevelCrossingController class has four instance variables:
- topLeft, topRight and bottom reference instances of Light at the top left, top right and bottom of the warning system display
- trainComing is a boolean instance variable which will be used when changing the state of the warning lights.
In the LevelCrossingController class write a public instance method with the signature colourLight(Light, OUColour), which returns no value. This method should set the colour of the specified light to the specified colour if it is one of OUColour.RED, OUColour.GREEN, OUColour.ORANGE or OUColour.BLACK. Otherwise the colour should be unchanged. You should make use of a method in Light.
(2 marks)
- b.In the UK the cycle of colours for level crossing warning lights is
- bottom orange, then as the barrier lowers
- the left and right top lights flash red,
- then all lights turn off as the barrier rises.
The initial state will be all lights off. An integer instance variable state is required which will take the values 0,1,2 or 3. The pattern of lights corresponding to the value of stateis:
| state | bottom | top left | top right | trainComing |
|---|---|---|---|---|
| 0 | black | black | black | false |
| 1 | orange | black | black | true |
| 2 | black | red | black | true |
| 3 | black | black | red | true |
- As the lights operate, the state will typically go:
- 0, 1, 2, 3, 2, 3, 2, 30
- The number of red flashes depends on the length of the approaching train, but we will allow a user to set this value.
- You'll find a video showing a typical sequence of lights below.
Video player: levelcrossing.mp4
- i.Add the instance variable state, suitably commented, to your LevelCrossingController class.
(1 mark)
- 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.
(3 marks)
-
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.
(3 marks)
- 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).
(2 marks)
- 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.
(1 mark)
-
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).
(1 mark)
- 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.
(5 marks)
- 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.
Remember to make use of the provided delay() method to slow the action down.
The OUWorkspace Graphical Display can be used to show your animation.
(6 marks)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
