Due by 2:59 pm on Friday, 10/24/2014)
Imperial Measurements of Distance
The major units of length measurement in the Imperial system are (in increasing order) the inch, foot, yard, rod, furlong, and mile, nautical mile, and league. They are are related thusly:
1 foot = 12 in. 1 yard = 3 ft. 1 rod = 5.5 yd. 1 furlong = 40 rd. 1 mile = 8 fl. 1 nautical mile = 1.15 mi. 1 league = 3 nm.
One inch equals 2.54 cm.
In the problems that follow, you will implement functions that calculate various unit conversions, based on the relations above.
As you implement these functions, reuse functionality by calling functions you've defined rather than reinventing the wheel. Your work will be assessed in part on the degree to which you avoid repeating code.
Implement the following methods:
The behavior of each of these methods is clear from the chosen names! All of them should be part of a library named LengthUnits.
Write a program, UnitConvert, which accepts distance conversion queries and prints the results. You may choose to accept input in a command-line argument style or interactively (either with a single prompt and display, or with a loop that can be exited when the user types "q")—that part is up to you. In either case, a query consists of three values: a number, n, the source unit (i.e., the measurement unit we're converting from), and the target unit (the unit we're converting to).
Use the following two-letter abbreviations to signal the various units:
- cm: centimeters
- in: inches
- ft: feet
- yd: yards
- rd: rods
- fu: furlongs
- mi: miles
- nm: nautical miles
- lg: leagues
For example, if your program supports command-line input, you might see this interaction:
John-Lasseter:~ jlasseter$ java UnitConvert 10.0 in cm 10.0 inches = 25.4 centimeters John-Lasseter:~ jlasseter$ java UnitConvert 10.0 cm lg Conversion of centimeters to leagues is not supported.
Use your LengthUnits library class. Don't re-do everything!
There are a lot of possible combinations here (92=81)! You only need to to support the conversions for which you defined methods. You may add others (and the challenge of doing this in an elegant way is fun), but you don't have to. Attempts to do so, however, will not be penalized in any way if they're wrong, so long as the usual standards of correct syntax and good coding style are met.
Dont forget to use the equals() method for testing equality on String values, rather than == (e.g. "if (s.equals("in"))").
Simplify your main by adding method definitions to the UnitConvert class that handle some of the messier parts of this problem. For example, a method that takes the two-letter abbreviation and returns the full name could be helpful. I'll leave the creativity of this up to you.
A printed copy of your library class, LengthUnits.java, and your unit converter program, UnitConvert.java.
An electronic copy of your work should be copied to your class turn-in folder, inside of a folder named lab7.
John H. E. Lasseter