Java Reference
In-Depth Information
Type Math.random inside the main method (i.e., inside the braces that surround the
body of the method). You will see a description of the method. It returns a double between
0 and 1, where the method can return 0 but it will never return 1. If we multiply this
number by 10, then we will get a number between 0 (inclusive) and 10 (exclusive). If we
cast this number to an integer, then we will get an integer between 0 and 9, which is exactly
what we want. Here is the implementation of Step 1 of the algorithm.
int x=( int )
(Math . random ( ) 10) ;
int y=( int )
(Math . random ( ) 10) ;
Note that the class Math provides other useful functions. For example Math.abs(-3) will
return the absolute value of a number. Alternatively, Math.sqrt(2.3) returns the square
root of a number. Note that the input and the output of the function is of type double .
The Math.max and Math.min functions are also useful for finding the max and min of two
numbers.
Note that the first line performs two tasks. It declares the variable x to be an integer
and it sets the value of x . Note that a variable cannot be used before its value is initialized.
Therefore, it is common to declare a variable and set its initial value in the same statement.
The expression (int) converts (or casts ) the double to an integer. Note that the casting
operation always rounds down the number. For example, if the parentheses are missing,
then the cast will apply to the result of Math.random() . Since this number is always greater
than or equal to 0 and smaller than 1, casting it to an integer will return 0. In other words,
omitting the parentheses will make x equal to 0.
Step 2 asks the user to compute x*y . It can be coded as follows.
System. out . print (x+ "*" +y+ "=" );
Note that the parameter to the println method contains several expressions connected
with “+”. The operator “+” is used to concatenate two strings. Of course, one of the
parameters must be a string. If the other parameter is not a string, then it is automatically
converted to a string. For example, if x =5and y = 6, then the above statement will print
“5*6= ”.
Step 3 reads an integer from the keyboard. It can be implemented as follows.
int z;
Scanner keyboard = new Scanner(System. in) ;
z = keyboard . nextInt () ;
The last step needs to check if the user entered the correct number. Here is the imple-
mentation.
if (z == x
{
System. out . println ( "Congratulations!" );
y)
}
else {
System. out . println ( "You need more practice" );
}
The implementation uses a new if-else structure; see Figure 2.6. An opening and
closing parentheses always follow an if statement. If the condition inside the parentheses
evaluates to true, then the first block is executed (a block is defined by opening and closing
braces). As the figure suggests, after the first block is executed the flow of control jumps
to the line after the end of the else block. Conversely, if the condition in the parentheses
is false, then the second block (i.e., the block after the else statement) is executed. The
== operator is used to compare two numbers. It returns true when the numbers are the
same and false otherwise. Java provides additional comparison operators; see Table 2.2.
The == and the != operators may be new to the reader.
 
Search WWH ::




Custom Search