Skip to content
Snippets Groups Projects
Commit b648a865 authored by maikov's avatar maikov
Browse files

Add toString() for matrices and numbers.

parent 8afcedbf
No related branches found
No related tags found
No related merge requests found
...@@ -73,4 +73,9 @@ public class MatrixValue extends Value { ...@@ -73,4 +73,9 @@ public class MatrixValue extends Value {
throw new InvalidMatrixException("Matrix value is not a square matrix"); throw new InvalidMatrixException("Matrix value is not a square matrix");
} }
} }
@Override
public String toString() {
return matrix.toString();
}
} }
...@@ -59,4 +59,9 @@ public class Number extends Value { ...@@ -59,4 +59,9 @@ public class Number extends Value {
public Value absoluteValue() { public Value absoluteValue() {
return new Number(Math.abs(this.getNumber())); return new Number(Math.abs(this.getNumber()));
} }
@Override
public String toString() {
return Double.toString(number);
}
} }
...@@ -107,4 +107,20 @@ public class Matrix { ...@@ -107,4 +107,20 @@ public class Matrix {
public double[] getElements() { public double[] getElements() {
return elements; return elements;
} }
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (int row = 0; row < rows; row++) {
stringBuilder.append('[');
for (int col = 0; col < cols; col++) {
stringBuilder.append(this.get(row, col));
if (col != cols-1) {
stringBuilder.append(',').append(' ');
}
}
stringBuilder.append(']').append('\n');
}
return stringBuilder.toString();
}
} }
...@@ -92,4 +92,15 @@ public class MatrixTest { ...@@ -92,4 +92,15 @@ public class MatrixTest {
3, 7, 2, 3, 7, 2,
4, 8, 1}, transpose.getElements()); 4, 8, 1}, transpose.getElements());
} }
@Test
void testMatrixToString() {
Matrix matrix = new Matrix(4, 2, new double[]{
2, 9,
4, 3,
7, 8,
1, 5
});
assertEquals("[2.0, 9.0]\n[4.0, 3.0]\n[7.0, 8.0]\n[1.0, 5.0]\n", matrix.toString());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment