Java Reference

In-Depth Information

}

It prompts the player to enter a choice and repeatedly reads the choice until a valid

choice is made. One may ask if the number 7 is a magic number. If it is, should it not be

declared as a constant? The answer is that declaring the number 7 as a constant will not be

very helpful here. We cannot change the number of choices just by changing this number.

Therefore, the number of choices is not really a parameter of the program and it does not

need to be declared as a constant. You can think of this as being an exception when it is

allowable to create a magic number.

The
currencyFormatter
method is shown next. It uses the
DecimalFormat
class to

format the dollar amounts in a pretty way.

{

DecimalFormat myFormatter =
new
DecimalFormat(
"$###,###.00"
);

return
myFormatter . format (amount) ;

public static
String currencyFormatter(
double
amount)

}

Note that the two zeros after the decimal dot mean that two digits will always be

displayed after the decimal dot. If the amount is 32 dollars, for example, then

32.00 will

be displayed. Conversely, the character # is non-imposing and suggests that there can be

up to 6 digits, but missing digits will not be filled with zeros.

The
computePrice
method is shown next.

public static double
computePrice(
double
basePrice ,
double
variation)

$

{

double
result = basePrice;

if
(Math . random()
>
0.5)

{

result += Math.random()

variation ;

∗

}
else
{

result
−
= Math . random()
∗
variation ;

return
((
int
)(result
∗
100))/100.0;

}

It starts with the
basePrice
. Then a random number is used to decide if we want to

add or subtract from it. Up to the value of the variable
variation
can be added or removed

from the variable
result
. Recall that
Math.random
returns a number between 0 inclusive

and 1 exclusive. After executing the first 6 lines, the value of the variable
result
can be

10.32544, for example. However, we want the number to have only 2 digits after the decimal

dot because it must be of type currency. If we multiply the number by 100, we will move the

decimal dot two places to the right. For example, we may get the number 1032.544. Now,

we can convert the result to an integer and remove everything after the decimal dot. We

will get the number 1032. Finally, we can convert the number back to decimal by dividing it

by 100.0. We will get 10.32, which is exactly what we wanted. Note that we need to divide

by 100.0 and not by 100. If we divide by 100, we will get the result 10 because the result of

dividing two integers is always an integer. Note that the whole method can be rewritten in

a single line as follows.

public static double
computePrice(
double
basePrice ,
double
variation)

{

return
((
int
) ( ( bas ePr i ce + (Math . random ( )
>
0.5 ? 1 :
−
1)

variation

∗

)
∗
100))/100.0;

}

However, this syntax uses the conditional operator and is less intuitive. In general, avoid

writing code that is non-trivial to understand.

The
getQuantity
method is shown next. The method is very general in that it can

be used to ask for any product and any action. Creating a method that is as reusable as