Question: This program is in java Design and code a program including the following classes, as well as a client class to test all the methods

This program is in java

Design and code a program including the following classes, as well as a client class to test all the methods coded:

A Passenger class, encapsulating a passenger. A passenger has two attributes: a name, and a class of service, which will be 1 or 2.

A Train class, encapsulating a train of passengers. A train of passengers has one attribute: a list of passengers, which must be represented with an ArrayList. Your constructor will build the list of passengers by reading data from a file called passengers.txt. You can assume that passengers.txt has the following format:

...

For instance, the file could contain: James 1 Ben 2 Suri 1 Sarah 1 Jane 2

...

You should include the following methods in your Train class:

a method returning the percentage of passengers traveling in first class

a method taking two parameters representing the price of traveling in first and second class and returning the total revenue for the train

a method checking if a certain person is on the train; if he/she is, the method returns true; otherwise, it returns false

Here are the Javadoc details for your classes:

Passenger:

/** * Constructor:
* Allows client to set beginning values for name and service * This constructor takes two parameters
* Calls mutator method to validate new value * @param newName the name of the passenger * @param newService the class of travel */
/** * Mutator method:
* Allows client to set value of class of travel * setService sets the value * in service to newService * if newService is not 1 or 2, service is assigned 2 * @param service the new class of travel * @return a reference to this object */
/** equals * @param o Passenger object * @return return true if name and class of tavel in p are equal to * corresponding elements in this object */

Train Class

/** * Constructor:
* Allows client to set beginning list of passengers for travelers * This constructor takes one parameter
* Fills ArrayList travelers with the Passenger objects from newTravelers * @param newTravelers the list of travelers */
/** * Mutator method:
* Allows client to set values of travelers * setTravelers sets the values * in travelers to the values in newTravelers * @param newTravelers the new ArrayList for travelers */
/** equals * @param o Train object * @return return true if elements of ArrayList in t are equal to * corresponding elements in this object * and ArrayLists have the same size */
/** * percentageFirstClassPassengers method * Computes the percentage of first class passengers on the train * @return a double, the percentage of rist class passengers on the train */
/** * trainRevenues method * Computes the total revenues for the train based on first and second class ticket prices * @param firstClassPrice the price of a first class ticket * @param secondClassPrice the price of a second class ticket * @return a double, the total revenues for the train */
 /** * isOnTrain method * Searches if a person is on the train * @param person the name of a passenger * @return a boolean, true if person is on the train, false otherwise */

SAMPLE OUTPUT

The trains are not equal After changing t1, the trains are equal The train passengers are: name: James; service: 1 name: Ben; service: 2 name: Suri; service: 1 name: Reyna; service: 1 name: Jane; service: 2 name: Mario; service: 2 name: Lucy; service: 1 name: Sergio; service: 2 name: Mary; service: 2

44.44% of the passengers travel in first class Enter the ticket price for first class > 52.00 Enter the ticket price for second class > 28.50 Total train revenues: $350.50 Enter the name of a passenger > Mario Mario is on the train

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!