Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective Practice polymorphism, array of objects, Object class and interfaces Creativity Feel free to be creative by adding more functionalities. Make sure that the required

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Objective Practice polymorphism, array of objects, Object class and interfaces Creativity Feel free to be creative by adding more functionalities. Make sure that the required functionality is not changed. Problem Amtrak has asked us to create an application to help them keep track of the list of their passengers in a given train. They also want to be able to do the following to list of the passengers Display the information of all the passengers in the train Delete a passenger from the list in a train Add a passenger to the list of the train Count the number of the passengers in the train Search the list of the passengers to find a particular person in the train display the last name of all the passengers A passenger has the following attributes first name Last name phone number email Seat number class (first class, coach class, business class) Train A train has a list of the passengers. Therefore, Train class does not extend the passenger class Design In order to apply the OOP, we will need to create the following user defined data types Person class Passenger class (A passenger is a person) Train class (a train has a list of the passengers) List interface Classes descriptions 1. Person class with the attributes, name, last, phone number and email address. This class will have the constructor, getter methods, setter methods, equals method and toString method. The method header for the equals method must be public boolean equals (Object o). In this method you must use instanceof keyword to make sure that the parameter o passed to this method is of type Person, then type cast it to the Person class. Then compare the last names of the two objects. If the last names are the same then you must compare the two passengers based on their first name. Person class -name: String -last name: String -phone: String email: String +Person (String name, String last, String phone, String email) setFirst(String name): void +setLast(String las): void +setPhone(String phone): void +setEmail(String email): void +getFirst(): String +getLast(): String +getPhone(): String +getEmail(): String +toString(): String +equals(Object o): boolean 2. Passenger class extends the person class since a passenger is a person. Passenger has the following attributes: seat number, class (first, coach, business). This class will have constructor, getter methods, setter methods, toString method, remember to call the toString methods from the parent class in the toString method for this class. There is no need to override the equals method from the Person class. Passenger class //extends the person class +seatNumber: int +class String +Passenger(String first, String last, String phone, String email, int seatNumber, String class) +setSeatNumber(int num): void +setClass(String class): void +getClass(): String +getSeatNumber(): int +toString(): String // must call the tostring method from the person class Page 3. List interface copy and paste the following interface into your program interface list public boolean add (object o) public Object search (Object o); public boolean delete (Object 0); public void print Last (); ) 1. Train class must implement the List interface. Train class has a list of the passengers (has -a relationship between the Trian class and the passenger class). a. Therefore, the attribute for this class is an array of passengers (Passenger train). b. This class should have a class variable to count the number of the passengers in the train. Every time a passenger is added to this train, the count must be incremented. Every time a passenger is deleted from the list, the count must be decremented by 1. c. This class must have a constructor to instantiate the array of passengers. This should be the only line of code in the constructor train = new Passenger [10]. Th d. Implement the toString method by using a for loop traversing the list(array) of the passengers, call the tostring method on each element of the array train[i].toString() in the for loop. e. getCount method is a static method. This method returns the number of the passengers in the train to call this method from the driver class: Train.getCount() f. This class implements the List interface providing the body for the methods add, search, delete, and printlast. Train +public static int count = 0; +train: Passenger[: +trainNumber: int +Train() +get Train(): Passenger +get TrainNumber(): int +setTrainNumber(int num): void +equals(Object o): Boolean +toString(): String +public static int getCount(); int //methods form the interface Description of the methods listed in the List interface that must be implemented in the Trian class public boolean add (Object o): This method gets the object o as its parameter. This method must check the class type of the Object o by using the keyword instanceof. if the type is of type Passenger, type cast it to the Passenger class then add it to the array. Every time a Passenger is added to the list, the class variable count must be incremented, don't forget to increment the count by 1. public Object search (object o: This method searches the list of the passenger to find the passenger with the particular last name, this method gets the Object o as its parameter. However, the Object o represents the passenger's last name therefore using the keyword instanceof would check the Class type of the Object o to see if it is of string type, if it is then type cast o to String class. Then search the array using the last name. if the last name is found return the object at the given index. Here is the code for this method; public object search object o) boolean b = o instanceof String: if(!) return null; String name = (string); for (int i 0; i

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

3642271561, 978-3642271564

More Books

Students also viewed these Databases questions

Question

Stages of a Relationship?

Answered: 1 week ago