RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab11a
Lists and for-each
expandable Kennels

new! improved! required! In lab you can work with a partner, but after lab when working on the homework the regular your-own-work policy applies (as on the class info sheet).

Today we will practice what we saw in lecture: lists, and the for-each loop. Make sure you have worked all of these problems through 6c, before Monday.

  1. We'll revisit our Dogs and Kennel example. Save-to-disk this Dog/Kennel project (.jar), and (from BlueJ) select Open Non-BlueJ....
    (how to open .jar files in BlueJ)
  2. Make sure the Dog method getAge returns the age field, and fix it.
  3. You can use Test Fixture to Object Bench to make some Dogs.
  4. In the code pad, make a new java.util.LinkedList<Dog>, and save the result in a variable (perhaps named “doggies”?).
  5. Add Dogs to the list. Have the list print itself. Ask the list how long it is. Remove a Dog from the list. Add the same Dog to the list multiple times. (Perhaps it's a list of “Doggy of the Year” awards, so we want multiple repetitions of the same Dog in the list?)
  6. Together, we'll write a for loop which adds the ages of all the Dogs.
    (If you want to do this in the code pad, hit shift-enter to avoid evaluting a line which is meant to go with the next.) (Solution)

  7. 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. 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”? After deciding that, write a constructor, and examples of the data (which you can promptly put into the test-fixture for that class).
    2. Of course, you'll need to remove the old, now-outdated fields dog1etc.. This means also removing the getters/setters for those fields. You can either comment out oneYearPasses and fix it momentarily, or delete it entirely.
    3. Write the methods addDog, and numDogs. (Discuss with your partner: what should these methods do? What are genuinely useful tasks that might correspond to those names?) Documentation and are test cases required for hw11. For test cases, a KennelB with three dogs, another KennelB with only one dog, another KennelB with zero dogs, all saved to the test fixture — this is be a minimal set of test cases for our functions below. Note that addDog plays the role of a setter, indirectly. We can also make sure that our test fixture includes a KennelBs noDogs, oneDog, twoDogs, and threeDogs, where we add the indicated number of Dogs to each, before saving to the test fixture..
    4. Write the method totalDogAge. We'll make this a private method, since we'll see shortly it's just intended as a helper for calculating the average age. (Be sure to include test cases first. Does your method handle empty KennelBs correctly?)
    5. If you are suspicous about exactly how the running-total is tracked, step through the problem using the debugger. (Set a breakpoint at the line inside the loop, and repreatedly press Run.)
    6. Write the method averageDogAge, as before. (Be sure to include test cases first. This makes you think: Does your method handle empty KennelBs correctly? You can return return Double.NaN (“not a number”) in this case. Note that floating-point division already returns Double.NaN if you divide by zero.
    7. 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.)
    8. 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.
    9. Write a method getAllWithSound, which takes in a String, and returns a list of all dogs who make that sound.
    10. 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 takes in a list 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.

      You will be graded on indentation, as you turn in this work for hw11.
      (I suggest (a) starting/pasting the project into eclipse, (b) quit eclipse to create test cases, (c) then using eclipse to write most of the code.
      If you want to do use BlueJ for most all your work, you can do that but then just before you print, paste your code into eclipse and indent with Ctrl-A Ctrl-I.)

      For hw11, you don't need to turn in anything for class Dog, but you do need documentation, test cases, and code for you class KennelB.


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”.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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