Exercise 4 11
For this project, I suggest creating an instance field to store the updated change value. So we can assign this field its original value in giveDollars() then update it accordingly afterwards. So, for example, if the total change was $2.38 in giveDollars(), we would update it to $2.38 - 2 = $0.38 so that the new change value is ready to go for giveQuarters(). Below is sample starter code for you to review:
public int giveDollars() { //Calculate the total change; we only need to do this once change = payment - purchase; int dollars = (int) change; //remove the decimal information change = change - dollars; //deduct the dollar amount from change return dollars; } public int giveQuarters() { int quarters = (int) (change / QUARTER_VALUE); change = change - quarters * QUARTER_VALUE; return quarters; }
page revision: 4, last edited: 25 Oct 2007 14:52