JAVA HOMEWORK HELP THANK YOU! The class below describes a parent class, Pet. In
ID: 3678320 • Letter: J
Question
JAVA HOMEWORK HELP THANK YOU!
The class below describes a parent class, Pet. In the next questions, you will write child classes for Dog and Cat.
For full credit, follow good principles of class design, encapsulation, and inheritance.
I recommend writing the class in your development environment to make sure it compiles and then copying the code into the appropriate question
public abstract class Pet {
private String name;
private int age;
public Pet(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age > 0) {
this.age = age;
}
}
public String toString() {
String s = "Name: " + name ;
s += " Age: " + age;
return s;
}
public abstract void printVetAppointmentReminder();
}
Question 1
Assume the Pet class implements Comparable. Write the new class header and the full compareTo method (including the method header).
Pets should be ordered based on their name (ignoring case) and then their age.
For example, "Fido, 8 years old" would come before "Spot, 3 years old" would come before "Spot, 5 years old."
Question 2
Write the class header and the instance data variables for the Dog class.
A dog is described by name, age, and breed (stored as a String).
Question 3
Write a constructor for the Dog class that initializes a dog object by specifying all information.
Question 4
Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.
Question 5
Write a toString method to return a text representation of the dog that includes the name, age, and breed.
Question 6
Write an equals method. Two Dog objects are considered the same (logically equivalent) if they have the same name, age, and breed.
Question 7
Is there anything else you need to add to your class so that it will compile? If so, include that here.
Question 8
Write the class header and the instance data variables for the Cat class.
A cat is described by name, age, and whether or not the cat is an indoor-only cat.
Question 9
Write a constructor for the Cat class that initializes a cat object by specifying all information.
Question 10
Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.
Question 11
Write a toString method to return a text representation of the cat that includes the name, age, and indoor status.
Question 12
Is there anything else you need to add to your class so that it will compile? If so, include that here.
Question 13
Write code that would go inside a driver program with a main method.
·Create an array to hold 6 Pet objects
·Fill the array with some cats and some dogs.
·Iterate the array and print a text representation of each pet.
·Iterate the array and output the name of each outdoor cat.
·Use polymorphism to do this!
·Sort the array by invoking a Java-provided sorting method.
Explanation / Answer
####### Pet.java############
public abstract class Pet implements Comparable<Pet> {
private String name;
private int age;
public Pet(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age > 0) {
this.age = age;
}
}
public String toString() {
String s = "Name: " + name ;
s += " Age: " + age;
return s;
}
public abstract void printVetAppointmentReminder();
// compare method
@Override
public int compareTo(Pet o) {
return this.name.compareToIgnoreCase(o.name);
}
}
########### Dog.java ###############
public class Dog extends Pet {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
@Override
public void printVetAppointmentReminder() {
// TODO Auto-generated method stub
}
@Override
public String toString() {
return super.toString() + " Breed: " + breed;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Dog){
Dog d = (Dog)obj;
return this.getName().equalsIgnoreCase(d.getName()) &&
this.getAge()==d.getAge() &&
this.getBreed().equalsIgnoreCase(d.getBreed());
}
return false;
}
}
################## Cat.java ##########################
public class Cat extends Pet {
private boolean indoor_only;
public Cat(String name, int age, boolean indoor_only) {
super(name, age);
this.indoor_only = indoor_only;
}
public boolean isIndoor_only() {
return indoor_only;
}
public void setIndoor_only(boolean indoor_only) {
this.indoor_only = indoor_only;
}
@Override
public String toString() {
return super.toString() + " Indoor status: " + indoor_only;
}
@Override
public void printVetAppointmentReminder() {
// TODO Auto-generated method stub
}
}
################## PetDriver.java ##################
import java.util.Arrays;
public class PetDriver {
public static void main(String[] args) {
// creating pet array
Pet[] pets = new Pet[]{
new Dog("Dark", 12, "bhobho"),
new Cat("Mikuu", 2, false),
new Cat("Chigra", 4, true),
new Dog("Alex", 5, "TikiTiku"),
new Cat("Sweetu", 3, true),
new Dog("Gingula", 6, "Wowwww")
};
System.out.println("***********Before Sorting************");
// printing all pets
for(Pet p : pets){
System.out.println(p.toString());
}
System.out.println();
System.out.println("***********Cat s with Indoor_only************");
// printing cat with only indoor_only
for(Pet p: pets){
if(p instanceof Cat && ((Cat)p).isIndoor_only())
System.out.println(((Cat)p).getName());
}
// sorting
Arrays.sort(pets);
System.out.println();
System.out.println("***********After Sorting************");
// printing all pets
for(Pet p : pets){
System.out.println(p.toString());
}
}
}
/*
Output:
***********Before Sorting************
Name: Dark
Age: 12
Breed: bhobho
Name: Mikuu
Age: 2
Indoor status: false
Name: Chigra
Age: 4
Indoor status: true
Name: Alex
Age: 5
Breed: TikiTiku
Name: Sweetu
Age: 3
Indoor status: true
Name: Gingula
Age: 6
Breed: Wowwww
***********Cat s with Indoor_only************
Chigra
Sweetu
***********After Sorting************
Name: Alex
Age: 5
Breed: TikiTiku
Name: Chigra
Age: 4
Indoor status: true
Name: Dark
Age: 12
Breed: bhobho
Name: Gingula
Age: 6
Breed: Wowwww
Name: Mikuu
Age: 2
Indoor status: false
Name: Sweetu
Age: 3
Indoor status: true
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.