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

Design a class that represents a dog. This class should have fields that represe

ID: 3735052 • Letter: D

Question

Design a class that represents a dog. This class should have fields that represent the breed, the weight, the sex and the birthdate (design a class of Date with integer fields for month, day and year). Include a constructor method with formal parameters for all of the fields, a toString method that prints all the instance variables (one per line with a simple heading, like -> Breed: German Shepherd), and an equals method to determine if one dog object is equal to another (all the instance variables of each Dog object must match to be equal – make sure you have included an equals method for Date).

Derive a PetDog class from the Dog class. This class should have String fields for owner and address, a parameterized constructor, a toString method and an equals method (PetDog objects must also have all the Dog variables equal and the same owner to be equal to each other.) These two methods will override the similarly named methods in the Dog class and can make use of the parent class methods.

Write a test class that demonstrates each of your methods.

Java

Explanation / Answer

Date.java

public class Date {

//Declaring instance variables

private int day;

private int month;

private int year;

//Parameterized constructor

public Date(int day, int month, int year) {

super();

this.day = day;

this.month = month;

this.year = year;

}

// getters and setters

public int getDay() {

return day;

}

public void setDay(int day) {

this.day = day;

}

public int getMonth() {

return month;

}

public void setMonth(int month) {

this.month = month;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

return day + "/"+month+"/" + year;

}

public boolean equals(Date other) {

if (day != other.day)

return false;

if (month != other.month)

return false;

if (year != other.year)

return false;

return true;

}

}

_________________

Pet.java

public class Pet {

//Declaring instance variables

private String breed;

private double weight;

private char sex;

private Date birthdate;

//Parameterized constructor

public Pet(String breed, double weight, char sex, Date birthdate) {

super();

this.breed = breed;

this.weight = weight;

this.sex = sex;

this.birthdate = birthdate;

}

// getters and setters

public String getBreed() {

return breed;

}

public void setBreed(String breed) {

this.breed = breed;

}

public double getWeight() {

return weight;

}

public void setWeight(double weight) {

this.weight = weight;

}

public char getSex() {

return sex;

}

public void setSex(char sex) {

this.sex = sex;

}

public Date getBirthdate() {

return birthdate;

}

public void setBirthdate(Date birthdate) {

this.birthdate = birthdate;

}

//This method will compare two Pet class objects

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Pet other = (Pet) obj;

if (birthdate == null) {

if (other.birthdate != null)

return false;

} else if (!birthdate.equals(other.birthdate))

return false;

if (breed == null) {

if (other.breed != null)

return false;

} else if (!breed.equals(other.breed))

return false;

if (sex != other.sex)

return false;

if (Double.doubleToLongBits(weight) != Double

.doubleToLongBits(other.weight))

return false;

return true;

}

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

System.out.println("Breed :" + breed);

System.out.println("Weight :" + weight);

System.out.println("Sex :" + sex);

System.out.println("Birthdate :" + birthdate);

return "";

}

}

____________________

PetDog.java

public class PetDog extends Pet {

//Declaring instance variables

private String owner;

private String address;

//Parameterized constructor

public PetDog(String breed, double weight, char sex, Date birthdate,

String owner, String address) {

super(breed, weight, sex, birthdate);

this.owner = owner;

this.address = address;

}

//getters and setters

public String getOwner() {

return owner;

}

public void setOwner(String owner) {

this.owner = owner;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

//This method will compare two PetDog class objects

public boolean equals(PetDog other) {

if (getBirthdate() == null) {

if (other.getBirthdate() != null)

return false;

} else if (!getBirthdate().equals(other.getBirthdate()))

return false;

if (getBreed() == null) {

if (other.getBreed() != null)

return false;

} else if (!getBreed().equals(other.getBreed()))

return false;

if (getSex() != other.getSex())

return false;

if (Double.doubleToLongBits(getWeight()) != Double

.doubleToLongBits(other.getWeight()))

return false;

else if (address == null) {

if (other.address != null)

return false;

} else if (!address.equals(other.address))

return false;

if (owner == null) {

if (other.owner != null)

return false;

} else if (!owner.equals(other.owner))

return false;

return true;

}

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

super.toString();

System.out.println("Owner :" + owner);

System.out.println("Address :" + address);

return "";

}

}

_____________________

Test.java

public class Test {

public static void main(String[] args) {

  

//Creating an instance of Date class

Date d1=new Date(2,5,2015);

//Creating an instance of Pet class

Pet p1=new Pet("German Shepherd", 25,'M',d1);

//Creating an instance of PetDog class

PetDog pd1=new PetDog(p1.getBreed(),p1.getWeight(),p1.getSex(),d1,"Johnson","Church Street,New York");

//Creating an instance of Date class

Date d2=new Date(2,5,2015);

//Creating an instance of Pet class

Pet p2=new Pet("German Shepherd", 25,'M',d2);

//Creating an instance of PetDog class

PetDog pd2=new PetDog(p2.getBreed(),p2.getWeight(),p2.getSex(),d2,"Johnson","Church Street,New York");

//Displaying the contents of Dog class objects

System.out.println("Displaying the PetDog#1 class object :");

System.out.println(p1);

System.out.println("Displaying the PetDog#2 class object :");

System.out.println(p2);

//comparing the two PetDog class objects

boolean bool=pd1.equals(pd2);

if(bool)

{

System.out.println("PetDog#1 and PetDog#2 dogs are equal.");

}

else

{

System.out.println("PetDog#1 and PetDog#2 dogs are not equal.");

}

}

}

__________________

Output:

Displaying the PetDog#1 class object :
Breed :German Shepherd
Weight :25.0
Sex :M
Birthdate :2/5/2015

Displaying the PetDog#2 class object :
Breed :German Shepherd
Weight :25.0
Sex :M
Birthdate :2/5/2015

PetDog#1 and PetDog#2 dogs are equal.

___________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote