Implement a java class named Dog that contains information about a dog. The Dog
ID: 3811009 • Letter: I
Question
Implement a java class named Dog that contains information about a dog. The Dog class has the following instance variables (a.k.a. fields or data):
The dog's name
The breed
The age of the dog
The Dog class will have methods to:
Create a new Dog (given a dog's name, breed and age) [constructor]
Create a new Dog (from another Dog) [copy constructor - like cloning a dog]
getName
getBreed
getAge
setName
setBreed
setAge
ageInHumanYears - calculates and returns the dog's age in human years, which is generally estimated as: (if the dog is <= 2 years old, multiply its age * 11, else, subtract the age - 2, multiply by 5 and add 22)
equals - method to check if one Dog is the same as another by comparing all instance variables
toString - method to turn a Dog into a string for display, e.g. display as "Dog [Name=Scooby, Breed=Great Dane, Age=7, Human Age=47]"
Once your Dog class is finished, please create a driver class named DogDemo with a main() method that does the following:
Creates a new Dog object named "scooby" with the following information:
name = "Scooby"
breed = "Great Dane"
age = 7
Creates another Dog object named "myDog" by copying scooby.
Print both objects to the console.
Compare the two Dogs using the equals method. If the Dogs are equal, print the message "Both dogs are the same." Otherwise print the message "The dogs are different."
Now, change each of the fields for "myDog" with mutators (set...) to the following values (if you have a dog, feel free to use your pet's info below instead):
name = "Toby"
breed = "Terrier"
age = 2
Print both objects to the console
Once more, compare the two Dogs using the equals method. If the Dogs are equal, print the message "Both dogs are the same." Otherwise print the message "The dogs are different."
Explanation / Answer
Dog.java
public class Dog {
//Declaring instance variables
private String name;
private String breed;
private int age;
//Parameterized constructor
public Dog(String name, String breed, int age) {
super();
this.name = name;
this.breed = breed;
this.age = age;
}
// Copy constructor
public Dog(Dog d) {
this.name = d.name;
this.breed = d.breed;
this.age = d.age;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//This method will calculate and return the human age
public int ageInHumanYears() {
if (age <= 2)
return age * 11;
else
return (age - 2) * 5 + 22;
}
//this method compares two dog objects
public boolean equals(Dog d) {
if (age != d.age)
return false;
if (breed == null) {
if (d.breed != null)
return false;
} else if (!breed.equals(d.breed))
return false;
if (name == null) {
if (d.name != null)
return false;
} else if (!name.equals(d.name))
return false;
return true;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Dog [name=" + name + ", breed=" + breed + ", age=" + age + "Human Age ="+ageInHumanYears()+"]";
}
}
_______________________
Driver.java
public class Driver {
public static void main(String[] args) {
//Creating the Dog class object by passing values as arguments
Dog scooby =new Dog("Scooby","Great Dane",7);
System.out.println("---Displaying scooby Dog Object---");
System.out.println(scooby.toString());
//Creating the Dog class object by using the copy constructor
Dog myDog=new Dog(scooby);
System.out.println("---Displaying myDog Dog Object---");
System.out.println(myDog.toString());
System.out.println(" ----Comparing scooby and myDog Dog objects----");
//comparing the two dog class objects
boolean bool=scooby.equals(myDog);
if(bool)
System.out.println("Both dogs are same");
else
System.out.println("Both dogs are different");
//calling the setters
myDog.setName("Toby");
myDog.setBreed("Terrier");
myDog.setAge(2);
System.out.println("---Displaying scooby Dog Object---");
System.out.println(scooby.toString());
System.out.println("---Displaying myDog Dog Object---");
System.out.println(scooby.toString());
System.out.println(" ----Comparing scooby and myDog Dog objects----");
//comparing the two dog class objects
bool=scooby.equals(myDog);
if(bool)
System.out.println("Both dogs are same");
else
System.out.println("Both dogs are different");
}
}
____________________
Output:
---Displaying scooby Dog Object---
Dog [name=Scooby, breed=Great Dane, age=7Human Age =47]
---Displaying myDog Dog Object---
Dog [name=Scooby, breed=Great Dane, age=7Human Age =47]
----Comparing scooby and myDog Dog objects----
Both dogs are same
---Displaying scooby Dog Object---
Dog [name=Scooby, breed=Great Dane, age=7Human Age =47]
---Displaying myDog Dog Object---
Dog [name=Scooby, breed=Great Dane, age=7Human Age =47]
----Comparing scooby and myDog Dog objects----
Both dogs are different
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.