|
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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.
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?
After adding any new fields, let's make two constructors.
For backwards compatability, we'll keep our existing constructor. That is, the constructor which takes a String and two Dogs, and creates a Kennel containing those dogs plus a dear 15-year-old named Bowser.
Note that this constructor is not given a list, as one of its parameters; we'll have have to create a new list ourselves. (Just like our old constructor wasn't given the dog bowser; it had to create that dog itself.)
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.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”. ↩
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Apr.08 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |