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

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).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!