Write a static method named \"countCoins\" that accepts one parameter (a Scanner
ID: 3644068 • Letter: W
Question
Write a static method named "countCoins" that accepts one parameter (a Scanner attached to an input file) whose data represents a person's money grouped into stacks of coins. Your method should add up the cash values of all the coins and print the total money at the end.The input consists of a series of pairs of tokens, where each pair begins with an integer and is followed by the type of coin, which will be either "pennies" (1 cent each), "nickels" (5 cents each), "dimes" (10 cents each), or "quarters" (25 cents each), case-insensitively. A given coin might appear more than once on the same line.
For example, if the input file contains the following single line of text:
3 pennies 2 quarters 1 pennies 3 nickels 4 dimes
In this example: 3 pennies are worth 3 cents;
2 quarters are worth 50 cents;
1 penny is worth 1 cent;
3 nickels are worth 15 cents;
4 dimes are worth 40 cents.
The total of these is 1 dollar and 9 cents. Therefore your method whould print:
Total money: $1.09
Here is a second example. Suppose the input file contains the following 4 lines of text. Notice the capitalization and spacing:
Copy the 4 lines below and paste into a text file named money.txt in the DrJava working directory to test with the program code at the end of this question.
12 QUARTERS 1 Pennies 33
PeNnIeS
10 niCKELs
Then your method should produce the following output:
Total money: $3.84
You may assume that the file contains at least 1 pair of tokens. You may also assume that the input is valid; that the input has an even number of tokens, that every other token is an integer, and that the others are valid coin types.
import java.util.*;
import java.io.*;
public class MatchIndex {
public static void main(String[] args) throws FileNotFoundException {
Scanner fileIn = new Scanner(new File("test.txt"));
matchIndex(fileIn);
}
// *** Your method code goes here ***
} // End of MatchIndex class
Explanation / Answer
// count up money as a double of dollars/cents; use printf at end
public static void countCoins(Scanner input) {
double total = 0.0;
while (input.hasNext()) {
int count = input.nextInt();
String coin = input.next().toLowerCase();
if (coin.equals("nickels")) {
count = count * 5;
} else if (coin.equals("dimes")) {
count = count * 10;
} else if (coin.equals("quarters")) {
count = count * 25;
}
total = total + (double) count / 100;
}
System.out.printf("Total money: $%.2f ", total);
}
// count up money as a total number of cents; use / and % at end public static void countCoins(Scanner input) {
int totalCents = 0;
while (input.hasNext()) {
int count = input.nextInt();
String coin = input.next().toLowerCase();
if (coin.equals("nickels")) {
totalCents += count * 5;
} else if (coin.equals("dimes")) {
totalCents += count * 10;
} else if (coin.equals("quarters")) {
totalCents += count * 25;
} }
int dollars = totalCents / 100;
int cents = totalCents % 100;
System.out.print("Total money: $" + dollars + ".");
if (cents < 10) {
System.out.print("0");
}
System.out.println(cents);
}
// implicit /100 in the multipliers
public static void countCoins(Scanner input) {
double total = 0.0;
while (input.hasNext()) {
double count = input.nextInt();
String coin = input.next().toLowerCase();
if (coin.equals("pennies")) {
count *= 0.01;
} else if (coin.equals("nickels")) {
count *= 0.05;
} else if (coin.equals("dimes")) {
count *= 0.10;
} else {
count *= 0.25;
}
total += count;
} System.out.printf("Total money: $%.2f ", total); }
Hope it will work out well
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.