1 / 21

Another problem for your consideration

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.

avani
Download Presentation

Another problem for your consideration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Another problem for your consideration

  2. 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

  3. Make change • Steps in our algorithm: • Get the price. • Calculate change. • Report the results.

  4. 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) } }

  5. 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.“ ); } }

  6. 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“ ); }

  7. 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

  8. 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?

  9. 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?

  10. 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?

  11. 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?

  12. 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?

  13. 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!

  14. 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" ); } }

  15. 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?

  16. 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?

  17. 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?

  18. More food for thought . . . • What happens if someone enters a bad value for the price of an item? • 150 • 72 • -50

  19. 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?

  20. 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.”

  21. 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?

More Related