Wednesday, January 18, 2012

Lab 4: Distances

For this lab you will write a program that asks the user for a distance in various units and then translates that distance into meters. Here are some examples of the program output:
Enter distance:1m
You entered 1.0 meters.

Enter distance:33.12m
You entered 33.12 meters.

Enter distance:1.456cm
You entered 0.01456 meters.

Enter distance:1234.88mm
You entered 1.2348800000000002 meters.

Enter distance:5.1234km
You entered 5123.400000000001 meters.

The user can only type a number (a double) followed by a "m", "km", "cm", or "mm".

Hint: use if-else to check the last two characters in the user's String. If it is not "km" or "cm" or "mm" then you can assume the it must be "m", meters.

Hint: to turn a string into a double use Double.parseDouble().

Hint: remember that 1m equals 100cm, and 1m equals 1000mm, and 1km equals 1000m.

No comments: