home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
lect11c-soln
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
Solution:
/** return the length of *a* shortest name in the list.
* @param hounds a NON-EMPTY list of Dogs.
* (questions involving min and max aren't defined on empty inputs.)
* @return the length of *a* shortest name in the list.
*/
static int shortestNameLength( java.util.LinkedList<Dog> hounds ) {
int shortestSoFar; // The length of the shortest name seen so far.
// Initialize it to the length of the first dog's name.
// (What would happen if we used 0 as the initial value? 1000?
shortestSoFar = d.get(0).getName().length();
for ( Dog d : hounds ) {
if (d.getName().length() < shortestSoFar) {
shortestSoFar = d.getName().length();
}
else { // We can omit this.
shortestSoFar = shortestSoFar; // legal, but pointless.
}
return shortestSoFar;
}
|
-
find a Dog which has a shortest name
soln
Solution: There might be multiple dogs which tie for shortest-name;
we're only being asked to return any one such of them.
/** return a dog who have a shortest name.
* @param hounds a NON-EMPTY list of Dogs.
* (questions involving min and max aren't defined on empty inputs.)
* @return any one dog who has a shortest name.
*/
java.util.LinkedList<Dog> quickDog( java.util.LinkedList<Dog> hounds ) {
Dog quickDogSoFar = hounds.get(0);
for ( Dog d : hounds ) {
if (d.getName().length() < quickDog.getName().length()) {
quickDogSoFar = d;
}
return quickDogSoFar;
}
|
-
returns a new list, with all dogs who don't go "woof"
soln
Solution:
/** return all dogs in a list the dogs who don't go woof.
* @param hounds The list to look for non-woofers in.
* @return all dogs in hounds who don't go woof.
*/
java.util.LinkedList<Dog> quickDogs( java.util.LinkedList<Dog> hounds ) {
java.util.LinkedList<Dog> nonwoofers = new java.util.LinkedList<Dog>();
for ( Dog d : hounds ) {
if (!d.getSound().equals("woof"))
nonwoofers.add( d );
}
return nonwoofers;
}
|
-
returns a new list, with all dogs who are tied for having the shortest name
soln
Solution: We realize that asking for “the dog with the
shortest name” would be ill-formed;
there might be multiple dogs which tie for shortest-name.
Therefore, we want to return a list of Dogs!
/** return the dogs who have a shortest name.
* @param hounds a NON-EMPTY list of Dogs.
* (questions involving min and max aren't defined on empty inputs.)
* @return all dogs who have a shortest name.
*/
java.util.LinkedList<Dog> quickDogs( java.util.LinkedList<Dog> hounds ) {
java.util.LinkedList<Dog> quickDogsSoFar;
quickDogsSoFar = new java.util.LinkedList<Dog>();
int shortLen = DogLoops.shortestNameLength( hounds );
for ( Dog d : hounds ) {
if (d.getName().length() < shortLen) {
quickDogsSoFar.add( d );
}
return quickDogsSoFar;
}
|
-
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;
}
|
Solution: Returns whether or not there exists a "non-woofers" in the list.
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
Solution: Return whether or not all dogs in the list
are non-woofers.
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