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

Hope some smart people can help me. Thank you. Please write whole code! The clas

ID: 3678117 • Letter: H

Question

Hope some smart people can help me. Thank you. Please write whole code!

The class below describes a parent class, Pet. In the next questions, you will write child classes for Dog and Cat.

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();

}

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."

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).

3.

Write a constructor for the Dog class that initializes a dog object by specifying all information.

4.

Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.

5.

Write a toString method to return a text representation of the dog that includes the name, age, and breed.

6.

Write an equals method. Two Dog objects are considered the same (logically equivalent) if they have the same name, age, and breed.

7.

Is there anything else you need to add to your class so that it will compile? If so, include that here.

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.

9.

Write a constructor for the Cat class that initializes a cat object by specifying all information.

10.

Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.

11.

Write a toString method to return a text representation of the cat that includes the name, age, and indoor status.

12.

Is there anything else you need to add to your class so that it will compile? If so, include that here.

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.

Thanks again! I appreaciate it!

Explanation / Answer

Good wishes,

first let me give you the code:

1.

public abstract class Pet implements Comparable{
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();

public int compareTo(Pet p) {

if((this.getName()).equals(p.getName()))
{
return -1;
}
else if((this.getName()).equals(p.getName()))
{
return 1;
}
else if((this.getName()).equals(p.getName()))
{
if((this.getAge())<(p.getAge()))
{
return -1;
}
else if((this.getAge())>(p.getAge()))
{
return 1;
}
else if((this.getAge())==(p.getAge()))
{
return 0;
}
}
}
}

2,3,4,5,6

public class Dog extends Pet{
private String name;
private int age;
private String breed;
public Dog(String name, int age, String breed) {
   super(name,age);
   this.name = name;
   this.age = age;
   this.breed = breed;
}
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public int getAge() {
   return age;
}
public void setAge(int age) {
   this.age = age;
}
public String getBreed() {
   return breed;
}
public void setBreed(String breed) {
   this.breed = breed;
}
public String toString() {
String s = "Name: " + name ;
s += " Age: " + age;
s += " Breed: " + breed;
return s;
}
public boolean equal(Dog d1,Dog d2) {
   if(((d1.getName()).equals(d2.getName()))&&((d1.getAge())==(d2.getAge()))&&((d1.getBreed()).equals(d2.getBreed())))
       return true;
   else
       return false;
}
}

7.

Nothing more to include

8,9,10,11

public class Cat extends Pet{
   private String name;
   private int age;
   private boolean indoor;
  
   public Cat(String name, int age, boolean indoor) {
       super(name, age);
       this.name = name;
       this.age = age;
       this.indoor = indoor;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   public boolean isIndoor() {
       return indoor;
   }
   public void setIndoor(boolean indoor) {
       this.indoor = indoor;
   }
   public String toString() {
       String s = "Name: " + name ;
       s += " Age: " + age;
       s += " Indoor status: " + indoor;
       return s;
       }
}

12.

Nothing more to include

13.

public class PetMain {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
Pet[] p=new Pet[6];
char[] c=new char[6];
p[0]=new Cat("kitty",11,true);
c[0]='c';
p[1]=new Dog("snoopy",1,"dashman");
c[1]='d';
p[2]=new Cat("clare",8,true);
c[2]='c';
p[3]=new Cat("rock",6,false);
c[3]='c';
p[4]=new Dog("blacky",3,"pug");
c[4]='d';
p[5]=new Dog("john",11,"bulldog");
c[5]='d';
for(int i=0;i<6;i++)
   System.out.println(p[i].toString());
for(int i=0;i<6;i++) {
   if(c[i]=='c'){
       if(p[i].getIndoor()){
   System.out.println(p[i].getName());
       }
   }
  
}
System.out.println(p);
  
   }

}

Hope this is clear.