110 likes | 177 Views
Welcome to Flight #600 – 58t. Changes (Prog600 – 58t). AP Computer Science 2010 By: Chia-Hsuan Wu. So, here is your mission.
E N D
Welcome to Flight #600 – 58t Changes (Prog600 – 58t) AP Computer Science 2010 By: Chia-Hsuan Wu
So, here is your mission • Write a class that will direct a cashier on how much change to give customer. The program should have two inputs: The amount of the purchase and the amount receivedfrom the customer and a call to the calculate method. The class methods will determine and output the number of dollars, quarters, dimes, nickels and pennies need to make the correct change for the transaction. The class will also print out a table of each type of coin/currency to return to the customer. Be sure to use lookup tables for the values of your coins and the monetary denominations.
Getting Ready… • Start by importing these two: • - import java.io.*; • - import java.util.Scanner; • Then the usual: • public class Prog60058t • { public static void main (String [] args) • { } • } • create a scanner so user can input amount. Scanner i = new Scanner (System.in);
Contacting Tower… • Print a line of text “Enter purchase price “ then create a value (double) for the user to enter System.out.print (“Enter purchase price “); double x = i.nextDouble (); • Repeat the action above and print “Enter amount given “ and assign a value to it. • Then from print out (different lines): • “++++++++++++++++” • “Correct change “ • “++++++++++++++++”
Taking Off… • Then create another value that calculate the difference of y and x (price and given). double TT = y – x; • Convert it into int. int EST = (int)TT; • EST is the amount of ONE DOLLAR BILL you’ll be giving. So print out the amount of ones System.out.println (“Ones = “ + EST); • Then create another value called “Left” to calculate the decimal value of TT double Left = TT – EST; //Decimal • Then create another value DL that will times Left to 100. (so we don’t have to deal with ugly decimal numbers!) double DL = (Left*100);
In The Air…Holy cow! You forgot the flight instructions. (gotta figure it out!) • Create If statement to determine the number of each coin. • First of all, check if DL is >= to 25. (if DL is >= 25 that means you will need to hand out quarters.) • YOU MUST CHECK ALL POSSIBILITIES
Hint 1: • I check if DL >= 25 • If yes then check if DL >= 77 and 50 • If no then check if DL >= 10 • If yes then divide DL by 10 and make it in int. And that’s the number of dimes • If no then same thing with nickels and pennies.
Hint 2: Check if DL is >= 25 No YES Check if DL is >=10 Print quarter = 1. Then DL – 25 Check if DL >= 50 Yes No No YES I’m sure you can figure out the rest Solution on the next page
Problem Solved. Ready to land. Solution: • Great job! That sure took a while. • Alright here is my solution: http://sites.google.com/site/prog60058t/ • Remember : it is not the only solution.
Congratulations!Successful Landed! • Final part of this program is to print out the total amount • So… System.out.println (“Total Amount = “ + TT); • Don’t forget to try the program before you finished • It should look like this: • Enter purchase price 10.01 • Enter amount given 20.00 • ++++++++++++++ • Correct Change • ++++++++++++++ • Ones = 9 • Quarters = 3 • Dimes = 2 • Nickels = 0 • Pennies = 4.0 • Total Amount = 9.99