Lab13: Processing array elements

Dice

Background

You will write today's program from scratch. Create a new Java class named ArrayPlay with a main method. For each step below, you should add code to the program without replacing prior code. When you are finished, this program will provide you with a reference for working with arrays including initializing, printing, and comparing.

Objectives

  • Use an array to store a sequence of values.
  • Display the contents of an array with a loop.
  • Practice manipulating various types of arrays.

Key Terms

initializer list

list of values used to instantiate and initialize an array in one step

array.length

read-only attribute of an array that stores the number of elements

subscript

integer value in brackets [ ] that specifies an element of an array

ArrayIndexOutOfBoundsException

error raised when trying to access A[i] where i < 0 or i >= A.length

Part 1: Sequentially accessing an array

Whenever you are asked to do something to your array and then print it, you should finish the update step first and then have a separate loop to do the printing.

  1. In the main method, create a single array of integers that is 6 elements long.
  2. Initialize each of the array elements to the value -1 using a loop of your choice.
  3. In another loop, print each element of the array on a separate line as shown:
        array[0] = -1
        array[1] = -1
        ...
    
  4. Add code to change the value of each element of the array to its subscript. For example, array[3] should hold the value 3.
  5. Print the new contents of the array. You may copy and paste the code from step 3. (Your program should now print both versions of the array.)
  6. If you have not seen an ArrayIndexOutOfBoundsException, make one by causing a loop to read past the end of the array. What happens?
  7. Fix the code that causes the ArrayIndexOutOfBoundsException. Then add code to re-initialize your array contents to all zeros.

Part 2: Randomly accessing an array

Download Die.class for use in Part 3. Make sure it's in the same directory as your ArrayPlay.java file.

  1. Create a new Die object. (Refer to Die.html for documentation.)
  2. Create a loop that will iterate at least 100 times. In the loop body:
    1. Roll the die. (Don't create a new object, just roll it again.)
    2. Based on the result of the roll, increment the corresponding cell of your array.
      The value of each array element should be the number of times that roll is made.
  3. After the loop finishes, print the results of the simulation. For example, if the array holds {20, 17, 19, 15, 12, 17} you would output:
    1 was rolled 20 times.
    2 was rolled 17 times.
    ...
    

Part 3: Working with multiple arrays

For this exercise, you will need two arrays of double values.

  1. Use an initializer list { } to assign one of the arrays to 10 double values 0.0 to 9.0.
  2. Instantiate the other array to store 10 elements, but do not initialize the contents yet.
  3. Print the contents of both arrays side by side on your screen. Label this output Before.
  4. Copy the contents of the first array into the second, then print their contents again. Label this output After.
  5. Finally, change the index 0 element of the first array to 99.0 and the index 5 element of the second array to 99.0.
  6. Again, print the contents of the two arrays side by side. Label this output After Change.
  7. Check that the contents of the two arrays are indeed different. If not, make the appropriate corrections.

Here is the expected output file for lab13.exp

Submit your final ArrayPlay.java file to Autolab.

Back to Top