JamesMadisonUniversity

Computer Science Department


CS Lab: Basic 2D Array Manipulation


Objectives:

At the end of this lab you should be able to:

  • Manipulate a column of a 2D array
  • Manipulate a row of a 2D array
  • Manipulate an entire 2D array

Background:

You are provided with a test file which consists of rows of comma separated data. You are also provided with a driver program which can be used to test your application as described below.  Your task will be to write a class that can manipulate the different dimensions of the array.  A basic stub is provided.You should also build a JUnit class to test your Array2D.java program. When you are confident it works correctly, you can submit it to WebCAT.

New Terms:

There are no new terms for this lab

Materials:

Array2DDriver.java
Array2D.java

File1.txt
File2.txt
Gaddis text - Chapter 8, Programming Challenge 12 (2D Array). (Chapter 7 in the Watermelon Book).

Prerequisites: You should have filled out the MD-Array worksheet from class on Thursday.

Acknowledgment This lab is taken from the Gaddis book, chapter 8 (7 in the Watermelon Book).

1 General Instructions:

  1. Set up your working environment.
  2. Download the Array2DDriver.java. This program will read in an array from a file with the name supplied as an argument and will perform each of the methods described in the Array2D.java program.  You should not make any changes to the driver.  
  3. Download each of the test files into your working directory.
  4. Download Array2D.java as your starter.

2 Array2D

  1. Create your fully documented stubs for each of the methods described. (Note: this has already been done for you.) 
  2. Create a test file to use in testing your methods. (NOTE: you can create attributes in the test program and refer to them throughout the tests. There is a setup option that would enable you to instantiate particular arrays that you want to test.
  3. Compile the Array2DDriver and correct any compile problems.
  4. Implement each of the methods, and test using your JUnit Test file. Be sure that you hand calculate the correct results before assuming that your result is correct. You should implement the methods using double values.
  5. You will have ragged arrays in the input. You must account for that in your methods.
  6. For any of the methods (other than the constructor), if the array is null, you should throw a NullPointerException. When you construct the exception object, include a message that "matrix is null".(See the NullPointerException constructor that accepts a String parameter.)
  7. For any of the methods that require a row or column, if that row does not exist in the matrix or the column doesn't exist for ANY row, you must throw an ArrayIndexOutOfBounds exception, sending it the message ROW: (or COL:) followed by the number of the row or column.
  8. When you are finished, run the program through the WebCAT lab that has been set up for that purpose. We will not do style checking, but will check with our evil test data. Do not submit your Array2DDriver.java program.

3 Thought Questions - Be prepared to answer these in class on Tuesday.

  1. In this application, we chose to use a System.exit(n) in main when there was not a file, when the file name was invalid, or the file could not be opened. Was this a good practice in this particular case?  Why or why not?  What are some alternatives to using a System.exit(n) in those cases.
  2. What is an alternative to accepting a null array in the constructor of our Array2D object?
  3. Why is it better to throw these exceptions than to return a value such as 0 when a row or column does not exist? 

Updated 02/03/2013 nlh)