Java Reference
In-Depth Information
f. (int) Math.round(x) + (int) Math.round(y)
g. s + t
h. s + n
i. 1 - (1 - (1 - (1 - (1 - n))))
j. s.substring(1, 3)
k. s.length() + t.length()
Additional review exercises are available in WileyPLUS.
173
174
PROGRAMMING EXERCISES
΢ Exercise P4.1. Enhance the CashRegister class by adding separate
methods enterDollars, enterQuarters, enterDimes,
enterNickels , and enterPennies .
Use this tester class:
public class CashRegisterTester
{
public static void main (String[] args)
{
CashRegister register = new
CashRegister();
register.recordPurchase(20.37);
register.enterDollars(20);
register.enterQuarters(2);
System.out.println("Change: " +
register.giveChange());
System.out.println("Expected: 0.13");
}
}
΢ Exercise P4.2. Enhance the CashRegister class so that it keeps track of
the total number of items in a sale. Count all recorded purchases and supply
a method
int getItemCount()
Search WWH ::




Custom Search