Question

Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes...

Inheritance and Polymorphism (use Pet.java)

  1. You are given a class named Pet.java.   Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods.   In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList. You don’t have to get attributes from user, you can make up information. Then, print the contents of the ArrayList in a readable fashion. Turn in Cat.java and Dog.java.

/**
* Parent class for inheritance question in sample final.
*

*
*/
public class Pet
{
   private String name;
   private int age;
   private String type;

   /**
   * @param name
   * @param age
   * @param type
   */
   public Pet(String name, int age, String type)
   {
       super();
       this.name = name;
       this.age = age;
       this.type = type;
   }

   /**
   * @return the name
   */
   public String getName()
   {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name)
   {
       this.name = name;
   }

   /**
   * @return the age
   */
   public int getAge()
   {
       return age;
   }

   /**
   * @param age
   * the age to set
   */
   public void setAge(int age)
   {
       this.age = age;
   }

   /**
   * @return the type
   */
   public String getType()
   {
       return type;
   }

   /**
   * @param type
   * the type to set
   */
   public void setType(String type)
   {
       this.type = type;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString()
   {
       return "Pet [name=" + name + ", age=" + age + ", type=" + type + "]";
   }

}

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

// do comment if any problem arises

// code

Pet.java:

public class Pet

{

   private String name;

   private int age;

   private String type;

   /**

   * @param name

   * @param age

   * @param type

   */

   public Pet(String name, int age, String type)

   {

       super();

       this.name = name;

       this.age = age;

       this.type = type;

   }

   /**

   * @return the name

   */

   public String getName()

   {

       return name;

   }

   /**

   * @param name

   * the name to set

   */

   public void setName(String name)

   {

       this.name = name;

   }

   /**

   * @return the age

   */

   public int getAge()

   {

       return age;

   }

   /**

   * @param age

   * the age to set

   */

   public void setAge(int age)

   {

       this.age = age;

   }

   /**

   * @return the type

   */

   public String getType()

   {

       return type;

   }

   /**

   * @param type

   * the type to set

   */

   public void setType(String type)

   {

       this.type = type;

   }

   /*

   * (non-Javadoc)

   *

   * @see java.lang.Object#toString()

   */

   @Override

   public String toString()

   {

       return "Pet [name=" + name + ", age=" + age + ", type=" + type + "]";

   }

}

Cat.java:

public class Cat extends Pet {

    private String color;

    private String breed;

    // constructor for Cat

    public Cat(String name, int age, String color, String breed) {

        super(name, age, "Cat");

        this.color = color;

        this.breed = breed;

    }

    // getters and setters

    public String getColor() {

        return color;

    }

    public String getBreed() {

        return breed;

    }

    public void setColor(String color) {

        this.color = color;

    }

    public void setBreed(String breed) {

        this.breed = breed;

    }

    // tostring method

    @Override

    public String toString() {

        return "Cat [name=" + super.getName() + ", age=" + super.getAge() + ", color=" + color + ", breed=" + breed

                + "]";

    }

}

Dog.java:

public class Dog extends Pet {

    private String breed;

    private String size;

    // constructor for Dog

    public Dog(String name, int age, String size, String breed) {

        super(name, age, "Dog");

        this.breed = breed;

        this.size = size;

    }

// getters and setters

    public String getBreed() {

        return breed;

    }

    public String getSize() {

        return size;

    }

    public void setBreed(String breed) {

        this.breed = breed;

    }

    public void setSize(String size) {

        this.size = size;

    }

    // tostring method

    @Override

    public String toString() {

        return "Dog [name=" + super.getName() + ", age=" + super.getAge() + ", breed=" + breed + ", size=" + size + "]";

    }

}

PetDriver.java:

import java.util.ArrayList;

public class PetDriver {

    public static void main(String[] args) {

        ArrayList<Pet> pets = new ArrayList<>();

        pets.add(new Dog("Billu", 5, "small", "Bull Dog"));

        pets.add(new Cat("Billu", 10, "Brown", "Persian Cat"));

        for (int i = 0; i < pets.size(); i++) {

            System.out.println(pets.get(i));

        }

    }

}

Output:

Add a comment
Know the answer?
Add Answer to:
Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes...
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
  • LAB: Pet information (derived classes)

    LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information:     Name: Dobby    Age: 2 Pet Information:     Name: Kreacher    Age: 3    Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Pet myPet = new Pet();       Dog myDog = new Dog();              String petName, dogName, dogBreed;       int petAge, dogAge;       petName = scnr.nextLine();       petAge = scnr.nextInt();       scnr.nextLine();...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Use inheritance to create a new class AudioRecording based on Recording class that: it will retain...

    Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);     

    public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);               private int minAge;               private Rating(int i)        {              minAge = i;        }               public int getMinAge()        {              return minAge;        }               public void setMinAge(int age)        {              minAge = age;        }               public String toString()        {              switch(this)              {              case GENERAL:                     return "G";              case PARENTALGUIDANCE:                     return "P";              case MATURE:                     return...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • 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