Solved Task 1
This commit is contained in:
parent
9cdf6a4ebc
commit
d00d56bbee
|
@ -26,4 +26,11 @@ public class ADS1_3_test {
|
|||
test("[(3 +3)* 35 +3]* {3 +2}", true);
|
||||
test("[({3 +3)* 35} +3]* {3 +2}", false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestBrackets() {
|
||||
//new Tests:
|
||||
test("(",false);
|
||||
test(")",false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
* @author
|
||||
* @version 1.00 2017/8/30
|
||||
*/
|
||||
/**
|
||||
public class ADS2_3_test {
|
||||
MyList list;
|
||||
|
||||
|
@ -97,3 +98,4 @@ public class ADS2_3_test {
|
|||
}
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -16,6 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
* @author
|
||||
* @version 1.00 2017/8/30
|
||||
*/
|
||||
/**
|
||||
public class ADS2_4_test {
|
||||
MySortedList list;
|
||||
|
||||
|
@ -78,3 +79,4 @@ public class ADS2_4_test {
|
|||
}
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -0,0 +1,46 @@
|
|||
package ch.zhaw.ads;
|
||||
|
||||
public class BracketServer implements CommandExecutor {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String execute(String command) throws Exception {
|
||||
return checkBrackets(command) ? "true" : "false";
|
||||
}
|
||||
|
||||
public boolean checkBrackets (String arg) {
|
||||
Stack stack = new ListStack();
|
||||
char[] chars = arg.toCharArray();
|
||||
for (char x: chars) {
|
||||
Character closingBracket = getClosingBracketForOpening(x);
|
||||
if(closingBracket != null){
|
||||
stack.push(closingBracket);
|
||||
} else if(isClosingBracket(x) && stack.pop() != (Character)x) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(stack.isEmpty()){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Character getClosingBracketForOpening(char openingBracket) {
|
||||
switch (openingBracket){
|
||||
case '(':
|
||||
return ')';
|
||||
case '[':
|
||||
return ']';
|
||||
case '{':
|
||||
return '}';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isClosingBracket(char x) {
|
||||
return x == ')' || x == ']' || x == '}';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package ch.zhaw.ads;
|
||||
|
||||
import ch.zhaw.ads.Stack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ListStack implements Stack {
|
||||
ArrayList<Object> arrayList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void push(Object x) throws StackOverflowError {
|
||||
arrayList.add(0, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object pop() {
|
||||
return arrayList.isEmpty() ? null : arrayList.remove(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return arrayList.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object peek() {
|
||||
return arrayList.isEmpty() ? null : arrayList.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAll() {
|
||||
for(Iterator it = arrayList.iterator(); it.hasNext();){
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFull() {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package ch.zhaw.ads;
|
||||
|
||||
/**
|
||||
* Interface für Abstrakten Datentyp (ADT) Stack
|
||||
*/
|
||||
public interface Stack {
|
||||
/**
|
||||
* Legt eine neues Objekt auf den Stack, falls noch nicht voll.
|
||||
*
|
||||
* @param x ist das Objekt, das dazugelegt wird.
|
||||
*/
|
||||
void push(Object x) throws StackOverflowError;
|
||||
|
||||
/**
|
||||
* Entfernt das oberste und damit das zuletzt eingefügte Objekt.
|
||||
* Ist der Stack leer, wird null zurückgegeben.
|
||||
*
|
||||
* @return Gibt das oberste Objekt zurück oder null, falls leer.
|
||||
*/
|
||||
Object pop();
|
||||
|
||||
/**
|
||||
* Testet, ob der Stack leer ist.
|
||||
*
|
||||
* @return Gibt true zurück, falls der Stack leer ist.
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* Gibt das oberste Objekt zurück, ohne es zu entfernen.
|
||||
* Ist der Stack leer, wird null zurückgegeben.
|
||||
*
|
||||
* @return Gibt das oberste Objekt zurück oder null, falls leer.
|
||||
*/
|
||||
Object peek();
|
||||
|
||||
/**
|
||||
* Entfernt alle Objekte vom Stack. Ein Aufruf von isEmpty()
|
||||
* ergibt nachher mit Sicherheit true.
|
||||
*/
|
||||
void removeAll();
|
||||
|
||||
/**
|
||||
* Testet, ob der Stack voll ist.
|
||||
*
|
||||
* @return Gibt true zurück, falls der Stack voll ist.
|
||||
*/
|
||||
boolean isFull();
|
||||
}
|
Loading…
Reference in New Issue