Lab: Experience with Data Structures


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. A Brief Discussion of Rectangular Arrays: A rectangular array is often conceptualized as a table consisting of rows and columns. This is illustrated below for a 2x4 rectangular array:

In lecture we discussed how to create arrays of arrays in Java, and how this technique could be used to store a "rectangular array" of values. In this lab you will create a RectangularArray class that uses an "ordinary" array as the underlying data structure (i.e., the realization in computer memory of the values). Such a data structure (for the same example as above) can be illustrated as follows:

Here, the first 4 elements correspond to the first row and the second 4 elements correspond to the second row. Obviously, this can be generalized for any n x m rectangular array.

2. Version 1: This part of the lab will help you think of an array as a contiguous data structure, and how such a data structure can be used to implement other abstract data types.
  1. Edit the RectangularArray class.
  2. Implement the constructor and the columns() and rows() methods.
  3. Implement the index() method.
  4. Implement the setElementAt() method.
  5. Implement the getElementAt() method.
  6. Write a Driver class that creates a 2x4 RectangularArray, fills it with the String objects "A", "B", ..., "H" and then prints the elements in table format.
  7. Copy RectangularArray.java to RectangularArray.v1 and into the worksheet.

3. Version 2: This part of the lab will help you review the use of exceptions.
  1. Modify the index() methods so that it throws an IndexOutOfBoundsException java.lang.IndexOutOfBoundsException .
  2. Modify the setElementAt() and getElementAt() methods so that they specify (i.e., re-throw) the exception.
  3. Copy RectangularArray.java to RectangularArray.v2 and into the worksheet.

4. Type-Safe Collections: These questions will help you review the use of type-safe generics.
  1. If you have not already done so, make the RectangularArray class type-safe.

  2. If you have not already dones so, modify your Driver accordingly.

Copyright 2010