IN JAVA WITHOUT USING SCANNERS Design a class that represents a dog. This class
ID: 3733915 • Letter: I
Question
IN JAVA WITHOUT USING SCANNERS 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.
Explanation / Answer
Below is the code. I am not using scanner as asked. I am inputting the main method using hardcode values. Let me know if you want to use JOptionPane. I'll edit the code. I have added the comments for your understanding. Let me know if you still have any issues in this in comments.
In equals method of PetDog class I have only considered owner name , as address was not asked .. Uncomment the code if you want to have address also to be compared in equals method of PetDog
Below is your code: -
Date class
//Class to represent the Date
public class Date {
private int month;
private int year;
private int day;
//Parameterized Constructor of Date class
public Date(int month, int year, int day) {
this.month = month;
this.year = year;
this.day = day;
}
//Default Constructor
public Date() {
}
//Getters and Setters
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;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
//toString method
public String toString() {
return this.day+"/"+this.month+"/"+this.year;
}
//equals and hashcodes method
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
//equals method check the reference of the object and its values.
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Date other = (Date) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
}
Dog class
//Class representing a dog.
public class Dog {
// private variable declaration
private String breed;
private double weight;
private String sex;
private Date birthDate;
// Parameterized constructor
public Dog(String breed, double weight, String sex, Date birthDate) {
this.breed = breed;
this.weight = weight;
this.sex = sex;
this.birthDate = birthDate;
}
// Default Constructor
public Dog() {
}
// toString method to print the details of the dog
public String toString() {
return " Breed: " + this.breed + " Weight: " + this.weight + " Kg Sex: " + this.sex + " BirthDate: "
+ this.birthDate;
}
// equals and hashcode methods
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((birthDate == null) ? 0 : birthDate.hashCode());
result = prime * result + ((breed == null) ? 0 : breed.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
long temp;
temp = Double.doubleToLongBits(weight);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dog other = (Dog) 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 == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight))
return false;
return true;
}
// Getter and Setter methods of all the variables
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
PetDog class
//Class representing a PetDog inherited by Dog class
public class PetDog extends Dog {
// private variable declaration
private String owner;
private String address;
// Parameterized constructor
public PetDog(String breed, double weight, String sex, Date birthDate, String owner, String address) {
super(breed, weight, sex, birthDate);
this.owner = owner;
this.address = address;
}
// Default Constructor
public PetDog() {
}
// getter and setter for variables
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;
}
// overridden equals and hashcode methods
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((owner == null) ? 0 : owner.hashCode());
return result;
}
// Equals method checks only for owner same not for address as specified in
// the question
// if you want to add address also then uncomment the lines in the method
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
PetDog other = (PetDog) obj;
/*
* 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;
}
@Override
public String toString() {
return "This is a petdog. Below are the details" + super.toString() + " Owner Name: " + this.owner
+ " Address: " + this.address;
}
}
DogTest class
//A test class having the main method to test the code
public class DogTest {
// main method to test the functionality
public static void main(String[] args) {
//creating Dog object 1 with default constructor
Dog dog = new Dog();
//adding values to dog object
dog.setBreed("German Shepherd");
dog.setBirthDate(new Date(3,1992,26)); //26 March 1992
dog.setSex("Male");
dog.setWeight(10.5);
//printing details of first dog
System.out.println("Dog 1 details are : "+dog);
System.out.println();
//Creating Petdog object with parameterized constructor
PetDog dog2 = new PetDog("Rottweiler",11,"Female",new Date(4,1992,26),"John Doe","21 Bakers Street New York");
//printing details of second dog object
System.out.println("Dog 2 details: "+dog2);
//checking if both dogs are same
System.out.println("Are both dogs(1 and 2) same ? "+dog.equals(dog2));
//creating a dog 3 object
PetDog dog3 = new PetDog("Rottweiler",11,"Female",new Date(4,1992,26),"John Doe","21 Bakers USA");
//checking if dog 2 and dog 3 are same
System.out.println("Are both dogs(2 and 3) same ? "+dog2.equals(dog3));
}
}
Output
Dog 1 details are :
Breed: German Shepherd
Weight: 10.5 Kg
Sex: Male
BirthDate: 26/3/1992
Dog 2 details:
This is a petdog. Below are the details
Breed: Rottweiler
Weight: 11.0 Kg
Sex: Female
BirthDate: 26/4/1992
Owner Name: John Doe
Address: 21 Bakers Street New York
Are both dogs(1 and 2) same ? false
Are both dogs(2 and 3) same ? true