It's not a secret anymore!*
The moose likes Beginning Java and the fly likes modify app to include array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "modify app to include array" Watch "modify app to include array" New topic
Author

modify app to include array

Giles Harney
Greenhorn

Joined: Jun 22, 2004
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
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Giles Harney
Greenhorn

Joined: Jun 22, 2004
Posts: 19
thank you Fred for your reply...

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

Joined: Oct 02, 2003
Posts: 11153
    
  16

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()

this way, if the size of your array changes, you don't have two worry about this loop.

some other things to be cautious of...

make sure your coinsNeeded array is big enough - you could probably use the same getLength() method above. which would again be good if you decided to add 50-cent coins.

you should probably put the data in the array in the opposite order than you have listed (which i think you figured out), or you could start at the last element of the array and work backwards in your loop...
 
Consider Paul's rocket mass heater.
 
subject: modify app to include array
 
Similar Threads
Calculating Change
Program: Change Due/Tendered
Vending Machine Project
Display nothing if == 0
Change Due/Tendered Program