Question: Class TrainTest import java.util.ArrayList; /** * The test class TrainTest. * * @author Lynn Marshall * @version May 2015 */ public class TrainTest extends junit.framework.TestCase

 Class TrainTest import java.util.ArrayList; /** * The test class TrainTest. *

Class TrainTest

import java.util.ArrayList;

/** * The test class TrainTest. * * @author Lynn Marshall * @version May 2015 */ public class TrainTest extends junit.framework.TestCase { /** * Default constructor for test class TrainTest */ public TrainTest() { }

/** * Sets up the test fixture. * * Called before every test case method. */ protected void setUp() { }

/** * Tears down the test fixture. * * Called after every test case method. */ protected void tearDown() { } public void testCreateEmptyTrain() { Train aTrain = new Train(); /* Verify that a new train has no cars. */ assertEquals(0, aTrain.cars().size()); } public void testAddCar() { Train aTrain = new Train(); Car car1 = new Car(1250, true); aTrain.addCar(car1); Car car2 = new Car(1300, false); aTrain.addCar(car2); Car car3 = new Car(1740, false); aTrain.addCar(car3); ArrayList cars = aTrain.cars(); assertEquals(3, cars.size()); /* Verify that each car added to the train was placed at * the end of the list. */ Car aCar; aCar = cars.get(0); /* Important - assertSame() does not compare the Car objects * referred to by car1 and aCar to detemine if they are equal * (have the same state). It verifies that car1 an aCar refer to * the same object; i.e., that the Car (reference) retrieved by get(0) * is the first first that was added to the ArrayList. */ assertSame(car1, aCar); aCar = cars.get(1); assertSame(car2, aCar); aCar = cars.get(2); assertSame(car3, aCar); } public void testIssueTicket() { Train aTrain = new Train(); Car car1 = new Car(1250, true); aTrain.addCar(car1); Car car2 = new Car(1300, false); aTrain.addCar(car2); Car car3 = new Car(1740, false); aTrain.addCar(car3); boolean result; /* Book all the seats in the business-class car. */ for (int i = 0; i cars = aTrain.cars(); for (int i = 0; i cars = aTrain.cars(); result = aTrain.cancelTicket(1300, 4); assertTrue(result); assertFalse(cars.get(1).seats()[3].isBooked()); /* Cancel ticket in a non-existent car. */ result = aTrain.cancelTicket(1500, 7); assertFalse(result); /* Cancel ticket in a non-existent seat. */ result = aTrain.cancelTicket(1300, 54); assertFalse(result); /* Cancel ticket for a seat that hasn't been booked. */ result = aTrain.cancelTicket(1740, 21); assertFalse(result); assertFalse(cars.get(2).seats()[20].isBooked()); /* Attempt to cancel the same ticket twice. */ result = aTrain.cancelTicket(1250, 11); assertTrue(result); assertFalse(cars.get(0).seats()[10].isBooked()); result = aTrain.cancelTicket(1250, 11); assertFalse(result); assertFalse(cars.get(0).seats()[10].isBooked()); } }

Class Train

import java.util.ArrayList;

public class Train { /** The cars in this train. */ private ArrayList cars; /** * Constructs an empty train; i.e., one that has no cars. */ public Train() { cars = new ArrayList(); } /** * Adds the specified car to this train. */ public void addCar(Car car) { cars.add(car); } public ArrayList cars() { return cars; } public boolean issueTicket(boolean businessClassSeat) { for(Car car : cars){ if(businessClassSeat){ if(car.isBusinessClass()){ if(car.bookNextSeat()){ return true; } } } else{ if(!car.isBusinessClass()){ if(car.bookNextSeat()){ return true; } } } } return false; } /** * Cancels the ticket for the specified seat in the specified car. * Returns true if successful, false otherwise. */ public boolean cancelTicket(int carId, int seatNo) { for(Car car : cars){ if(car.id()==carId){ return car.cancelSeat(seatNo); } } return false; } }

4. Add a new test method testBookCancelTicket which is to be used to check that we can properly book a ticket after it has been cancelled Ensure that vou avoid using a boolean "result". o Create a train with four cars (two business and two economy) as in the previous methods As in testCancelTicket fill the seats in the first business car and half the second business car, but just one of the economy cars o o Cancel any three tickets in the economy car. o Book four economy tickets and check that you get each of the three cancelled (starting with the one nearest the front of the car), and then, for the fourth, the first seat in the second economy car Cancel three business car tickets: one in the first business car and two near the front of the second. o o Book four business tickets and check that you get each of the three cancelled (starting with the one in the first business car), and then, for the fourth, seat 16 of the second business car. o Ensure that you avoided using a boolean "result". 5. You should have noticed by now that the first part most methods is identical Move the repetitive code to TrainTest's constructor. Note that you will need to add five fields: aTrain (type Train) and carl, car2, car3, and car4 (type Car) The TrainTest constructor will create the train and the four cars, and add the four cars to the train. o Method testCreateEmptyTrain is unchanged, although renaming the Train object to emptyTrain makes the code a little clearer. The other methods can now be shortened o o 4. Add a new test method testBookCancelTicket which is to be used to check that we can properly book a ticket after it has been cancelled Ensure that vou avoid using a boolean "result". o Create a train with four cars (two business and two economy) as in the previous methods As in testCancelTicket fill the seats in the first business car and half the second business car, but just one of the economy cars o o Cancel any three tickets in the economy car. o Book four economy tickets and check that you get each of the three cancelled (starting with the one nearest the front of the car), and then, for the fourth, the first seat in the second economy car Cancel three business car tickets: one in the first business car and two near the front of the second. o o Book four business tickets and check that you get each of the three cancelled (starting with the one in the first business car), and then, for the fourth, seat 16 of the second business car. o Ensure that you avoided using a boolean "result". 5. You should have noticed by now that the first part most methods is identical Move the repetitive code to TrainTest's constructor. Note that you will need to add five fields: aTrain (type Train) and carl, car2, car3, and car4 (type Car) The TrainTest constructor will create the train and the four cars, and add the four cars to the train. o Method testCreateEmptyTrain is unchanged, although renaming the Train object to emptyTrain makes the code a little clearer. The other methods can now be shortened o o

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!