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

JAVA Create a clas caled Pet which contains: - A field for thee name of the pet

ID: 3769879 • Letter: J

Question

 JAVA Create a clas caled Pet which contains:         - A field for thee name of the pet         - A field for the age of the pet         - Appropriate constructor and accessors Create a clas 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. 

Explanation / Answer

import java.util.Scanner;
import java.io.Console;

class Pet
{
   String pet_name = new String();
   int pet_age;

   Pet()
   {
       pet_name = "";
       pet_age = 0;
   }

   public String get_pet_name()
   {
       return pet_name;
   }

   public void set_pet_name(String pet_name)
   {
       this.pet_name = pet_name;
   }
  
   public int get_pet_age()
   {
       return pet_age;
   }

   public void set_pet_age(int pet_age)
   {
       this.pet_age = pet_age;
   }
}

class Dog extends Pet
{
   String dog_breed = new String();
   int body_weight;
  
   Dog()
   {
       dog_breed = "";
       body_weight = 0;
   }
  
   public String get_dog_breed()
   {
       return dog_breed;
   }

   public void set_dog_breed(String dog_breed)
   {
       this.dog_breed = dog_breed;
   }
  
   public int get_body_weight()
   {
       return body_weight;
   }

   public void set_body_weight(int body_weight)
   {
       this.body_weight = body_weight;
   }
  
   public String toString()
   {
       String s1 = " Pet Name: " + pet_name + " Pet Age: " + String.valueOf(pet_age);
       String s2 = " Dog Breed: " + dog_breed + " Dog Body Weight: " + body_weight;
       String s = s1 + s2;
       return s;
   }
}

class Cat extends Pet
{
   String coat_cat = new String();
   boolean lap_cat;
  
   Cat()
   {
       coat_cat = "";
       lap_cat = false;
   }
  
   public String get_coat_cat()
   {
       return coat_cat;
   }

   public void set_coat_cat(String coat_cat)
   {
       this.coat_cat = coat_cat;
   }
  
   public boolean get_lap_cat()
   {
       return lap_cat;
   }

   public void set_lap_cat(boolean lap_cat)
   {
       this.lap_cat = lap_cat;
   }
  
   public String toString()
   {
       String s1 = " Pet Name: " + pet_name + " Pet Age: " + String.valueOf(pet_age);
       String s2 = " Coat of the Cat: " + coat_cat + " Lap Cat: " + lap_cat;
       String s = s1 + s2;
       return s;
   }
}

class Fish extends Pet
{
   String fish_type = new String();
   String scales_color = new String();
  
   Fish()
   {
       fish_type = "";
       scales_color = "";
   }
  
   public String get_fish_type()
   {
       return fish_type;
   }

   public void set_fish_type(String fish_type)
   {
       this.fish_type = fish_type;
   }
  
   public String get_scales_color()
   {
       return scales_color;
   }

   public void set_scales_color(String scales_color)
   {
       this.scales_color = scales_color;
   }
  
   public String toString()
   {
       String s1 = " Pet Name: " + pet_name + " Pet Age: " + String.valueOf(pet_age);
       String s2 = " Fish Type: " + fish_type + " Fish Scales Color: " + scales_color;
       String s = s1 + s2;
       return s;
   }

}

class Pets
{
  
   public static void main(String args[])
   {
       int number_of_pets;
       String pet_type = new String();
      
       Console co = System.console();
      
       System.out.println(" Enter Number of Pets: ");
       number_of_pets = Integer.parseInt(co.readLine());
      
       Object[] p = new Object[number_of_pets];

       for(int i=1; i<number_of_pets; i++)
       {
          
       System.out.println(" Enter type of pet (Dog, Cat, Fish): ");
       pet_type = co.readLine();
      
      
       switch(pet_type)
       {
           case "Dog":          
                           Dog d = new Dog();
                           d.set_pet_name(co.readLine(" Enter Pet Name: "));
                           d.set_pet_age(Integer.parseInt(co.readLine(" Enter Pet Age: ")));
                           d.set_dog_breed(co.readLine(" Enter Dog Breed: "));
                           d.set_body_weight(Integer.parseInt(co.readLine(" Enter Dog Body Weight: ")));
                           p[i] = d;
                           break;
                          
           case "Cat":
                           Cat c = new Cat();
                           c.set_pet_name(co.readLine(" Enter Pet Name: "));
                           c.set_pet_age(Integer.parseInt(co.readLine(" Enter Pet Age: ")));
                           c.set_coat_cat(co.readLine(" Enter Cat Coat: "));
                           c.set_lap_cat(java.lang.Boolean.parseBoolean(co.readLine(" Is this Lap Cat(True/False): ")));
                           p[i] = c;
                           break;
      
           case "Fish":
                           Fish f = new Fish();
                           f.set_pet_name(co.readLine(" Enter Pet Name: "));
                           f.set_pet_age(Integer.parseInt(co.readLine(" Enter Pet Age: ")));
                           f.set_fish_type(co.readLine(" Enter Fish Type: "));
                           f.set_scales_color(co.readLine(" Enter Fish Scales Color: "));
                           p[i] = f;
                           break;
                          
           default:       System.out.println(" Wrong Entry..... ");
       }
      
       }
      
       for(int i=1; i< number_of_pets; i++)
       {
      
           System.out.println(p[i]);
       }
   }

}