ITEC 120
Testing the Random class

Objectives

In this lab you will call and test methods fo the Random class. You will learn to generate a random number within a given range. We will use random numbers in several assignments this semester.

Assignment

The Random class generates a stream of random numbers.  The Random class provides a method named nextInt that returns the next integer in a sequence.  You will learn to use this method by developing learning tests.  You will not read any documentation. You will use black-box tests (i.e., you will not look at the method's code) to understand how to use the method.  Basically, this means you are going to figure out how this method works by experimenting with it and seeing what it does.

  1. Create a test driver, a class, named RandomTest . Your test driver has one method, the main method. 
  2. Import the Random class from the java.util package (the same package as the Scanner).
  3. Create a variable named bound with the value 3. In step 5, you will pass this variable to the nextInt method of the Random class.
  4. Create an instance of the Random class – create a variable named random and assign the variable to a new instance of Random just like you create a new instance of the Scanner. 
  5. Use the variable random to call the nextInt method of the Random class and print the result of each call.  The nextInt method requires one integer parameter.  Pass bound to the method.  Each call to nextInt is identical.  Write and print one call.  When the call is working, copy and paste the call until you have ten calls. 
  6. Compile and execute your test driver. Your test driver should print ten numbers. Look at these numbers. What range are they in? Did your program generate any 3s? Did it generate any 0s? How about 1s or 2s? Run the program again. Are the results different this time? Do these numbers seem random to you?
  7. Now change your program to generate 20 numbers between 0 and 4, including 0 and 4.
    Compile, run, and look at the results. Does it seem to be working correctly?
  8. Now change your program to generate 20 numbers between 1 and 5, inclusive.
    Compile, run, examine the output.
  9. Now change your program to ask the user to enter the minimum and maximum values of the range of numbers, and generate 20 numbers in the range from minimum to maximum, inclusive.

    We could express this range of numbers in interval notation: [minimum, maximum]
    The square brackets mean the range includes the numbers minimum and maximum.
    (A parenthesis means not including.)

    A sample run of the program would look something like this (user input in red):


     Please enter a minimum number:
5
     Please enter a maximum number:
8

     Generating 20 numbers in the range [5,8]
     5
     7
     8
     8
     6
     5
     5
     6
     5
     7
     6
     8
     7
     7
     6
     7
     5
     8
     7
     8


 

Submit Your Assignment

Your grade for this assignment will be based on your completion of two things: