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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab10b
Lists and for-each
lab10b

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 four different Dogs, and save them to the test-fixture. (Test Fixture to Object Bench will make some Dogs; you can add to that and then replace the old fixture with Object Bench to Test Fixture.)
  3. In the code pad, make a new java.util.LinkedList<Dog>, and save the result in a variable (perhaps named “doggies”?).
  4. 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?)
  5. Together, we'll write a for loop which adds the ages of all the Dogs.

  6. 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. Write methods addDog, and numDogs. (Discuss with your partner: what should these methods do? What are genuinely useful tasks that might correspond to those names?)
    3. 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?)
    4. 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.)
    5. Write the method averageDogAge, as before. (Be sure to include test cases first. This makes you think: Does your method handle empty KennelBs correctly?)
    6. 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 string-concatenate Strings for each Dog.
    7. 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 says whether or not some Dog in the KennelB makes that sound.
      (Just as totalAge added together a number for each Dog, this method will keep a boolean, corresponding to ...?)

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