Java Reference
In-Depth Information
7. Calculate the remaining change.
8. Find the number of nickels.
9. Calculate the remaining change.
10. The remaining change is the number of pennies.
From the previous discussion and algorithm, it appears that the program needs
variables to hold the number of half-dollars, quarters, and so on. However, the
numbers of half-dollars, quarters, and so on are not used in later calculations, so the
program can simply output these values without saving them in variables. The only
thing that keeps changing is the change, so the program needs only one variable:
VARIABLES
int change;
The program performs calculations using the values of a half-dollar, 50 ; a quarter, 25 ;
a dime, 10 ; and a nickel, 5 . Because these data are special and the program uses these
values more than once, it makes sense to declare them as named constants. (Using
named constants also simplifies later modification of the program.)
NAMED
CONSTANTS
static final int HALFDOLLAR = 50;
static final int QUARTER = 25;
static final int DIME = 10;
static final int NICKEL = 5;
In the preceding sections, we analyzed the problem and determined the formulas to
do the calculations. We also determined the necessary variables and named constants.
We can now expand the algorithm given in the section Problem Analysis and
Algorithm Design to solve the problem given at the beginning of this programming
example (expressing change in cents).
MAIN
ALGORITHM
1. Prompt the user for the input.
2. Get the input.
3. Echo the input by displaying the entered change on the screen.
4. Compute and print the number of half-dollars.
5. Calculate the remaining change.
6. Compute and print the number of quarters.
7. Calculate the remaining change.
8. Compute and print the number of dimes.
9. Calculate the remaining change.
10. Compute and print the number of nickels.
11. Calculate the remaining change.
12. Print the remaining change.
 
Search WWH ::




Custom Search