Java homework looking for help 1Write the class header and the instance data var
ID: 3841667 • Letter: J
Question
Java homework looking for help
1Write the class header and the instance data variables for the Dog class.
2A dog is described by name, age, and breed (stored as a String).
3Write a constructor for the Dog class that initializes a dog object by specifying all information.
4Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.
5Write a toString method to return a text representation of the dog that includes the name, age, and breed.
6Write an equals method. Two Dog objects are considered the same (logically equivalent) if they have the same name, age, and breed.
7Is there anything else you need to add to your class so that it will compile? If so, include that here.
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 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 +"]";
}
}
______________________
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(myDog.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=7]
---Displaying myDog Dog Object---
Dog [name=Scooby, breed=Great Dane, age=7]
----Comparing scooby and myDog Dog objects----
Both dogs are same
---Displaying scooby Dog Object---
Dog [name=Scooby, breed=Great Dane, age=7]
---Displaying myDog Dog Object---
Dog [name=Toby, breed=Terrier, age=2]
----Comparing scooby and myDog Dog objects----
Both dogs are different
_____________Plz Could you give thump up .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.