# Creating method to change money?

• November 19th, 2012, 05:47 AM
j4ze
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:
Code :

``` 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]); } } } } }```
• November 19th, 2012, 06:58 AM
jps
Re: Creating method to change money?
Quote:

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.

Quote:

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.
• November 19th, 2012, 07:33 AM
aesguitar
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:

Code java:

```//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?
• November 19th, 2012, 09:26 AM
j4ze
Re: Creating method to change money?
When changing my method from
Quote:

public int makeChange(int belob)
to
Quote:

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:
Code :

```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?
• November 19th, 2012, 03:21 PM
aesguitar
Re: Creating method to change money?
What I typed in my first response was intentional. The
Code java:

` /*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.

Code :

```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```