Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homework 4-2 Textbook Section 9.10 Create a class to represent a Person object that inherits from ( extends ) the Passenger class. Use the description

Homework 4-2 Textbook Section 9.10

Create a class to represent aPerson object that inherits from (extends) thePassenger class.

Use the description provided below in UML.

Person

  • numOffspring : int
  • Person() //default values for numOffspring should be 0
  • Person(int)
  • Person(String, int, double, double, char, int, int) //pass values to parent's overloaded constructor //and assign valid values to numOffspring or 0 if invalid
  • setNumOffspring(int) : void //if the value is invalid set numOffspring to 0
  • getNumOffspring() : int
  • printDetails() : void @Override //see Note1 below:
  • toString() : String @Override //see Note1 below:
  • equals(Object) : boolean @Override //see Note2:

NOTE1:

For both theprintDetails() andtoString() methods include the data from the Passenger class, as well as Person attributes. Format the Person data as "Person: Number of Offspring: %4d "

Hint:Use super.printDetails() and super.toString()

NOTE2:

For theequals(Object) method, two Person objects are considered equal if the

parent class's equals method returns true, if theirnumOffspring are equal

BELOW IS THE JAVA CODE FOR PASSENGER: public class Passenger {

private String name; private int birthYear; private double weight; private char gender; private int numCarryOn; private double height;

public Passenger() { this.name = ""; this.birthYear = 1990; this.weight = 0.0; this.gender = 'u'; this.numCarryOn = 0; }

public Passenger(String name, int birthYear, double weight, double height, char gender, int numCarryOn) { this.setName(name); this.setBirthYear(birthYear); this.setWeight(weight); this.setHeight(height); this.setGender(gender); this.setNumCarryOn(numCarryOn); }

public double getHeight() { return height; }

public void setHeight(double height) { if (height < 0) { height = -1; } this.height = height; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getBirthYear() { return birthYear; }

public void setBirthYear(int birthYear) { this.birthYear = birthYear; }

public double getWeight() { return weight; }

public void setWeight(double weight) { if (weight < 0) { weight = -1; } this.weight = weight; }

public char getGender() { return gender; }

public void setGender(char gender) { if (gender != 'f' && gender != 'm') { gender = 'u'; } this.gender = gender; }

public int getNumCarryOn() { return numCarryOn; }

public boolean isFemale() { return (this.gender == 'F' || this.gender == 'f'); }

public boolean isMale() { return (this.gender == 'M' || this.gender == 'm'); }

public void setNumCarryOn(int numCarryOn) { if (numCarryOn > 2) { numCarryOn = 2; } if (numCarryOn < 0) { numCarryOn = 0; } this.numCarryOn = numCarryOn; }

public double calculateBMI() { double BMI = (this.getWeight() * 703) / (Math.pow(getHeight(), 2)); return BMI; }

public int calculateAge(int currYear) { int age = currYear - getBirthYear(); if (age < 0) { age = -1; } return age; }

public void gainWeight() { setWeight(getWeight() + 1); }

public void gainWeight(double weight) { setWeight(getWeight() + weight); if (Double.compare(getWeight(), 0.0) < 0) { setWeight(0.0); } }

public void loseWeight() { setWeight(getWeight() - 1); if (Double.compare(getWeight(), 0.0) < 0) { setWeight(0.0); } }

public void loseWeight(double weight) { setWeight(getWeight() - weight); if (Double.compare(getWeight(), 0.0) < 0) { setWeight(0.0); } }

public void printDetails() { System.out.printf( "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %10c | Number of Carry On: %10d ", this.getName(), this.getBirthYear(), this.getWeight(), this.getHeight(), this.getGender(), this.getNumCarryOn()); }

@Override public String toString() { String s = String.format( "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %10c | Number of Carry On: %10d", this.getName(), this.getBirthYear(), this.getWeight(), this.getHeight(), this.getGender(), this.getNumCarryOn()); return s; }

@Override public boolean equals(Object p) { if (p instanceof Passenger) { Passenger pass = (Passenger) p; return (getBirthYear() == pass.getBirthYear() && getGender() == pass.getGender() && getName().equals(pass.getName()) && getWeight() - pass.getWeight() <= 0.5 && getHeight() - pass.getHeight() <= 0.5); } return false; } }

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions