Another problem for your consideration

21
Another problem Another problem for your for your consideration consideration

description

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. - PowerPoint PPT Presentation

Transcript of Another problem for your consideration

Page 1: Another problem for your consideration

Another problem for your Another problem for your considerationconsideration

Page 2: Another problem for your consideration

Make changeMake change

Write a program that determines the change to be Write a program that determines the change to be dispensed from a vending machine.dispensed from a vending machine. An item in the machine can cost between 25 An item in the machine can cost between 25

cents and a dollar, in 5-cent increments.cents and a dollar, in 5-cent increments. The machine accepts only a single dollar bill to The machine accepts only a single dollar bill to

pay for the item.pay for the item.

For example, a possible sample dialog might be:For example, a possible sample dialog might be:

Enter price of an item: 45Enter price of an item: 45You bought an item for 45 cents and gave me a You bought an item for 45 cents and gave me a

dollar so your change is:dollar so your change is:2 quarters,2 quarters,0 dimes, and0 dimes, and1 nickel1 nickel

Page 3: Another problem for your consideration

Make changeMake change

Steps in our algorithm:Steps in our algorithm:1.1. Get the price.Get the price.

2.2. Calculate change.Calculate change.

3.3. Report the results.Report the results.

Page 4: Another problem for your consideration

/*/*

File : Change.javaFile : Change.java

Author: G.J. GreveraAuthor: G.J. Grevera

Desc. : Purchase an item and make change.Desc. : Purchase an item and make change.

*/*/

public class Change {public class Change {

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get the price//get the price

//calculate change//calculate change

//report results (change)//report results (change)

}}

}}

Make change v1Make change v1

Page 5: Another problem for your consideration

/*/*

File : Change.javaFile : Change.java

Author: G.J. GreveraAuthor: G.J. Grevera

Desc. : Purchase an item and make change.Desc. : Purchase an item and make change.

*/*/

public class Change {public class Change {

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get the price//get the price

int price = 35;int price = 35;

//calculate change//calculate change

int quarters = 0;int quarters = 0;

int dimes = 0;int dimes = 0;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "Bye.“ );System.out.println( "Bye.“ );

}}

}}

Make change v2Make change v2

Page 6: Another problem for your consideration

public static void main ( String args[] ) {public static void main ( String args[] ) {

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = 35;int price = 35;

//calculate change//calculate change

int quarters = 0;int quarters = 0;

int dimes = 0;int dimes = 0;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

Make change v3Make change v3

Page 7: Another problem for your consideration

public static void main ( String args[] ) {public static void main ( String args[] ) {

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = 35;int price = 35;

//calculate change//calculate change

int quarters = 0;int quarters = 0;

int dimes = 0;int dimes = 0;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

Make change v3Make change v3

Enter price of an item: You bought an item for 35 cents and gave me a dollar soyour change is: 0 quarters, 0 dimes, and 0 nickels

Page 8: Another problem for your consideration

import java.util.Scanner;import java.util.Scanner;

public class Change {public class Change {

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = int price = in.nextInt()in.nextInt();;

//calculate change//calculate change

int quarters = 0;int quarters = 0;

int dimes = 0;int dimes = 0;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

}}

Make change v4Make change v4

Next, how do we calculate our change?

Page 9: Another problem for your consideration

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = 0;int quarters = 0;

int dimes = 0;int dimes = 0;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

Make change v5Make change v5

How do we calculate the number of quarters in our change?

Page 10: Another problem for your consideration

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = change / 25;int quarters = change / 25;

int dimes = 0;int dimes = 0;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

Make change v5Make change v5

Ok.How do we calculate the number of dimes in our change?

Page 11: Another problem for your consideration

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = change / 25;int quarters = change / 25;

change = change – quarters * 25;change = change – quarters * 25;

int dimes = 0;int dimes = 0;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

Make change v5Make change v5

Ok.How do we calculate the number of dimes in our change?

Page 12: Another problem for your consideration

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = change / 25;int quarters = change / 25;

change = change – quarters * 25;change = change – quarters * 25;

int dimes = change / 10;int dimes = change / 10;

int nickels = 0;int nickels = 0;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

Make change v5Make change v5

Ok.How do we calculate the number of nickels in our change?

Page 13: Another problem for your consideration

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = change / 25;int quarters = change / 25;

change = change – quarters * 25;change = change – quarters * 25;

int dimes = change / 10;int dimes = change / 10;

change = change – dimes * 10;change = change – dimes * 10;

int nickels = change / 5;int nickels = change / 5;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters,“ );System.out.println( "\t" + quarters + " quarters,“ );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels“ );System.out.println( "\t" + nickels + " nickels“ );

}}

Make change v5Make change v5

Done!

Page 14: Another problem for your consideration

Make change v5 (final)Make change v5 (final)

/*/*

File : Change.javaFile : Change.java

Author: G.J. GreveraAuthor: G.J. Grevera

Desc. : Purchase an item and make change.Desc. : Purchase an item and make change.

*/*/

import java.util.Scanner;import java.util.Scanner;

public class Change {public class Change {

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = change / 25;int quarters = change / 25;

change = change - quarters * 25;change = change - quarters * 25;

int dimes = change / 10;int dimes = change / 10;

change = change - dimes * 10;change = change - dimes * 10;

int nickels = change / 5;int nickels = change / 5;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters," );System.out.println( "\t" + quarters + " quarters," );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels" );System.out.println( "\t" + nickels + " nickels" );

}}

}}

Page 15: Another problem for your consideration

Food for thought . . .Food for thought . . .

What message appears if there is What message appears if there is exactly 1 in the change?exactly 1 in the change? 1 quarters1 quarters 1 dimes1 dimes 1 nickels1 nickels

How can we fix this to say ‘1 quarter’ How can we fix this to say ‘1 quarter’ instead?instead?

Page 16: Another problem for your consideration

Make change v6Make change v6

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = change / 25;int quarters = change / 25;

change = change - quarters * 25;change = change - quarters * 25;

int dimes = change / 10;int dimes = change / 10;

change = change - dimes * 10;change = change - dimes * 10;

int nickels = change / 5;int nickels = change / 5;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters," );System.out.println( "\t" + quarters + " quarters," );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels" );System.out.println( "\t" + nickels + " nickels" );

}}

How can we fix this to say ‘1 quarter’ instead?

Page 17: Another problem for your consideration

Make change v6Make change v6 public static void main ( String args[] ) {public static void main ( String args[] ) {

. . .. . .

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change " cents and gave me a dollar so your change is:" );is:" );

if (quarters != 1)if (quarters != 1)

System.out.println( "\t" + quarters + " quarters," );System.out.println( "\t" + quarters + " quarters," );

elseelse

System.out.println( "\t" + quarters + " quarter," );System.out.println( "\t" + quarters + " quarter," );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels" );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?

Page 18: Another problem for your consideration

More food for thought . . .More food for thought . . .

What happens if someone enters a What happens if someone enters a bad value for the price of an item?bad value for the price of an item? 150150 7272 -50-50

Page 19: Another problem for your consideration

Make change v7Make change v7

public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

//calculate change//calculate change

int change = 100 - price;int change = 100 - price;

int quarters = change / 25;int quarters = change / 25;

change = change - quarters * 25;change = change - quarters * 25;

int dimes = change / 10;int dimes = change / 10;

change = change - dimes * 10;change = change - dimes * 10;

int nickels = change / 5;int nickels = change / 5;

//report results (change)//report results (change)

System.out.println( "You bought an item for " + price +System.out.println( "You bought an item for " + price +

" cents and gave me a dollar so your change is:" );" cents and gave me a dollar so your change is:" );

System.out.println( "\t" + quarters + " quarters," );System.out.println( "\t" + quarters + " quarters," );

System.out.println( "\t" + dimes + " dimes, and" );System.out.println( "\t" + dimes + " dimes, and" );

System.out.println( "\t" + nickels + " nickels" );System.out.println( "\t" + nickels + " nickels" );

}}

How can we fix this to output an error message if a value bigger than $1 was entered?

Page 20: Another problem for your consideration

Make change v7Make change v7 public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

if (price > 100)if (price > 100)

System.out.println( “Price must be less than or equal to System.out.println( “Price must be less than or equal to $1.00“ );$1.00“ );

. . .. . .

But this continues even if a message appears!

As James Brown said, “Can’t go on. Can’t go on.”

Page 21: Another problem for your consideration

Make change v7Make change v7 public static void main ( String args[] ) {public static void main ( String args[] ) {

//get ready to read from the keyboard//get ready to read from the keyboard

Scanner in = new Scanner( System.in );Scanner in = new Scanner( System.in );

//prompt for the price//prompt for the price

System.out.print( "Enter price of an item: " );System.out.print( "Enter price of an item: " );

//get the price//get the price

int price = in.nextInt();int price = in.nextInt();

if (price > 100) {if (price > 100) {

System.out.println( “Price must be less than or equal to System.out.println( “Price must be less than or equal to $1.00“ );$1.00“ );

System.exit( 0 ); //end program immediatelySystem.exit( 0 ); //end program immediately

}}

. . .. . . Ok.

How about a price less than 0?

How about a price with pennies?