Question: make a class to represent an object of type Passenger using the description provided below in UML Passenger - name : String - birthYear :


make a class to represent an object of type Passenger using the description provided below in UML


Passenger


- name : String

- birthYear : int

- weight : double

- gender : char

- numCarryOn : int


+ Passenger() // Set name="", birthYear=1900, weight=0.0, gender='u', numCarryOn=0

+ Passenger(String, int, double, char, int)


+ calculateAge(int) : int // use birthYear and the argument of currentYear to calculate age.

// If currentYear < birthYear, return -1.

+ gainWeight() : void //increase weight by 1.

+ gainWeight(double): void // increase weight by the input amount. Weight cannot drop below zero.

+ getBirthYear() : int

+ getGender() : char

+ getName() : String

+ getWeight() : double

+ getNumCarryOn: int

+ isFemale() : boolean // return true if gender is 'f'

+ isMale() : boolean // return true if gender is 'm'

+ loseWeight() : void // decrease by 1. Weight cannot drop below zero.

+ loseWeight(double) : void // decrease weight by the input amount. Weight cannot drop below zero.

+ printDetails() : void // prints Passenger attributes in the following format:

// "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Gender: %c | NumCarryOn: %2d "

+ setGender(char) : void // if input value is not 'm' or 'f', set gender to 'u' (unknown)

+ setName(String) : void

+ setBirthYear(int) : void

+ setWeight(double) : void //if input value is negative, set weight to -1

+ setNumCarryOn(int): void //if input value is negative, set numCarryOn to 0, if it is greater than 2 set to 2


View javadoc for Passenger and Airplane classes for more detailsHERE


Homework 3-2 Textbook Section 8.21

make a class to represent an Airplane object containing instances of the Passenger objects


Airplane

  • passengers : Passenger [ ]
  • airplaneName : String
  • numPassengers : int //calculated controlled variable no setter

  • Airplane() //default array length = 100 , numPassengers = 0
  • Airplane(String) //default array length = 100 , numPassengers = 0
  • Airplane(int) //max size of array, if value is negative set to 0
  • Airplane(String, int) //name of airplane, max size of array, if value is negative set to 0

  • addPassenger(Passenger) : void
  • getAirplaneName() : String
  • getPassenger(int) : Passenger //return null if index is invalid
  • getNumPassengers() : int //notice no setter... this is a controlled variable
  • getFirstPassenger() : Passenger
  • getLastPassenger() : Passenger
  • getPassengers() : Passenger [ ]
  • setAirplaneName(String) : void
  • printAllDetails() : void // prints Airplane attributes Passenger attributes as formatted below:

// "AirplaneName: %20s | Number of Passengers: %4d | Airplane Size: %4d "

// "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Gender: %c "

  • removePassenger(int) : Passenger
  • removeAllPassengers() : void
  • getTotalWeightOfAllPassengers() : double
  • getAverageWeightOfAllPassengers() : double
  • getNumberOfPassengersAboveWeight(double) : int
  • getNumberOfPassengersBelowWeight(double) : int
  • increaseWeightOfAllPassengers() : void
  • increaseWeightOfAllPassengers(double) : void


View javadoc for Passenger and Airplane classesHERE



Homework 3-3

make a Driver class to use your Airplane and Passenger classes and create instances of them.


In the main method do the following:

  1. Create an Airplane that will store up to 100 Passengers
  2. Create 5 Passenger Objects with the details specified in the table below
  3. Add the 5 Passenger objects to the Airplane
  4. Call the printDetails method from the Airplane to print all the Airplane and Passenger details.


variable name

name

birthYear

weight

gender

numCarryOn

p1

Albert

1879

198.5

'm'

2

p2

Grace

1906

105

'f'

1

p3

Tim

1955

216.3

'm'

2

p4

Steve

1955

160

'm'

2

p5

Sergey

1973

165.35

'm'

1




Step by Step Solution

3.41 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres the Java code implementing the Passenger Airplane and Driver classes as described Java Represents a Passenger object with attributes and methods ... View full answer

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 Programming Questions!