CMPS 240 Summer 2019
Programming Assignment #2: Implementing a Two-dimensional Matrix via an Array

For this assignment, you are to complete the development of the Java class TwoDimMatrixViaArray, which implements the Java interface TwoDimMatrix.

As its name suggests, an instance of a class that implements the TwoDimMatrix interface is intended to represent a two-dimensional matrix. Note that the interface has a generic type parameter; the intent is that an implementing class will have a corresponding parameter having as its purpose to provide the client with the freedom to choose, when a matrix is created, the data type of the values that may occupy the cells in the matrix.

By examining the comments describing the first constructor of the class TwoDimMatrixViaArray, it should be clear that the intent —similar to that in the previous programming assignment— is to allow the client to choose the row and column index ranges. Hence, for example, a client can create a 4×3 matrix where the rows are numbered -2 through 1 and the columns are numbered 10 through 12.

From the name of the class, it should be clear that the intent is for the values logically occupying the matrix to be stored, physically, in a one-dimensional array.

Hence, the central problem for the programmer is to figure out how to map a two-dimensional matrix index (i,j) into the corresponding array index k. For the example of a two-dimensional matrix with row index range 0..M-1 and column index range 0..N-1, one suitable mapping is (i,j) ⟶ i×N + j. (This mapping corresponds to what's called row-major ordering, in which the matrix's values are stored in the array one row after another.)

Because we are allowing the row and column index ranges to begin at non-zero values, the mapping function will be a little more complicated. (Of course, this function should be implemented in the form of a private method that is invoked from both the get() and put() methods.)

Note that IndexOutOfBoundsException is defined in the package java.lang.