trouble with a java problem

This method accepts 1 integer, amount (the amount of money). Output the minimum number of in quarters, dimes, nickels and pennies used to make up the amount. For example, an amount of 32 would require 1 quarter, 1 nickel and 2 pennies.

This is the question^

My codes are:

public static int change (int amount) {

int quarters = amount / 25 ;

int firstresult = amount % 25 ;

return quarters ;

int nickel = firstresult / 5 ;

int secondresult = firstresult % 55 ;

return nickel ;

int penny = secondresult / 1 ;

int thirdresult = secondresult % 1 ;

return penny ;

}

The codes were working when i used System.out.println instead of return, but our teacher required us to use return (functions).

I get the compile error: Unreachable statement.

I am very new to java and I hope to learn more about it.

Re: trouble with a java problem

You are doing unconditional returns. This means that any subsequent statements will never be reached. So you should probably break this up into several methods, each one figuring its own denomination requirements.

A more efficient approach would be using a class or an array and use a single method.

Regards,

Jim

Re: trouble with a java problem

The way I would solve this is similar to how I would solve a binary conversion.

Say the amount is 32, like your example.

Since 32 > 25, we can assume there is one quarter. Then we would subtract 25 from 32 to get: 7. Since 7 < 10, we can assume there are no dimes. We do nothing for that. Since 7 > 5, we can assume that there is a nickle. We then subtract 5 from 7 to get: 2. Since 2 > 1, we can assume that there is a penny, subtract 1 from 2 to get: 1, since 1 = 1, we can assume that there is a penny. 1 - 1 = 0. Since the remainder is 0, the problem is solved. 1 Quarter, 0 Dimes, 1 Nickle, and 2 Pennies. My way may not be the fastest way of solving, but it gets the right answer.

Simple, yet effective way:

Code:

`private static void change(int money) {`

int num = money;

int q = 0;

int d = 0;

int n = 0;

int p = 0;

while (num != 0) {

if (num >= 25) {

q++;

num -= 25;

} else if (num >= 10) {

d++;

num -= 10;

} else if (num >= 5) {

n++;

num -= 5;

} else if (num >= 1) {

p++;

num--;

}

}

System.out.println("Quarters: " + q);

System.out.println("Dimes : " + d);

System.out.println("Nickles : " + n);

System.out.println("Pennies : " + p);

}

It gets your expected output:

Code:

`Enter amount to be broken: 32`

Quarters: 1

Dimes : 0

Nickles : 1

Pennies : 2

This could move easily be converted to return statements, rather than modulus division. I think so anyway. Maybe I just like more code. Lol.

However, your problem is very simple. When you return, it ends the method. So any code after the first return is un-reachable, since the method would have ended before any other code gets the chance to run.

You will have to implement some kind of loop to call the change function, or do as jim said.

Re: trouble with a java problem

@OP

The reason the compiler error is happening is that the return statement ends the function when it is encountered. It does not just give the value but also terminates it. So:

Code:

`int a = 1;`

return a; // This terminates the function and returns the value of a whis is 1

a++; // This will never be executed because the flow cannot reach it, it is thus an unreachble block of code.

Your teacher probably wants that you break your function in smaller pieces, each one returning the amount for a given coin so that you understand the working of a return statement:

[code]

int quarters = findTheAmountOfQuarters(amount);

//...

public int findTheAmountOfQuarters(amount) {

return amount / 25;

}

[/quote]

@Others

None of this is OOP, I understand OOP is just learning the language and does not understant even returns statements yet, but should we not be sugesting the modeling of a Money class that would be configured to represtent a given amount in monetary units and then dispensed to fill the ammount?

Re: trouble with a java problem

Quote:

Originally Posted by

**notivago** but should we not be sugesting the modeling of a Money class that would be configured to represtent a given amount in monetary units and then dispensed to fill the ammount?

Not necessarily. A lot of folks who come here are learning their first language. So they just start out writing code in main, not really understanding the class keyword or the significance of main. They they migrate to simply doing functional programming. Even when they begin to use classes, they are using them more like C structures than objects. I mean you can always suggest something, but you may get the "we haven't had that yet" reply.

Regards,

Jim