Você está na página 1de 3

Java Algorithms and Pseudocodes

Vending machine change (Savitch, p69)


Given
the user enters an amount of change from 1 to 99 cents
Figure out
how the program will respond by telling the user the ONE combination of coins that equal the
change
Pseudocode: starting skeleton
1. Enter a whole number from 1 to 99.
2. I will output a combination of coins equal to the number entered.
Example to help understand the problem
amount entered = 87
87 cents in coins:
3 quarters = 75
1 dime = 10
0 nickels = 0
2 pennies = 2
So, the variables needed would be:
int amount, quarters, dimes, nickels, pennies;

Lay out the algorithm in pseudocode


1. Read the amount into the variable amount.
2. Set the variable quarters = maximum number of quarters possible in amount.
3. Reset amount to the change left after giving out that many quarters.
4. Set the variable dimes = maximum number of dimes possible in amount.
5. Reset amount to the change left after giving out that many dimes.
6. Set the variable nickels = maximum number of nickels possible in amount.
7. Reset amount to the change left after giving out that many nickels.
8. pennies = amount;

9. Output the original amount and the numbers of each coin.


Understanding the algorithm
The algorithm changes the value of amount. But you need the original amount at the end, so that you
can output it. So use one more variable, called originalAmount, to save the original amount. Modify
your pseudocode.
Algorithm pseudocode modified
1. Read the amount into the variable amount.
2. originalAmount = amount;
3. Set the variable quarters = maximum number of quarters possible in amount.
4. Reset amount to the change left after giving out that many quarters.
5. Set the variable dimes = maximum number of dimes possible in amount.

6. Reset amount to the change left after giving out that many dimes.
7. Set the variable nickels = maximum number of nickels possible in amount.
8. Reset amount to the change left after giving out that many nickels.
9. pennies = amount;
10. Output originalAmount and the numbers of each coin.

Translating the pseudocode into Java program


***********************
Line 1: Read the amount into the variable amount.
This calls for:
prompting the user
getting user input from keyboard
So,

System.out.println(Enter a whole number from 1 to 99.);


System.out.println(I will output a combination of coins );
System.out.println(that equals the change you requested.);
Scanner keyboard = new Scanner(System.in);
amount = keyboard.nextInt();

***********************
Line 2 sets the value of originalAmount it is already Java code, so no need to translate.
Thus far, the main part of the program looks like this:
public static void main(String[] args)
{
// Declare the variables first
int amount, originalAmount, quarters, dimes, nickels, pennies;
System.out.println(Enter a whole number from 1 to 99.);
System.out.println(I will output a combination of coins );
System.out.println(that equals the change you requested.);
Scanner keyboard = new Scanner(System.in);
amount = keyboard.nextInt();
originalAmount = amount;

***********************
Line 3: Set the variable quarters = maximum number of quarters possible in amount.
Line 4: Reset amount to the change left after giving out that many quarters.
Let us understand this with an example:
for 87 cents, you can have a maximum of 3 quarters, because 3 x 25 = 75
remainder amount is 87 75 = 12

This tells us what operators to use: / and %


87 / 25 = 3 (maximum number of 25s in 87)
87 % 25 = 12 (the remainder)
Now,
87 is your amount
25 is your quarters
So, replace the numbers with variable names:
amount/25 = quarters;
amount%25 = amount;

But in the actual code, reverse the left and right-hand sides:
quarters = amount/25;
amount = amount%25;

***********************
Lines 5-8:
You realize that dimes and nickels are treated in a similar way, so:
dimes = amount/10;
amount = amount%10;
nickels = amount/5;
amount = amount%5;

***********************
Line 9:
pennies = amount;

This is already Java code, so no need to translate.


***********************
Line 10: Output originalAmount and the numbers of each coin.
This is a simple printout statement.
Now your code is ready.

Você também pode gostar