RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw-ec03-mpg
extra credit homework: calculating mileage
hw-ec03; 20pts; Due Apr.27 (Fri) 17:00

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

  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 v).
    Which of these two signatures is a better way to approach the problem? Explain your choice in one to two sentences.
  7. Solve the above question.
  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. 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.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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