Question

Assignment (to be done in Java):

Change the Person class to an abstract class.

Person Class:


public class Person extends Passenger{
private int numOffspring;

public Person() {
   this.numOffspring = 0;
}

public Person (int numOffspring) {
   this.numOffspring = numOffspring;
}

public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring)
{
   super(name, birthYear, weight, height, gender, numCarryOn);
  
   if(numOffspring < 0) {
       this.numOffspring = 0;
   }
   this.numOffspring = numOffspring;
}

public int getNumOffspring() {
   return numOffspring;
   }
public void setNumOffspring(int numOffspring) {
   if(numOffspring < 0)
       this.numOffspring = 0;
   this.numOffspring = numOffspring;
}

@Override
public void printDetails() {
   System.out.printf("Person: Number of Offspring: %4d\n", getNumOffspring());
  
}

@Override
public boolean equals(Object p)
{
   if (p instanceof Person)
   {
   Person person = (Person) p;
   return (super.equals(p) && getNumOffspring() == person.getNumOffspring());
   }
   return false;
   }

}

Passenger Class (just in case):

import java.util.Calendar;
import java.util.Date;

public class Passenger
{
private String name;
private int birthYear;
private double weight;
private double height;
private char gender;
private int numCarryOn;
  

public Passenger()
{
this.name="";
this.birthYear=1900;
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.name=name;
this.birthYear=birthYear;
this.weight=weight;
this.height=height;
this.gender=gender;
this.numCarryOn=numCarryOn;
}   
  
public double calculateBMI ()
{
return ((weight * 703) / (height * height));
}
  

public int calculateAge(int currentYear)
{

if(currentYear < birthYear)
return -1;
else
return currentYear-this.birthYear;
}

public void gainWeight()
{
this.weight+=1;
}

public void gainWeight(double gain)
{
this.weight+=gain;
}

public int getBirthYear()
{
return this.birthYear;
}

public char getGender()
{
return this.gender;
}

public String getName()
{
return this.name;
}

public double getWeight()
{
return this.weight;
}

public double getHeight()
{
return this.height;
}


public int getNumCarryOn()
{
return this.numCarryOn;
}

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

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

public void loseWeight()
{
this.weight-=1;
if(this.weight < 0) {
System.out.println("Weight can not drop below zero");
this.weight+=1;
}
}

public void loseWeight(double loss)
{
this.weight-=loss;
if(this.weight < 0) {
System.out.println("Weight can not drop below zero");
this.weight+=loss;
}
}
  
@Override
public String toString ()
{
String s = String.format ("Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %c | NumCarryOn: %2d\n",
this.name,this.birthYear,this.weight, this.height, this.gender,this.numCarryOn);
return s;
}
  
@Override
public boolean equals (Object o)
{
if (o == this) return true;
  
Passenger p = (Passenger) o;
if ( (p.birthYear == this.birthYear) && (p.gender == this.gender) && (p.name == this.name) &&
(Math.abs(p.weight - this.weight) < 0.5) && (Math.abs(p.height - this.height) < 0.5) )
return true;
else
return false;
}
  

public void printDetails()
{
System.out.printf("Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %c |"
+ " NumCarryOn: %2d\n",this.name,this.birthYear,this.weight, this.height, this.gender,this.numCarryOn);
}


public void setGender(char gender)
{
if(gender != 'm' || gender != 'f' ||(gender != 'M' || gender != 'F'))
this.gender='u';
else
this.gender = gender;
}


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

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

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


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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You only need to add abstract keyword in front of Person class and then create some other class (in my code Individual) which inherits from it. See the code below:

// abstract keyword addded
abstract class Person extends Passenger{
        private int numOffspring;
        public Person() {
           this.numOffspring = 0;
        }

        public Person (int numOffspring) {
           this.numOffspring = numOffspring;
        }

        public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring)
        {
           super(name, birthYear, weight, height, gender, numCarryOn);
          
           if(numOffspring < 0) {
               this.numOffspring = 0;
           }
           this.numOffspring = numOffspring;
        }
        
        public int getNumOffspring() {
           return numOffspring;
        }
        public void setNumOffspring(int numOffspring) {
           if(numOffspring < 0)
               this.numOffspring = 0;
           this.numOffspring = numOffspring;
        }

        @Override
        public void printDetails() {
           System.out.printf("Person: Number of Offspring: %4d\n", getNumOffspring());
          
        }

        @Override
        public boolean equals(Object p)
        {
           if (p instanceof Person)
           {
                   Person person = (Person) p;
                   return (super.equals(p) && getNumOffspring() == person.getNumOffspring());
           }
           return false;
        }
        
        
}

// this is the new class which inherits from abstract class Person
class Individual extends Person {

}

As we made the Person class as abstract, now it can't be instantiated. To make that possible in a virtual fashion, we make another class which inherits from it and hence can access all its public attributes & public methods.

From the code, you can see I've created a class Individual which inherits from abstract class Person.

This class can be instantiated & can be used to access Person's methods as shown below:

// this is just the main function
public static void main(String[] args) {
                
        Individual i = new Individual();
        i.setNumOffspring(2);
        i.printDetails();
}

Sample Output for this above code snippet:

<terminated > AbstractClass [Java Application] D:\Installed Softwares\java-11.0.8\bin\javaw.exe (Oct 6, 2020, 2:43:09 PM) Per

Hope this solves your concern. For any further doubt, feel free to reach back, I'd be happy to help! :)

If it helped, please consider giving an upvote, I'd appreciate that! :)

Cheers & Have a great learning ahead!

Add a comment
Know the answer?
Add Answer to:
Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have done a decent amount of coding... but I need you to help me understand...

    I have done a decent amount of coding... but I need you to help me understand what I do not. Please, post comments so that I can learn from you. Here are the instructions for this portion of the project: Create the Monkey class, using the specification document as a guide. The Monkey class must do the following: Inherit from the RescueAnimal class Implement all attributes with appropriate data structures Include accessors and mutators for all implemented attributes Here is...

  • import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public...

    import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weigth) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators ...... } public class Breed { private String name; private double averageWgt; public Breed(String name,double averageWgt) { this.name = name; this.averageWgt= averageWgt; } public double getWeight() { return averageWgt;...

  • What is the final value of the count field? public class Person { private String name;...

    What is the final value of the count field? public class Person { private String name; private int age; private static int count=0; public Person(String name, int age) { this.name = name; this.age age; count++; } public static void main(String[] args) { Person a - new Person("John Doe". 35): Person b new Person("Jane Doe", 38): for(int i=0;i<5;i++) Person tempa

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • public class CylindricalTank {    private String idTag;           // Unique String identifying the tank...

    public class CylindricalTank {    private String idTag;           // Unique String identifying the tank    private double radius;           // The non-negative radius of the base of the tank in meters    private double height;           // The non-negative height of the tank in meters    private double liquidLevel;       // The current height of the liquid in the tank in meters    /**    * CylindricalTank General Constructor    */    public CylindricalTank(String tag, double...

  • Write Junit test for the following class below: public class Player {       public int turnScore;    public int roundScore;    public int lastTurnScore;    public String name;    public int chipPile;...

    Write Junit test for the following class below: public class Player {       public int turnScore;    public int roundScore;    public int lastTurnScore;    public String name;    public int chipPile;       public Player (String name) {        this.name = name;        this.turnScore = 0;        this.chipPile = 50;    }          public int getChip() {        return chipPile;    }       public void addChip(int chips) {        chipPile += chips;    }       public int getRoundScore() {        return roundScore;    }       public void setRoundScore(int points) {        roundScore += points;    }       public int getTurnScore() {        return turnScore;   ...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT