Algorithms and OOD (CSC 207 2014F) : Labs

Laboratory: Arrays


Summary: In this laboratory, you will begin working with arrays in Java. The particular focus of this lab is working with arrays of numbers to describe values.

Preparation

Create a new package for this laboratory named taojava.labs.arrays.

Exercises

Exercise 1: Printing Arrays

Consider the following main class.

/**
 * A short illustration of printing arrays.
 */

import java.io.PrintWriter;
import java.util.Arrays;

public class ArrayPrintingExperiment
{
  // +------+--------------------------------------------------------------
  // | Main |
  // +------+

  public static void main (String[] args)
    throws Exception
  {
    PrintWriter pen = new PrintWriter (System.out, true);
    int[] littlevals = new int[] { 1, 2, 3 };
    Integer[] bigvals = new Integer[] { new Integer (4),
                                        new Integer (5),
                                        new Integer (6) };
    String[] names = { "Sam", "Will", "Jon", "Dan" };

    // Print out our arrays.
    pen.println("Using Arrays.toString(array)");
    pen.println("bigvals: " + Arrays.toString(bigvals));
    pen.println("littlevals: " + Arrays.toString(littlevals));
    pen.println("names: " + Arrays.toString(names));
    pen.println();
    pen.println("Using array.toString()");
    pen.println("bigvals: " + bigvals.toString());
    pen.println("littlevals: " + littlevals.toString());
    pen.println("names: " + names.toString());

    pen.close();
  } // main (String[])
} // class ArrayPrintingExperiment

a. What do you expect the output of this program to be?

b. Confirm your prediction experimentally.

c. Why do you think the designers of Java made the choices that they did?

Exercise 2: Printing Arrays, Revisited

a. Create a utility class, Utils. In case you've forgotten, a “utility” class is a class that provides static methods for use elsewhere, but which does not necessarily contain a main method.

b. To Utils, add a static method, printValues(PrintWriter pen, int[] values), that prints the values in values, surrounded by curly braces and with elements separated by commas. (Yes, that's more or less how you would write them in Java.) (And no, that's not how Arrays.toString prints them.)

c. Create a main class, Exercise1, which creates a few arrays and prints them out. Note that a “main” class is one that has a main method.

In case you've forgotten, if we've imported java.io.PrintWriter, we can create a new PrintWriter with

    PrintWriter pen = new PrintWriter(System.out, true);

d. To Utils, add a static method, printValues (PrintWriter pen, String[] values), that prints the values in values, surrounded by curly braces and with elements separated by commas. (Yes, that's more or less how you would write them in Java.)

Yes, I know that principles of refactoring and polymorphism suggest that you should not to write two nearly identical methods. Unfortunately, this is one of those times that you have to duplicate code because you don't have enough background to do something else. (Actually, the design of arrays in Java is enough of a pain in the neck that most programmers end up duplicating code for array utilities.)

e. Update your main class to print a few arrays of strings.

Exercise 3: Atypical Input

Create a main class, ArraySizeExperiments, in which you you determine experimentally what happens in each of the following situations.

a. You try to create an array of size 0.

b. You try to create an array of negative size.

c. You try to create an astoundingly large array, such as one of size Integer.MAX_VALUE.

d. You try to use an index less than 0.

e. You try to use an index equal to the length (size) of the array.

f. You try to use an index greater than the length (size) of the array.

Exercise 4: Default Elements

Create a main class, ArrayDefaultExperiments, in which you determine experimentally what the default values are in each of the following kinds of arrays.

a. An array of integers that is constructed but not initialized.

b. An array of boolean values that is constructed but not initialized.

c. An array of strings that is constructed but not initialized.

d. An array of BigIntegers that is constructed but not initialized.

For the last two, you may see values that only make partial sense. Feel free to ask for an explanation.

Exercise 5: Finding the Largest Value

Add a static method, int largest(int[] values), to Utils. As you might expect, this method should determine the largest value in the array.

The strategy for determining the largest value is fairly straightforward:

Assume the value in index 0 is the largest
For each remaining index
  If the value at that index is larger
    update your assumption 

Write a simple set of unit tests for this new method.

Exercise 6: Array Lists

Read the documentation for Java's ArrayList class, available at http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html. Then answer the following questions (either by reading the documentation or by trying experimentally).

a. How do you create a new ArrayList?

b. When can you add an element at position i?

c. If you have not yet set the element at position i, what happens if you try to get the element at position i? (In arrays, you can get any element if you've made the array a particular size.)

d. The documentation says that ArrayLists provide a “[r]esizable-array implementation of the List interface”. How do we get the array to resize?

e. List two other interesting things you learned by reading the documentation or by experimenting.

Exercise 7: Vectors

Redo the previous exercise using java.util.Vector rather than java.util.ArrayList.

Exercise 8: ArrayLists vs. Vectors

a. Give two reasons a programmer might choose to use an ArrayList rather than a Vector.

b. Give two reasons a programmer might choose to use a Vector rather than an ArrayList.

c. Give two reasons a programmer might prefer an array rather than a Vector or an ArrayList.