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.

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).
Acknowledgment This lab is taken from the Gaddis book, chapter 8

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.)  Since this class is not storing any data (attributes) you may make each of the methods static.  (We will discuss static methods in more detail later.)
  2. Compile the Array2DDriver and correct any compile problems.
  3. Implement each of the methods, and test using the test data provided or make your own 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.
  4. You will have ragged arrays in the input. You must account for that in your methods.
  5. For any of the methods, if the array is null, you should throw a NullPointerException.
  6. 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.
  7. When you are finished and your program fully tested, demo for the instructor or upload it to the Blackboard assignment.

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

  1. We implemented this application with two classes containing all static methods.  What would be different about Array2D if we had stored the matrix as an attribute (field) of the Array2D class?
  2. 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.

 

 

Updated 04/11/2011 nlh)