Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

canMakeChange

Language/Type: Java method basics mod return
Author: Marty Stepp (on 2010/12/28)

In this problem, we'll address the following question: Can a cash register containing a given amount of pennies (1-cent coins) and a given amount of nickels (5-cent coins) give a customer a given exact amount of cents of change? For example, if there are 3 pennies and 5 nickels in the cash register, is it possible to give exactly 19 cents of change? (No.) If there are 2 pennies and 7 nickels in the register, is it possible to give exactly 26 cents of change? (Yes.)

Write a method named canMakeChange that accepts three integer parameters representing the number of pennies in the cash register, the number of nickels in the cash register, and the desired amount of change to make. The method should return true if the coins in the register could be used to produce this exact amount of change, and false if not. For example, if the register contains 3 pennies and 4 nickels, it is able to exactly produce 17 cents of change (using 2 of the pennies and 3 of the nickels), so the call of canMakeChange(3, 4, 17) should return true. If the register contains 1 penny and 10 nickels, it is not able to exactly produce 8 cents of change (in this case, there are not enough pennies to reach exactly 8), so the call of canMakeChange(1, 10, 8) should return false.

The following are several sample calls to your method and the values they should return. You may assume that no negative parameter values are passed, but otherwise your method should work with any values passed.

Call Value Returned
canMakeChange(3, 4, 12)      //  3 pennies,  4 nickels,  12c change?
true
canMakeChange(1, 5, 26)      //  1 penny,    5 nickels,  26c change?
true
canMakeChange(24, 2, 31)     // 24 pennies,  2 nickels,  31c change?
true
canMakeChange(87, 19, 134)   // 87 pennies, 19 nickels, 134c change?
true
canMakeChange(0, 0, 0)       //  0 pennies,  0 nickels,   0c change?
true
canMakeChange(1, 1, 9)       //  1 penny,    1 nickel,    9c change?
false
canMakeChange(2, 7, 8)       //  2 pennies,  7 nickels,   8c change?
false
canMakeChange(4, 3, 39)      //  4 pennies,  3 nickels,  39c change?
false
canMakeChange(3, 80, 14)     //  3 pennies, 80 nickels,  14c change? 
false
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.