public class Change2 { public static void main(String[] args) { // Get the price and payment. int price = getPrice(); int payment = getPayment(); // Now make sure they paid enough for the sale, and that the sale // amount is a positive number. if (!validInput(price,payment)) { // If we made it here then the user gave us bad input. // Print a message and exit the program. System.out.println("Invalid input. Please try again."); return; } // If we made it here then we have valid input. // Calculate the amount of change to give. int change = payment - price; // Now get the number of quarters to return . int quarters = getNumCoins(25,change); // Compute remaining change to give. int remainder = change % 25; // Now get the number of dimes. int dimes = getNumCoins(10,remainder); // Compute remaining change to give. remainder = remainder % 10; // Get the number of nickels. int nickels = getNumCoins(5,remainder); // Compute remaining change to give. remainder = remainder % 5; // The remaining change to give must be given in pennies. int pennies = remainder; // Print the results. printResults(quarters, dimes, nickels, pennies, change); } public static int getPrice() { // Declare a TextInput Object. TextInput tin = new TextInput(); // Prompt the user to enter the price. System.out.print("What is the price? "); // Read and return the price from the keyboard. return tin.readInt(); } public static int getPayment() { // Declare a TextInput Object. TextInput tin = new TextInput(); // Prompt the user to enter the payment. System.out.print("What is the payment? "); // Read and return the payment from the keyboard. return tin.readInt(); } public static boolean validInput(int price, int payment) { if (price < 1) { // We don't give items away so price must be at least 1 return false; } // If we made it here then the price must be valid. // Now make sure that the payment is at least as high // as the price. if (payment < price) { return false; } // If we made it here then the input must be valid return true; } public static int getNumCoins(int value, int changeLeft) { return changeLeft / value; } public static void printResults(int quarters, int dimes, int nickels, int pennies, int change) { System.out.println("Quarters: " + quarters); System.out.println(" Dimes: " + dimes); System.out.println(" Nickels: " + nickels); System.out.println(" Pennies: " + pennies); System.out.println("Total change: " + change); } }