Monday, 6 June 2016

Chapter 2 Exercise 22, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*2.22 (Financial application: monetary units) 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.


import java.util.Scanner;
 
public class ProgrammingEx2_22 {
 public static void main(String[] args) {
  // Create a Scanner
  Scanner input = new Scanner(System.in);
 
  // Receive the amount
  System.out
    .print("Enter the input as an integer whose last two digits represent the cents: ");
  int amount = input.nextInt();
 
  int remainingAmount = amount;
 
  // Find the number of one dollars
  int numberOfOneDollars = remainingAmount / 100;
  remainingAmount = remainingAmount % 100;
 
  // Find the number of quarters in the remaining amount
  int numberOfQuarters = remainingAmount / 25;
  remainingAmount = remainingAmount % 25;
 
  // Find the number of dimes in the remaining amount
  int numberOfDimes = remainingAmount / 10;
  remainingAmount = remainingAmount % 10;
 
  // Find the number of nickels in the remaining amount
  int numberOfNickels = remainingAmount / 5;
  remainingAmount = remainingAmount % 5;
 
  // Find the number of pennies in the remaining amount
  int numberOfPennies = remainingAmount;
 
  // Display results
  System.out.println("Your amount " + amount + " consists of");
  System.out.println("    " + numberOfOneDollars + " dollars");
  System.out.println("    " + numberOfQuarters + " quarters ");
  System.out.println("    " + numberOfDimes + " dimes");
  System.out.println("    " + numberOfNickels + " nickels");
  System.out.println("    " + numberOfPennies + " pennies");
 }
}

No comments :

Post a Comment