CS 149: Introduction to Programming
James Madison University, Spring 2018 Semester

Lab13: Processing array elements

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

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 of your choice.

  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 contents of one element of the first array and a different element of the second array.

  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.

Part 4: CodingBat Problems

For the remainder of today's lab, solve as many CodingBat problems as you can. At a minimum, you must solve three from each category. Remember to log into CodingBat first so you will receive credit.

  1. Quickly review the Warmup-2 problems that use arrays: arrayCount9, arrayFront9, array123, array667, noTriples, has271. All of them have solutions available; try to solve them first before looking at the answer.

  2. Solve at least three of the Array-1 problems. None of these problems require any loops. Pick a variety of problems; don't just do the first three.

  3. Solve at least three of the Array-2 problems. Each of these problems requires ONE loop. You may find (on CodingBat's site) the Java Arrays and Loops help document useful.

Submit your ArrayPlay.java file via Canvas by the end of the day. About half of your lab grade will be based on your CodingBat results.