Question
For this assignment, you will implement the provided matrix interface (see Matrix.java). This interface le de nes not only which methods you must implement, but
For this assignment, you will implement the provided matrix interface (see Matrix.java). This interface le de nes not only which methods you must implement, but gives documentation that describes how they should work. Already provided for you is a base le to modify (ser222_01_02_hw02_base.java). The base contains an currently empty matrix class as well as some simple testing code. In order for it to be a reliable type, we will implement it as an immutable type. Creating this ADT will involve creating 9 methods:
MyMatrix(int[][] matrix) - a constructor. public int getElement(int y, int x) - see interface. public int getRows() - see interface. public int getColumns() - see interface. public MyMatrix scale(int scalar) - see interface. public MyMatrix plus(Matrix other) - see interface. public MyMatrix minus(Matrix other) - see interface.
public MyMatrix multiply(Matrix other) - see interface.
boolean equals(Object other) - see interface.
String toString() - see interface.
Be aware that some of the methods throw exceptions - this should be implemented.
Testing
Whenever you build a piece of software, you want to have some level of certitude that that piece of software does what you expect. This means testing! For this initial homework, you are provided with a set of simple tests to check if your program is functioning correctly. In the future, you may need to create your own tests. The following code is included in base le:
int[][] data1=newint[0][0]; int [][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int [][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
int [][] data4 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
Matrix m1 = new ser222_01_02_hw02_base(data1);
Matrix m2 = new ser222_01_02_hw02_base(data2);
Matrix m3 = new ser222_01_02_hw02_base(data3);
Matrix m4 = new ser222_01_02_hw02_base(data4);
System.out.println("m1 > Rows: " + m1.getRows() + " Cols: " + m1.getColumns());
System.out.println("m2 > Rows: " + m2.getRows() + " Cols: " + m2.getColumns());
System.out.println("m3 > Rows: " + m3.getRows() + " Cols: " + m3.getColumns());
//check for reference issues
System.out.println("m2 > " + m2);
data2 [1][1] = 101;
System.out.println("m2 > " + m2);
//test equals System.out.println("m2==null: " + m2.equals(null)); //false System . out . println ("m3==\"MATRIX\": " + m2. equals ("MATRIX")); //false
System.out.println("m2==m1: " + m2.equals(m1)); //false
System.out.println("m2==m2: " + m2.equals(m2)); //true
System.out.println("m2==m3: " + m2.equals(m3)); //false
System.out.println("m3==m4: " + m3.equals(m4)); //true
//test operations (valid) System.out.println("2 m2: " + m2.scale(2));
System.out.println("m2 + m3: " + m2.plus(m3));
System.out. println("m2 m3: " + m2.minus(m3));
//test operations (invalid)
//System.out.println("m1 + m2" + m1.plus(m2));
//System.out.println("m1 m2" + m1.minus(m2));
The results from running this code should be:
m1>Rows: 0 Cols: 0
m2>Rows: 3 Cols: 3
m3>Rows: 3 Cols: 3
m2 >
1 2 3
4 5 6
7 8 9
m2 >
1 2 3
4 5 6
7 8 9
m2==null: false
m3=="MATRIX": false
m2==m1: false
m2==m2 : t r u e
m2==m3: false
m3==m4 : true
2 m2:
2 4 6 8 10 12
14 16 18
m2 + m3:
2 6 10 6 10 14
10 14 18
m2 m3:
0 -2 -4
2 0 -2
4 2 0
Matrix.java:
/**
* A simple matrix ADT.
*/
public interface Matrix {
/**
* Returns the element at particular point in the matrix.
* @param y y position
* @param x x position
* @return element
*/
public int getElement(int y, int x);
/**
* Returns the number of rows in the matrix.
* @return rows
*/
public int getRows();
/**
* Returns the number of columns in the matrix.
* @return columns
*/
public int getColumns();
/**
* Returns this matrix scaled by a factor. That is, computes kA where k is a
* constant and A is a matrix (this object).
*
* @param scalar scalar
* @return matrix
*/
public Matrix scale(int scalar);
/**
* Returns this matrix added with another matrix. That is, computes A+B
* where A and B are matrices (this object, and another respectively).
* @param other addend
* @return matrix
* @throws RuntimeException if matrices do not have matching dimensions.
*/
public Matrix plus(Matrix other);
/**
* Returns this matrix subtracted by another matrix. That is, computes A-B
* where A and B are matrices (this object, and another respectively).
* @param other subtrahend
* @return matrix
* @throws RuntimeException if matrices do not have matching dimensions.
*/
public Matrix minus(Matrix other);
/**
* Returns this matrix multiplied by another matrix. That is, computes AB
* where A and B are matrices (this object, and another respectively).
* @param other multiplicand
* @return matrix
* @throws RuntimeException if matrices do not have matching dimensions.
*/
public Matrix multiply(Matrix other);
/**
* Returns true if this matrix matches another matrix.
* @param other another matrix
* @return equality
*/
@Override
public boolean equals(Object other);
/**
* Returns a string representation of this matrix. A new line character will
* separate each row, while a space will separate each column.
* @return string representation
*/
@Override
public String toString();
}
ser222_01_02_hw02_base.java:
/**
* An implementation of the Matrix ADT. Provides four basic operations over an
* immutable type.
*/
public class ser222_01_02_hw02_base implements Matrix {
//TODO: implement interface.
/**
* Entry point for matrix testing.
* @param args the command line arguments
*/
public static void main(String[] args) {
int[][] data1 = new int[0][0];
int[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
int[][] data4 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
Matrix m1 = new ser222_01_02_hw02_base(data1);
Matrix m2 = new ser222_01_02_hw02_base(data2);
Matrix m3 = new ser222_01_02_hw02_base(data3);
Matrix m4 = new ser222_01_02_hw02_base(data4);
System.out.println("m1 --> Rows: " + m1.getRows() + " Columns: " + m1.getColumns());
System.out.println("m2 --> Rows: " + m2.getRows() + " Columns: " + m2.getColumns());
System.out.println("m3 --> Rows: " + m3.getRows() + " Columns: " + m3.getColumns());
//check for reference issues
System.out.println("m2 --> " + m2);
data2[1][1] = 101;
System.out.println("m2 --> " + m2);
//test equals
System.out.println("m2==null: " + m2.equals(null)); //false
System.out.println("m3==\"MATRIX\": " + m2.equals("MATRIX")); //false
System.out.println("m2==m1: " + m2.equals(m1)); //false
System.out.println("m2==m2: " + m2.equals(m2)); //true
System.out.println("m2==m3: " + m2.equals(m3)); //false
System.out.println("m3==m4: " + m3.equals(m4)); //true
//test operations (valid)
System.out.println("2 * m2: " + m2.scale(2));
System.out.println("m2 + m3: " + m2.plus(m3));
System.out.println("m2 - m3: " + m2.minus(m3));
//not tested... multiply(). you know what to do.
//test operations (invalid)
//System.out.println("m1 + m2" + m1.plus(m2));
//System.out.println("m1 - m2" + m1.minus(m2));
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started