|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
This homework requires using static methods and static fields.
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 it's a bit more fun to drive
(it has a better stereo, and it's bright orange).
Gas is currently $2.3392.679/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.
1
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:
1 There might be a later challenge-extra-credit, which will let you 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. After that, we can decide that really, a CompoundTrip can actually contain not just SimpleTrips, but also other subsidiary CompoundTrips! Then the code will start to get extremely interesting. See me in the last couple of weeks of class, if this sounds interesting. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |