RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab11b
Lists and for-each
expandable Kennels

Today we will practice what we saw in lecture: lists, and the for-each loop.

We'll revisit our Dogs and Kennel example. Our old version of Kennel was stupid. (What are two examples of how it can't represent what we want.) Write a new class KennelB, which has an address, and can hold as many Dogs as you like.

  1. Save-to-disk this Dog/Kennel project (.jar), and (from BlueJ) select Open Non-BlueJ....
    (how to open .jar files in BlueJ)
  2. As per The Design Recipe, the very first step is to decide how to represent our data. In particular, how to represent “as many Dogs as you like”? How many fields do we need for a Kennel, and of what type?

  3. Draw a picture of a Kennel whose address is "123 puppy lane", and contains three dogs (one of which is the 15-year-old bowser). Notice how our picture differs from our old version which had three Dog fields.
  4. After adding any new fields, let's make two constructors.

  5. Once your constructor is completed, make a few objects and inspect them. (You can choose DogTest's Test Fixture to Object Bench, to create a bunch of Dogs from our previous lab.)
  6. Of course, you'll need to remove the old, now-outdated fields dog1, etc.. This means also removing the getters/setters for those fields. For methods like oneYearPasses and averageAge (which refer to the now out-of-date fields), leave their signature but either comment out or delete their code.
  7. Write the methods addDog, and numDogs; the provided code has documentation for these. Note that addDog plays the role of a setter, indirectly. Note that the provided test fixture for kennels creates several Kennels, each with different numbers of Dogs.
  8. Write the method averageDogAge, as before. (Test cases have been written for you.)
  9. We will check off up to here at the end of next Tuesday's lab.

    Here are some further exercises provided for self practice, and/or those who are bored.
  10. Write the method cacophony, which returns the result of have each Dog speak.
    (Just as totalAge added together a number for each Dog, this method will “add” (string-concatenate) Strings for each Dog, separated by a space. For example, "woof arf woof woof bark ". (A trailing space after the last sound is acceptable.)
  11. Sometimes pet-owners are picky about adopting, and only want Dogs which make a certain sounds (say, “rrrruff”). Write the method containsSound, which takes in a String, and returns how many Dogs in the KennelB make that sound.
  12. Write a method getAllWithSound, which takes in a String, and returns a LinkedList<Dog> of all dogs who make that sound. (Do not return a String!)
  13. Other tasks:

    Knowing that the average age of dogs in a kennel is 5 is fine, although it leaves open the question of whether you have many dogs who are all around 5yrs old, or whether half the dogs are about 1yr old and half are around 9. (Or, something in between.)

    We can measure this spread by calculating not the average age of the dog, but instead the average distance-of-the-age-from-5. (Take the absolute value1, since we just want to know how far it is from 5yrs, and don't care whether it's older or younger.) That is, for each dog add up |a-5| (where a is the dog's age), and divide this total by the number of dogs.

    to do Write a KennelB method which first computes the average age of its residents (call2 the average μ), and then further computes the average distance-of-each-age-from-μ.

    For example: if the Dogs' ages are (8, 9, 13), then we can compute that their average μ is 10. Now, we can add up the distance-from-μ which is 2+1+3, and see that the average distance-from-μ is 2 -- meaning that (on average) each Dog's age is only 2 away from average (a reasonably tight clustering -- not a KennelB with high variety of ages).
    On the other hand, if a KennelB has dogs ages 1, 2, 18 and 19, then the average age is still μ=10, but the average distance-from-μ is (9+8+8+9)/4 = 8.5 -- not very tightly clustered at all; this single number summarizes3 how this KennelB has Dogs with very dissimilar ages.

    This measure, by the way, is the same notion that's behind the “standard deviation”, except there they don't take absolute value to get a positive number, instead they square each distance-from-μ (and then take the square-root at the very end, to keep the units in years instead of square-years).

    Be sure to make test-cases before starting, using the KennelBs which you saved to the test fixture earlier, and computing the average-distance-from-average by hand so that you know what to assert as the correct answer.


1You can easily write your own absolute-value method, but you might as well use the built-in Math.abs.      

2“mu” for “mean”, which is another name for the average.      

3A single number which summarizes other information is a called a “statistic”.      

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Apr.08 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme