Browse Source

REFACTORING Moved some methodes in order to get a cleaner Struckture

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
e7a4fe7a7d
  1. 78
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java
  2. 101
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java
  3. 27
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java
  4. 82
      src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java
  5. 53
      src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java

78
src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java

@ -5,9 +5,6 @@ import javafx.scene.control.*;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatrixCalcController {
// Hier werden die fx:id Attribute verknuepft.
@ -32,80 +29,25 @@ public class MatrixCalcController {
@FXML
public void initialize() {
/**
* Convert Strings to matricies, multiply them and output the result.
* Convert Strings to matricies, multiply them and output the result.
*/
multiplyButton.setOnMouseClicked((event) -> {
MatrixCalcMath math = new MatrixCalcMath();
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
if (checkInput(stringMatrixA) && checkInput(stringMatrixB)) {
MatrixCalcMath math = new MatrixCalcMath();
if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB)) {
double[][] matrixA = math.stringToMatrix(stringMatrixA);
double[][] matrixB = math.stringToMatrix(stringMatrixB);
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
double[][] result = math.matrixMultiplication(matrixA, matrixB);
outputMatrixToOutputText(result);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
}
// System.out.println(matrixATextArea.getText());
});
}
/**
* Prints a given 2D-Array to the output text Field.
* @param output2DArray The Array that gets Displayed
*/
private void outputMatrixToOutputText(double[][] output2DArray) {
//convert array to String
String DisplayableString = convertsArrayToStringInOrderToDisplayIt(output2DArray);
// Display output
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
}
/**
* Converts Array to String in oder to Display it.
* @param array2D the array wich will be converted to an Displayable String
* @return The Displayable String
*/
private String convertsArrayToStringInOrderToDisplayIt(double[][] array2D) {
String displayableString = "";
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[0].length; j++) {
displayableString += array2D[i][j] + " ";
// System.out.println(result[i][j]);
}
displayableString += "\n\n";
}
return displayableString;
}
/**
* Chcks if the Input is Valid, with Regex. Returns true if the Matrix can be
* matched by the regular Expression.
*
* @param matrix It is the InputMatrix
* @return true if the Matrix is valid Input.
*/
public boolean checkInput(String matrix) {
boolean isMatched = false;
if (matrix.length() == 0) {
throw new IllegalArgumentException("Please insert a Matrix");
}
// Matches digits witch following spaces 1 to 3 times
String row1 = "(\\d*\\u0020*){1,3}";
// Matches newlineCurrierReturn followed by digits witch following spaces 1 to
// 3times
String row2 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
String row3 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
// TODO get the input check more stricktly missing matrix slots are allowed.
Pattern p = Pattern.compile(row1 + row2 + row3);
Matcher m = p.matcher(matrix);
isMatched = m.matches();
// System.out.println(isMatched);
return isMatched;
}
}

101
src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java

@ -0,0 +1,101 @@
package com.ugsbo.matrixcalc;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatrixCalcIOUtils {
/**
* Prints a given 2D-Array to the output text Field.
*
* @param output2DArray The Array that gets Displayed
*/
protected String outputMatrixToOutputText(double[][] output2DArray) {
// convert array to String
String DisplayableString = convertsArrayToStringInOrderToDisplayIt(output2DArray);
// Display output
return DisplayableString;
}
/**
* Converts Array to String in oder to Display it.
*
* @param array2D the array wich will be converted to an Displayable String
* @return The Displayable String
*/
protected String convertsArrayToStringInOrderToDisplayIt(double[][] array2D) {
String displayableString = "";
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[0].length; j++) {
displayableString += array2D[i][j] + " ";
// System.out.println(result[i][j]);
}
displayableString += "\n\n";
}
return displayableString;
}
/**
* Chcks if the Input is Valid, with Regex. Returns true if the Matrix can be
* matched by the regular Expression.
*
* @param matrix It is the InputMatrix
* @return true if the Matrix is valid Input.
*/
protected boolean checkInput(String matrix) {
boolean isMatched = false;
if (matrix.length() == 0) {
throw new IllegalArgumentException("Please insert a Matrix");
}
// Matches digits witch following spaces 1 to 3 times
String row1 = "(\\d*\\u0020*){1,3}";
// Matches newlineCurrierReturn followed by digits witch following spaces 1 to
// 3times
String row2 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
String row3 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
// TODO get the input check more stricktly missing matrix slots are allowed.
Pattern p = Pattern.compile(row1 + row2 + row3);
Matcher m = p.matcher(matrix);
isMatched = m.matches();
// System.out.println(isMatched);
return isMatched;
}
/**
* Converts a String form the Inputfield to an 2D-Array aka Matrix.
*
* @param stringMatrix The String form the Inputfield
* @return Matrix as a 2D-Array
*/
public double[][] stringToMatrix(String stringMatrix) {
ArrayList<String[]> singleNumbersArr = new ArrayList<String[]>();
// Splitting the strings into their rows
String[] singleNumbers = null;
String[] rows = stringMatrix.split("\n");
for (int i = 0; i < rows.length; i++) {
// System.out.println(rows[i]);
// Splitting rows into their Numbers
singleNumbers = rows[i].split("\\s");
singleNumbersArr.add(singleNumbers);
}
int rowlength = singleNumbersArr.get(0).length; // row.length
int columCount = singleNumbersArr.size(); // output.length
double[][] matrix = new double[columCount][rowlength];
for (int columIndex = 0; columIndex < columCount; columIndex++) {
for (int rowIndex = 0; rowIndex < singleNumbers.length; rowIndex++) {
matrix[columIndex][rowIndex] = Double.parseDouble(singleNumbersArr.get(columIndex)[rowIndex]);
}
}
return matrix;
}
}

27
src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java

@ -227,31 +227,4 @@ public class MatrixCalcMath {
}
return false;
}
public double[][] stringToMatrix(String stringMatrix) {
ArrayList<String[]> singleNumbersArr = new ArrayList<String[]>();
//Splitting the strings into their rows
String[] singleNumbers = null;
String[] rows = stringMatrix.split("\n");
for (int i = 0; i < rows.length; i++) {
//System.out.println(rows[i]);
//Splitting rows into their Numbers
singleNumbers = rows[i].split("\\s");
singleNumbersArr.add(singleNumbers);
}
int rowlength = singleNumbersArr.get(0).length; //row.length
int columCount = singleNumbersArr.size(); //output.length
double[][] result = new double[columCount][rowlength];
for (int columIndex = 0; columIndex < columCount; columIndex++) {
for (int rowIndex = 0; rowIndex < singleNumbers.length; rowIndex++) {
result[columIndex][rowIndex] = Double.parseDouble(singleNumbersArr.get(columIndex)[rowIndex]);
}
}
return result;
}
}

82
src/test/java/com/ugsbo/matrixcalc/StringToMatrixTest.java → src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java

@ -1,29 +1,63 @@
package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the funktionality to Calculate the Determinant of a Matrix.
* Tests how well worng input can be determent.
*/
public class StringToMatrixTest {
public class MatrixIOUtilsTest {
private MatrixCalcMath matrixMath;
private MatrixCalcIOUtils util;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
util = new MatrixCalcIOUtils();
}
@Test(expected = IllegalArgumentException.class)
public void inputEmptySouldThrowAIllegalArgumentException() {
String input = "";
util.checkInput(input);
}
@Test
public void checkIfA1by3MatrixIsValidInput() {
String input = "1 2 3";
boolean result = util.checkInput(input);
assertTrue("The 1 by 3 Matrix should be Matched as valid input.", result);
}
@Test
public void checkIfA2by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3";
boolean result = util.checkInput(input);
assertTrue("The 2 by 3 Matrix should be Matched as valid input.", result);
}
@Test
public void checkIfA3by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3\n1 2 3";
boolean result = util.checkInput(input);
assertTrue("The 3 by 3 Matrix should be Matched as valid input.", result);
}
@Test
public void StringWithSingleDigitNumbersToMatrix_ReturnsEquivalentMatrix() {
String inputString = "1";
double[][] expected = { { 1.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
}
@ -31,9 +65,9 @@ public class StringToMatrixTest {
@Test
public void StringWithfourDigitNumbersToMatrix_ReturnsEquivalentMatrix() {
String inputString = "1 2\n3 4";
double[][] expected = { { 1.0, 2.0 }, {3.0, 4.0} };
double[][] result = matrixMath.stringToMatrix(inputString);
double[][] expected = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
@ -43,44 +77,44 @@ public class StringToMatrixTest {
public void StringWithSixDigitNumbersToMatrix_Returns1by6Matrix() {
String inputString = "1 2 3 4 5 6";
double[][] expected = { { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns2by3Matrix() {
String inputString = "1 2 3\n4 5 6";
double[][] expected = { { 1.0, 2.0, 3.0}, { 4.0, 5.0, 6.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns3by2Matrix() {
String inputString = "1 2\n3 4\n5 6";
double[][] expected = { { 1.0, 2.0}, { 3.0, 4.0}, { 5.0, 6.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
double[][] expected = { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
assertArrayEquals("The therd row is not correct", expected[2], result[2], 0.1);
}
@Test
public void StringWithNineDigitNumbersToMatrix_Returns3by3Matrix() {
String inputString = "1 2 3\n4 5 6\n7 8 9";
double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } };
double[][] result = matrixMath.stringToMatrix(inputString);
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
assertArrayEquals("The thierd row is not correct", expected[2], result[2], 0.1);
}
}
}

53
src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java

@ -1,53 +0,0 @@
package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
/**
* Tests how well worng input can be determent.
*/
public class MatrixInputcheckTest {
private MatrixCalcController controller;
@Before
public void setup() {
controller = new MatrixCalcController();
}
@Test(expected = IllegalArgumentException.class)
public void inputEmptySouldThrowAIllegalArgumentException() {
String input = "";
controller.checkInput(input);
}
@Test
public void checkIfA1by3MatrixIsValidInput() {
String input = "1 2 3";
boolean result = controller.checkInput(input);
assertTrue("The 1 by 3 Matrix should be Matched as valid input.", result);
}
@Test
public void checkIfA2by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3";
boolean result = controller.checkInput(input);
assertTrue("The 2 by 3 Matrix should be Matched as valid input.", result);
}
@Test
public void checkIfA3by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3\n1 2 3";
boolean result = controller.checkInput(input);
assertTrue("The 3 by 3 Matrix should be Matched as valid input.", result);
}
}
Loading…
Cancel
Save