|
home—info—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. Make sure you have worked all of these problems through 6c, before Monday.
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”. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Nov.11 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |