Java Reference
In-Depth Information
Every time we call a method, we need to check if the method returns data. If it does,
we need to save the result somewhere. For example, the following code will compile.
callBob(8 , 12) ;
However, we never save the result of calling the method. Bob would paint the room, which
is good. However, he will send us the bill and we are not saving the amount anywhere. As
a result, we will not know how much to pay Bob.
When calling a method, we always need to check the return type. If the return
type is not void , then we need to save the result that is returned by the method in a
variable for future reference.
384.00000000000006, not bad for a
small project. Note that if you do the math by hand, the result is exactly 384 dollars and
0 cents. Java prints the 6 as the fourteenth digit after the decimal dot because computers
are not very good at working with real numbers. Java can only store a finite number of
real numbers (i.e., 2 64 )usinga double (i.e., 8 bytes). If the real number cannot be exactly
represented, then the closest real number is chosen.
Type and execute the program. You should get:
$
4.2 Formatting Output
It will be nicer if we get the program to print
384.00000000000006.
When it comes to currency, we probably want to see only two digits after the decimal
dot. Java has the printf method (stands for formatted printing) that supports formatted
output. Accidentally, there is a format method that can be called on a System.out object
and that does exactly the same thing. We can print a number with two digits after the
decimal dot as follows.
System. out . printf ( "
384.00 instead of
$
$
$%.2f on painting and"
+ "
$%.2f," ,
$%.2f on hardwood for total of
bobPrice ,peterPrice , totalPrice);
The printf method is interesting because it takes as input a variable number of argu-
ments. The first argument is always a string. This is the string that is displayed. The string
can contain zero or more % constructs. An argument needs to be specified for every such
construct. In our example, the first string contained three % constructs. This means that
we need to specify three additional parameters. Every time a % construct is seen in the
string, it is substituted with the next argument of the method. In our case, bobPrice is
equal to
384.00. The
f after the % sign means that a floating point number will be printed next (either a float
or a double ). A list of some possible characters that can be used after the % sign is shown
in Table 4.1.
Between the % sign and the character, one can specify the size of the output using two
numbers separated by a dot. To the right of the dot, one can specify the number of digits
after the decimal dot to be displayed. To the left of the dot, one can specify the total number
of characters to be displayed. For example, the following statement displays 3 digits after
the decimal dot.
System. out . printf ( "%.3f" ,23.3228) ;
76.80, peterPrice is equal to
307.20, and totalPrice is equal to
$
$
$
 
Search WWH ::




Custom Search