Java Reference
In-Depth Information
CashRegister register = new CashRegister();
register.recordPurchase(19.93);
register.enterPayment(20, 0, 0, 0, 0);
System.out.print("Change: ");
System.out.println(register.giveChange());
The code segment prints the total as 0.07000000000000028 . Explain
why. Give a recommendation to improve the code so that users will not be
confused.
΢ Exercise R4.7. Let n be an integer and x a floating-point number. Explain
the difference between
n = (int) x;
and
n = (int) Math.round(x);
΢΢΢Exercise R4.8. Let n be an integer and x a floating-point number.
Explain the difference between
n = (int) (x + 0.5);
and
n = (int) Math.round(x);
For what values of x do they give the same result? For what values of x
do they give different results?
΢ Exercise R4.9. Explain the differences between 2, 2.0, ȏ2Ȑ, Ȓ2ȓ, and Ȓ2.0ȓ.
΢ Exercise R4.10. Explain what each of the following two program segments
computes:
int x = 2;
int y = x + x;
and
String s = "2";
String t = s + s;
172
Search WWH ::




Custom Search