The Metro class simulates the activity of a Metro bus as it moves back and forth along its route, making
stops along the way. The stops on the route are numbered consecutively starting from 101 up to and
including 101 + a number minus 1. The number, n, is provided when the Metro object is created. You
may assume that the number of stops (n) will always be greater than 2.
if a metro object is created:
Metro metro1 = new Metro(6) // stops 101 - 106
The Metro will start at stop number 101 and proceed to go to the other stops along its route to the last
stop before returning to the original path and then returning to the original stop as seen below. When
the Metro reaches the first stop, it reverses direction. The stops are numbered 101 through 100 + n-1. n
is assumed to be > 3. If n were 6, the route would look as below.
106
101
1
102
1
105
103
104
The route will always have a loop at one end as seen above. In this particular case, if the bus is at stop
106, the next stop is 103. Your code must be able to determine the next stop after the highest stop
number. Your code should work for any value of n that is used to create the metro object.
The following table contains a sample code execution sequence and the corresponding results. You will
create 2 Metro objects, metro1 and metro2.
Statement or
Expression
Metro metro1 = new
Metro(6);
metro1.getStop();
metro1.move();
metro1.getStop();
metro1.move();
metro1.move();
metro1.move();
metro1.getStop();
metro1.move();
metro1.getStop();
metro1.move();
metro1.getStop();
metro1.move();
metro1.getStop();
metro1.move();
Value returned
(blank if no value)
101
102
105
106
103
102
Comment
The route for metro21 has six stops numbered
101–106 .
metro1 is at stop 101 (first stop on the route).
metro1 moves to the next stop ( 102 ).
metro1 is at stop 102 .
metro1 moves to the next stop ( 103 ).
metro1 moves to the next stop ( 104 ).
metro1 moves to the next stop ( 105 ).
metro1 is at stop 105 .
metro1 moves to the next stop ( 106 ).
metro1 is at stop 106 .
metro1 moves to the next stop ( 103 ).
metro1 is at stop 103 .
metro1 moves to the next stop ( 102 ).
metro1 is at stop 102 .
metro1 moves to the next stop ( 101 ).
metro1.getStop();
metro1.move();
metro1.getStop();
Metro metro2 = new
Metro(5);
metro1.getStop();
metro2.getStop();
metro2.move();
metro2.move();
metro2.getStop();
metro2.move();
metro2.move();
metro2.getStop();
metro2.move();
metro2.getStop();
metro2.move();
metro2.getStop();
metro2.move();
metro2.move();
metro2.getStop();
101
102
102
101
103
105
102
101
103
metro1 is at stop 101 .
metro1 moves to the next stop (102)
metro1 is at stop 102 .
The route for metro2 has four stops numbered
101–105 .
metro1 is at stop 102 .
metro2 is at stop 101 (first stop on the route).
metro2 moves to the next stop (102) .
metro2 moves to the next stop (103) .
metro2 is at stop 103 .
metro2 moves to the next stop (104) .
metro2 moves to the next stop (105) .
metro2 is at stop (105) .
metro2 moves to the next stop (102) .
metro2 is at stop 102 .
metro2 moves to the next stop (101) .
metro2 is at stop 101 .
metro2 moves to the next stop (102) .
metro2 moves to the next stop (103) .
metro2 is at stop 103 .
Write the complete Metro class and the main class which will create the objects metro1 and metro2.
Make sure you include any needed constructors, instance variables and methods. Your implementation
must meet all specifications and conform to the example.
Print from the main class only.
You need to print everytime the metro moves.
You also need to print it’s location when getStop() is called.