RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw-ec02-mpg
extra credit homework: calculating mileage
Due Nov.29 (Thu) in class

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.)

    (20pts)
  1. Write a class Car, which contains only two field of interest: the city-mpg and highway-mpg it gets. Include a constructor, and getters for those two fields.
  2. The cost of gas is something which does not vary from car to car (that is, it's not part of a car's state). Thus is should not be a regular field. Instead, make it a static field, along with a getter and setter. Should the getter/setter methods themselves be static? Explain why or why not, in a one-to-two sentence comment.
  3. Write the method gasRequired, which takes two inputs: a distance (in miles), and whether or not this is a city or highway trip. It returns how many gallons of gas are required to travel that far, in this car.
    Optional: if making a highway trip of less than 5mi, print a warning message to the console, using System.err.println. (You'll still return an answer.)
  4. Write a method costRequired, which takes the same inputs as gasRequired as well as the current price of gas, and returns how much gas-money is required to make the indicated trip.
  5. 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.)

  6. Write an alternative, non-static version of the previous funciton. The non-static method should call the static method (or vice versa).
    Which of these two signatures is a better way to approach the problem? Explain your choice in one to two sentences.
  7. Solve the question posed in the opening paragraph.
  8. 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.

  9. (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.

  10. (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:

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Nov.09 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme