diff --git a/Test/Aufgabe2_KellerautomatTest.java b/Test/Aufgabe2_KellerautomatTest.java index b3dedea..d4a393c 100644 --- a/Test/Aufgabe2_KellerautomatTest.java +++ b/Test/Aufgabe2_KellerautomatTest.java @@ -12,5 +12,6 @@ class Aufgabe2_KellerautomatTest { assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("34+34+")); assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("34+*")); assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("8+9+7*2*")); + assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("57-")); } } \ No newline at end of file diff --git a/doc/uebung_2 Schenk, Brandenberger, Fassbind.pdf b/doc/uebung_2 Schenk, Brandenberger, Fassbind.pdf deleted file mode 100644 index 0c3f969..0000000 Binary files a/doc/uebung_2 Schenk, Brandenberger, Fassbind.pdf and /dev/null differ diff --git a/src/Aufgabe2_Kellerautomat.java b/src/Aufgabe2_Kellerautomat.java index 4807cc1..314d372 100644 --- a/src/Aufgabe2_Kellerautomat.java +++ b/src/Aufgabe2_Kellerautomat.java @@ -4,8 +4,16 @@ import java.util.Collections; import java.util.List; public class Aufgabe2_Kellerautomat { - private static String[] words = {"34+62+89+43+***", "31+78+987+1214++7++++++", "34+34+", "34+*", "8+9+7*2*"}; + /** + * List of Words which will be calculated by Main Method + */ + private static String[] words = {"34+62+89+43+***", "31+78+987+1214++7++++++", "34+34+", "34+*", "8+9+7*2*", "57-"}; + + /** + * Calculates all words in Array words + * @param args no args needed + */ public static void main(String[] args) { for (String word : words) { try { @@ -16,6 +24,12 @@ public class Aufgabe2_Kellerautomat { } } + /** + * Calculates the given Word + * @param word the word to calculate + * @return returns the result of the calculation + * @throws IllegalArgumentException if the word is invalid and can not be caluclated (not accepted words) + */ public static int calculate(String word) throws IllegalArgumentException { List wordChars = new ArrayList<>(); for (char symbol : word.toCharArray()) { @@ -122,25 +136,47 @@ public class Aufgabe2_Kellerautomat { return -1; } - private static void dontAccept(String word) { + /** + * Called if word is invalid, throws exception with errormessage + * @param word the invalid word + */ + private static void dontAccept(String word) throws IllegalArgumentException{ throw new IllegalArgumentException("Word: " + word + "\nnot accepted"); } + /** + * Called if end state is reached, prints the accepted word and result (word is accepted) + * @param word the accepted word + * @param result the result + */ private static void accept(String word, int result) { System.out.println("Word: " + word + "\naccepted\nResult " + result); } + /** + * checks if a character is a operator + * @param symbol character to check + * @return true if character is a operator, false if not + */ private static boolean isOperator(char symbol){ return symbol == '+' || symbol == '*'; } - private static int doOperation(char operator, int firstNumber, int secondNumber){ + /** + * Calculates the result of a operation + * @param operator the operator + * @param firstNumber the first number of caluclation + * @param secondNumber the second number of calculation + * @return the result of the operation + * @throws IllegalArgumentException if the operator is invalid and can not be caluclated + */ + private static int doOperation(char operator, int firstNumber, int secondNumber) { if(operator == '+'){ return firstNumber + secondNumber; } else if(operator == '*'){ return firstNumber * secondNumber; } - return -1; + throw new IllegalArgumentException("Invalid Operator: " + operator + "\n"); } }