Programming Fundamentals in Java

Module [303] - Java Multi-Dimensional Arrays

This module should be shorter than usual but it will allow you to fullfill two objectives;

  • First, it will supplement what we learned about java arrays by considering multi-dimensional arrays. The hands-on "Homework" will give you a somewhat advanced problem to work with that should help you understand the various things to keep in mind when working with higher-dimensionality arrays.
  • Second, it will give us a bit more practice with harder CodingBat exercises on 1-dimensional arrays. Students often have difficulties getting a hold on arrays so do not hesitate to use the extra time in this module to book office hours time with your instructor or TAs.
Overall, this module should be a very good opportunity to prepare for the final and take the time to seek help while doing so.

  • Textbook Reading Assignments
  • The following sections of the textbook are assigned for this module.
    • Chapter #8 – Multidimensional Arrays

There will be no wrap-up video for this module since the various programming skills we have been studying apply similarly to multi-dimensional arrays than 1-dimensional ones.

Module Learning Outcomes

In this module, you will develop further your programming skills as follows;

Tracing
Ability to read and trace Java programs leveraging 2 dimensional arrays.
Implementing
Ability to write Java programs leveraging 2 dimensional arrays.
Testing
Ability to develop tests for Java programs leveraging 2 dimensional arrays.
Debugging
Ability to troubleshoot Java programs leveraging 2 dimensional arrays.
Designing
Ability to design Java programs leveraging 2 dimensional arrays.

Let us create a new file named MyMatrix.java. It will feature the following method;

   /**
   *  Returns true if all elements of the matrix are in ascending order, false otherwise. 
   *  When determining if two consecutive elements are in order, we read them from the first row
   *  to the last row and, inside each row, from the first column to the last column.
   **/
   public static boolean isMatrixSorted(int[][] data){
      return true;   
   }

The above implementation always return true so you will have to rewrite it to do better than this.

In order to test this method, you will also write a main method in the same java file. Your main will repeatedly call isMatrixSorted on various matrices to test whether it is working or not. Here is what your main method must look like, just add more tests to it;

   public static void main(String[] args) {
      String[] msg = {  "Already sorted Matrix",
                        "Mixed up matrix" };
      boolean[] expected = {true, false};
      int[][] test0 = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} };
      int[][] test1 = { {5,3,2,1,4}, {9,8,6,7,10}, {13,12,14,15,11} };
      runTest(msg[0], isMatrixSorted(test0) == expected[0]);
      runTest(msg[1], isMatrixSorted(test1) == expected[1]);
   };

The runTest method is given to you below and must be part of your java file too;

  public static void runTest(String msg, boolean result){
    System.out.printf("Testing [%-30.30s]\t --> %s\n", msg, result?"shiny":"FAILED");
  }
  • Assignment Introduction
  • Video Link: YouTube
  • This first video goes over the above code. Watch it if you need help understanding how it works. You may skip it if you already have a good grasp on what has to be done.
  • A01 - isMatrixSorted
  • Video Link: YouTube
  • This is the apprenticeship solution of implementing the isMatrixSorted method.
  • A02 - Working for the Better Tests Bureau
  • Video Link: YouTube
  • The way we tested in A01 leaves to be desired. While we were able to store the message and the expected boolean result for test number i at index i of two separate arrays, we had no such convenience when storing the various matrices. The only thing holding us back from actually using a loop to apply all tests without tedious coding is that we do not have our matrices stored in an array. Let us refactor B01 so that we store the matrix used in test number i index i of an array of all the 2D matrices we are using for our tests. Doing so will give you experience with 3D matrices.

The following exercises are from the Array-3 group from the JavaBat website. They are still focussed on 1-dimensional arrays but the programs we write are a bit more difficult to design, requiring multiple nested loops.

  • JB1 - Generating an Array Holding a Specific Series
  • URL Link: squareUp
  • Given n>=0, create an array length n*n with the following pattern, shown here for n=3: {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups).
  • JB2 - In a Mirror, Darkly
  • URL Link: maxMirror
  • We will say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array.
  • JB3 - Generating an Array holding a specific Series
  • URL Link: seriesUp
  • Given n>=0, create an array with the pattern {1, 1, 2, 1, 2, 3, ... 1, 2, 3 .. n} (spaces added to show the grouping). Note that the length of the array will be 1 + 2 + 3 ... + n, which is known to sum to exactly n*(n + 1)/2.
  • JB4 - Maximal Span in a 1D array
  • URL Link: maxSpan
  • Consider the leftmost and righmost appearances of some value in an array. We will say that the "span" is the number of elements between the two inclusive. A single value has a span of 1. Returns the largest span found in the given array
  • B01 -Sort Students on Grades
  • This exercise is #8.3 in the 11th & 10th editions of our text. Rewrite Listing 8.2, GradeExam.java, to display the students in increasing order of the number of correct answers.
  • B02 - Matrix Addition
  • This exercise is #8.5 in the 11th & 10th editions of our text. Write a method to add two matrices. The header of the method is as follows;
    public static double[][] addMatrix(double[][] a, double[][] b)
    
    Refer to the textbook for hints and details.
  • B03 - Nine Heads and Tails
  • This exercise is #8.11 in the 11th & 10th editions of our text. Refer to the textbook for details.
  • B04 - Largest in Matrix
  • This exercise is #8.13 in the 11th & 10th editions of our text. Write the following method that returns the location of the largest element in a two-dimensional array; public static int[] locateLargest(double[][] a) The return value is a one-dimensional array that contains two elements. These two elements indicate the row and column indices of the largest element in the two-dimensional array. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. Refer to the textbook for details.
  • B05 - Tic Tac Toe
  • This exercise is #8.9 in the 11th & 10th editions of our text. Refer to the textbook for details.