CSU Banner

CS 150, Spring 2018

Lab 11 - Practice with Loops, Arrays, and the Start of P7

Wednesday - April 4, 2018


Lab 10 Regrade: If you need a regrade for the week 10 lab, please open Lab 10 requirements and show one of the TAs your completed work within the first 5 minutes of class. The TAs will not accept regrades after this time. Additionally, if you forgot to take the attendance quiz last week for the single attendance point, your maximum possible grade for the previous lab is now 3 out of 4.

Objectives of this Lab

  1. Introduction to programming with arrays,
  2. practice writing loops combined with arrays, and
  3. start the code necessary to complete P7.

Getting Started

Create a new Java Project named Lab11, and make a class named Lab11. The code you write in today's lab will be very similar to the requirements for part one and two of P7; however, the instructions are not identical, so do not assume today's code completely implements the beginning of P7.


Concept Review Questions


Note of caution: When working with loops it is essential that you make sure to terminate your program before rerunning if you encounter a problem in the execution of your code. If your code has an infinite loop and you do not terminate before running the program again, you run the risk of slowing the performance of your computer down significantly.

Today's Assignment

    Part One
  1. Create a new Java Project in Eclipse called Lab11 and make a class called Lab11.
  2. Declare and allocate a boolean array with space to hold 10 elements.
  3. Declare and allocate two integer arrays with space to hold 10 elements.
  4. Next, declare and initialize a String array to the following Strings:
    "cat", "dog", "fish", "bird", "horse", "lizard"
  5. Last, declare and initialize two integer variables. One will be the seed value for the random number generator. Initialize this variable to Integer.parseInt(args[0]). The second will represent the random number generated by the nextInt() method call on the Random object. Initialize this variable to -1.

  6. Part Two
  7. First, write a for loop that assigns false to every element in the boolean array.
  8. Next, instantiate the Random class and set the seed of the Random class object using the following code:
      Random r = new Random();
      r.setSeed(yourSeedValueVariable); 
    NOTE: Do not forget you will need to import the Random class library at top of your Lab11.java file in order to be able to use methods of the Random class.
  9. Using the nextInt() method of the Random class, generate a random value in the range 0-5 and assign to your random number integer. The right-hand side of the assignment should read r.nextInt(6)
    NOTE: The nextInt() method of the Random class "returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence".
  10. Next, you will determine the best pet for a person by using the random number as the access point (i.e. index) of the String array. Print out the result with the format given in the example below.

  11. Testing Your Code
  12. When you are ready to test your code you will need to set up one of the options under the Run Configurations for your project.
  13. First, do a "dry" run of your project by clicking the Run button. You will receive an error message in your console, but this serves to update the Run menu to your current project.
  14. Next, go to the Run menu and select Run Configurations...
  15. In the Run Configurations pane click the (x)= Arguments tab underneath Name: Lab11.
  16. In the Program Arguments box, enter a value 0-9. Click Apply and then Run.
  17. NOTE: Do not make any other modifications to your run configurations at this time, as they may cause undesired results when your program executes.
  18. NOTE: The integer program argument you place into Program Arguments is used in your program when the following line of code executes: Integer.parseInt(args[0]). args[0] specifically refers to the zeroth position of the command line arguments and, in Eclipse, tells a program to use whatever value is listed first in the Program Arguments box. For those of you who plan to continue into CS163/165, you will use this type of setup again several times in these classes. One benefit of this setup is that we no longer need to prompt the user for a value, but instead set the value in the background, and simply tell the program where to go to find the value we want to use.

  19. Part Three
  20. Using a for loop, assign the elements of one of your integer arrays to the values 1-10.
  21. Using the nextInt() method of the Random class, generate another random value. This time the range is 0-9 and reassign your random number integer.
  22. Next, you will determine a person's favorite number by using the random number as the access point (i.e. index) of the integer array you assigned in step 17. Print out the result with the format given in the example below.
  23. Now write a for loop to iterate over the other integer array. After the for loop completes, this array will be populated with the values 1-10 but in a random order.
  24. Inside the for loop write a do-while loop. Inside this inner loop, use the nextInt() method of the Random class and generate another random value. Again the range is 0-9 and reassign your random number integer a third time.
  25. Your do-while loop must continue to iterate as long as the random number generated has already been placed in the integer array holding the values 1-10 in a random order. In this program, we are keeping track of this by using the boolean array. So, since the nextInt() method generates a value 0-9, we can also use this as the index value to check the boolean array.

    For the boolean expression of the while, this time you will not enter an expression to be evaluated, such as 5 <= 3, but instead access the boolean array at the integer value generated inside the do-while loop. Your expression should evaluate to true if the element at that location is set to true. If so, the do-while loop will iterate again and generate a new integer value.
  26. Next, while still inside the for loop, but outside of the do-while loop, use the random integer value from the do-while loop as the index location and assign the boolean array to true.
  27. Then, before exiting the for loop, use the for loop variable as the index location and assign the respective integer array element to (random value + 1).
  28. Lastly, print out the values in the random number array using the format below as guidance. Be sure to print a blank line after the completion of the for loop.

Sample Console Output (Two runs shown)

The best pet for you is a cat.
Your favorite number 1-10 is 6.
Random order of numbers 1-10: 
	2 3 9 5 1 7 8 10 6 4 
	

The best pet for you is a lizard.
Your favorite number 1-10 is 3.
Random order of numbers 1-10: 
	5 7 6 2 3 4 8 1 9 10 	



© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building