Design and code a program including the following classes, as well as a client c
ID: 3681659 • Letter: D
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 - 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.
Explanation / Answer
Passenger.java
public class Passenger {
private String name;
private int serviceClass;
public Passenger()
{
}
public Passenger(String pName,int sClass)
{
name=pName;
serviceClass=sClass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getServiceClass() {
return serviceClass;
}
public void setServiceClass(int serviceClass) {
this.serviceClass = serviceClass;
}
public boolean equals(Passenger p)
{
if(this.getName()==p.getName() && this.getServiceClass()==p.getServiceClass())
{
return true;
}
return false;
}
public String toString()
{
return "Name: "+getName()+" Service Class: "+getServiceClass();
}
}
Train.java
import java.util.ArrayList;
public class Train {
private ArrayList<Passenger> passengerList;
public Train()
{
passengerList=new ArrayList<Passenger>();
}
public void addPassenger(Passenger p)
{
passengerList.add(p);
}
public Passenger getPassenger(int i)
{
if(i<passengerList.size())
{
return passengerList.get(i);
}
return null;
}
public int passengerCount()
{
return passengerList.size();
}
public double percentageFirstClass()
{
int count=0;
for(Passenger p:passengerList)
{
if(p.getServiceClass()==1)
{
count++;
}
}
return count*100/passengerList.size();
}
public int calculateRevenue(int priceFirst,int priceSecond)
{
int revenue=0;
for(Passenger p:passengerList)
{
if(p.getServiceClass()==1)
{
revenue+=priceFirst;
}
else
{
revenue+=priceSecond;
}
}
return revenue;
}
public boolean find(String name)
{
for(Passenger p:passengerList)
{
if(p.getName().equals(name))
{
return true;
}
}
return false;
}
public String toString()
{
String s="";
for(Passenger p:passengerList)
{
s+=p.toString()+" ";
}
return s;
}
}
TrainTest.java
public class TrainTest {
public static void main(String[] args) {
Train train=new Train();
//size of train should be zero at this time
System.out.println("Passenger Count "+train.passengerCount());
Passenger a1=new Passenger("James",1);
Passenger a2=new Passenger("Ben",2);
Passenger a3=new Passenger("Suri",1);
Passenger a4=new Passenger("Sarah",1);
Passenger a5=new Passenger("Jane",2);
Passenger a6=new Passenger("Rocky",2);
Passenger a7=new Passenger("Megan",1);
Passenger a8=new Passenger("Lindsay",1);
Passenger a9=new Passenger("Karla",2);
Passenger a10=new Passenger("Mike",1);
//adding the passenger to the train
train.addPassenger(a1);
train.addPassenger(a2);
train.addPassenger(a3);
train.addPassenger(a4);
train.addPassenger(a5);
train.addPassenger(a6);
train.addPassenger(a7);
train.addPassenger(a8);
train.addPassenger(a9);
train.addPassenger(a10);
//printing the name of all passengers in the train
for(int i=0;i<train.passengerCount();++i)
{
System.out.println(train.getPassenger(i).getName());
}
System.out.println("Passenger Count: "+train.passengerCount());
System.out.println("Total Revenue for train "+train.calculateRevenue(10, 7));
System.out.println("Percentage of first class passengers "+train.percentageFirstClass());
System.out.println(train.toString());
}
}
Output:
Passenger Count 0
James
Ben
Suri
Sarah
Jane
Rocky
Megan
Lindsay
Karla
Mike
Passenger Count: 10
Total Revenue for train 88
Percentage of first class passengers 60.0
Name: James Service Class: 1
Name: Ben Service Class: 2
Name: Suri Service Class: 1
Name: Sarah Service Class: 1
Name: Jane Service Class: 2
Name: Rocky Service Class: 2
Name: Megan Service Class: 1
Name: Lindsay Service Class: 1
Name: Karla Service Class: 2
Name: Mike Service Class: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.