Homework Assignment 2: Height Converter
Overview
For this assignment, you will create a program that converts height values. In the minimal version, the program will ask the user for a height in feet and inches and then report that back to the user in centimeters. The standard (normal, successful completion) version will also ask the user for a height in centimeters as well and print out the height in inches. The challenge (extra-credit) version will print out the centimeter height in feet and fractional inches.
Educational objectives
- Gather information from the user
- Perform mathematical calculations
- Display results to specified accuracy
Instructions
Remember, the three versions are enhancements of each other. All the work in the Minimal version is included in the Standard version. Similarly, all the work in the Standard version is included in the Challenge version. You should submit only one version of the code. It is acceptable to submit something which is "intermediate", that is, between two of these levels. That would be a partial implementation of the enhancements for the next level.
Minimal version
Sample runs, Minimal version
The user input is shown in blue bold text.
Please enter the height to be converted.
First, enter the feet:5
Now, enter the inches:10
Your height is 5 feet 10 inches, or 70 inches. This is 177.80 cm.
The program prompts for the feet. It assumes that the user will enter an integer value, in this example 5. Next, the program prompts for the inches. Once again, it assumes that the user will enter an integer value, in the example, 10. Now the program does some calculations. First it calculates the total height in inches. This is just feet * 12 + inches. Then it calculates the height in centimeters. By definition since 1959, an inch is 2.54 centimeters. So, the height in centimeters is height in inches * 2.54. The program prints out the input height in feet and inches, as well the the height in inches and in centimeters. Print out the centimeter value withtwo places after the decimal point. For this assignment, assume that the inch and feet values will be integers.
Here is another example:
Please enter the height to be converted.
First, enter the feet:3
Now, enter the inches:7
Your height is 3 feet 7 inches or 43 inches. That is 109.22 cm.
The same calculations occurred here. The only difference is in the input values.
Notice that there are four output values that will change based on the values of the input. They are highlighted here in yellow.
Please enter the height to be converted.
First, enter the feet:3
Now, enter the inches:7
Your height is3feet7inches or43inches. That is109.22cm.
At this point, we must assume that the user will enter the kind of values we expect,intvalues. If they enter something else, our program will crash. Later in the term, we'll find out how we can make our code more "bullet-proof". Right now, it's going to be fragile.
Notes for Minimal Version
- The program shall prompt the user for height in integer feet and integer inches.
- The program shall only process a height. After the one height is converted, the program ends.
- The calculated values shall all be reported to the hundredths, two places after the decimal point, always.
Standard version
There are two enhancements for the Standard version over the Minimal version:
- Adjust the echoed height in feet and inches.
- Ask for a second height in centimeters and convert it to feet and inches.
The program will still assume that the user input is accurate. If unexpected values are entered, the behavior is "undefined".
Adjust the Echoed Height in Feet and Inches
The program will work if the user inputs "goofy" values.
Please enter the height to be converted.
First, enter the feet:2
Now, enter the inches:30
Your height is 2 feet 30 inches, or 54 inches. That is 137.16 cm.
Or even:
Please enter the height to be converted.
First, enter the feet:6
Now, enter the inches:-2
Your height is 6 feet -2 inches, or 70 inches. That is 177.80 cm.
[participation fodder: Why does this work, even if the inches are greater than 11 or less than 0?]
For this enhancement, alter the output to report the "appropriate" values for feet and inches. So, our "goofy" examples become:
Please enter the height to be converted.
First, enter the feet:2
Now, enter the inches:30
Your height is 4 feet 6 inches, or 54 inches. That is 137.16 cm.
and
Please enter the height to be converted.
First, enter the feet:6
Now, enter the inches:-2
Your height is 5 feet 10 inches, or 70 inches. That is 177.80 cm.
Hint: Work "backwards" from the calculated total inches. How many whole feet is that? (integer division) How many inches are left over? (remainder)
Centimeters to Inches
For the Standard version, the program shall ask for a second height, this time in centimeters. Assume that the input value is integer. The program will now convert that height in centimeters to inches and report that height in decimal inches. Print out the inches withthree places following the decimal point. Here is an example of the second half of the program.
Please enter a second height to be converted.
Enter the height in centimeters:175
Your height is 175 cm, or 68.898 inches.
To convert from centimeters to inches, we "reverse" the process. That is, divide by 2.54, rather than multiply.
Sample runs, Standard version
So, here is an example of the full program run:
Please enter the height to be converted.
First, enter the feet:6
Now, enter the inches:-2
Your height is 5 feet 10 inches, or 70 inches. That is 177.80 cm.
Please enter a second height to be converted.
Enter the height in centimeters:175
Your height is 175 cm, or 68.898 inches.
Please note: The program first asks for integer feet, then integer inches, finally integer centimeters. The first two values work together to provide the first height value, in feet and inches. The third input value is the second height value, in centimeters.
In the Standard version, there are six values that vary based on the input values: four, based on the feet and inches input (highlighted in yellow), and two, based on the centimeter input (highlighted in cyan).
Please enter the height to be converted.
First, enter the feet:6
Now, enter the inches:-2
Your height is5feet10inches, or70inches. That is177.80cm.
Please enter a second height to be converted.
Enter the height in centimeters:175
Your height is175cm, or68.898inches.
Challenge version (extra-credit)
For the Challenge version, there is another enhancement over the Standard version:
- Update the report of the height converted from centimeters to inches to include feet and fractional inches.
Feet and Inches
This first enhancement is really quite simple. In addition to reporting the converted height in inches, report it in feet and inches.
Please enter a second height to be converted.
Enter the height in centimeters:175
Your height is 175 cm. That's 68.898 inches, or 5 feet 8.898 inches.
Fractional Inches
"Imperial units" sensibilities are offended by reporting values with feet and decimal inches. That's just not the way it is done. Update the inches to report the value to the nearest eighth. Our example would be:
Please enter a second height to be converted.
Enter the height in centimeters:175
Your height is 175 cm. That's 68.898 inches, or 5 feet 8-7/8 inches.
For this assignment, the fractional inch value can always be reported in eighths of an inch.
Please enter a second height to be converted.
Enter the height in centimeters:200
Your height is 200 cm. That's 78.740 inches, or 6 feet 6-6/8 inches.
or even:
Please enter a second height to be converted.
Enter the height in centimeters:127
Your height is 127 cm. That's 50.000 inches, or 4 feet 2-0/8 inches.
We'll learn about theifstatement next week. That's how we could fix this to report "6 feet 6-3/4 inches" for 200 cm, or "4 feet 2 inches" for 127 cm.
Note: This can be accomplished using only the basic operations of addition (+), subtraction (-), multiplication (*), division (/), and remainder (%). Remember that int values use integer division, where double values use floating-point division. Using type conversion tointwill be helpful, (int)x.
Hint: If you add one-half, truncation (throwing away the fractional part) will look like rounding.
Original value |
+ 0.5 |
Truncated value |
3.0 |
3.5 |
3 |
3.2 |
3.7 |
3 |
3.4 |
3.9 |
3 |
3.6 |
4.1 |
4 |
3.8 |
4.3 |
4 |
4.0 |
4.5 |
4 |
Sample run, Challenge version
Please enter the height to be converted.
First, enter the feet:6
Now, enter the inches:-2
Your height is5feet10inches, or70inches. That is177.80cm.
Please enter a second height to be converted.
Enter the height in centimeters:175
Your height is175cm. That's68.898inches, or5feet8-7/8 inches.
In the Standard version, there are nine values that vary based on the input values: four, based on the feet and inches input (highlighted in yellow), and five, based on the centimeter input (highlighted in cyan).
Report
In addition to the Java source code, the assignment includes a brief written report that answers the following questions:
- How did you approach (get started with) this assignment?
- How did you test your program?
- What works and what doesn't work?
- What did you learn from this assignment? What would you do differently next time?
Submit the written report in the same submission as the Java code.
Notes about the Report
- The written report does not have to be long. A paragraph or two is probably sufficient for this assignment.
- The file format for written report is ASCII text (.txt file). As a plain text file, you don't have to worry about issues like bold, italics, images. It should make it simpler.
- You may find it easier to write the written report if you start it early and take notes about the process as you work through the problem, rather than trying to write the report at the end, from memory.
- The final question: "What would you do differently next time?" is asking about how you would approach the next programming assignment based on what you learned in this assignment. It is not about this specific programming problem.
- Put your name and this assignment number and name in the header at the top of the written report.
- The header for the report shall include the level you want the assignment graded: Minimal, Standard, or Challenge.
Important Note: Failure to indicate level will result in a style deduction.
Submission
Two files:
- The Java source code file that converts user-entered height values (.java)
- The written report in an ASCII, plain-text file (.txt)
Please make sure that you include the "level" in the header. The potential level values are: Minimal, Standard, and Challenge. The differences in the requirements for these levels is outlined above.
Grading Rubric
15 points for the Standard Version:
Functionality: 10 points
7/ Minimal Version (program runs, asks for height in feet and inches, echoes those values and reports total inches and centimeters, to 2 places)
10/ Standard Version (Minimal, plus echoes "corrected" feet and inch values, and asks for height in centimeters and reports decimal inch equivalent, to 3 places)
11/ Challenge Version (Standard, plus reports centimeter heights in feet and fractional inches)
Style: 2 points
2/ Conforms to the current guidelines and assignment-specific instructions
Documentation: 3 points
1/ ASCII report file
1/ report addresses questions
1/ required comments in the code