java Create a class called Pet which contains: - A field for the name of the pet
ID: 3770240 • Letter: J
Question
java
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.
Explanation / Answer
//Pet.java
public class Pet
{
//instance variables
private String name;
private int age;
//Constructor to set name and age
public Pet(String name, int age)
{
this.name=name;
this.age=age;
}
//Set name of Pet
public void setName(String name)
{
this.name=name;
}
//Set age of Pet
public void setage(int age)
{
this.age=age;
}
//Returns name of pet
public String getName()
{
return name;
}
//Returns age of pet
public int getage()
{
return age;
}
}//end of Pet class
----------------------------------------------------------------------
//Dog.java
//The Dog class extends Pet class
public class Dog extends Pet
{
private String breed;
private double weight;
//Construcor to set name,age, breed and weight
public Dog(String name, int age,
String breed, double weight)
{
super(name, age);
this.breed=breed;
this.weight=weight;
}
//Set breed
public void setBreed(String breed)
{
this.breed=breed;
}
//Set weight
public void setWeight(double weight)
{
this.weight=weight;
}
//Return breed
public String getBreed()
{
return breed;
}
//Return weight
public double getWeight()
{
return weight;
}
@Override
public String toString() {
return "Name: "+getName()
+"Age: "+getage()
+"Breed: "+getBreed()
+"Weight: "+getWeight();
}
}//end of Dog
----------------------------------------------------------------------
//Cat.java
//The Cat class extends Pet class
public class Cat extends Pet
{
//instance varibels of Cat class
private String coat;
private boolean lapCat;
//Costructor
public Cat(String name, int age,
String coat, boolean lapCat)
{
super(name, age);
this.coat=coat;
this.lapCat=lapCat;
}
//Set coat
public void setCoat(String coat)
{
this.coat=coat;
}
//Set lap cat
public void setLap(boolean lapCat)
{
this.lapCat=lapCat;
}
//Return coat
public String getCoat()
{
return coat;
}
//Return lap
public boolean getLap()
{
return lapCat;
}
/*Override toString method that
returns the string representation of Cat object */
@Override
public String toString() {
return "Name: "+getName()
+"Age: "+getage()
+"Coat: "+coat
+"Lapcat: "+lapCat;
}
}
----------------------------------------------------------------------
//Fish.java
//Dish class extends Pet class
public class Fish extends Pet
{
//instance variables
private String type;
private String color;
//Fish constructor
public Fish(String name, int age,
String type, String color)
{
//Calling Pet class constructor
super(name, age);
this.type=type;
this.color=color;
}
//Set type of fish
public void setType(String type)
{
this.type=type;
}
//Set color of fish
public void setColor(String color)
{
this.color=color;
}
//Return coat
public String getType()
{
return type;
}
//Return color
public String getColor()
{
return color;
}
/*Override toString method that
returns the string representation of Fish object */
@Override
public String toString() {
return "Name: "+getName()
+"Age: "+getage()
+"Type: "+type
+"Color: "+color;
}
}//end of Fish class
----------------------------------------------------------------------
/*Tester program that prompts user to enter number of pets to enter.
* Then prompts the corrsponding values for dog, cat and fish.
* */
//Driver.java
import java.util.ArrayList;
import java.util.Scanner;
public class Driver
{
//Create an instance of Scanner
private static Scanner scanner =new Scanner(System.in);
public static void main(String[] args)
{
//declare an integer nPets
int nPets;
//Create an instance of ArrayLIst of type Pet
ArrayList<Pet>list=new ArrayList<Pet>();
System.out.println("Enter number of pet's ?");
//read nPets
nPets=Integer.parseInt(scanner.nextLine());
for (int index = 0; index < nPets; index++)
{
int choice=menu();
switch (choice)
{
case 1:
//calling readDog
Dog dog=readDog();
//Add to list
list.add(dog);
break;
case 2:
//calling readCat
Cat cat=readCat();
//Add to list
list.add(cat);
break;
case 3:
//calling readFish
Fish fish=readFish();
//Add to list
list.add(fish);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
//print fish list first
System.out.println("Fish List");
for (Pet pet : list)
{
if(pet instanceof Fish)
System.out.println(pet.toString());
}
System.out.println("Cat List");
//print cat list
for (Pet pet : list)
{
if(pet instanceof Cat)
System.out.println(pet.toString());
}
System.out.println("Dog List");
//print dog list
for (Pet pet : list)
{
if(pet instanceof Dog)
System.out.println(pet.toString());
}
}
//Read fish object values
private static Fish readFish()
{
String name;
int age;
String type;
String color;
System.out.println("Enter name :");
name=scanner.nextLine();
System.out.println("Enter age :");
age=Integer.parseInt(scanner.nextLine());
System.out.println("Enter type :");
type=scanner.nextLine();
System.out.println("Enter color :");
color=scanner.nextLine();
//return Fish object
return new Fish(name, age, type, color);
}
//Read cat object values
private static Cat readCat() {
String name;
int age;
String coat;
boolean lap;
System.out.println("Enter name :");
name=scanner.nextLine();
System.out.println("Enter age :");
age=Integer.parseInt(scanner.nextLine());
System.out.println("Enter coat :");
coat=scanner.nextLine();
System.out.println("Enter y if cat is lap, or enter n :");
char chlap=scanner.nextLine().charAt(0);
if(chlap=='y')
lap=true;
else
lap=false;
//Return Cat object
return new Cat(name, age, coat, lap);
}
//Read dog object values
private static Dog readDog()
{
String name;
int age;
String breed;
double weight;
System.out.println("Enter name :");
name=scanner.nextLine();
System.out.println("Enter age :");
age=Integer.parseInt(scanner.nextLine());
System.out.println("Enter breed :");
breed=scanner.nextLine();
System.out.println("Enter weight :");
weight=Double.parseDouble(scanner.nextLine());
//Return Dog object
return new Dog(name, age, breed, weight);
}
/**The method menu that prints menu choices*/
private static int menu()
{
System.out.println("1.Dog");
System.out.println("2.Cat");
System.out.println("3.Fish");
System.out.println("Enter your choice");
int choice=Integer.parseInt(scanner.nextLine());
return choice;
}
}
----------------------------------------------------------------------
Sample output:
Enter number of pet's ?
3
1.Dog
2.Cat
3.Fish
Enter your choice
1
Enter name :
Jim
Enter age :
3
Enter breed :
france
Enter weight :
20.5
1.Dog
2.Cat
3.Fish
Enter your choice
2
Enter name :
nani
Enter age :
4
Enter coat :
small
Enter y if cat is lap, or enter n :
y
1.Dog
2.Cat
3.Fish
Enter your choice
3
Enter name :
Golden fish
Enter age :
1
Enter type :
sea
Enter color :
gold
Fish List
Name: Golden fishAge: 1Type: seaColor: gold
Cat List
Name: naniAge: 4Coat: smallLapcat: true
Dog List
Name: JimAge: 3Breed: franceWeight: 20.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.