Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is some code that is currently working (with a class and interface, which

ID: 3911303 • Letter: B

Question

Below is some code that is currently working (with a class and interface, which are seperated and labeled), but I am not sure if it is immutable. If it is, can someone explain to me why or, if not, can someone show me how to make it immutable? Thanks!

//Copyable Code:

Matrix.java

public interface Matrix
{
/**
* Returns the element at particular point in the matrix.
*
* @param y
* y position
* @param x
* x position
* @return element
*/
public double 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 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

public class ser222_01_02_hw02_base implements Matrix
{
private double[][] newMatrix;
private int rowvalue = 0;
private int columnvalue = 0;

public ser222_01_02_hw02_base(double[][] matrix)
{
if (matrix.length == 0)
{
rowvalue = 0;
columnvalue = 0;
}
else
{
rowvalue = matrix.length;
columnvalue = matrix[0].length;

}
newMatrix = new double[rowvalue][columnvalue];
newMatrix = matrix;
}

public ser222_01_02_hw02_base(int x, int y)
{
rowvalue = x;
columnvalue = y;
newMatrix = new double[x][y];
}

public double[][] getMatrix()
{
return newMatrix;
}

@Override
public double getElement(int y, int x)
{
// TODO Auto-generated method stub
return newMatrix[y][x];
}

@Override
public int getRows()
{
// TODO Auto-generated method stub
return rowvalue;
}

@Override
public int getColumns()
{
// TODO Auto-generated method stub
return columnvalue;
}

@Override
public Matrix scale(int scalar)
{
// TODO Auto-generated method stub
double newval = 0;
int i, j;
ser222_01_02_hw02_base mat1 = new ser222_01_02_hw02_base(getRows(),
getColumns());
for (i = 0; i < rowvalue; i++)
{
for (j = 0; j < columnvalue; j++)
{
newval = (getElement(i, j) * scalar);
mat1.setElement(i, j, newval);
}
}
return mat1;
}

@Override
public Matrix plus(Matrix other)
{
// TODO Auto-generated method stub
double sum = 0;
int i, j;
if ((getRows() == other.getRows()) &&
(getColumns() == other.getColumns()))
{
ser222_01_02_hw02_base mat2 = new ser222_01_02_hw02_base(other.getRows(),
getColumns());
for (i = 0; i < other.getRows(); i++)
{
for (j = 0; j < other.getColumns(); j++)
{
sum = (getElement(i, j) +
other.getElement(i, j));
mat2.setElement(i, j, sum);
}

}
return mat2;
}
else
return new ser222_01_02_hw02_base(0, 0);
}

@Override
public Matrix minus(Matrix other)
{
// TODO Auto-generated method stub
double diff = 0;
int i, j;
if ((getRows() == other.getRows()) &&
(getColumns() == other.getColumns()))
{
ser222_01_02_hw02_base mat3 = new ser222_01_02_hw02_base(other.getRows(),
getColumns());
for (i = 0; i < other.getRows(); i++)
{
for (j = 0; j < other.getColumns(); j++)
{
diff = (getElement(i, j) -
other.getElement(i, j));
mat3.setElement(i, j, diff);
}
}
return mat3;
}
else
return new ser222_01_02_hw02_base(0, 0);
}

public void setElement(int i, int j, double val)
{
newMatrix[i][j] = val;
}

public boolean equals(Object other)
{
if (other == this)
{
return true;
} else
return false;
}

public String toString()
{
StringBuffer sb = new StringBuffer("[");
for (int i = 0; i < rowvalue; i++)
{
sb.append("[");
for (int j = 0; j < columnvalue; j++)
sb.append(" " + newMatrix[i][j] + " ");
sb.append("]");
}
return sb + "]";
}

public static void main(String[] args)
{
double[][] data1 = new double[0][0];
double[][] data2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
double[][] data3 = { { 1, 4, 7 }, { 2, 5, 8 }, { 3, 6, 9 } };

ser222_01_02_hw02_base m1 = new ser222_01_02_hw02_base(data1);
ser222_01_02_hw02_base m2 = new ser222_01_02_hw02_base(data2);
ser222_01_02_hw02_base m3 = new ser222_01_02_hw02_base(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));
}
}

Explanation / Answer

Rules for making it Immutable

a)
Declare the class as final so it can't be extended.
b) Declare all fields private so that direct access is not allowed.
------>Don't provide setter methods for variables.
------>
Make all mutable fields final so that it's value can be assigned only once.

Make this change
public final class ser222_01_02_hw02_base implements Matrix {

-> You have declared all fields as private and I see no setters and getters so its fine


Thanks, let me know if there is any doubts/concern.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote