Java Reference
In-Depth Information
Run Java Application command and set the “Prompt for parameters” option for it. 3
After you set this preference, when you run a program, a window will appear in which
you can enter options for the java run command—for example, -enableassertions .)
Self-Test Exercises
40. Fix the bug in the code in the earlier subsection “Tracing Variables.”
41. Add some suitable output statements to the following code so that all variables
are traced:
int n, sum = 0;
for (n = 1; n < 10; n++)
sum = sum + n;
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
42. What is the bug in the following code? What do you call this kind of loop bug?
int n, sum = 0;
for (n = 1; n < 10; n++)
sum = sum + n;
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
43.
Write an assertion check that checks to see that the value of the variable time is
less than or equal to the value of the variable limit . Both variables are of type int .
3.5
Random Number Generation
The generation of random numbers is too important to be left to chance.
ROBERT COVEYOU
Games and simulation programs often require the computer to generate random
numbers. For example, a card game might need a way to randomly shuffle the cards in
the deck or to roll a pair of dice. In this section, we briefly discuss two ways to generate
random numbers in Java. Although we generally use the term random , Java really
generates pseudorandom numbers. That is, Java can generate a sequence of numbers
that looks random but this sequence of numbers is initialized by a “seed” value. If the
same seed value is used to initialize the random number generator, then the exact same
sequence of numbers will be generated.
random
numbers
pseudorandom
3 If you are running applets, you also need to select the “Prompt for parameters” option for the Run
Java Applet command on the Tools submenu.
 
 
Search WWH ::




Custom Search