Question: 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
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 cents. For example, the input 1156 represents 11 dollars and 56 cents.
Listing 2.10

1 import java.util.Scanner; 2 import class 3 public class ComputeChange { 4. public static void main(String[] args) { // 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 enter input int remainingAmount = (int) (amount * 100); // Find the number of one dollars int number0fOneDo1lars = remainingAmount / 100; remainingAmount = remainingAmount % 100; 15 16 dollars 17 18 // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; 19 20 quarters 21 remainingAmount = remainingAmount % 25; 22 // Find the number of dimes in the remaining amount int numberofDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; 23 24 25 26 // Find the number of nickels in the remaining amount int number0fNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; 27 28 29 30 // Find the number of pennies in the remaining amount int number0fPennies = remainingAmount; 31 32 33 // Display results System.out.printn("Your amount " + amount + System.out.println(" System.out.println(" System.out.println(" System.out.println(" System.out.println(" 34 consists of"); 35 36 + numberofOneDollars + " dollars"); + number0fQuarters + " quarters "); + numberofDimes + + numberofNickels + 37 dimes"); nickels"); pennies"); 38 39 40 + numberofPennies + 41 { 42
Step by Step Solution
3.35 Rating (164 Votes )
There are 3 Steps involved in it
Output Enter an amount as integer for example 1156 3456 Your amount 1156 consists of 34 do... View full answer
Get step-by-step solutions from verified subject matter experts
