CS 139 Algorithm Development
Lab11A: Array Play

Objectives

Students will be able to:

Key Terms

array
a homogeneous collection of values
array.length
an attribute of the array that is the number of elements in the array
subscript
an integer value in brackets ( [ ] )  that is used to specify a particular element of an array
initializer list
a list of values used to instantiate and initialize an array to the values of the items in the list
ArrayIndexOutOfBoundsException
an exception that is raised when a program tries to access an element using a subscript that either is negative or is equal to or larger than the number of elements in the array

Materials

You will write this program from scratch. You may download Die.class to use in Part 3. Documentation for this class is found here.Write the answers in the worksheet that is provided (Lab10B.rtf) and submit the worksheet via Blackboard.


Part 1 General Instructions: Getting Started

Create a working directory for this lab. Download a copy of Die.class to that directory.

Create and edit a new java file, ArrayPlay.java. (If you have a template feel free to use it.)  ArrayPlay will contain a single main method.  For each, you will add code to the program, not replace prior code.  When you are done, this program will provide you with a reference for working with arrays including initializing, printing, and manipulating.

Part 2 - Sequentially accessing an array

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

  1. In the main method, create a single array of integers that is 6 elements long. (Do you want to use a constant, literal, or variable for the length? Hint, look at later steps.)
  2. Initialize each of the array elements to the value, -1. (use an appropriate loop to do so).
  3. In another loop, print each element of the array on a separate line with an appropriate heading before the output.
  4. TEST IT!
  5. Add code to change the value of each element of the array to its subscript. (Use a loop). For example, myArray[3] should hold the value 3.
  6. In another loop, print each element of the array in step 5 on a separate line (with an appropriate heading before the group.)
  7. If you have not seen an ArrayIndexOutOfBoundsException, make one. Change your loop so that it reads past the end of the array. Run it. What happens?
  8. Add code to reinitialize your array to all zeros.

Part 3 - Randomly accessing an array

  1. Using the downloaded Die.class, create a new Die object. (This is an instantiation. It is like instantiating a Scanner object in your PAs.)

  2. Create a loop that will iterate at least 100 times. In the loop body:
    1. Roll the die.

    2. Take the result of the roll, map it to the location in the array, and increment that cell of the array.
      (The die values will go 1 - 6; your array subscripts are 0 - 5. Think about how you will map the die value to the array).
      The value of each array element will be a count of the number of times that roll is made. So if 1 comes up three times, array[0] should have the value 3.

    3. After the loop finishes, print the results of the rolls as they correspond to die faces. In other words, how many times was a 1 rolled?, 2? etc.

      For example, if the array holds 20, 17, 19, 15, 12, and 17 in positions 0 - 5, you would output

      1 was rolled 20 times.
      2 was rolled 17 times. ... etc.

Part 4 - Working with two arrays

  1. You will need two double arrays.  Declare these and then....
    1. Use an intializer list to intialize one of the arrays to 10 double values (of your choice).  See the book regarding initializer lists.

    2. Instantiate the other array to 10 elements, but do not initialize the contents.

    3. Print the contents of both arrays. Print them side by side on your screen. Label this output, Before.

    4. Copy the contents of the first array to the second array and again print their contents side by side. Label this output After.

    5. Finally, change the contents of one element of the first array and a different element of the second array. (assign a different number than before).

    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, try to figure out why they are not and make the appropriate corrections.

Part 5 Turning in your work 

Either show your work to the lab instructor or upload the completed code to Blackboard.

Part 6 Thought Questions - These do not need to be turned in.

  1. Is it always necessary to initialize an array? Why or why not?

  2. An array is a reference type. What will it mean when you try to pass an array to a method or a function? What if you built an initialization function? What would you pass and what would you return? Is it even necessary to return a value?

  3. Is it possible to process an array from back to front instead of from front to back (length - 1 to 0 rather than the usual 0 to length - 1)? Try it.

Part 7 More Practice - Coding Bat exercises

15. Do the Array-1 problem, commonEnd. http://codingbat.com/prob/p191991. Realize that the arrays may be of different lengths.

Part 8 OPTIONAL - An array of objects

  1. Create a String array of length 6. Use an initializer list to initialize the list to 6 random String values.

  2. Use a loop to display the length of each of the items in the array and the String value itself one line per array element. (If you have trouble, think about how you would print the data if there were 6 different variables. A subscripted variable can be used just like a regular variable).

  3. Use the "enhanced for loop" described on page 464 in Gaddis, to do step 16 (use a separate loop so you have both variants.)

  4. Finally write a small method that takes in the array that you have built in step 15 and returns the longest String in the array. (Think about how you will keep track of that and how you will return it). In the main method, write a call to that method and display the result with an appropriate label.


Updated 11/01/12 (nlh)