## Solution for

Programming Exercise 2.4

THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.

Exercise 2.4:Write a program that helps the user count his change. The program should ask how many quarters the user has, then how many dimes, then how many nickles, then how many pennies. Then the program should tell the user how much money he has, expressed in dollars.

Discussion

The program will need variables to represent the number of each type of coin. Since the number of coins has to be an integer, these variables are of type

int. I'll call the variablesquarters,dimes,nickles, andpennies.The total value of the coins, when expressed in dollars, can be a non-integer number such as 1.57 or 3.02. Since the total value in dollars is a real number, I will use a variable of type

doubleto represent it. The variable is nameddollarsThe outline of the program is clear enough:

Declare the variables. Ask the user for the number of each type of coin, and read the responses. Compute the total value of the coins, in dollars. Display the result to the user.The function

TextIO.getlnInt()can be used to read each of the user's responses. The alternative functionTextIO.getInt()could also be used, but it is less safe. Suppose, for example, that the user responds to the request to type in the number of quarters by entering "7 quarters". AfterTextIO.getlnInt()reads the number 7, it will discard the extra input "quarters".TextIO.getInt()will read the 7 correctly, but the extra input is not discarded. Later, when the program tries to read the number of dimes, it sees the left-over input and tries to read that, without giving the user a chance to type in another response. You might want to experiment and see what happens if you changegetlnInt()togetInt(). (Of course, if the user's response is "I have 7 quarters" or "seven", then you are out of luck in any case.)Since one quarter is worth 0.25 dollars, the number of dollars in N quarters is 0.25*N. Similarly, a dime is worth 0.10 dollars, a nickle is 0.05 dollars, and a penny is 0.01 dollars. So, to get the total value of all the user's coins, I just have to add up

(0.25*quarters) + (0.10*dimes) + (0.05*nickles) + (0.01*pennies). This value is assigned to the variable,dollars, and that is the result that is displayed to the user.Alternatively, I could first have computed the total number of cents in all the coins, and then divided by 100 to convert the amount into dollars:

int totalCents; // Total number of cents in the coins. totalCents = 25*quarters + 10*dimes + 5*nickles + pennies; dollars = totalCents/100.0;Since

totalCentsis if typeint, it is essential here that I computedollarsastotalCents/100.0and not astotalCents/100. The value computed bytotalCents/100is an integer. For example, iftotalCentsis 397, thentotalCents/100is 3. UsingtotalCents/100.0forces the computer to compute the answer as a real number, giving 3.97.

The Solution

public class CountChange { /* This program will add up the value of a number of quarters, dimes, nickles, and pennies. The number of each type of coin is input by the user. The total value is reported in dollars. This program depends on the non-standard class, TextIO. */ public static void main(String[] args) { int quarters; // Number of quarters, to be input by the user. int dimes; // Number of dimes, to be input by the user. int nickles; // Number of nickles, to be input by the user. int pennies; // Number of pennies, to be input by the user. double dollars; // Total value of all the coins, in dollars. /* Ask the user for the number of each type of coin. */ TextIO.put("Enter the number of quarters: "); quarters = TextIO.getlnInt(); TextIO.put("Enter the number of dimes: "); dimes = TextIO.getlnInt(); TextIO.put("Enter the number of nickles: "); nickles = TextIO.getlnInt(); TextIO.put("Enter the number of pennies: "); pennies = TextIO.getlnInt(); /* Add up the values of the coins, in dollars. */ dollars = (0.25 * quarters) + (0.10 * dimes) + (0.05 * nickles) + (0.01 * pennies); /* Report the result back to the user. */ TextIO.putln(); TextIO.putln("The total in dollars is $" + dollars); } // end main() } // end class

[ Exercises | Chapter Index | Main Index ]