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

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

1 import java.util.Scanner; 2 3 public class ComputeChange { public static void

main(String[] args) { 4 // Create a Scanner Scanner input = new

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.printn(" 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

Step by Step Solution

3.55 Rating (159 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program Plan Create a class called FinancialApplication Initialise the main method with variables am... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!