Back to 111 Main Page

Mobius strip

Assignment 06

Task

Use conditionals to covert from Imperial (or, more specifically, US customary units) lengths to metric.

Concepts: comparison/relational operators; if, else, else if; .equals (string comparison)
Textbook: 5.1 - 5.2

Steps

Ask the user to enter a decimal length in inches, feet, or miles (one of these). Convert this length to centimeters, meters, and kilometers (all three of these).

Here are the basic conversion rates you may need to know:

  • 1 foot = 12 inches
  • 1 mile = 5280 feet
  • 1 meter = 100 centimeters
  • 1 kilometer = 1000 meters
  • 1 inch = 2.54 centimeters

If the user enters an unrecognized unit, give an error message rather than bogus output.

You must support these units as input in all cases: "miles", "feet", "inches". (You may also choose to support the singular form of these as well: "mile", "foot", "inch"). You must output the 3 metric results 1 per line. You may use either the full form ("centimeters", "meters", or "kilometers") or the abbreviation ("cm", "m", or "km") for these units.

Output

Here's some sample output of 3 different executions:

D:\TA\grading\A06>java ZtomaszeA06
This program converts US customary units of length to metric units of length.
Enter a length (number) and units (inches/feet/miles) to convert:
3 hands
Sorry, but 'hands' is not a unit understood by this program.

D:\TA\grading\A06>java ZtomaszeA06
This program converts US customary units of length to metric units of length.
Enter a length (number) and units (inches/feet/miles) to convert:
2 miles

2.0 miles equals:
321868.8 centimeters
3218.688 meters
3.218688 kilometers

D:\TA\grading\A06>java ZtomaszeA06
This program converts US customary units of length to metric units of length.
Enter a length (number) and units (inches/feet/miles) to convert:
0.3937 inches

0.3937 inches equals:
0.999998 centimeters
0.00999998 meters
9.999980000000001E-6 kilometers

Tips

  • Remember to use .equals (or, even better, .equalsIgnoreCase) instead of == when comparing Strings.
  • Is there any code repeated in all your cases when you use an if/else statement? If so, that code can probably be moved so that it occurs in only one place, either before or after the if.
  • A nextDouble() followed by a next() is probably the easiest way to read in input for this.

What to Submit

Upload your UsernameA06.java file to Tamarin.

Grading [5 points]

1 - Compiles
Your program compiles successfully (no errors)
1 - Input
You read in a decimal and a unit from the user.
1.5 - Output
You print your output according to the above requirements.
1.5 - Correctness
The given results are correct.

FAQs

Examples?
This is an example of using if/else and comparing Strings: Rumpelstiltskin.java

Also, this is my solution to the demo we did in lab: LegalAge.java. There's definitely more than one way to do it, so find one that works for you. You may want to try it different ways: as a series of unconnected ifs (as I did), as nested ifs, some combination, or even as a single if/else-if/else series. (What would you have to do to get this last one to work?)
I'm completely stuck!
Read the length and the units entered by the user into two separate variables. Use a double variable for the length and a String for the units. (Temporarily print them out to see if you are reading them in correctly.)

Now that you have the user's input, test to see what units the user entered. If the units they entered equals "miles", then you need to convert the length they entered accordingly. Otherwise, if they entered "feet", then convert the length accordingly for feet.

It's probably easiest to convert each Imperial length to the same metric units--say, meters. Once you have the equivalent length calculated in meters, it's easy to covert from that to centimeters and kilometers.


~ztomasze Index : TA Details: ICS111: A06
http://www2.hawaii.edu/~ztomasze
Last Edited: 21 Sep 2008
©2008 by Z. Tomaszewski.