Two-Dimensional Arrays

In this lab, you will practice manipulating two dimensional arrays, writing JUnit tests, and submitting assignments through Web-CAT.

Instructions

  1. Create a new Eclipse project.
  2. Download and import the file Array2D.java.
  3. Create a JUnit test file named Array2DTest.java.
  4. Implement the methods in Array2D.java and provide unit tests for each. Be sure that you hand calculate the correct results -- don't assume that your method will provide the correct value.
  5. Once all of your methods are implemented and tested, submit your finished work through Web-CAT. Web-CAT will not perform any style checking, but it will run both sets of unit tests on your code: yours and the instructor's. In order to receive full credit on the assignment you must:

    • Pass all of the instructor's unit tests.
    • Pass all of your own unit tests.
    • Include tests for every method in Array2D.java.

    You can find submission instructions on this page: Help Submitting Assignments Using Web-CAT .

    Your login should be the first part of your JMU email address, and your password will be your student ID number (unless you have an existing Web-CAT account). I encourage you to pick a more convenient password.

Suggestions/Comments

  1. I recommend that you complete the methods in the order they appear in the file. Some of the later methods will be easier to implement if you include calls to earlier methods. As always, avoid reinventing the wheel.
  2. Avoid putting off testing until the end. You should test each method as it is completed. This may prevent you from repeating the same mistakes from one method to the next.
  3. It is worthwhile to think about how you will test each method before you start implementing it. As an example to get you started, consider each of the following calls to isRowValid:
    double[][] data = {{}, 
                       {1}, 
                       null, 
                       {2, 3}};
    
    System.out.println(Array2D.isRowValid(data, -1));
    System.out.println(Array2D.isRowValid(data, 0));
    System.out.println(Array2D.isRowValid(data, 1));
    System.out.println(Array2D.isRowValid(data, 2));
    System.out.println(Array2D.isRowValid(data, 3));
    System.out.println(Array2D.isRowValid(data, 4));
    
    
    What do you expect will be printed in each case?
  4. Web-CAT is configured to deduct points if there are any methods that are not executed by at least one of your JUnit tests. Unfortunately, Web-CAT includes the default constructor of the Array2D class in this calculation. In other words, you will lose points if you never instantiate an object of type Array2D, even though it doesn't make sense to instantiate an object of type Array2D. You can deal with this problem by including the following statement in one of your tests.
    Array2D t = new Array2D();
    
  5. The methods in Array2D.java use a return value of Double.MIN_VALUE to indicate an error condition. It would make more sense to throw an exception. Never fear! We will start working with exceptions next week.

This is a modified version of a lab previously used by David Bernstein, Michael Norton and Nancy Harris.