PET LIST Create a class called Pet which contains: - A field for the name of the
ID: 3770621 • Letter: P
Question
PET LIST
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. Have all .java files.
Explanation / Answer
// Pet.java
class Pet {
String name;
int age;
public Pet(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
// Dog.java
class Dog extends Pet {
String breed;
double weight;
public Dog(String name, int age, String breed, double weight) {
super(name, age);
this.breed = breed;
this.weight = weight;
}
public String getBreed() {
return breed;
}
public double getWeight() {
return weight;
}
@Override
public String toString() {
return "Dog [breed=" + breed + ", weight=" + weight + ", name=" + name
+ ", age=" + age + "]";
}
}
// Cat.java
class Cat extends Pet {
String coat;
boolean isLap;
public Cat(String name, int age, String coat, boolean isLap) {
super(name, age);
this.coat = coat;
this.isLap = isLap;
}
public String getCoat() {
return coat;
}
public boolean isLap() {
return isLap;
}
@Override
public String toString() {
return "Cat [coat=" + coat + ", isLap=" + isLap + ", name=" + name
+ ", age=" + age + "]";
}
}
// Fish.java
class Fish extends Pet {
String type;
String color;
public Fish(String name, int age, String type, String color) {
super(name, age);
this.type = type;
this.color = color;
}
public String getType() {
return type;
}
public String getColor() {
return color;
}
@Override
public String toString() {
return "Fish [type=" + type + ", color=" + color + ", name=" + name
+ ", age=" + age + "]";
}
}
// Driver22.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Driver22 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of pets: ");
int n = sc.nextInt();
ArrayList<Pet> pets = new ArrayList<Pet>();
String name, breed, coat, type, color;
int age;
double weight;
boolean lap;
for (int i = 0; i < n; i++) {
System.out.print("Enter the type of pet: ");
int petType = sc.nextInt();
System.out.print("Enter name: ");
name = sc.next();
System.out.print("Enter age: ");
age = sc.nextInt();
switch (petType) {
case 1:
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;
case 2:
System.out.print("Enter coat of Cat: ");
coat = sc.next();
System.out.print("Is cat lap: ");
lap = sc.nextBoolean();
pets.add(new Cat(name, age, coat, lap));
break;
case 3:
System.out.print("Enter type: ");
type = sc.next();
System.out.print("Enter color: ");
color = sc.next();
pets.add(new Fish(name, age, type, color));
break;
default:
System.out.println("Enter correct type of Pet!!!");
i--;
break;
}
}
for (Iterator iterator = pets.iterator(); iterator.hasNext();) {
Pet pet = (Pet) iterator.next();
if (pet instanceof Fish) {
System.out.println(pet);
}
}
for (Iterator iterator = pets.iterator(); iterator.hasNext();) {
Pet pet = (Pet) iterator.next();
if (pet instanceof Cat) {
System.out.println(pet);
}
}
for (Iterator iterator = pets.iterator(); iterator.hasNext();) {
Pet pet = (Pet) iterator.next();
if (pet instanceof Dog) {
System.out.println(pet);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.