210 likes | 307 Views
Another problem for your consideration. Make change. Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in 5-cent increments. The machine accepts only a single dollar bill to pay for the item.
E N D
Make change Write a program that determines the change to be dispensed from a vending machine. • An item in the machine can cost between 25 cents and a dollar, in 5-cent increments. • The machine accepts only a single dollar bill to pay for the item. For example, a possible sample dialog might be: Enter price of an item: 45 You bought an item for 45 cents and gave me a dollar so your change is: 2 quarters, 0 dimes, and 1 nickel
Make change • Steps in our algorithm: • Get the price. • Calculate change. • Report the results.
Make change v1 /* File : Change.java Author: G.J. Grevera Desc. : Purchase an item and make change. */ public class Change { public static void main ( String args[] ) { //get the price //calculate change //report results (change) } }
Make change v2 /* File : Change.java Author: G.J. Grevera Desc. : Purchase an item and make change. */ public class Change { public static void main ( String args[] ) { //get the price int price = 35; //calculate change int quarters = 0; int dimes = 0; int nickels = 0; //report results (change) System.out.println( "Bye.“ ); } }
Make change v3 public static void main ( String args[] ) { //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = 35; //calculate change int quarters = 0; int dimes = 0; int nickels = 0; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); }
Make change v3 public static void main ( String args[] ) { //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = 35; //calculate change int quarters = 0; int dimes = 0; int nickels = 0; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); } Enter price of an item: You bought an item for 35 cents and gave me a dollar so your change is: 0 quarters, 0 dimes, and 0 nickels
Make change v4 import java.util.Scanner; public class Change { public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int quarters = 0; int dimes = 0; int nickels = 0; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); } } Next, how do we calculate our change?
Make change v5 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = 0; int dimes = 0; int nickels = 0; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); } How do we calculate the number of quarters in our change?
Make change v5 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = change / 25; int dimes = 0; int nickels = 0; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); } Ok. How do we calculate the number of dimes in our change?
Make change v5 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = change / 25; change = change – quarters * 25; int dimes = 0; int nickels = 0; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); } Ok. How do we calculate the number of dimes in our change?
Make change v5 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = change / 25; change = change – quarters * 25; int dimes = change / 10; int nickels = 0; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); } Ok. How do we calculate the number of nickels in our change?
Make change v5 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = change / 25; change = change – quarters * 25; int dimes = change / 10; change = change – dimes * 10; int nickels = change / 5; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters,“ ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels“ ); } Done!
Make change v5 (final) /* File : Change.java Author: G.J. Grevera Desc. : Purchase an item and make change. */ import java.util.Scanner; public class Change { public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = change / 25; change = change - quarters * 25; int dimes = change / 10; change = change - dimes * 10; int nickels = change / 5; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters," ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels" ); } }
Food for thought . . . • What message appears if there is exactly 1 in the change? • 1 quarters • 1 dimes • 1 nickels • How can we fix this to say ‘1 quarter’ instead?
Make change v6 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = change / 25; change = change - quarters * 25; int dimes = change / 10; change = change - dimes * 10; int nickels = change / 5; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters," ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels" ); } How can we fix this to say ‘1 quarter’ instead?
Make change v6 public static void main ( String args[] ) { . . . //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); if (quarters != 1) System.out.println( "\t" + quarters + " quarters," ); else System.out.println( "\t" + quarters + " quarter," ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels" ); } How can we fix this to say ‘1 dime’ instead? How can we fix this to say ‘1 nickel’ instead?
More food for thought . . . • What happens if someone enters a bad value for the price of an item? • 150 • 72 • -50
Make change v7 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); //calculate change int change = 100 - price; int quarters = change / 25; change = change - quarters * 25; int dimes = change / 10; change = change - dimes * 10; int nickels = change / 5; //report results (change) System.out.println( "You bought an item for " + price + " cents and gave me a dollar so your change is:" ); System.out.println( "\t" + quarters + " quarters," ); System.out.println( "\t" + dimes + " dimes, and" ); System.out.println( "\t" + nickels + " nickels" ); } How can we fix this to output an error message if a value bigger than $1 was entered?
Make change v7 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); if (price > 100) System.out.println( “Price must be less than or equal to $1.00“ ); . . . But this continues even if a message appears! As James Brown said, “Can’t go on. Can’t go on.”
Make change v7 public static void main ( String args[] ) { //get ready to read from the keyboard Scanner in = new Scanner( System.in ); //prompt for the price System.out.print( "Enter price of an item: " ); //get the price int price = in.nextInt(); if (price > 100) { System.out.println( “Price must be less than or equal to $1.00“ ); System.exit( 0 ); //end program immediately } . . . Ok. How about a price less than 0? How about a price with pennies?