Back to 111 Main Page

Mobius strip

Assignment 4

Task

Combine everything you've learned so far to write a simple program that converts Imperial (or, more specifically, US customary units) lengths to metric.

Steps

Ask the user to enter a length in either inches, feet, or miles. Then, ask the user whether to convert this distance to centimeters, meters, or kilometers. Finally, print the result of the conversion.

You are free to create whatever text-based user interface you think best for this program. Here are a couple user interface possibilities, with user input marked in green. An "expert" interface:

This program converts US customary units of length to metric units of length.
Enter a length (number) and units (inches/feet/miles) to convert.
> 100 feet
Convert 100.0 feet to centimeters, meters, or kilometers?
> kilometers
100.0 feet == 0.03048 kilometers.

A simple, menu-based interface:

This program converts US customary units of length to metric units of length.
Choose a unit to convert FROM:
1. Inches
2. Feet
3. Miles
> 1
Enter the number of inches to convert:
> 100
Convert 100.0 inches TO:
1. Centimeters
2. Meters
3. Kilometers
> 2
100.0 inches == 2.54 meters.

Here are the basic conversion rates you need to know:

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

We will examine this program in a studio format in lab next week. (More details on this to follow.) Therefore, please name your class with your last name, followed by your first name, and then the assignment number: LastFirst#.java. So, for example, if I did this assignment, I would name my file TomaszewskiZach4.java

What to submit

Attach your LastFirst4.java to an email.

FAQs

Where's that demo code you talked about writing in class?
Here: Rumpelstiltskin.java

It shows how to read in a String with Scanner's nextLine method, and how to compare Strings with equalsIgnoreCase. (You could instead use the next method of Scanner, and the equals method to compare Strings. In this case, it would only read in one word, rather than the whole line, and Strings' case would make a difference.)
Reminders:
  • The number in your .java name is 4 (the assignment number), not your ID number.
  • Don't forget that you need to import java.util.Scanner; in order to use it.
  • You only need to create your Scanner once: Scanner keybd = new Scanner(System.in); After that, you can use keybd to read in all your input. (Narayan's section: If you're using methods, you do need to create a Scanner in each method in which you are reading in from the keyboard.)
Ah, I'm stuck!
To focus, try to think of how you would do this manually on paper. You need four things:
  • fromLength (the number of whatever you're converting FROM)
  • fromUnits (inches, feet, or miles)
  • toLength (the number you're converting TO)
  • toUnits (centimeters, meters, or kilometers)
You get fromLength, fromUnits, and toUnits from the user. (So maybe do that first.) Then you just need to calculate toLength.

There are two main schools of thought on how to do this. (There are also probably a number of variations and minor schools too.) School 1: Have 9 cases representing all the possible conversions formulas based on fromUnits and toUnits. Figure out which one of the nine cases to run, and so calculate toLength accordingly. Print the results.

School 2: Convert all Imperial lengths to inches. (3 cases to handle here). Then convert inches to cm. (So now we have toLength, but as cm). Then figure out which metric unit to convert toLength to (3 cases here as well, for a total of 6). Print results.

Grading

Out of 20 points:

1 - Submission
Follows required submission policies.
1 - File name
Named your file as requested.
2 - Compiles
4 - Input
Allows the user to input a decimal length as either inches, feet, or miles.
4 - Target units
Allows the user to request the inputted length to be converted to either centimeters, meters, or kilometers.
8 - Correct conversion
Correctly prints out the result of the conversion.


~ztomasze Index : TA Details: ICS111: Assignment 4
http://www2.hawaii.edu/~ztomasze
Last Edited: 07 Feb 2008
©2008 by Z. Tomaszewski.