Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method named countCoins that accepts as its parameter a Scanner f

ID: 3643871 • Letter: W

Question

Write a static method named countCoins that accepts as its parameter a Scanner for 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 text:
3 pennies 2 quarters 1 pennies 3 nickels 4 dimes
3 pennies are worth 3 cents, and 2 quarters are worth 50 cents, and 1 penny is worth 1 cent, and 3 nickels are worth 15 cents, and 4 dimes are worth 40 cents. The total of these is 1 dollar and 9 cents, therefore your method would produce the following output if passed this input data. Notice that it says 09 for 9 cents.
Total money: $1.09
Here is a second example. Suppose the input file contains the following text. Notice the capitalization and spacing:
12 QUARTERS 1 Pennies 33
PeNnIeS
10 niCKELs
Then your method would 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 CountMoney {

public static void main(String[] args) throws FileNotFoundException {
Scanner fileIn = new Scanner(new File("money.txt"));
countCoins(fileIn); // money = $3.84
}

// *** Your method code goes here ***

} // End of CountMoney 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote