January 15

CS 117: Introduction to Computer Science
Change Maker

Assigned Wednesday January 10, 2007.
Due Monday, January 15, 2007, electronically (i.e. by hsp), by 5:00 PM.

Create a class named ChangeMaker with a main method that asks the user for a purchase price and an amount tendered, and then displays the change in dollars, quarters, dimes, nickels, and pennies. The user should enter in both numbers in cents, for example 3450 for $34.50 and 70 for $0.70. Use InputDialog and MessageDialog for input and output. Display the output in the following format:



Purchase Price: $34.50
Amount Tendered: $40.00

Your change is: $5.50

5 one-dollar bill(s)
2 quarter(s)

Thank you for your business. Come back soon.


Your answer should be the natural one, i.e. the one that gives away as little small change as possible. You may assume that both the purchase price and the amount tendered is less than $1000, and greater or equal to $0 and the amount tendered is greater than or equal to the price. Here is an example that will help you get started: Suppose the price is $2.75 and $5.00 is tendered by the customer.

     

          int price, given, change, dollarsInChange;

          price = 275;
          given = 500;
       
          change = given-price; 
          dollarsInChange = change/100;  // Why is this true??
          change = change-100*dollarsInChange;

Before stepping away from an assignment like this and moving on to other things, take a little time to think about what the assignment was for. Did the assignment increase your mastery of particular programming tools or language constructs? Did it illuminate some idea? Even a relatively dull assignment (making change is, I admit, not the most stimulating of computational tasks) is likely to have been assigned for a purpose, and you should make sure you understand what that purpose was.

Start early, have fun, and keep in touch.

Assignment from C. Thomas Wu, An Introduction to Object Oriented Programming With Java, McGraw-Hill, 2003.