solved s2 ex2

This commit is contained in:
navid.sassan 2020-10-05 00:12:58 +02:00
parent 514e94147b
commit 4f31b86af4
6 changed files with 435 additions and 124 deletions

View File

@ -0,0 +1,52 @@
package ch.zhaw.ads;
import java.util.Map;
import static java.util.Map.entry;
public class BracketServer implements CommandExecutor {
@Override
public String execute(String command) {
StringBuffer result = new StringBuffer(100);
result.append("Die Eingabe war \"");
result.append(command);
result.append("\"\n");
return result.toString();
}
private Map<Character, Character> openingBrackets = Map.ofEntries(
entry('(', ')'),
entry('[', ']'),
entry('{', '}'),
entry('<', '>')
);
private Map<Character, Character> closingBrackets = Map.ofEntries(
entry(')', '('),
entry(']', '['),
entry('}', '{'),
entry('>', '<')
);
public boolean checkBrackets(String input) {
ListStack<Character> bracketStack = new ListStack<>();
for (int i = 0; i < input.length(); i++) {
char cur_char = input.charAt(i);
if (openingBrackets.containsKey(cur_char)) {
bracketStack.push(cur_char);
}
if (closingBrackets.containsKey(cur_char)) {
if (bracketStack.isEmpty()) {
return false;
}
Character last = (Character) bracketStack.pop();
if (last != closingBrackets.get(cur_char)) {
return false;
}
}
}
return bracketStack.isEmpty();
}
// search bracket from start, then remove matching bracket from the end of the string. then remove opening bracket.
}

View File

@ -0,0 +1,72 @@
/**
* AnyServer -- Praktikum Experimentierkasten -- ADS
*
* @author E. Mumprecht
* @version 1.0 -- Geruest fuer irgendeinen Server
*/
package ch.zhaw.ads;
public class ListStack<T> implements CommandExecutor {
@Override
public String execute(String command) {
StringBuffer result = new StringBuffer(100);
result.append("Die Eingabe war \"");
result.append(command);
result.append("\"\n");
return result.toString();
}
Object[] data;
private int top;
private int capacity;
private static final int DEFAULT_CAPACITY = 100;
public ListStack() {
this(DEFAULT_CAPACITY);
}
public ListStack(int capacity) {
this.capacity= capacity;
removeAll();
}
public void removeAll() {
data = new Object[capacity];
top = 0;
}
public void push(Object x) {
// public void push(Object x) throws StackOverflowException {
// if (isFull()){
// throw new StackOverflowException();
// }
data[top] = x;
top++;
}
public Object pop() {
if (isEmpty()){
return null;
}
top--;
Object topItem= data [top];
data [top] = null;
return topItem;
}
public Object peek() {
if (isEmpty()){
return null;
}
return data [top-1];
}
public boolean isEmpty() {
return (top == 0);
}
public boolean isFull() {
return top == data.length;
}
}

View File

@ -0,0 +1,117 @@
package ch.zhaw.ads;
import java.util.AbstractList;
public class MyList<T extends Object> extends AbstractList<T> {
private Object[] data;
private Node<T> head;
private Node<T> tail;
private int size;
public MyList() {
clear();
}
public boolean add(Object obj) {
try {
if (isEmpty()) {
head = tail = new Node<T>((T) obj);
size++;
return true;
}
Node<T> newNode = new Node<T>((T) obj, tail, null);
// the old tail now has a nextNode
tail.setNextNode(newNode);
// and move tail to newNode
tail = newNode;
size++;
return true;
} catch (ClassCastException e) {
return false;
}
}
public boolean remove(Object obj) {
if (isEmpty()) {
return false;
}
try {
Node<T> nodeToRemove = new Node<T>((T) obj);
for (Node<T> node = head; node.hasNextNode(); node = node.getNextNode()) {
if (node.equals(nodeToRemove)) {
unlink(node);
return true;
}
}
} catch (ClassCastException e) {
return false;
}
// reaching this means that no element was found
return false;
}
private void unlink(Node<T> node) {
Node<T> previousNode = node.getNextNode();
Node<T> nextNode = node.getNextNode();
if (previousNode == null) {
// this means the node which will be unlinked was the first element. the new first element is the nextNode of the unlinked node
head = nextNode;
} else {
previousNode.setNextNode(nextNode);
}
// same thing, other way around
if (nextNode == null) {
tail = previousNode;
} else {
nextNode.setPreviousNode(previousNode);
}
size--;
}
public T get(int index) {
if (index > 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
// check if the index is closer to head or tail
// shifting the size to the right by one bit basically divides it by 2 (works because its binary)
if (index < (size >> 1)) {
Node<T> node = head;
for (int i = 0; i < index; i++) {
node = node.getNextNode();
}
return node.getValue();
} else {
Node<T> node = tail;
for (int i = size - 1; i > index; i--) {
node = node.getPreviousNode();
}
return node.getValue();
}
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void clear() {
size = 0;
head = null;
tail = null;
}
void other() {
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,61 @@
package ch.zhaw.ads;
public class Node<E> {
private E value;
private Node<E> previousNode;
private Node<E> nextNode;
public Node(E object) {
this(object, null, null);
}
public Node(E object, Node<E> previousNode, Node<E> nextNode) {
this.value = object;
this.previousNode = previousNode;
this.nextNode = nextNode;
}
public boolean hasNextNode() {
return this.nextNode != null;
}
public boolean hasPreviousNode() {
return this.previousNode != null;
}
public boolean equals(Node<E> node) {
if (node == null) {
return false;
}
return value.equals(node.value);
}
public Node<E> clone(){
return new Node<E>(value, previousNode, nextNode);
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Node<E> getPreviousNode() {
return previousNode;
}
public void setPreviousNode(Node<E> previousNode ) {
this.previousNode = previousNode ;
}
public Node<E> getNextNode() {
return nextNode;
}
public void setNextNode(Node<E> nextNode ) {
this.nextNode = nextNode ;
}
}

View File

@ -1,31 +1,40 @@
package Praktikum_02_Code; package ch.zhaw.ads;
import org.junit.Test; import org.junit.Test;
import org.junit.Before; import org.junit.Before;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class BracketServerTest {
public class BracketServerTest {
BracketServer bs;
BracketServer bs;
@Before
public void setUp() throws Exception { @Before
bs = new BracketServer(); public void setUp() throws Exception {
} bs = new BracketServer();
}
private void test(String s, boolean b) {
assertEquals(s, b, bs.checkBrackets(s)); private void test(String s, boolean b) {
} assertEquals(s, b, bs.checkBrackets(s));
}
@Test
public void testBracket() { @Test
test(")",false); public void testBracket() {
test("(",false); test(")",false);
test("()",true); test("(",false);
test("(()]",false); test("()",true);
test("((([([])])))",true); test("(()]",false);
test("[])",false); test("((([([])])))",true);
test("[(3 +3)* 35 +3]* {3 +2}",true) ; test("[])",false);
test("[({3 +3)* 35} +3]* {3 +2}",false); test("[(3 +3)* 35 +3]* {3 +2}",true) ;
} test("[({3 +3)* 35} +3]* {3 +2}",false);
} // new tests
test("(", false);
test(")", false);
test("<(<>)>", true);
test("<(<)>>", false);
test("<*(<*<>*>)*>", true);
test("<(<**>)*>", false);
}
}

View File

@ -1,93 +1,93 @@
package Praktikum_02_Code; package Praktikum_02_Code;
import org.junit.Test; import org.junit.Test;
import org.junit.Before; import org.junit.Before;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.*; import java.util.*;
public class ListTest { public class ListTest {
List list; List list;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
list = new MyList(); list = new MyList();
} }
@Test @Test
public void testAdd() { public void testAdd() {
list.clear(); list.clear();
list.add("A"); list.add("A");
Object o = list.get(0); Object o = list.get(0);
assertEquals(o, "A"); assertEquals(o, "A");
} }
@Test @Test
public void testAdd2() { public void testAdd2() {
list.clear(); list.clear();
list.add("A"); list.add("A");
list.add("B"); list.add("B");
Object o = list.get(0); Object o = list.get(0);
assertEquals(o, "A"); assertEquals(o, "A");
o = list.get(1); o = list.get(1);
assertEquals(o, "B"); assertEquals(o, "B");
} }
@Test @Test
public void testAdd3() { public void testAdd3() {
list.clear(); list.clear();
list.add("A"); list.add("A");
list.add("B"); list.add("B");
list.add("C"); list.add("C");
Object o = list.get(0); Object o = list.get(0);
assertEquals(o, "A"); assertEquals(o, "A");
o = list.get(1); o = list.get(1);
assertEquals(o, "B"); assertEquals(o, "B");
o = list.get(2); o = list.get(2);
assertEquals(o, "C"); assertEquals(o, "C");
} }
@Test @Test
public void testSize() { public void testSize() {
list.clear(); list.clear();
assertEquals(0, list.size()); assertEquals(0, list.size());
testAdd2(); testAdd2();
assertEquals(2, list.size()); assertEquals(2, list.size());
} }
@Test @Test
public void testRemove() { public void testRemove() {
list.clear(); list.clear();
list.add("A"); list.add("A");
list.remove("A"); list.remove("A");
assertEquals(0, list.size()); assertEquals(0, list.size());
list.add("A"); list.add("A");
list.remove("B"); list.remove("B");
assertEquals(1, list.size()); assertEquals(1, list.size());
list.remove("A"); list.remove("A");
assertEquals(0, list.size()); assertEquals(0, list.size());
} }
@Test @Test
public void testMixed() { public void testMixed() {
list.clear(); list.clear();
List list2 = new LinkedList(); List list2 = new LinkedList();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Character c = (char) ('A' + (Math.random()*26)); Character c = (char) ('A' + (Math.random()*26));
int op = (int)(Math.random()*2); int op = (int)(Math.random()*2);
switch (op) { switch (op) {
case 0 : list.add(c); list2.add(c); break; case 0 : list.add(c); list2.add(c); break;
case 1 : list.remove(c); list2.remove(c); break; case 1 : list.remove(c); list2.remove(c); break;
} }
} }
assertEquals(list2.size(), list.size()); assertEquals(list2.size(), list.size());
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
char c1 = (char)list.get(i); char c1 = (char)list.get(i);
char c2 = (char)list2.get(i); char c2 = (char)list2.get(i);
assertEquals(c1,c2); assertEquals(c1,c2);
} }
} }
} }