home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
lect11c
list practice
more dogs
Suppose we have a class DogLoops.
Write a static function which takes in a list of Dogs,
and uses a for-each loop to...
-
find the length of the shortest name in the list
soln
-
find a Dog which has a shortest name
soln
-
returns a new list, with all dogs who don't go "woof"
soln
-
returns a new list, with all dogs who are tied for having the shortest name
soln
-
What does the following method do?
boolean mystery( java.util.LinkedList<Dog> ds ) {
boolean ssf = false; //something-so-far
for (Dog d : ds) {
ssf = ssf || !d.getSound.equals("woof");
}
return ssf;
}
|
Note how this method looks extremely similar to
computing the sum of all dog ages -- it just
is using a boolean and || instead
of int and +.
If we replaced || with &&
and the false with true,
then what would this method do?
soln
Reminder:
Things to ask yourself, when writing a loop:
- What is a sample test case list, and what will my answer be?
-
What information do I need, from each item in the list?
-
What information will I accumulate?
When I'm halfway through the loop,
what will that piece of information be?
-
How do I update my accumulated-information,
upon seeing one more item?
This will be the task you do over-and-over,
in the body of the loop!
Note that for the KennelB lab,
each Kennel has a list of Dogs already,
so those list-of-Dog methods would presumably
not need static methods.
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs