RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab08b
Objects containing objects
lab08b

    A class Kennel: fields, constructor

  1. Open a new project (in BlueJ), and add the file Dog.java.
  2. Remind yourself about what fields and methods Dog has. BlueJ, make one or two, and Inspect them, and call a few methods.
  3. We will add a second class to this project: Class Kennel. A Kennel has three things: an Address, and exactly two Dogs. write a class for this, Write the constructor, and getters for the two Dogs (but no setters (hah hah)).
  4. Test cases

  5. In BlueJ, we will create a new Kennel instance, by calling its constructor. Remind yourself: what three inputs did we just say were needed, in order to create a new Kennel? That is: before we can call the Kennel constructor, what will we need to create at least two of?
  6. In BlueJ's Code Pad, walk up to your Kennel and call each of the getters.
  7. Click Inspect on the Kennel. Within the Kennel, double-click on the fields which hold references to Dogs, to see the (separate) Dog instances.
  8. Go up to one of the dogs on the bench, and change its age. What happens to the various inspector windows -- do you see where the change occurs?
    Question: Does the Kennel contain the same Dogs it did a moment ago?
  9. How would you find the age of the second dog in the Kennel you created? In the code pad, ask it!
    Important concept: Each class is responsible for maintaining its fields only — not the fields of other classes. So we should not expect (or want) a method “getSecondDogsAge” inside Kennel. However, we can ask the Kennel for one of the Dogs inside it, and then ask that Dog itself what its age is.
  10. From the Code Pad (not via right-clicking), go ahead and create (at least) two more Dogs, and then create a second Kennel.
  11. Save all these objects to the test fixture.
  12. Kennel methods

  13. Customers often want to know, whether a particular Kennel is full of older, wiser dogs, or if it's instead full of young pups. Let's let them ask the Kennel that question directly! We'll go about writing a Kennel method avgAge, which returns the average age of all (the two) Dogs it contains.

    1. First write the contract, an empty stub function, and the comments.
    2. Second, write two test cases in BlueJ.
    3. Now, complete the method. Note that to compute this answer, the Kennel will ask questions of the Dogs inside of it.
    4. Finally, in BlueJ, check your test cases.
      (If you don't pass the test cases, go back to (c).)
  14. Write a Kennel method oneYearPasses. This causes every Dog inside the Kennel to become older. (Remind yourself: how do we tell a Dog to become older?) Write a stub , then test cases then finish the code , and then test .

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme