This was a homework assignment for a data structures course. The goal was to implement a given interface and the assignment was auto-graded by a system that ran some validation tests. Sadly I did not really get any feed back on the code structure or any advice on how to improve on what I did if anything was wrong or non-idiomatic for java, a language I have not really use outside of this course.
I am particularly interested in if my code for handling the math operations is silly or "overly clever" or maybe if there is a better way I could have achieved a similar pattern that would be more idiomatic for someone well versed in java.
interface MathOperation
{
int Run(int a, int b);
}
interface MatrixOperation
{
void Run(int row, int column);
}
public class CompletedMatrix implements Matrix
{
private int[][] _matrix;
public CompletedMatrix(int[][] matrix)
{
if (!NullCheck(matrix))
{
throw new IllegalArgumentException("Cannot perform operation on null matrix");
}
_matrix = new int[matrix.length][];
for (int row = 0; row < matrix.length; row++)
{
_matrix[row] = matrix[row].clone();
}
}
@Override
public int getElement(int y, int x)
{
return _matrix[y][x];
}
@Override
public int getRows()
{
return _matrix.length;
}
@Override
public int getColumns()
{
int columns = 0;
if (this.getRows() != 0)
{
columns = _matrix[0].length;
for (int row = 1; row < this.getRows(); row++)
{
if (columns != _matrix[row].length)
{
throw new RuntimeException("Matrices do not have matching dimensions");
}
}
}
return columns;
}
@Override
public Matrix plus(Matrix other)
{
return ExecuteMatrixMathOperation(other, (a, b) -> a + b);
}
@Override
public Matrix minus(Matrix other)
{
return ExecuteMatrixMathOperation(other, (a, b) -> a - b);
}
@Override
public Matrix multiply(Matrix other)
{
return ExecuteMatrixMathOperation(other, (a, b) -> a * b);
}
private Matrix ExecuteMatrixMathOperation(Matrix b, MathOperation op)
{
ValidateMatrix(b);
int[][] result = new int[this.getRows()][this.getColumns()];
MatrixOperation((row, column) ->
{
result[row][column] = op.Run(this.getElement(row, column), b.getElement(row, column));
});
return new CompletedMatrix(result);
}
private void ValidateMatrix(Matrix other)
{
if (!NullCheck(other))
{
throw new IllegalArgumentException("Cannot perform operation on null matrix");
}
if (!DimensionsMatch(other))
{
throw new RuntimeException("Matrices do not have matching dimensions");
}
}
private boolean NullCheck(Object other)
{
return (this != null && other != null);
}
private boolean DimensionsMatch(Matrix other)
{
return (this.getRows() == other.getRows() && this.getColumns() == other.getColumns());
}
private void MatrixOperation(MatrixOperation op)
{
for(int row = 0; row < this.getRows(); row++)
{
for(int column = 0; column < this.getColumns(); column++)
{
op.Run(row, column);
}
}
}
}
This is a trimmed down interface given in the assignment
public interface Matrix
{
public int getElement(int y, int x);
public int getRows();
public int getColumns();
public Matrix plus(Matrix other);
public Matrix minus(Matrix other);
public Matrix multiply(Matrix other);
}
Matrix
\$\endgroup\$multiply
doesn't do what matrix multiplication is suppose to do. \$\endgroup\$