|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
My wife's car gets 28 mpg (city) / 32mpg (highway).
My car is a bit of a gas hog:
it gets only 22 mpg (city) / 24mpg (highway),
but (sigh) it's a bit more fun to drive
(it has a better stereo,
I can fit in it,
and it's bright orange).
Gas is currently $2.339 2.679 2.899/gal.
When we drive from Radford to Roanoke,
we'd like to know
how much more does
it cost us to take the fun car.
(Compute only gas costs.
Environmental costs, while very real, are much harder to quantify.)
Write a static method which takes in two Car objects, and the information regarding a trip (distance and city/highway category), and returns the difference in cost, depending on which car makes the trip. (The result will be positive if the first car gets worse mileage than the second. That is, this method returns the relative cost of taking the first car instead of the second.)
Optional (for a further 5pts):
Note that every trip consists of two pieces of information --
a number of miles, and whether those are city or highway miles.
Make a separate class SimpleTrip, which bundles together those
two pieces of info into a single class.
A constructor and the getters are the only methods for this class.
Modify the appropriate Car methods
to use SimpleTrips rather than be handed the trip info all piecemeal.
(For a further 5pts):
Make a class CompoundTrip,
which contains (a List of) smaller SimpleTrips.
You'll then have a Car method that can compute the
gas costs for both SimpleTrips and CompoundTrips.
(For a further 5pts, after talking about inheritance;
this can be turned in on the last day of class; see me)
Make a class Trip, which SimpleTrip
and CompoundTrip each extend.
Modify CompoundTrip so that it actually contains
a list of Trips (not just SimpleTrips).
Make several interesting examples-of-data in your test fixture.
Make test cases for them.
What happens if you don't actually change the code from the previous part --
does it still compile? (Why or why not?)
Does it still give the correct answer? (Why or why not?)
As always, each of the above steps should include unit tests, created with BlueJ's unit test tools.
A general note on extra-credit homeworks:
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.09 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |