Thursday, September 15, 2011

Java prgram. please help do it for me ive tried everything?

Create a Change application that prompts the user for an amount less than $1.00 and then displays

the minimum number of coins necessary to make the change. The change can be made up of quarters,

dimes, nickels, and pennies. The application output should look similar to:



enter change in cents:



minimum number of coins is:212

quarters:8

dimes:1

nickels:0

pennies:2
Java prgram. please help do it for me ive tried everything?
8 + 1 + 2 looks more like (total) 11 to me...



Start writing a program and then ask for help if you need it...
Java prgram. please help do it for me ive tried everything?
This took me more than a day one time. The problems are:

having the same names for

Count, Denomination, Denominational Value, and the coin's Formal Name.



Setting up the most sane translator, I create names for slot numbers. Those are

PENNIES = 0

NICKEL = 1;

DIME = 2;

QUARTER = 3;



those names can conveniently point to a 'which' of 'what collection'



Then, to count how many of each coin, I use Recursion... a method to call itself. Recursion is the most elegant solution.



//=======================

/* this is recursion

* read down to the call that calls to itself:

* Going in: the condition to stop repeating

*/

static private void configureChange( int chgDue ) {

// if less than a nickel, return to the program in progress

if( chgDue %26lt; 5) {

coinCOUNTS[ PENNY ] = chgDue;

return;

}

if( chgDue %26gt;= coinDENOMINATIONS[ QUARTER ] ) {

// add '1' to ea. individual coin slot

coinCOUNTS[ QUARTER ]++;



// call the same method again! loop! but first take value

// of a coin off the subTotal due

configureChange( chgDue - coinDENOMINATIONS[ QUARTER ] );



} else if( chgDue %26gt;= coinDENOMINATIONS[ DIME ] ) {

coinCOUNTS[ DIME ]++;

configureChange( chgDue - coinDENOMINATIONS[ DIME ] );

} else if( chgDue %26gt;= coinDENOMINATIONS[ NICKEL] ) {

coinCOUNTS[ NICKEL ]++;

}

}
We aren't here to do your homework or schoolwork for you!
  • layered hair
  • hair bands
  • No comments:

    Post a Comment