Question

Write a program called CountCoins.java that prompts the user for the input file name (you can...

Write a program called CountCoins.java that prompts the user for the input file name (you can copy the getInputScanner() method given in the ProcessFile assignment) then reads the file. The file contains a series of pairs of tokens, where each pair begins with an integer and is followed by the type of coin, which will be “pennies” (1 cent each), “nickels” (5 cents each), “dimes” (10 cents each), or “quarters” (25 cents each), case-insensitively. Add up the cash values of all the coins and print the total amount of money. For example, if the input file contains the following text:


3 pennies 2 quarters 1 Pennies 23 NiCkeLs 4 DIMES


For the input above, your method should produce the following output:
Total money: $2.09

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CountCoins {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter input file name: ");
        String inName = in.nextLine();
        File file = new File(inName);
        Scanner fin;
        try {
            fin = new Scanner(file);
            double total = 0;
            int n;
            String type;
            while (fin.hasNextInt()) {
                n = fin.nextInt();
                type = fin.next();
                if (type.equalsIgnoreCase("pennies")) {
                    total += n*0.01;
                } else if (type.equalsIgnoreCase("quarters")) {
                    total += n*0.25;
                } else if (type.equalsIgnoreCase("nickels")) {
                    total += n*0.05;
                } else if (type.equalsIgnoreCase("dimes")) {
                    total += n*0.10;
                }
            }
            System.out.printf("Total money: $%.2f\n", total);
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " does not exists!");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a program called CountCoins.java that prompts the user for the input file name (you can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (In Python 3) Write a program with total change amount as an integer input, and output...

    (In Python 3) Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes So...

  • Write a program with total change amount as an integer input, and output the change using...

    Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes

  • In c++ Write a program that asks users to input the amount of money (in cents)...

    In c++ Write a program that asks users to input the amount of money (in cents) that will be exchanged to the least number of coins of the same value, output the results. Assume there are quarters (25c), dimes (10c), nickels(5c) and pennies(1c) available. ■CA 32 cmd.e eas input the anount of money (in cents) you want to exchange 39 39 can be exchanged to uarter: 25 me: 1 kel: e enny: 4 ress any key to continue-.

  • All items at a "Penny Fair" are priced less than $1.00. Write a program that makes...

    All items at a "Penny Fair" are priced less than $1.00. Write a program that makes change with a minimum number of coins when a customer pays for an item with a one dollar bill. Prompt for the item price in cents, report the change due, and the number of coins of each denomination required. Use only quarters, dimes, nickels, and pennies for change (no 50 cent pieces). See sample output but your program should work for any item price....

  • it c++ coding. please write on atom software. 1. Write a program that converts dollars into coins a. Request a dolla...

    it c++ coding. please write on atom software. 1. Write a program that converts dollars into coins a. Request a dollar amount as an integer: $5.34 is input as 534 b. Use a constant variable to represent each coin as a fixed value: const int NICKEL 5; c. Use division and the mod function to calculate the number of each coin. Change Calculator Enter dollar amount (as an integer): $534 The equivalent in coins: 21 Quarters 0 Dimes 1 Nickels...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • Write a script called makeChange that prompts the user for a monetary amount in cents less...

    Write a script called makeChange that prompts the user for a monetary amount in cents less than or equal to 99 cents (i.e. less than a loonie) and then prints how change would be made for that amount out of quarters (25-cent coins), dimes (10-cent coins), nickels (5-cent coins) and pennies (1-cent coins). The change your script specifies should be the minimum number of coins. For example, when making change for 88 cents, the result should indicate 3 quarters, 1...

  • Word Problems Workshee You have a jar of loose change which contains pen total value of...

    Word Problems Workshee You have a jar of loose change which contains pen total value of the change in the jar is $104. In total, there are exactly 1000 coins in the jar There are four times as many pennies as there are nickels and there are twice as many pennies as there are dimes. How many of each type of coin are there? 4. nies, nickels, dimes, and quarters. The

  • Write a class encapsulation the concept of coins (Coins java), assuming that coins have the following...

    Write a class encapsulation the concept of coins (Coins java), assuming that coins have the following attributes: a number of quarters, a number of dims, a number of nickels, and a number of pennies. Include a constructor (accept 4 numbers that represent the number of coins for each coin type), mutator and accessot methods, and method tostring. The tostring method should return a string in the following format: Total Value: $5.50 10 quarters, 20 dimes, 19 nickels, and 5 pennies...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT