Rewrite Listing 2.10, ComputeChange. java, to fix the possible loss of accuracy when converting a float value
Question:
Rewrite Listing 2.10, ComputeChange. java, to fix the possible loss of accuracy when converting a float value to an int value. Read the input as a string such as "11.56". Your program should extract the dollar amount before the decimal point and the cents after the decimal amount using the indexOf and substring methods.
Listing 2.10
Transcribed Image Text:
1 import java.util.Scanner; 2 3 public class ComputeChange { public static void main(String[] args) { 4 // Create a Scanner Scanner input = new Scanner(System.in); // Receive the amount System.out.print( "Enter an amount in double, for example 11.56: "); double amount = input.nextDouble(); 10 11 12 13 14 15 16 int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; 17 18 // Find the number of quarters in the remaining amount int number0fQuarters = remainingAmount / 25; 19 20 21 remainingAmount = remainingAmount % 25; 22 // Find the number of dimes in the remaining amount int number0fDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 // Find the number of nickels in the remaining amount int number0fNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; // Display results System.out.println("Your amount System.out.println(" System.out.println(" System.out.printın(" System.out.println(" System.out.println(" " consists of"); + numberofOneDollars + " dollars"); + numberofQuarters + " quarters "); + amount + + numberofDimes + " dimes"); 38 + numberofNickels + " nickels"); pennies"); 39 + numberofPennies + 40 %3D 41 42 } Enter an amount, for example, 11.56: 11.56 JEnter Your amount 11.56 consists of 11 dollars 2 quarters 0 dimes 1 nickels 1 pennies line# 11 13 16 17 20 21 24 25 28 29 32 variables amount 11.56 remainingAmount 1156 56 numberofOneDollars 11 numberofQuarters numberofDimes numberofNickels numberofPennies
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 66% (3 reviews)
Program Plan Create a class called FinancialApplication Initialise the main method with variables am...View the full answer
Answered By
Rishabh Ojha
During my undergraduate i used to participate as TA (Teaching Assistant) in several electronics and computers subject. I'm passionate about learning Computer Science as my bachelors are in Electronics but i learnt most of the Computer Science subjects on my own which Machine Learning also. At Present, i'm a working professional pursuing my career as a Machine Learning Engineer and i want to help others learn during my free hours, that's all the motivation behind giving tuition. To be frank i have no prior experience of tutoring but i have solved problems on opensource platforms like StackOverflow and github. ~Thanks
4.90+
3+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Rewrite Listing 2.10, ComputeChange .java, to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the...
-
A hospital administrator worries about the possible loss of electric power as a result of a power blackout. The hospital, of course, has a standby generator, but it too is subject to failure, having...
-
Using a present value table, your calculator, or a computer program present value function, verify that the present value of $100,000 to be received in five years at an interest rate of 16%,...
-
Paul made the following acquisitions and disposals of ordinary shares in Crimmon plc: No further shares were acquired during 2020. Against which acquisitions will each of the above disposals be...
-
Suppose that a and b are integers, a 4 (mod 13), and b 9 (mod 13). Find the integer c with 0 c 12 such that a) C 9a (mod 13). b) C 11b (mod 13). c) C a + b (mod 13). d) C 2a + 3b (mod 13). e)...
-
Ammonium dinitramide (ADN), NH 4 N(NO 2 ) 2 , was considered as a possible replacement for aluminum chloride as the oxidizer in the solid fuel booster rockets used to launch the space shuttle. When...
-
Problem 8 is the same as problem 7 with respect to initial measurement of the ARO liability. Now assume that Tadpoles credit standing improves over time, causing the credit-adjusted risk-free rate to...
-
Cretin Enterprises uses a predetermined overhead rate of $21.40 per direct labor-hour. This predetermined rate was based on 8,000 estimated direct labor-hours and $171,200 of estimated total...
-
Given that f(x) = x2 -6 and g(x)=-7x + 5, find d() () if it exists.
-
What are the syntax and semantics of the two forms of the foreach statement?
-
On August 31, 2018, Freemont Co. purchased $18,000 of inventory with a one-year, 8 percent note payable. Journalize the following for the company: 1. Accrual of interest expense on December 31, 2018...
-
Assume a vehicle plate number consists of three uppercase letters followed by four digits. Write a program to generate a plate number.
-
Prepare the cash flow statement under the direct format for Michael Hart Associates using the data provided in E22-7. In E22-7 Michael Hart Associates closed its books for the current year. The firm...
-
How can radio broadcasting manage to support itself commercially to avoid the free rider problem, despite the characteristics of a public good
-
Suppose that with some probability there can be a pandemic next year. If there is a pandemic next year, then the consumption will be c(p) . If there is no pandemic then the consumption will be c(n) ....
-
What is the primary objective of a page table, and how does it function within the framework of virtual memory management systems?
-
Activity: In a Health Care Setting This activity will help you to better understand how disinfection works in various health related settings. You will be determining what agent(s) will best suit the...
-
QUESTION ONE Let us assume that currently, the Zambian Ministry of Transport has a rule that allows commercial truck drivers to drive up a limit of 90 hours per week; after 40 hours per week,...
-
The assets and liabilities of Beckwith Garden Supply, Inc., as of December 31, 2017, and revenues and expenses for the year ended on that date follow:...
-
Outline some of the major problems confronting an international advertiser.
-
Modify Listing 28.4, UnweightedGraph.java, to override the remove(V v) method defined in the Graph interface. Data from Listing 28.4, 1mport java.util.: 3 publ1c class UnweightedGraph 1mplements...
-
Modify Listing 28.4, UnweightedGraph.java, to override the remove(int u, int v) method defined in the Graph interface. Data from Listing 28.4, 1mport java.util.: 3 publ1c class UnweightedGraph...
-
The weighted nine tails problem in the text uses a 3 * 3 matrix. Assume that you have 16 coins placed in a 4 * 4 matrix. Create a new model class named WeightedTailModel16. Create an instance of the...
-
Roger Inc. owns 90% of the common shares of Soldier Inc. For the year ended December 31, 20X4, Solider earned $20,000 of net income and paid $5,000 in dividends. What is the journal entry made by...
-
Techie Ted operates as a consultant on technical matters to start up businesses. Ted's average gross receipts for the past three years is $3,000,000. He operates the business from a very old building...
-
The following are the number of units produced and total cost of production of Company ZYX for the first three months of the year: 100,000 units @ $1,650,000 125,000 units @ $2,050,000 90,000 units...
Study smarter with the SolutionInn App