Lab6: Bus Stop
In Lab6, you will create a Bus
class simulating the activity of a bus. A bus moves back and forth along a single route, making stops along the way. The stops on the route are numbered consecutively from 1 up to and including a number that is provided when the Bus
object is created. You may assume that the number of stops will always be greater than 1.
The bus starts at the first stop and is initially heading towards the last stop. At each step of the simulation, the bus is at a particular stop and is heading toward either the first or last stop. When the bus reaches the first or last stop, it reverses direction.
The following table contains a sample code execution sequence and the corresponding results:
Bus bus1 = new Bus(3);
The route for bus1 has three stops numbered 1~3
bus1.getCurrentStop();
1
bus1 is at stop 1 (first stop on the route).
bus1.move();
bus1 moves to the next stop (2)
bus1.getCurrentStop();
2
bus1 is at stop 2 .
bus1.move();
bus1 moves to the next stop (3)
bus1.getCurrentStop();
3
bus1 is at stop 3.
bus1.move();
bus1 moves to the next stop (2)
bus1.getCurrentStop();
2
bus1 is at stop 2.
bus1.move();
bus1 moves to the next stop (1)
bus1.move();
bus1 moves to the next stop (2)
bus1.getCurrentStop();
2
bus1 is at stop 2.
bus1.getCurrentStop();
2
bus1 is still at stop 2.
Bus bus2 = new Bus(5);
The route for bus2 has five stops numbered 1–5 .
bus1.getCurrentStop();
2
bus1 is still at stop.
bus2.getCurrentStop();
1
bus2 is at stop 1 (first stop on the route).
Your task
Write the complete Bus class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.
Your code should also include a main() method to test all methods in Bus class. I recommend you put your main() method in another class other than Bus.
In your code, you should follow "data encapsulation" standard: Keep All Instance Variables Private. Violate this standard would cause you lose points.
Submit
Submit all your work via 钉钉作业本 by 8AM, Wednesday.
Last updated