Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Thursday Lab #7: Method Definitions
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

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.

Problems

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.

1. Implement the following methods:

1. inches2cm
2. feet2inches
3. yards2feet
4. rods2yards
5. furlongs2rods
6. miles2furlongs
7. feet2cm
8. yards2cm
9. rods2inches
10. miles2feet
11. nauts2miles
12. nauts2feet
13. leagues2miles
14. leagues2yards

The behavior of each of these methods is clear from the chosen names! All of them should be part of a library named LengthUnits.

2. 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.```

Notes

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

Turn In

• 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