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
> 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.
> 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 =).