Question: Rewrite the Dog class given in Listing 5.1 by utilizing the information and encapsulation principles described in Section 5.2. The new version should include accessor

Rewrite the Dog class given in Listing 5.1 by utilizing the information and encapsulation principles described in Section 5.2. The new version should include accessor and mutator methods. Also define an equals method for the class that returns true if the dog’s name, age, and breed match the same variables for the other object that is being compared. Include a main method to test the functionality of the new Dog class.


Listing 5.1

public class Dog { public String name; public String breed; public int age; public void writeOutput () { System.out.printin(

public class Dog { public String name; public String breed; public int age; public void writeOutput () { System.out.printin("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: Later in this chapter we will see that the modifier public for instance variables should be replaced with private. age); System.out.println("Age in human years: getAgeInHumanYears ()); %3D + System.out.printin(); } public int getAgeInHumanYears () { int humanAge if (age

Step by Step Solution

3.34 Rating (166 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class Dog private String name private String breed private int age Accessors an... View full answer

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 Java An Introduction to Problem Solving and Progra Questions!