In Java PET LIST Create a class called Pet which contains: A field for the name
ID: 3574358 • Letter: I
Question
In Java
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.Explanation / Answer
Hi,
Please Create a Package named "PetsPackage" having the following classes -
Cat.java
Dog.java
Fish.java
Main.java
Pet.java
Given below is the detailed code for each of the classes along with the comments:
//Cat.java
package PetsPackage;
//class Cat which inherits the class pet
public class Cat extends Pet{
//variables local to the class cat
String catCoat;
String islapCat;
//assign the values to the local variables. Also call the parent constructor for name and age assignment
public Cat(String petName, int petAge, String catCoat, String islapCat) {
super(petName, petAge);
this.catCoat = catCoat;
this.islapCat = islapCat;
}
// to get cat's coat
public String getCatCoat() {
return catCoat;
}
//to set cat's coat
public void setCatCoat(String catCoat) {
this.catCoat = catCoat;
}
// to get islapcat?
public String getIslapCat() {
return islapCat;
}
//to set isLapCat?
public void setIslapCat(String islapCat) {
this.islapCat = islapCat;
}
//String representation of the object
public String toString(){
return "Name,Age,Coat and isLapCat :"+petName+" "+petAge+" "+catCoat+" "+islapCat;
}
}
----------------------------------------------------
//Dog.java
package PetsPackage;
//class Dog which inherits the class pet
public class Dog extends Pet{
//variables local to the class dog
String dogBreed;
int dogWeight;
//to get dog's breed
public String getDogBreed() {
return dogBreed;
}
// to set the dog's breed
public void setDogBreed(String dogBreed) {
this.dogBreed = dogBreed;
}
//to get the dog's weight
public int getDogWeight() {
return dogWeight;
}
//to set the dog'd weight
public void setDogWeight(int dogWeight) {
this.dogWeight = dogWeight;
}
//assign the values to the local variables. Also call the parent constructor for name and age assignment
public Dog(String petName, int petAge, String dogBreed, int dogWeight) {
super(petName, petAge);
this.dogBreed = dogBreed;
this.dogWeight = dogWeight;
}
//String representation of the object
public String toString(){
return "Name,Age,Breed and Weight :" +petName+" "+petAge+" "+dogBreed+" "+dogWeight;
}
}
-----------------------------------------------------
//Fish.java
package PetsPackage;
//class Fish which inherits the class pet
public class Fish extends Pet{
//variables local to the class fish
String fishType;
String fishColor;
//assign the values to the local variables. Also call the parent constructor for name and age assignment
public Fish(String petName, int petAge, String fishType, String fishColor) {
super(petName, petAge);
this.fishType = fishType;
this.fishColor = fishColor;
}
//to get fish type
public String getFishType() {
return fishType;
}
//to set fish type
public void setFishType(String fishType) {
this.fishType = fishType;
}
//to get fish color
public String getFishColor() {
return fishColor;
}
//to set fish color
public void setFishColor(String fishColor) {
this.fishColor = fishColor;
}
////String representation of the object
public String toString(){
return "Name,Age,Type and Scales Color"+petName+" "+petAge+" "+fishType+" "+fishColor;
}
}
--------------------------------------------
//Main.java
package PetsPackage;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
//ArrayList to store the pet details
static List<Pet> pets = new ArrayList<Pet>();
//main method to take the count of pets and iterate
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of pets you have (n)");
int number=input.nextInt();
//iterate for the number of pets entered
for(int i=1;i<=number;i++)
{
System.out.println("Enter the type of your pet "+i+":: DOG/CAT/FISH");
String petType=input.next();
//method call to get further details of the pet
getpetDetails(petType,input);
}
//close the scanner instance
input.close();
//method to print the details of pet from the arrayList
printPetDetails();
}
//method to print the details of the pet by iterating over the ArrayList
private static void printPetDetails() {
//iterate over,find the entries for fish and print
for(int i=0;i<pets.size();i++)
if(pets.get(i) instanceof Fish){
System.out.println(pets.get(i).toString());
}
//iterate over,find the entries for cat and print
for(int i=0;i<pets.size();i++)
if(pets.get(i) instanceof Cat){
System.out.println(pets.get(i).toString());
}
//iterate over,find the entries for dog and print
for(int i=0;i<pets.size();i++)
if(pets.get(i) instanceof Dog){
System.out.println(pets.get(i).toString());
}
}
//method to get the specific details of the pets
private static void getpetDetails(String petType,Scanner petDetails) {
try{
System.out.println("Enter the pet's name:");
String petName = petDetails.next();
System.out.println("Enter the pet's age:");
int petAge=petDetails.nextInt();
//switch based on the type of pet
switch(petType.toUpperCase()){
case "DOG":
System.out.println("Enter the pet's breed:");
String petBreed=petDetails.next();
System.out.println("Enter the pet's weight");
int petWeight=petDetails.nextInt();
//create a object of the dog class and assign the value by calling the parameterized constructor
Dog dog=new Dog(petName,petAge,petBreed,petWeight);
//add the object to the arrayList
pets.add(dog);
break;
case "CAT":
System.out.println("Enter the pet's coat:");
String petCoat=petDetails.next();
System.out.println("Is the pet a lap cat? Please enter YES or NO");
String petlap=petDetails.next();
//create a object of the cat class and assign the value by calling the parameterized constructor
Cat cat=new Cat(petName,petAge,petCoat,petlap);
//add the object to the arrayList
pets.add(cat);
break;
case "FISH":
System.out.println("Enter the pet's type:");
String typePet=petDetails.next();
System.out.println("Enter the pet's color of scales");
String petScalesColor=petDetails.next();
//create a object of the fish class and assign the value by calling the parameterized constructor
Fish fish=new Fish(petName,petAge,typePet,petScalesColor);
//add the object to the arrayList
pets.add(fish);
break;
}
}
catch(Exception ex){
System.out.println("Exception caught."+ ex.getMessage() + "/n Please restart the program");
}
}
}
---------------------------------------------
//Pet.java
package PetsPackage;
// create the parent pet class
public class Pet {
//String variable for the name and age of the pet
protected String petName;
protected int petAge;
//constructor to assign the values
public Pet(String petName, int petAge) {
this.petName = petName;
this.petAge = petAge;
}
//to get the age
public int getPetAge() {
return petAge;
}
// to set the age
public void setPetAge(int petAge) {
this.petAge = petAge;
}
//to get the name
public String getPetName() {
return petName;
}
//to set the name
public void setPetName(String petName) {
this.petName = petName;
}
}
----------------------------------------
Thank You!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.