ITEC120
hw03:
classes containing other classes
Reading: Chapter 5.
Due Oct.05 (Thu) 1718:00, to be done individually.
This problem is the book's PP 4.2,
slightly modified, and extended.
Make all fields private, and all methods public.
Of course, use javadoc comments (@param etc.).
As usual, turn in a hard copy (of both files)
and submit on WebCT;
please include your name (in comments) at the top of each file.
-
Design and implement a class Dog.
which
contains private instance data representing the dog's name and age.
Define a public Dog constructor to accept
and initialize the instance data.
-
Include public getter and setter methods for the name and age.
-
Include a toString method which returns a one-line description of the dog.
-
Write a static method testDog, which
makes some Doginstances, sets some fields,
and prints before-and-after messages to the screen
(using System.out.println).
You'll use this method to test that your code works as expected,
and you'll add to this method as you add more Dog methods.
(This method corresponds to the book's “test driver”.
See
lab04-PizzaServer-soln.java's
testClone for one example.)
-
Include a public method which returns the age of the dog in
“dog years”
(seven times the dog's age).
Add to your testDog method to be testing this, as well.
-
Modified:
Create a class Kennel;
every Kennel contains three Dogs.
Every new kennel begins with a Dog named Bowser
who is initially 15 years old.
Include a Kennel constructor which takes two names and two ages,
representing the other two dogs.
Note that you'll use BlueJ's New Class... button;
you should see a tan box for each of class Dog
and class Kennel.
-
Write getter and setter methods for each of a Kennel's two
other Dog fields.
(We'll say that kennel owners never get rid of (nor let others see) Bowser,
their favorite,
to avoid writing a getter/setter for that.)
-
Write a toString method for Kennels.
-
Write a static method testKennel, which
creates a couple of different
Kennels,
calls their methods,
and checks that things change as expected.
If you aren't sure why this is a static method,
be sure to ask in lecture!
-
Write a Dog method ageOneYear,
which increments the dog's age by 1.
Update your testDog method, of course.
-
Write a Kennel method oneYearPasses,
which simply ages each of the three dogs in the kennel.
Update your testKennel method, of course.
Here is a solution
Dog.java
and
Kennel.java.