Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Class TrainTest import java.util.ArrayList; /** * The test class TrainTest. * */ public class TrainTest extends junit.framework.TestCase { /** * Default constructor for test class

image text in transcribed

Class TrainTest

import java.util.ArrayList;

/** * The test class TrainTest. * */ 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; } }

Part 3: Improving Class TrainTest 1. Modify testAddCar): o Add a fourth car (a business class car) to testAddCar() and ensure that it functions properly. o Rewrite the code to remove the variable "aCar" as it is not needed. 2. Modify testlssueTicket() so that it also uses a fourth car: o Business seat booking: Fill all the seats in both business cars (instead of just one As before, check that another business seat cannot be booked. Check that all seats in the 2nd business car are also booked. o Economy seat booking At the end of the test check that the second business car seats are still all booked. o Now rewrite the code so that you can remove the boolean "result" as it's not 3.Modify testCancelTicket so that it also uses a fourth car: o Create a train with four cars (two business and two economy) as in the previous methods o Fill the seats in the first business car and half the second business car, but just one of the economy cars. o In addition to the other existing checks: cancel one of the booked seats near the front half of the second business car and ensure that functions properly, and attempt to cancel one of the unbooked seats near the back of the second business car and ensure that functions correctly o Now rewrite the code so that you can remove the boolean "result" as it's not needed. Part 3: Improving Class TrainTest 1. Modify testAddCar): o Add a fourth car (a business class car) to testAddCar() and ensure that it functions properly. o Rewrite the code to remove the variable "aCar" as it is not needed. 2. Modify testlssueTicket() so that it also uses a fourth car: o Business seat booking: Fill all the seats in both business cars (instead of just one As before, check that another business seat cannot be booked. Check that all seats in the 2nd business car are also booked. o Economy seat booking At the end of the test check that the second business car seats are still all booked. o Now rewrite the code so that you can remove the boolean "result" as it's not 3.Modify testCancelTicket so that it also uses a fourth car: o Create a train with four cars (two business and two economy) as in the previous methods o Fill the seats in the first business car and half the second business car, but just one of the economy cars. o In addition to the other existing checks: cancel one of the booked seats near the front half of the second business car and ensure that functions properly, and attempt to cancel one of the unbooked seats near the back of the second business car and ensure that functions correctly o Now rewrite the code so that you can remove the boolean "result" as it's not needed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

4.1 Random Variables

Answered: 1 week ago