Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating method to change money?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating method to change money?

    Im writing a method that must take an integer (amount) as input and return a number of denominations for that amount.

    Ex.
    I have the denominations: 100, 50, 20, 10, 5, 1

    I call my method with an amount; makeChange(135)
    The method should return: 1x100, 1x20, 1x10, 1x5

    I've searched all over the internet without luck. Where should i put my return statement? Since the method is of the type int, the return statement should as well, right?

    This is my code so far:
    	public int makeChange(int belob) {
    		int[] denominations = 
    			 {100, 50, 20, 10, 5, 1};
     
    		int[] bills = new int[denominations.length];
     
    	      // Loop through each denomination (starting at largest)
     
    	      for (int i=0; i<denominations.length; i++) {
     
    	         // Use one of that denomination until we need something smaller
     
    	         while (belob>=denominations[i]) {
    	            bills[i]++;
    	            belob -= denominations[i];
     
    	      // Output the result
     
    	      for (int i=0; i<denominations.length; i++) {
    	         if (bills[i]>0) {
    	            System.out.println(bills[i]+" x "+denominations[i]);
    	         }
    	      }
    	         }
    	      }
    	}


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Creating method to change money?

    Where should i put my return statement?
    At the end of the method after the method has done the work and is ready to return.

    Since the method is of the type int, the return statement should as well, right?
    The parameter type has nothing to do with the return. It looks like you need an int parameter for the amount of money and a group of int representing the number of each denomination needed to total the given amount.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Creating method to change money?

    Does your method need to return anything? In your method, you have it print out what you want it to print out, it seems. Besides, you can't have it return what you want as an int because "x" and "," are not int's. Maybe by return, you mean print out, and by that I would change your method:

    //old method
    public int makeChange(int belob)
    {
    //code here
    }
     
    //to
    public /*v___*/ makeChange(int belob)
    {
    //code here
    }

    I would also make the println into something that does not skip a line everytime it is called.

    Does your code print out roughly what you want it to, or does it not even print out the right values?

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating method to change money?

    When changing my method from
    public int makeChange(int belob)
    to
    public makeChange(int belob)
    Eclipse gives me an error, that my return type is missing. Seems that i must have a return statement somewhere.

    Right now i cant seem to get the method working no matter what i do, and eclipse points out that my code is filled with errors. :S

    --- Update ---

    Okay, I've solved some of my problems and i have my method working-ish, its just the wrong output :S
    I wrote some code testing it on 135 like this:
    public class QuestionOne {
    	public static void main(String[] args){
    		System.out.println("testing with 135");
    		QuestionOne.makeChange(135);
    	}
     
    	public static void makeChange(int belob) {
    		int[] denominations = 
    			 {100, 50, 20, 10, 5, 1};
     
    		int[] bills = new int[denominations.length];
     
    	      // Loop through each denomination (starting at largest)
     
    	      for (int i=0; i<denominations.length; i++) {
     
    	         // Use one of that denomination until we need something smaller
     
    	         while (belob>=denominations[i]) {
    	            bills[i]++;
    	            belob -= denominations[i];
     
    	      // Output the result
     
    	      for (int i1=0; i1<denominations.length; i1++) {
    	         if (bills[i1]>0) {
    	            System.out.println(bills[i1]+" x "+denominations[i1]);
    	         }
    	      }
    	         }
    	      }
    	}
    }

    my output is:

    testing with 135
    1 x 100
    1 x 100
    1 x 20
    1 x 100
    1 x 20
    1 x 10
    1 x 100
    1 x 20
    1 x 10
    1 x 5

    What could be wrong? And why doesnt it output 2x100 instead of 1x100 two times?

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Creating method to change money?

    What I typed in my first response was intentional. The
     /*v___*/

    was a hint at what you should do.

    As to your current issue, it's really quite simple, but tricky. Look closely at where your print statement is, and look at your output. Doesn't it seem to repeat itself, adding the next part that is needed so that the last 4 things it prints are the expected output? You put the print statement in the wrong spot, try moving it outside of every loop you have. It should be outside, by itself. As the making it on one line, you can figure out what to change to make that work.

    After I fixed the code, this was my output for 135, 225, and 315.

    testing with 135
    1 x 100, 1 x 20, 1 x 10, 1 x 5, 
    Expected: 1 x 100, 1 x 20, 1 x 10, 1 x 5
     
    testing with 225
    2 x 100, 1 x 20, 1 x 5, 
    Expected: 2 x 100, 1 x 20, 1 x 5
     
    testing with 315
    3 x 100, 1 x 10, 1 x 5, 
    Expected: 3 x 100, 1 x 10, 1 x 5

Similar Threads

  1. Deposit Money
    By spyxdaxworld in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 11:55 PM
  2. creating a new method that's not outputting correct result
    By orbin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 23rd, 2012, 12:12 AM
  3. NEED HELP! converting change given to money denominatons
    By Mrod359 in forum Object Oriented Programming
    Replies: 1
    Last Post: June 15th, 2012, 05:30 PM
  4. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 07:58 AM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM