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

Rewrite the Dog class given in Listing 5.1 by utilizing the information and encapsulation principles described in Section 5.2 of the text.

The new version should include accessor and mutator methods. Also define an equals method for the class that returns true if the dogs 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.

Write a writeOutput method that produces output that looks just like this:

Name: name-of-dog Breed: dog's-breed Age in calendar years: dog's-age Age in human years: converted-dog's-age

IN JAVA:

public class Dog

{

private String name;

private String breed;

private int age;

// Accessors and Mutators

// create these for each of the three instance variables

/* your code goes here */

// Equals method

public boolean equals(Dog otherDog)

{

/* your code goes here */

}

public void writeOutput()

{

// print out the Dog's characteristics as shown above

/* your code goes here */

}

public int getAgeInHumanYears()

{

// implement this method

/* your code goes here */

}

public static void main(String[] args)

{

// create appropriate test(s) for your Dog class

/* your code goes here */

/* DO NOT CHANGE ANY OF THE LINES BELOW THIS COMMENT */

Dog balto = new Dog();

balto.setName("Balto");

balto.setBreed("Siberian Husky");

balto.setAge(8);

balto.writeOutput();

Dog scooby = new Dog();

scooby.setName("Scooby");

scooby.setBreed("Great Dane");

scooby.setAge(42);

scooby.writeOutput();

System.out.println("Are scooby and balto the same? " +

scooby.equals(balto));

Dog baltoJunior = new Dog();

baltoJunior.setName("Balto");

baltoJunior.setBreed("Siberian Husky");

baltoJunior.setAge(balto.getAge()-4);

System.out.println("Are baltoJunior and balto the same? " +

baltoJunior.equals(balto));

Dog baltoClone = new Dog();

baltoClone.setName("Balto");

baltoClone.setBreed("Siberian Husky");

baltoClone.setAge(8);

System.out.println("Are baltoClone and balto the same? " +

baltoClone.equals(balto));

}

}

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!