Java Reference
In-Depth Information
is 11.56 , then the initial remainingAmount is 1156 . The division operator yields the inte-
ger part of the division, so 1156 / 100 is 11 . The remainder operator obtains the remainder
of the division, so 1156 % 100 is 56 .
The program extracts the maximum number of singles from the remaining amount and
obtains a new remaining amount in the variable remainingAmount (lines 16-17). It then
extracts the maximum number of quarters from remainingAmount and obtains a new
remainingAmount (lines 20-21). Continuing the same process, the program finds the maxi-
mum number of dimes, nickels, and pennies in the remaining amount.
One serious problem with this example is the possible loss of precision when casting a
double amount to an int remainingAmount . This could lead to an inaccurate result. If you
try to enter the amount 10.03 , 10.03 * 100 becomes 1002.9999999999999 . You will
find that the program displays 10 dollars and 2 pennies. To fix the problem, enter the amount
as an integer value representing cents (see Programming Exercise 2.22).
loss of precision
2.33
Show the output with the input value 1.99 .
Check
Point
2.18 Common Errors and Pitfalls
Common elementary programming errors often involve undeclared variables, unini-
tialized variables, integer overflow, unintended integer division, and round-off errors.
Key
Point
Common Error 1: Undeclared/Uninitialized Variables and Unused Variables
A variable must be declared with a type and assigned a value before using it. A common error
is not declaring a variable or initializing a variable. Consider the following code:
double interestRate = 0.05 ;
double interest = interestrate * 45 ;
This code is wrong, because interestRate is assigned a value 0.05 ; but interestrate
has not been declared and initialized. Java is case sensitive, so it considers interestRate
and interestrate to be two different variables.
If a variable is declared, but not used in the program, it might be a potential programming
error. So, you should remove the unused variable from your program. For example, in the fol-
lowing code, taxRate is never used. It should be removed from the code.
double interestRate = 0.05 ;
double taxRate = 0.05 ;
double interest = interestRate * 45 ;
System.out.println( "Interest is " + interest);
If you use an IDE such as Eclipse and NetBeans, you will receive a warning on unused
variables.
Common Error 2: Integer Overflow
Numbers are stored with a limited numbers of digits. When a variable is assigned a value that
is too large ( in size ) to be stored, it causes overflow . For example, executing the following
statement causes overflow, because the largest value that can be stored in a variable of the int
type is 2147483647 . 2147483648 will be too large for an int value.
what is overflow?
int value = 2147483647 + 1 ;
// value will actually be -2147483648
Likewise, executing the following statement causes overflow, because the smallest value that
can be stored in a variable of the int type is -2147483648 . -2147483649 is too large in size
to be stored in an int variable.
 
 
 
Search WWH ::




Custom Search