Question: A passenger train is made up of one or more business-class cars and economy-class cars. Each car contains several seats. When you purchase a


A passenger train is made up of one or more business-class cars and economy-class cars. Each car contains several seats. When you purchase a ticket for a train trip, a specific seat is booked for you. Purchasing a ticket to ride in a business-class car costs more than a ticket for a seat in an economy-class car. The train-v2 project contains an incomplete implementation of a class that models a train. Train has a private field, cars, the list of cars in the train. The list is implemented as an ArrayList of Car objects. (A summary of some of the ArrayList methods is provided at the end of this lab.) Your job is to complete the implementation of Train. You cannot define additional constructors or methods in Seat, Car, or Train, or change the specification of any of the constructors and methods. All fields must be private; i.e., it is not permitted for objects of one class to directly access the fields of objects of another class. You can test your Train class interactively, using BlueJ's object bench. You can use the BlueJ debugger (which you learned how to use in Lab 3) to locate any bugs in your code. In addition, you have been provided with a test class called TrainTest. To use this class, you must enable BlueJ's testing tools (see Lab 2 if you've forgotten how to do this). After you write each method (and it compiles without errors), click the Run Tests button. The test results will tell you if your methods are working. Write the methods in the order listed below, and make sure you finish each step before moving on to the next step. At this point in the course, you are not expected to understand all of the code in TrainTest (in particular, the use of methods assertEquals(), assert Same (), assertTrue () and assert False ()), but you are welcome to try to figure out what each of the test methods do. Part 1 - Developing Class Train Section A 1. Add Javadoc comments at the top of the class. See class Seat for an example. 2. Class Train has one constructor, which has no parameters. Write this constructor so that it initializes the cars field with the reference to a new ArrayList object that can store references to Car objects. This constructor does not initialize the ArrayList with Car objects. If your constructor works, testCreateEmptyTrain () should pass. 3. Generate the Javadoc and check that it looks good for the portions of class Train completed. Part 2 - Developing Class Train Section B 1. Method addCar () has a single parameter of type Car and returns nothing. This method adds the specified Car object to the list of cars in this train. Write this method. If your method works, testAddCar () should pass. 2. Did you remember to add Javadoc comments? Generate the Javadoc and check that it looks good for the portions of class Train completed. Part 3 - Developing Class Train Section C 1. Method issueTicket () has a single boolean parameter and returns a boolean. If the parameter is true, the method attempts to issue a ticket for a seat in a business-class car; if false, it attempts to issue a ticket for a seat in an economy-class car. This method will attempt to book a seat in the first car of the appropriate type, but if a seat is not available it will attempt to book a seat in the second car of the appropriate type, and so on. This method must ensure that a request to issue a ticket in a business-class car will never result in a seat being booked in an economy-class car, and vice-versa. If a ticket is successfully issued, the method returns true; otherwise, it returns false. This method must NOT call the seats () method in class Car (that method is reserved for use by test cases). Hint: recall that class Car has a method called bookNextSeat (). Write this method. Before testing this method, from the BlueJ menu select View > Show Terminal. If your method works, testIssueTicket () should pass, and the tickets will be "printed" in the Terminal Window. 2. Did you remember to add Javadoc comments? Generate the Javadoc and check that it looks good for the portions of class Train completed. Part 4 - Developing Class Train Section D 1. Method cancelTicket () has two parameters, an integer car id and an integer seat number, and returns a boolean. If the car id and seat number are valid, and if the specified seat in the specified car has been booked, this method cancels the booking for that seat and returns true. If the car id or seat number are not valid, or if the specified seat has not been booked, the method returns false. This method must NOT call the seats () method in class Car (that method is reserved for use by test cases). Hint: recall that class Car has a method called cancelSeat (). Write this method. If your method works, testCancelTicket () should pass. 2. Did you remember to add Javadoc comments? Generate the Javadoc and check that it looks good the entire Train class. 3. You will build on Lab 5 in Lab 7, so be sure to save your work. Submission Submit Train.java. API Summary - Class ArrayList // Appends object o (of type E) to the end of this list. Returns true if // successful, otherwise returns false. boolean add (E o);
Step by Step Solution
There are 3 Steps involved in it
Based on the provided instructions heres a structured approach to completing the Train class java im... View full answer
Get step-by-step solutions from verified subject matter experts
