Skip to main content

Section 4.14 Assessment: Call a Method of Random

Subgoals for Calling a Method.

  1. Classify method as static method or instance method
    1. If static, use the class name
    2. If instance, must have or create an instance
  2. Write (instance / class) dot method name and ( )
  3. Determine whether parameter(s) are appropriate
    1. Number of parameters passed must match method declaration
    2. Data types of parameters passed must be compatible with method declaration
  4. Determine what the method will return and where it will be stored
  5. Evaluate right hand side of assignment. Value is dependent on method’s purpose

Exercises Exercises

    1.

      Q1: Which of the following will correctly declare and instantiate a new variable to generate Random values?
    • Random rand = rand();
    • Random rand = rand(50);
    • Random rand = new Random();
    • Random rand = new Random(50);

    2.

      Q2: Fill in the blanks for the following code to create a Random number between 1 and 50.
      // assume you have declared and initialized variable rand to
      // be an instance of the Random class
      
      _______ result = rand._______ ( __________ ) + ___________ ;
         D                     A           B              C
      
      Fill in blank A.
    • rand
    • nextInt
    • nextDouble
    • new
    • next

    3.

      Q3: Fill in the blanks for the following code to create a Random number between 1 and 50.
      // assume you have declared and initialized variable rand to
      // be an instance of the Random class
      
      _______ result = rand._______ ( __________ ) + ___________ ;
         D                     A           B              C
      
      Fill in blank B.
    • nothing goes here
    • 1
    • 50
    • 49

    4.

      Q4: Fill in the blanks for the following code to create a Random number between 1 and 50.
      // assume you have declared and initialized variable rand to
      // be an instance of the Random class
                      
      _______ result = rand._______ ( __________ ) + ___________ ;
         D                     A           B              C
      
      Fill in blank C.
    • 0
    • 1
    • 50
    • 49

    5.

      Q5: Fill in the blanks for the following code to create a Random number between 1 and 50.
      // assume you have declared and initialized variable rand to
      // be an instance of the Random class
                      
      _______ result = rand._______ ( __________ ) + ___________ ;
         D                     A           B              C
      
      Fill in blank D.
    • int
    • double
    • Random
    • String
You have attempted of activities on this page.