Java Algorithm

Embed Size (px)

Citation preview

  • 8/19/2019 Java Algorithm

    1/3

    Java Algorithms and Pseudocodes

    Vending machine change (Savitch, p69)

    Given

    • the user enters an amount of change from 1 to 99 cents

     Figure out 

    • how the program will respond by telling the user the ONE combination of coins that equal the

    change

     Pseudocode: starting skeleton

    1. Enter a whole number from 1 to 99.2. I will output a combination of coins equal to the number entered.

     Example to help understand the problem

    • amount entered = !

    • ! cents in coins"◦ # quarters = !$

    ◦ 1 dime = 1%

    ◦ % nic&els = %

    ◦ 2 pennies = 2

    'o( the )ariables needed would be"int amount, quarters, dimes, nickels, pennies;

     Lay out the algorithm in pseudocode

    1. *ead the amount into the )ariable amount.

    2. 'et the )ariable quarters = ma+imum number of quarters possible in amount.#. *eset amount to the change left after gi)ing out that many quarters.

    ,. 'et the )ariable dimes = ma+imum number of dimes possible in amount.$. *eset amount to the change left after gi)ing out that many dimes.

    -. 'et the )ariable nickels = ma+imum number of nic&els possible in amount.

    !. *eset amount to the change left after gi)ing out that many nic&els.8. pennies = amount;

    9. Output the original amount and the numbers of each coin.

    Understanding the algorithm he algorithm changes the )alue of amount. /ut you need the original amount at the end( so that you

    can output it. 'o use one more )ariable( called originalAmount( to sa)e the original amount. 0odifyyour pseudocode.

     Algorithm pseudocode modified 

    1. *ead the amount into the )ariable amount.2. originalAmount = amount;

    #. 'et the )ariable quarters = ma+imum number of quarters possible in amount.

    ,. *eset amount to the change left after gi)ing out that many quarters.

    $. 'et the )ariable dimes = ma+imum number of dimes possible in amount.

  • 8/19/2019 Java Algorithm

    2/3

    -. *eset amount to the change left after gi)ing out that many dimes.

    !. 'et the )ariable nickels = ma+imum number of nic&els possible in amount.

    . *eset amount to the change left after gi)ing out that many nic&els.9. pennies = amount;

    1%. Output originalAmount and the numbers of each coin.

    ranslating the pseudocode into !ava program

    ine 1" *ead the amount into the )ariable amount.

    his calls for"

    •  prompting the user 

    • getting user input from &eyboard

    'o(System.out.println(“Enter a !ole num"er #rom $ to 99.%&;System.out.println(“' ill output a com"ination o# coins %&;System.out.println(“t!at equals t!e c!ange you requested.%&;

    Scanner key"oard = ne Scanner(System.in&;amount = key"oard.net'nt(&;

    ine 2 sets the )alue of originalAmount 3 it is already 4a)a code( so no need to translate.

    hus far( the main part of the program loo&s li&e this"

     public static )oid main5'tring67 args8

    :: ;eclare the )ariables firstint amount, originalAmount, quarters, dimes, nickels, pennies;

    System.out.println(“Enter a !ole num"er #rom $ to 99.%&;System.out.println(“' ill output a com"ination o# coins %&;System.out.println(“t!at equals t!e c!ange you requested.%&;

    Scanner key"oard = ne Scanner(System.in&;amount = key"oard.net'nt(&;

    originalAmount = amount;

    ine #" 'et the )ariable quarters = ma+imum number of quarters possible in amount.

    ine ," *eset amount to the change left after gi)ing out that many quarters.

    et us understand this with an e+ample"

    • for ! cents( you can ha)e a ma+imum of # quarters( because # + 2$ = !$

    • remainder amount is ! 3 !$ = 12

  • 8/19/2019 Java Algorithm

    3/3

    his tells us what operators to use" ) and *

    ! : 2$ = # 5ma+imum number of 2$s in !8

    ! < 2$ = 12 5the remainder8

     Now(

    ! is your amount

    2$ is yourquarters

    'o( replace the numbers with )ariable names"amount)2+ = quarters;amount*2+ = amount;

    /ut in the actual code( re)erse the left and righthand sides"quarters = amount)2+;amount = amount*2+;

    ines $">ou reali?e that dimes and nic&els are treated in a similar way( so"dimes = amount)$;amount = amount*$;

    nickels = amount)+;amount = amount*+;

    ine 9"pennies = amount;

    his is already 4a)a code( so no need to translate.

    ine 1%" Output originalAmount and the numbers of each coin.

    his is a simple printout statement.

     Now your code is ready.