DogKennel For this exercise you will define a class called D
Dog/Kennel
For this exercise, you will define a class called Dog, as well as a driver class called Kennel. (Listing 4.3 and 4.4 can provide a good reference for this exercise)
The Dog class should contain the following:
Instance data that represents the dog’s name and age.
A constructor called Dog that initializes instance data.
The constructor should accept two parameters to transfer a name of type String and an age of type int.
Two accessor methods, called getName, and getAge to get the name and age of the dog.
The accessor methods do not take any parameters, but can return the name and ages of the dogs respectively.
Two mutator methods, called setName and setAge to set the name and age of the dog.
Each method takes one parameter (either String or int) but does not return a value. These methods use the parameter to update the instance data.
The Kennel class should contain your main method. Your main method, in turn should contain the following:
At least 4 objects of type Dog. (An example of one is provided in the shell).
Output statements to test the toString method (see listing 4.3 and 4.4 –pg 177-179 - for example.)
Output statements to test the accessor and mutator methods.
Retrieve and update the name and age of one of the dogs. (It doesn’t have to be the same dog).
Output statements to test the personYears method.
Code Shell – Dog.java
//*********************************************************
// Dog.java
//
// This class contains data and methods related to the
// names and ages of dogs.
//*********************************************************
public class Dog
{
// declare variables to store the name/age of the dog
// Tip: This should be private!
//Methods are below. They should be public
//------------------------------------------------------
// Constructor: Sets up Dog object with specified data.
//------------------------------------------------------
public Dog(…) // Complete constructor header
{
}
//------------------------------------------------------
// Name accessor – declare getName here
//------------------------------------------------------
//------------------------------------------------------
// Name mutator – declare setName here
//------------------------------------------------------
//------------------------------------------------------
// Age accessor – declare getAge here
//------------------------------------------------------
//------------------------------------------------------
// Age mutator – declare setAge here
//------------------------------------------------------
//------------------------------------------------------
// Declare personYears here
// Computes & returns this dog\'s age in \"person-years\".
//------------------------------------------------------
//------------------------------------------------------
// declare toString here
// Returns a string representation of this dog.
//------------------------------------------------------
}
Code Shell – Kennel.java
//*********************************************************
// Kennel.java
//
// This is the driver program for the Dog class
//*********************************************************
public class Kennel
{
//------------------------------------------------------
// Creates and exercises some Dog objects.
//------------------------------------------------------
public static void main (String[] args)
{
// Create objects - test constructor
Dog d1 = new Dog(\"Cooper\", 1);
… // create 3 more objects
// Test toString
System.out.println(d1);
… // create corresponding statements
System.out.println();
// Retrieve a name: call getName from an output
// statement
System.out.print(/* call getName*/ +
\" also answers to the name \");
// Update a name
String newname; = \"Kruger\";
// call setName here
// Print updated name
// Retreive and update an age
int newage;
newage = /* call getAge*/ + 1;
// call setAge here
System.out.println();
// Print new age for the dog. Call personYears here
}
}
Sample output:
Dog: Cooper Age: 1 Person-Years: 7
Dog: Pico Age: 2 Person-Years: 14
Dog: Remi Age: 7 Person-Years: 49
Dog: Kroogs Age: 3 Person-Years: 21
Kroogs also answers to the name Kruger.
Happy Birthday, Remi!
You are now 8, which is 56 years old in person-years.
Solution
//*********************************************************
// Kennel.java
//
// This is the driver program for the Dog class
//*********************************************************
public class Kennel
{
//------------------------------------------------------
// Creates and exercises some Dog objects.
//------------------------------------------------------
public static void main (String[] args)
{
// Create objects - test constructor
Dog d1 = new Dog(\"Cooper\", 1);
Dog d2 = new Dog(\"Snoopy\", 2);
Dog d3 = new Dog(\"Puppy\", 3);
Dog d4 = new Dog(\"Tiger\", 4);
// create 3 more objects
// Test toString
System.out.println(d1.toString());
System.out.println(d2.toString());
System.out.println(d3.toString());
System.out.println(d4.toString());
//System.out.println(d2.getName());
//System.out.println(d2.getAge());
// Update a name
String newname = \"Kruger\";
// call setName here
d2.setName(newname);
// Print updated name
System.out.println(d2.getName());
System.out.println(d3.getAge());
// Retreive and update an age
int newage;
newage = d3.getAge() + 1;
// call setAge here
d3.setAge(newage);
// Print new age for the dog. Call personYears here
System.out.println(d3.getAge());
}
}
class Dog{
String name;
int 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;
}
public Dog(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int personYears() {
return age*7;
}
@Override
public String toString() {
return \"Dog [name=\" + name + \", age=\" + age + \", PersonYears=\"+this.personYears()+\"]\";
}
}




