Results 1 to 3 of 3
Thread: Recursive Change Machine
- 04-01-2011, 02:37 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
Recursive Change Machine
I need to create a change machine using recursion but the exact directions state "The method will display all combinations of quarters, dimes, nickels, and pennies that equal the desired amount."
What that sounds like it's asking is for me to print out all possible coin combinations that could be used to give change. ie $1 = 100 pennies, 95 pennies and 1 nickel, 90 pennies and 2 nickels, 90 pennies and one dime,....., 4 quarters.
I have asked whether or not I really need to find that or not, but I have a lot to do so in case it is, I need to figure out how I could do it.
Now I have written code that should return the least amount of coins needed and this is the meat and potatoes of it. This is not the main method, it will print out how many of each coin will be needed in the main.
If anyone has any idea on how I could change this or how I could call these in my main to actually get and then display all possible combinations of coins, I'd really appreciate your help. ThanksJava Code://ChangeMostQuarters will be called first, each change method gives as many of that coin as possible public class ChangeMachine { private static int pennies = 0; private static int nickels = 0; private static int dimes = 0; private static int quarters = 0; public static void ChangeAllPennies(int exchange){ if(exchange >= 1){ pennies++; exchange--; } ChangeAllPennies(exchange); } public static void ChangeMostNickels(int exchange){ if(exchange >=5){ nickels++; exchange-=5; ChangeMostNickels(exchange); } else ChangeAllPennies(exchange); } public static void ChangeMostDimes(int exchange){ if(exchange >= 10){ dimes++; exchange-=10; ChangeMostDimes(exchange); } else ChangeMostNickels(exchange); return; } public static void ChangeMostQuarters(int exchange){ if(exchange >=25){ quarters++; exchange -= 25; ChangeMostQuarters(exchange); } else ChangeMostDimes(exchange); return; } }
- 04-01-2011, 03:42 AM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
What's with the "return;" in some of the voids? Is there any reason for that?
Besides that the code looks fine, what errors have you gotten?
- 04-01-2011, 03:46 AM #3
Similar Threads
-
transfering the file from one machine to another machine using ftp in java
By rkraj in forum Java SoftwareReplies: 0Last Post: 02-07-2011, 01:27 PM -
machine epsilon
By rosh72851 in forum Advanced JavaReplies: 9Last Post: 09-24-2008, 03:16 AM -
32 bit machine or 64 bit machine
By Kapil Gupta in forum Advanced JavaReplies: 13Last Post: 06-24-2008, 03:29 AM -
Getting name/ip of a machine
By Java Tip in forum Java TipReplies: 0Last Post: 03-02-2008, 07:11 PM -
Turing machine Help please!!
By bladeb2k in forum Java AppletsReplies: 1Last Post: 08-15-2007, 09:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks