Java Reference
In-Depth Information
The lengths are given in feet and inches, and you need to find the equivalent length in
centimeters. One inch is equal to 2.54 centimeters. The first thing the program needs
to do is convert the length given in feet and inches to all inches. To convert the length
from feet and inches to inches, you multiply the number of feet by 12 (1 foot is equal to
12 inches), and add your answer to the given inches. Then you can use the conversion
formula, 1 inch = 2.54 centimeters, to find the equivalent length in centimeters.
Suppose the input is 5 feet and 7 inches. You find the total inches as follows:
PROBLEM
ANALYSIS
AND
ALGORITHM
DESIGN
2
totalInches = (12 * feet) + inches
= 12 * 5 + 7
= 67
You can then apply the conversion formula, 1 inch = 2.54 centimeters, to find the
length in centimeters.
centimeters = totalInches * 2.54
= 67 * 2.54
= 170.18
Based on this analysis, you can design an algorithm as follows:
1. Get the length in feet and inches.
2. Convert the length into total inches.
3. Convert total inches into centimeters.
4. Output centimeters.
The input for the program is two numbers: one for feet and one for inches. Thus,
you need two variables: one to store feet and the other to store inches. Because the
program will first convert the given length into inches, you need a third variable to
store the total inches. You need a fourth variable to store the equivalent length in
centimeters. In summary, you need the following variables:
VARIABLES
int feet;
//variable to store feet
int inches;
//variable to store inches
int totalInches;
//variable to store total inches
double centimeters; //variable to store length in centimeters
Recall that to calculate the equivalent length in centimeters, you need to multiply the
total inches by 2.54 . Instead of using the value 2.54 directly in the program, you
will declare this value as a named constant. Similarly, to find the total inches, you
need to multiply the feet by 12 and add the inches. Instead of using 12 directly in the
program, you will also declare this value as a named constant. Using named constants
makes it easier to modify the program later. Because the named constants will be
placed before the method main , you must use the modifier static to declare these
named constants (see the earlier section, Creating a Java Application Program).
NAMED
CONSTANTS
Search WWH ::




Custom Search