Instructions
Please log into cs.bridgewater.edu or your AWS VM and rename the repository directory csci102/labs/apr15 to csci102/labs/apr20.
Please place all of your code for the following assignment in csci102/labs/apr20.
Program Specifications
Write a class that models a 2-dimensional array named Matrix
according to the following specifications.
-
- The class should be generic.
- The class should contain a constructor.
- The constructor should have two integer parameters. The first parameter specifies the number of rows in the 2D array, and the second parameter specifies the number of columns in the 2D array.
- If either of arguments passed into the array are less than or equal to zero, the constructor should throw an
IllegalArgumentException
. - The constructor should create an underlying 2D array to hold the elements of the matrix.
- The class has a method named
getNumberOfRows
which returns an integer whose value is equal to the number of rows in the 2D array. - The class has a method named
getNumberOfColumns
which returns an integer whose value is equal to the number of columns in the 2D array. - The class has a method named
add
.- The add method should have 3 parameters. The first parameter is an integer which indicates a row index, the second is an integer which indicates a column index, and the third is an element of the generic type.
- If the row or column arguments are not valid indices for the 2D array, an
IllegalArgumentException
is thrown. - If the third parameter is null, a
NullPointException
is thrown. - If an exception is not thrown, the element is stored in the 2D array at the location specified by the row and column.
- The class should have a method named
get
.- The get method takes two integers as arguments indicating the row and column of an element in the 2D array that is to be returned.
- The method throws an
IllegalArgumentException
if the row or column arguments are not valid indices for the 2D array.
- The class overrides
toString
, returning a string containing all of the elements in the matrix with spaces between the elements and each row of elements separated by new line characters. - The class overrides
equals
and considers all of the elements in the matrix as significant elements. - The class overrides
hashCode
. - The class implements the
Iterable
interface.