CSCI 161: Introduction to Programming I
Spring 2007
Lab 5: Dog and Kennel (Writing Classes)

Due Thursday, March 8th at beginning of class


Overview

Design and implement a class called Dog that contains instance data that represents the dog's name and age. Define the Dog constructor to default the name to "" and age to zero.

 

Include getter and setter methods for the name and age (four total). Include a method computePersonAge to compute and return the age of the dog in "person years" (seven times the dog's age). Include a toString method that returns a one-line description of the dog (name, age in dog years, and age in person years).

 

Create a driver class called Kennel, whose main method instantiates two Dog's: Snoopy and Scooby. Using a getter and setter, have Scooby age one year.

 

This project requires two Java source files: Kennel.java and Dog.java. Kennel.java will contain the Kennel class and method main.

 

Input Specification

No input is required.

 

Output Specification

After instantiating Dog's Snoopy and Scooby, output each using System.out.println (dogObject) to obtain the text below. Using a Dog object in a String context causes the toString method to be invoked. Then age Scooby by 1 year.

Name: Snoopy; Age: 5; Person Age: 35.

Name: Scooby; Age: 7; Person Age: 49.

 

After Scooby ages...

Name: Scooby; Age: 8; Person Age: 56.

Use only console-based output.

 

Required Methods

public Dog ()

public void   setName (String name)

public String getName ()

public void   setAge  (int age)

public int    getAge  ()

public int    computePersonAge ()

public String toString ()

 

Hints

Listings 4.1 and 4.2 serve as a good model.