This week's giveaway is in the Java/Jakarta EE forum.We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# modify app to include array

Greenhorn
Posts: 19
I am trying to understand how to create this array... Actually I am trying to understand why an array should be used?

Would my first array be something like;

int [ ] coins = { 1, 5, 10, 25 };

?

Thanks,
-Giles

----
�Use an array to store the four values corresponding to coin types. Also, use an array to store the number amount of each coin based on the user�s input.
�To calculate the number amount of each coin, use a for loop that divides the remaining cents by the coin value.
�Assume valid data is entered.

import javax.swing.JOptionPane;
import java.lang.Math;

class CountMoney {
public static void main (String[] args){

double amount, sum;

String inputAmount = JOptionPane.showInputDialog(
"Enter number of cents:");

amount=Double.parseDouble(inputAmount);

int remainingAmount = (int)amount;

// Find the number of quaters in the remaining amount
int numOfQuarters = remainingAmount/25;
remainingAmount = remainingAmount%25;

// Find the number of dimes in the remaining amount
int numOfDimes = remainingAmount/10;
remainingAmount = remainingAmount%10;

// Find the number of nickels in the remaining amount
int numOfNickels = remainingAmount/5;
remainingAmount = remainingAmount%5;

// Find the number of pennies in the remaining amount
int numOfPennies = remainingAmount;

// Display results
if (numOfQuarters == 1) {
System.out.println(numOfQuarters + "--Quarter");
}
else if (numOfQuarters > 1) {
System.out.println(numOfQuarters + "--Quarters");
}
if (numOfDimes == 1) {
System.out.println(numOfDimes + "--Dime");
}
else if (numOfDimes > 1) {
System.out.println(numOfDimes + "--Dimes");
}
if (numOfNickels == 1) {
System.out.println(numOfNickels + "--Nickel");
}
else if (numOfNickels > 1) {
System.out.println(numOfNickels + "--Nickels");
}
if (numOfPennies == 1) {
System.out.println(numOfPennies + "--penny");
}
else if (numOfPennies > 1) {
System.out.println(numOfPennies + "--pennies");
}
}
{

} // method main
}

lowercase baba
Bartender
Posts: 12602
50
There are a couple of reasons why you might want to use an array. first, can then use a loop with a counter to access each member. you don't have to keep re-writing code like you did for your 4 coin types, you can just do (pseudo code):

this will then put the number of coins needed into an array as well. (note that you may want to re-think the order you put the values in your coinValues array, or how you loop through it...)

Why would you do this? well, with a loop like this, you could EASILY add 50-cent peices, or silver dollars, or (if you live in Canada) \$2 coins... and you will automatically extract those values into your coinsNeeded array.

you still then have to write the output case, but this can save you writing a lot of code...

Giles Harney
Greenhorn
Posts: 19

lets see if i am on the right track;

while (i < size of array)
coinsNeeded[i] = remaining amount / coinValues[i];
remaining amount = remaining amount % coinValues[i];
i++;

for (int i = 0; i<=3; i++)
{
coinsNeeded[i] = remaining amount / coinsValues[i];
remaining amount = remaining amount % coinValues[i];
}

'i' would be the counter, correct?
so, if on the first pass would this be correct;

coinsNeeded[0]= remaining amount / coinsValues[0];
which would mean, set the first element in the coinsNeeded array equal to the result of the remaining amount being divided by the first element in the coinsValues array (25?) ?

thanks!
-Giles

fred rosenberger
lowercase baba
Bartender
Posts: 12602
50
This looks pretty good to me. of course it still needs a little fleshing out, but you're on the right track.

and instead of i<=3, you can use i<coins.getLength()