Question
Note: Passenger.java is provided at the bottom of this Create four new classes, FirstClassPassenger , SecondClassPassenger , ThirdClassPassenger , CrewPassenger , each of which inherit
Note: Passenger.java is provided at the bottom of this
Create four new classes, FirstClassPassenger, SecondClassPassenger, ThirdClassPassenger, CrewPassenger, each of which inherit from the Passenger class, like this:
public class FirstClassPassenger extends Passenger { public FirstClassPassenger(boolean child, string sex, boolean survior) { super(1, child, sex, survivor); // 1 = 1st class passenger } }
2.Once you have created these four subclasses of Passenger, change the code in PassengerData.addPassenger to construct the specific, appropriate class based on the status parameter in the addPassenger method.
Note that you will need to make use of polymorphism to in your PassengerData class (effectively, this means not changing the type of the collection array, even though each element of it will be allocated as an instance of a subtype)
//Passenger.java
public class Passenger { private int status; private boolean child; private String sex; private boolean survivor; public Passenger(int status, boolean child, String sex, boolean survivor) { this.sex= sex; this.child= child; this.status= status; this.survivor = survivor; } @Override public String toString() { String classStatus=""; if(status==1) classStatus="1st class"; else if(status==2) classStatus="2nd class"; else if(status==3) classStatus="3rd class"; else classStatus="Crew"; String survivorStatus=""; if(survivor==true) survivorStatus="Survived"; else survivorStatus=" did not survive"; String childStatus=""; if(child==true) childStatus="Child"; else childStatus="adult"; String result="The passenger belongs to "+classStatus+" and is "+childStatus+ " Sex: "+sex+" and "+survivorStatus+" "; return result; } }
//TitanicData.java
public class TitanicData { private Passenger[] passengers; private int count; private int numSurvivors; public TitanicData() { passengers= new Passenger[10]; count=0; numSurvivors=0; } public void addPassenger(int status, boolean child, String sex, boolean survivor) { passengers[count]= new Passenger(status,child,sex,survivor); if(survivor==true) numSurvivors++; count++; } public void increaseSize() { Passenger[] temp = new Passenger[passengers.length*2]; for (int i = 0; i < passengers.length; i++) { temp[i] = passengers[i]; } passengers = temp; } @Override public String toString() { String report =" -------------------------------------- "; for (int i = 0; i < count; i++) { report+= passengers[i].toString(); } return report+" The total survived passengers are: "+numSurvivors; } }
//TitanicTester.java
public class TitanicTester { public static void main(String[] args) { TitanicData titanic = new TitanicData(); titanic.addPassenger(4, false, "male", false); titanic.addPassenger(3, false, "female", false); titanic.addPassenger(1, true, "male", true); titanic.addPassenger(2, false, "male", false); titanic.addPassenger(3, true, "female", true); titanic.addPassenger(3, true, "male", false); titanic.addPassenger(1, true, "female", false); titanic.addPassenger(3, false, "female", false); System.out.println(titanic); } }
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