Java programming 5th edition chapter 10 Inheritance programs Part 1 Create a cla
ID: 3801675 • Letter: J
Question
Java programming 5th edition chapter 10 Inheritance programs
Part 1
Create a class called Pet that has 2 instance variables, one for the pet's name and one for the pet's age (int type). It should have a default constructor and a values constructore. Also include a set method that sets both attributes, a get method for each attribute, and a method that redefines toString() to print the attributes.
Part 2
Create a Dog class that extends the Pet class above. It adds instance variables for the dog's breed, weight (int type), and age in dog years (this will be a calculated attribute). It should have a default constructor and a values constructor (age in dog years will be 0). Include one set method that sets the name, pet age, breed, and weight attributes, get methods that get each of the 3 new attributes, and a method that redefines toString() to print all attributes. Also, include a method to calculate the age in dog years by multiplying the pet age by 7. Make sure your methods don't repeat the code already written in the Pet class.
Part 3
Create a client program that uses both of the classes created above. Make it do the following in this order:
1. Instantiate an object of the Pet class called pet1 with the default constructor.
2. Instantiate an object of the Pet class called pet2 with the values constructor. Use Spot for the pet's name and 5 for the pet's age.
3. Use the Pet class print method to print pet1 and pet2.
4. Call the set method to reset the name and age for pet1. Use Max for the pet's name and 8 for the pet's age.
5. Use teh get methods to get the attributes for pet1 and print tehm in the client (not with the print method).
6. Instantiate an object of the Dog class called dog1 with the default constructor.
7. Instantiate an object of the Dog class called dog2 with the values constructor. Use Lassie for the pet's name, 3 for the pet's age, collie for the dog's breed, and 76 for the dog's weight.
8. Call the method to calculate the age in dog years for dog2.
9. Use the Dog class print method to print attributes for dog1 and dog2.
10. Use the set method to set new attributes for dog1. Use Ginger for the pet's name, 13 for the pet's age, beagle for the dog's breed, and 35 for the dog's weight.
11. Call the method to calculate the age in dog years for dog1.
12. Use the get methods to get the attributes for dog1 and print them in the client (not with the print method).
Explanation / Answer
Pet.java
public class Pet {
private String name;
private int age;
public Pet() {
super();
}
public Pet(String name, int age) {
super();
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) {
this.age = age;
}
@Override
public String toString() {
return "Name=" + name + ", age=" + age;
}
}
____________________
Dog.java
public class Dog extends Pet {
private int weight;
private String breed;
public Dog() {
super();
}
public Dog(String name,int age,int weight, String breed) {
super(name,0);
this.weight = weight;
this.breed = breed;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public int calculateAge()
{
return 7*getAge();
}
@Override
public String toString() {
return super.toString()+" weight=" + weight + ", breed=" + breed + "]";
}
}
_____________________
Client.java
public class Client {
public static void main(String[] args) {
Pet pet1=new Pet();
Pet pet2=new Pet("Spot", 5);
System.out.println("Pet class Object 1"+pet1.toString());
System.out.println("Pet class Object 2"+pet2.toString());
pet1.setName("Max");
pet1.setAge(8);
System.out.println("Pet Name ="+pet1.getName()+" Age ="+pet1.getAge());
Dog dog1=new Dog();
Dog dog2=new Dog("Lassie",3,76,"collie");
System.out.println("Tyhe age of dog is "+dog2.calculateAge());
System.out.println("Dog 1 info :"+dog1.toString());
System.out.println("Dog 2 info :"+dog2.toString());
dog1.setName("Ginger");
dog1.setAge(13);
dog1.setBreed("beagle");
dog1.setWeight(35);
System.out.println("The age of dog 1 is "+dog1.calculateAge());
System.out.println("Dog 1 info: Name="+dog1.getName()+" Age="+dog1.getAge()+" Breed="+dog1.getBreed()+" Weight="+dog1.getWeight());
}
}
______________________
Output:
Pet class Object 1Name=null, age=0
Pet class Object 2Name=Spot, age=5
Pet Name =Max Age =8
Tyhe age of dog is 0
Dog 1 info :Name=null, age=0 weight=0, breed=null]
Dog 2 info :Name=Lassie, age=0 weight=76, breed=collie]
The age of dog 1 is 91
Dog 1 NameGinger Age=13 Breed=beagle Weight=35
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.