Need help creating a program. Im very new to java...

"Write a recursive program which enumerates all the ways to give the change in coins (quarters,

dimes, nickels, pennies) for a given amount of money. That is, given a target sum s it should

produce a collection of arrays of size 4 (e.g., [8, 3, 1, 2], [9, 0, 2, 2], ... for a target amount of 2.37)

giving the number of coins of each type. The input to the program will be a single number given on

the standard input. The output will be a sequence of lines, where each line is a quadruple enclosed

in square brackets and the 4 numbers are separated by commas and denote the number of coins of

each denomination (quarter,dime,nickel,penny). For instance the beginning of the output would be

[8,3,1,2]

[9,0,2,2]

...

with the first line reporting 8 quarters, 3 dimes, 1 nickel, 2 pennies".

I've been assigned this program as practice for an in class test, supposedly you can do this in 20 or so lines of code, I don't see the logic here at all. Can anyone help me, its due tonight at midnight and I just have no clue where to start.

Thanks for any help in advance,

Dave

[1153 byte] By [peter.-.franka] at [2007-11-26 17:30:52]
# 1
package peterfrank;import java....
suparenoa at 2007-7-8 23:58:49 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/docs/books/tutorial/getStarted/ http://java.sun.com/docs/books/tutorial/Get reading, and let us know when you get stuck on something specific.
hunter9000a at 2007-7-8 23:58:49 > top of Java-index,Java Essentials,Java Programming...
# 3

> I've been assigned this program as practice for an in

> class test, supposedly you can do this in 20 or so

> lines of code, I don't see the logic here at all. Can

> anyone help me, its due tonight at midnight and I

> just have no clue where to start.

If you have no clue whatsoever, you cannot be helped.

Start doing this on a piece of paper. After you've written down the steps, try to write a Java program of it. If you get stuck along the way, post your code here and explain what it is you need help with.

Before posting code, read this: http://forum.java.sun.com/help.jspa?sec=formatting

Good luck.

prometheuzza at 2007-7-8 23:58:49 > top of Java-index,Java Essentials,Java Programming...
# 4

Can you do it on paper? Ignore Java for a moment and figure out the steps you would take to solve the problem using pencil and paper. Then you can figure out how to code it, using your textbook and the tutorials. As hunter said, once you make a start of it, we can help you with specific problems.

Too slow. prometheuzz said it quicker and better. :)

Message was edited by:

fragglemo

fragglemoa at 2007-7-8 23:58:49 > top of Java-index,Java Essentials,Java Programming...
# 5

I'll give you a hint. Ok actually I'll give you two.

1) Think from the smallest type of coin you have to the largest.

2) Coins are multiples of other types of coins (10 pennies equals 1 dime etc)

Might also want to think through how a cashier gives change back (specifically how do they count it and ensure that it's right when the register doesn't tell them how much)

Hope this helps,

PS.

Message was edited by:

puckstopper31

puckstopper31a at 2007-7-8 23:58:50 > top of Java-index,Java Essentials,Java Programming...
# 6
> peter.-.frank> ...> DaveI think the root problem might be that you have a multiple personality disorder. Get that fixed first, then maybe you can talk about trying out software development for a career choice.
warnerjaa at 2007-7-8 23:58:50 > top of Java-index,Java Essentials,Java Programming...
# 7
It works out for me. It's like pair programming, but you only need the one keyboard.jim.-.bobKev
kevjavaa at 2007-7-8 23:58:50 > top of Java-index,Java Essentials,Java Programming...
# 8

> peter.-.frank

> ...

> Dave

shhh thats my secret identity. Don't tell anybody! anyway i went to my TA and got him to let me slip the date by 24 hours. Now with his help I managed to get this code worked out

public final class MakeChange

{

public static void makeChange( int [ ] coins, int differentCoins,

int maxChange, int [ ] coinsUsed, int [ ] lastCoin )

{

coinsUsed[ 0 ] = 0; lastCoin[ 0 ] = 1;

for( int cents = 1; cents <= maxChange; cents++ )

{

int minCoins = cents;

int newCoin = 1;

for( int j = 0; j < differentCoins; j++ )

{

if( coins[ j ] > cents )// Cannot use coin j

continue;

if( coinsUsed[ cents - coins[ j ] ] + 1 < minCoins )

{

minCoins = coinsUsed[ cents - coins[ j ] ] + 1;

newCoin = coins[ j ];

}

}

coinsUsed[ cents ] = minCoins;

lastCoin[ cents ] = newCoin;

}

}

My new problem is that i need a scanner method to get the input into the code. My TA said that was up to me. I've looked online and all i can see is that you import java.util.scanner and then they have a line of code in the main method call that says

Scanner s = new Scanner(Scanner.in);

I never see anything that says how this is used or how you insert the value into the code or how the scanner knows what to do. Could someone please help me with this or even point me in the direction of a website that would explain it? Thanks alot for your help so far =).

peter.-.franka at 2007-7-8 23:58:50 > top of Java-index,Java Essentials,Java Programming...
# 9
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/scannerFile.html http://www.eg.bucknell.edu/~csci204/2007-spring/ScannerTest.html http://java.sun.com/j2se/1.5.0/docs/api/index.html...
suparenoa at 2007-7-8 23:58:50 > top of Java-index,Java Essentials,Java Programming...
# 10
> Now with his help I managed to get this code worked outI must be missing it. Where's the recursion (mentioned by the requirements)?~
yawmarka at 2007-7-8 23:58:50 > top of Java-index,Java Essentials,Java Programming...