/** * Title: Chapter 2, "Primitive Data Types and Operations" * Description: Examples for Chapter 2 * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // ComputeChange.java: Break down an amount into smaller units public class ComputeChange { /**Main method*/ public static void main(String[] args) { double amount; // Amount entered from the keyboard // Receive the amount entered from the keyboard System.out.println( "Enter an amount in double, for example 11.56"); amount = MyInput.readDouble(); int remainingAmount = (int)(amount*100); // Find the number of one dollars int numOfOneDollars = remainingAmount/100; remainingAmount = remainingAmount%100; // Find the number of quarters in the remaining amount int numOfQuarters = remainingAmount/25; remainingAmount = remainingAmount%25; // Find the number of dimes in the remaining amount int numOfDimes = remainingAmount/10; remainingAmount = remainingAmount%10; // Find the number of nickels in the remaining amount int numOfNickels = remainingAmount/5; remainingAmount = remainingAmount%5; // Find the number of pennies in the remaining amount int numOfPennies = remainingAmount; // Display results System.out.println("Your amount " + amount + " consists of "); System.out.println(numOfOneDollars + "\t dollars"); System.out.println(numOfQuarters + "\t quarters"); System.out.println(numOfDimes + "\t dimes"); System.out.println(numOfNickels + "\t nickels"); System.out.println(numOfPennies + "\t pennies"); } } // ------------------- // a class for reading various types from the keyboard (System.in) class MyInput { public static String readString() { String string = ""; java.io.BufferedReader bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); try { string = bufferedReader.readLine(); } catch (java.io.IOException ex) { throw new RuntimeException(ex); } return string; } public static int readInt() { return Integer.parseInt(readString()); } public static double readDouble() { return Double.parseDouble(readString()); } // test all the methods of this class public static void main(String[] args) { System.out.println("Testing 'readString()'"); System.out.print("Input your string : "); System.out.println("Your string was '" + readString() + "'"); System.out.println("\nTesting 'readInt()'"); System.out.print("Input your int : "); System.out.println("Your int was '" + readInt() + "'"); System.out.println("\nTesting 'readDouble()'"); System.out.print("Input your double : "); System.out.println("Your double was '" + readDouble() + "'"); } }