Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help me understand how the program fucntions? Count Coins Write a static method named countCo-ins that accepts as its parameter a Scanner for an input

help me understand how the program fucntions?

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Count Coins Write a static method named countCo-ins 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 (as a String), which will be either "pennies" (1 cent each), "nickels" (5 cents each), "dimes" (10 cents each), or "quarters" (25 cents each), caseinsensitively. A given coin might appear more than once on the same line. For example, suppose the input file contains the following text: 3 pennies 2 quarters l pennies 3 nickels 4 dimes This file includes 3 pennies (worth 3 cents), 2 quarters (worth 50 cents), 1 penny (worth 1 cent), 3 nickels (worth 15 cents), and 4 dimes (worth 40 cents). The total of these is 3 + 50 +1 +15 + 40 = 109 cents, or 1 dollar and 9 cents. Therefore, your method would produce the following output if passed this input data: Total money: $1.09 Notice that for a one digit number of cents, we include a leading zero. (That is, we print $1.09 instead of $1.9.) Now suppose the input file contains the following text: 12 QUARTERS 1 Pennies 33 PeNnIeS 10 niCKELS Then your method would produce the following output: Total money: $3.84 Notice that the method calculates correctly regardless of the spacing between tokens (including line breaks) and the capitalization of the types of coins, You may assume that the le contains at least 1 pair of tokens. You may also assume that the input is valid; that is, that the input has an even number of tokens, that every other token is an integer, and that the others are valid coin types. 1 import java. util. *; 2 import java. io. *; 3 public class CountCoins { 4 public static void main(String args) throws FileNotFoundException { Scanner input1 = new Scanner (new File ("inputi. txt") ) ; countCoins (input1) ; 6 00 - 0) UT Scanner input2 = new Scanner (new File ("input2. txt") ) ; countCoins (input2) ; 10 11 12 / / TODO: Your Code Here -> Write a method 13 / / called countCoins for this problem 14 } 15

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions