Results 1 to 3 of 3
  1. #1
    EZlats is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default 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.

    Java 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;
        }
    }
    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. Thanks

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    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?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Having a return statement in a void method is perfectly fine. However it is pointless as the last line of code in the method.

    You might want to seek clarification from your teacher but I imagine this is supposed to be done with a single recursive method.

Similar Threads

  1. Replies: 0
    Last Post: 02-07-2011, 01:27 PM
  2. machine epsilon
    By rosh72851 in forum Advanced Java
    Replies: 9
    Last Post: 09-24-2008, 03:16 AM
  3. 32 bit machine or 64 bit machine
    By Kapil Gupta in forum Advanced Java
    Replies: 13
    Last Post: 06-24-2008, 03:29 AM
  4. Getting name/ip of a machine
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:11 PM
  5. Turing machine Help please!!
    By bladeb2k in forum Java Applets
    Replies: 1
    Last Post: 08-15-2007, 09:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •