Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Pet which contains: - A field for the name of the pet - A

ID: 3768041 • Letter: C

Question

 Create a class called Pet which contains:         - A field for the name of the pet         - A field for the age of the pet         - Appropriate constructor and accessors Create a class called Dog which extends the Pet class and has:         - A field for breed of dog         - A field for body weight         - Appropriate constructor and accessors         - A toString method that prints the name, age, breed and weight of the dog Create a class called Cat which extends the Pet class and has:         - A field that describes the coat of the cat (example: short/long/plush/silky/soft)         - A field for whether it is a lap cat         - Appropriate constructor and accessors         - A toString method that prints the name, age and coat type of the cat, and whether it is a lap cat Create a class called Fish which extends the Pet class and has:         - A field for type of fish         - A field for the color of its scales         - Appropriate constructor and accessors         - A toString method that prints the name, age, type and scale color of the fish  Write a main which asks the user to enter the number of pets (n) and then ask for the details for each pet. Ask for the correct information depending on the type of pet. Create a Dog,Cat or Fish object as required. Add each pet to an ArrayList of Pets. After all information is entered and stored, print out the gathered information of all objects in the list, starting with the all the Fish first, then Cats and then Dogs.  Hint: You can use the instanceOf operator (Page 667 of your textbook) to find out if a pet is a fish, cat or dog. 

Explanation / Answer

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Scanner;

class Pet {

   private String name;

private int age;

  

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public int getAge() {

       return age;

   }

   public void setAge(int age) {

       this.age = age;

   }

  

   public Pet(String n, int a) {

       name=n;

       age=a;

   }

  

@Override

   public String toString() {

       String str = "Name: "+getName()+" ";

       str+="Age: "+getAge()+" ";

       return str;

   }

}

class Dog extends Pet {

   private String breed;

private double weight;

  

   public Dog(String n, int a, String br, double w) {

       super(n, a);

       breed=br;

       weight=w;

   }

  

@Override

   public String toString() {

       String str=super.toString();

       str+="Breed: "+getBreed()+" ";

       str+="Weight: "+getWeight()+" ";

       return str;

   }

   public String getBreed() {

       return breed;

   }

   public void setBreed(String breed) {

       this.breed = breed;

   }

   public double getWeight() {

       return weight;

   }

   public void setWeight(double weight) {

       this.weight = weight;

   }

}

class Cat extends Pet {

   private String coat;

private boolean isLapCat;

  

   public Cat(String n, int a, String c, boolean l) {

       super(n, a);

       coat=c;

       isLapCat = l;

   }

   public String getCoat() {

       return coat;

   }

   public void setCoat(String coat) {

       this.coat = coat;

   }

   public boolean isLapCat() {

       return isLapCat;

   }

   public void setLapCat(boolean isLapCat) {

       this.isLapCat = isLapCat;

   }

  

@Override

   public String toString() {

       String str=super.toString();

       str+="Coat Type: "+getCoat()+" ";

       str+="Is Lap Cat: "+isLapCat()+" ";

       return str;

   }

}

class Fish extends Pet {

   private String type;

   private String color;

  

   public String getType() {

       return type;

   }

   public void setType(String type) {

       this.type = type;

   }

   public String getColor() {

       return color;

   }

   public void setColor(String color) {

       this.color = color;

   }

   public Fish(String n, int a, String t, String c) {

       super(n, a);

       type=t;

       color=c;

   }

  

@Override

   public String toString() {

       String str=super.toString();

       str+="Type of fish: "+getType()+" ";

       str+="Color of scales: "+getColor()+" ";

       return str;

   }

}

public class Test {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

      

       System.out.print("Enter n: ");

       int n=sc.nextInt();

      

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

      

       for (int i = 0; i < n; i++) {

           System.out.print("1 to enter fish 2 to enter cat 3 to enter dog enter: ");

           int ch = sc.nextInt();

          

           String name;

           int age;

           System.out.print("Enter name: ");

           name=sc.next();

           System.out.print("Enter age: ");

           age=sc.nextInt();

          

           switch (ch) {

               case 1:

                   String type;

                   String color;

                   System.out.print("Enter type of fish: ");

                   type=sc.next();

                   System.out.print("Enter color of scales: ");

                   color=sc.next();

                   pets.add(new Fish(name, age, type, color));

                   break;

                  

               case 2:

                   String coat;

                   boolean isLapCat;

                   System.out.print("Enter coat type: ");

                   coat=sc.next();

                   System.out.print("Is it a lap cat? 1 for yes and 0 for no: ");

                   isLapCat=(sc.nextInt() == 1 ? true : false);

                   pets.add(new Cat(name, age, coat, isLapCat));

                   break;

                  

               case 3:

                   String breed;

                   double weight;

                   System.out.print("Enter breed: ");

                   breed = sc.next();

                   System.out.print("Enter weight: ");

                   weight=sc.nextDouble();

                   pets.add(new Dog(name, age, breed, weight));

                   break;

  

               default:

                   break;

           }

       }

      

       System.out.println("All fishes:");

       for (Iterator iterator = pets.iterator(); iterator.hasNext();) {

           Pet pet = (Pet) iterator.next();

           if (pet instanceof Fish) {

               System.out.println(pet);

           }

       }

      

       System.out.println("All Cats:");

       for (Iterator iterator = pets.iterator(); iterator.hasNext();) {

           Pet pet = (Pet) iterator.next();

           if (pet instanceof Cat) {

               System.out.println(pet);

           }

       }

      

       System.out.println("All Dogs:");

       for (Iterator iterator = pets.iterator(); iterator.hasNext();) {

           Pet pet = (Pet) iterator.next();

           if (pet instanceof Fish) {

               System.out.println(pet);

           }

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote