The code below defines methods that perform various operations on a matrix. The
ID: 3598173 • Letter: T
Question
The code below defines methods that perform various operations on a matrix. The code I have written below works fine other than the fact that it is supposed to be immutable. The main method tests the code to determine if it is actually immutable. However I am missing something important in public class BurgessMatrix implements Matrix because the line data2[1][1] = 101 in the main method should not be able to change this data point to 101.
The expected output is:
My code so far:
/**
* An implementation of the Matrix ADT. Provides four basic operations over an
* immutable type.
*
* @author (Breanna Burgess)
* @version (1.0)
*/
public class BurgessMatrix implements Matrix
{
private int[][] myMatrix;
private int row = 0;
private int column = 0;
public BurgessMatrix(int[][] matrix)
{
if(matrix.length == 0)
{
row = 0;
column = 0;
}
else
{
row = matrix.length;
column = matrix[0].length;
}
myMatrix = new int[row][column];
myMatrix = 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)
{
return myMatrix[y][x];
}
/**
* Returns the number of rows in the matrix.
* @return rows
*/
public int getRows()
{
return row;
}
/**
* Returns the number of columns in the matrix.
* @return columns
*/
public int getColumns()
{
return column;
}
/**
* Sets new element at a particular point in the matrix.
* @param y y position
* @param x x position
* @param value new value
*/
public void setElement(int y, int x, int value)
{
myMatrix[y][x] = value;
}
/**
* 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)
{
int newElement;
int i, j;
BurgessMatrix matrix1 = new BurgessMatrix(myMatrix);
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
newElement =(getElement(i,j)*scalar);
matrix1.setElement(i, j, newElement);
}
}
return matrix1;
}
/**
* 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) throws RuntimeException
{
int sum = 0;
int i, j;
if((getRows() == other.getRows()) && (getColumns() == other.getColumns()))
{
BurgessMatrix matrix2 = new BurgessMatrix(myMatrix);
for(i = 0; i < other.getRows(); i++)
{
for(j = 0; j < other.getColumns(); j++)
{
sum = (getElement(i, j) + other.getElement(i, j));
matrix2.setElement(i, j, sum);
}
}
return matrix2;
}
else
throw new RuntimeException("Dimensions do not match. Matrices cannot be added together");
}
/**
* 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) throws RuntimeException
{
int difference;
int i, j;
if((getRows() == other.getRows()) && (getColumns() == other.getColumns()))
{
BurgessMatrix matrix3 = new BurgessMatrix(myMatrix);
for(i = 0; i < other.getRows(); i++)
{
for(j = 0; j < other.getColumns(); j++)
{
difference = (getElement(i,j) - other.getElement(i, j));
matrix3.setElement(i, j, difference);
}
}
return matrix3;
}
else
throw new RuntimeException("Dimensions do not match. Matrices cannot be subtracted from each other.");
}
/**
* Returns true if this matrix matches another matrix.
* @param other another matrix
* @return equality
*/
@Override
public boolean equals(Object other)
{
if(other == this)
return true;
else
return false;
}
/**
* 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()
{
StringBuffer sb = new StringBuffer("[");
for(int i = 0; i < row; i++)
{
sb.append("[");
for(int j = 0; j < column; j++)
{
sb.append(" " + myMatrix[i][j] + " ");
}
sb.append("]");
}
return sb + "]";
}
/**
* 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}};
Matrix m1 = new BurgessMatrix(data1);
Matrix m2 = new BurgessMatrix(data2);
Matrix m3 = new BurgessMatrix(data3);
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
//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));
}
}
My output: (which is incorrect)
The results from running this code should be: ml- 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 nul false m3 "MATRIX": fals e m2 ml: false m2 m2: true m2 m3: false 2 m2:Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package matrix;
/**
*
* @author Rammkrishhna
*/
public class BurgessMatrix implements Matrix{
private int[][] myMatrix;
private int row = 0;
private int column = 0;
public BurgessMatrix(int[][] matrix)
{
this.column = 0;
this.row = 0;
if(matrix.length == 0)
{
row = 0;
column = 0;
}
else
{
row = matrix.length;
column = matrix[0].length;
}
myMatrix = new int[row][column];
myMatrix = 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)
{
return myMatrix[y][x];
}
/**
* Returns the number of rows in the matrix.
* @return rows
*/
public int getRows()
{
return row;
}
/**
* Returns the number of columns in the matrix.
* @return columns
*/
public int getColumns()
{
return column;
}
/**
* Sets new element at a particular point in the matrix.
* @param y y position
* @param x x position
* @param value new value
*/
public void setElement(int y, int x, int value)
{
myMatrix[y][x] = value;
}
/**
* 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)
{
int newElement;
int i, j;
BurgessMatrix matrix2 = new BurgessMatrix(myMatrix);
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
newElement =(getElement(i,j)*scalar);
matrix2.setElement(i, j, newElement);
}
}
return matrix2;
}
public Matrix oldMatrix()
{
int newElement;
int i, j;
BurgessMatrix matrix2 = new BurgessMatrix(myMatrix);
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
newElement =(getElement(i,j)*1);
matrix2.setElement(i, j, newElement);
}
}
return matrix2;
}
/**
* 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) throws RuntimeException
{
int sum = 0;
int i, j;
if((getRows() == other.getRows()) && (getColumns() == other.getColumns()))
{
BurgessMatrix matrix2 = new BurgessMatrix(myMatrix);
for(i = 0; i < other.getRows(); i++)
{
for(j = 0; j < other.getColumns(); j++)
{
sum = (getElement(i, j) + other.getElement(i, j));
matrix2.setElement(i, j, sum);
}
}
return matrix2;
}
else
throw new RuntimeException("Dimensions do not match. Matrices cannot be added together");
}
/**
* 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) throws RuntimeException
{
int difference;
int i, j;
if((getRows() == other.getRows()) && (getColumns() == other.getColumns()))
{
BurgessMatrix matrix3 = new BurgessMatrix(myMatrix);
for(i = 0; i < other.getRows(); i++)
{
for(j = 0; j < other.getColumns(); j++)
{
difference = (getElement(i,j) - other.getElement(i, j));
matrix3.setElement(i, j, difference);
}
}
return matrix3;
}
else
throw new RuntimeException("Dimensions do not match. Matrices cannot be subtracted from each other.");
}
/**
* Returns true if this matrix matches another matrix.
* @param other another matrix
* @return equality
*/
@Override
public boolean equals(Object other)
{
if(other == this)
return true;
else
return false;
}
/**
* 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()
{
StringBuffer sb = new StringBuffer("[");
for(int i = 0; i < row; i++)
{
sb.append("[");
for(int j = 0; j < column; j++)
{
sb.append(" " + myMatrix[i][j] + " ");
}
sb.append("]");
}
return sb + "]";
}
/**
* Entry point for matrix testing.
* @param args the command line arguments
*/
public static void main(String[] args)
{
int[][] data1 = new int[0][0];
final int[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
final int[][] data22 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
Matrix m1 = new BurgessMatrix(data1);
Matrix m2 = new BurgessMatrix(data2);
Matrix m3 = new BurgessMatrix(data3);
Matrix m4 = new BurgessMatrix(data22);
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
//test operations (valid)
System.out.println("m2: " + m4.oldMatrix());
System.out.println("2 * m2: " + m2.scale(2));
System.out.println("m2 + m3: " + m4.plus(m3));
System.out.println("m2 - m3: " + m2.minus(m3));
//test operations (invalid)
//System.out.println("m1 + m2" + m2.plus(m2));
//System.out.println("m1 - m2" + m2.minus(m2));
}
}
Matrix interface:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package matrix;
/**
*
* @author Rammkrishhna
*/
public interface Matrix {
public int getRows();
public int getColumns();
public int getElement(int y, int x);
public Matrix oldMatrix();
public Matrix scale(int scalar);
public Matrix plus(Matrix other);
public Matrix minus(Matrix other);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.