Question
Design and code a program including the following classes, as well as a client class to test all the methods coded: A Passenger class -
Design and code a program including the following classes, as well as a client class to test all the methods coded:
A Passenger class - use defined class encapsulating a passenger. A passenger has two attributes: a name, and a class of service, which will be 1 or 2. Provide an appropriate constructor, get and set methods along with equals( ) and toString( ) methods.
A Train class, encapsulating a train of passengers. This class requires only one instance variable: passengerList of ArrayList type You should include the following methods in your Train class:
a. Implement a default constructor which initializes passengerList instance variable to a new ArrayList object
b. A method that will take a Passenger object as a parameter and add the passenger to the passengerList.
c. A method returning the reference to the Passenger Object at a specific index position. The index position with be passed as a parameter to the method. Hint: The method header would be public Passenger getPassenger(int i) { Code goes here }
d. A method returning the number of passengers in the train.
e. A method returning the percentage of passengers traveling in the first class
f. A method taking two parameters representing the price of traveling in first and second class and returning the total revenue for the train.
g. A method checking if a certain person is on the train; if he/she is, the method returns true; otherwise, it returns false.
h. Implement the toString( ) and equals ( ) method.
i. Include JavaDoc comments for both Passenger and Train class.
Next you will implement a test class called TrainTest with the main ( ) method. In the main method,
Create a Train object (At this time the train is empty i.e. the passenger List is zero).
Next create at least 10 Passengers using the information: James 1 Ben 2 Suri 1 Sarah 1 Jane 2 ..
Add the Passengers to the Train.
Use for loop to print the names of all the passengers in the train.
Demonstrate the various method of the Train class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started