added javadoc
This commit is contained in:
parent
2eed96c8f4
commit
d044d6bbe8
|
@ -12,5 +12,6 @@ class Aufgabe2_KellerautomatTest {
|
||||||
assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("34+34+"));
|
assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("34+34+"));
|
||||||
assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("34+*"));
|
assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("34+*"));
|
||||||
assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("8+9+7*2*"));
|
assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("8+9+7*2*"));
|
||||||
|
assertThrows(IllegalArgumentException.class,()->Aufgabe2_Kellerautomat.calculate("57-"));
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
|
@ -4,8 +4,16 @@ import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Aufgabe2_Kellerautomat {
|
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) {
|
public static void main(String[] args) {
|
||||||
for (String word : words) {
|
for (String word : words) {
|
||||||
try {
|
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 {
|
public static int calculate(String word) throws IllegalArgumentException {
|
||||||
List<Character> wordChars = new ArrayList<>();
|
List<Character> wordChars = new ArrayList<>();
|
||||||
for (char symbol : word.toCharArray()) {
|
for (char symbol : word.toCharArray()) {
|
||||||
|
@ -122,25 +136,47 @@ public class Aufgabe2_Kellerautomat {
|
||||||
return -1;
|
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");
|
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) {
|
private static void accept(String word, int result) {
|
||||||
System.out.println("Word: " + word + "\naccepted\nResult " + 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){
|
private static boolean isOperator(char symbol){
|
||||||
return symbol == '+' || 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 == '+'){
|
if(operator == '+'){
|
||||||
return firstNumber + secondNumber;
|
return firstNumber + secondNumber;
|
||||||
} else if(operator == '*'){
|
} else if(operator == '*'){
|
||||||
return firstNumber * secondNumber;
|
return firstNumber * secondNumber;
|
||||||
}
|
}
|
||||||
return -1;
|
throw new IllegalArgumentException("Invalid Operator: " + operator + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue