From 4f31b86af40e09b8a1d039e3c6e14278be82ce60 Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Mon, 5 Oct 2020 00:12:58 +0200 Subject: [PATCH] solved s2 ex2 --- .../main/java/ch/zhaw/ads/BracketServer.java | 52 +++++ 02/src/main/java/ch/zhaw/ads/ListStack.java | 72 +++++++ 02/src/main/java/ch/zhaw/ads/MyList.java | 117 +++++++++++ 02/src/main/java/ch/zhaw/ads/Node.java | 61 ++++++ .../java/ch/zhaw/ads/BracketServerTest.java | 71 ++++--- 02/src/test/java/ch/zhaw/ads/ListTest.java | 186 +++++++++--------- 6 files changed, 435 insertions(+), 124 deletions(-) create mode 100644 02/src/main/java/ch/zhaw/ads/BracketServer.java create mode 100644 02/src/main/java/ch/zhaw/ads/ListStack.java create mode 100644 02/src/main/java/ch/zhaw/ads/MyList.java create mode 100644 02/src/main/java/ch/zhaw/ads/Node.java diff --git a/02/src/main/java/ch/zhaw/ads/BracketServer.java b/02/src/main/java/ch/zhaw/ads/BracketServer.java new file mode 100644 index 0000000..22dfb5c --- /dev/null +++ b/02/src/main/java/ch/zhaw/ads/BracketServer.java @@ -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 openingBrackets = Map.ofEntries( + entry('(', ')'), + entry('[', ']'), + entry('{', '}'), + entry('<', '>') + ); + + private Map closingBrackets = Map.ofEntries( + entry(')', '('), + entry(']', '['), + entry('}', '{'), + entry('>', '<') + ); + + public boolean checkBrackets(String input) { + ListStack 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. +} diff --git a/02/src/main/java/ch/zhaw/ads/ListStack.java b/02/src/main/java/ch/zhaw/ads/ListStack.java new file mode 100644 index 0000000..c5d5653 --- /dev/null +++ b/02/src/main/java/ch/zhaw/ads/ListStack.java @@ -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 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; + } +} diff --git a/02/src/main/java/ch/zhaw/ads/MyList.java b/02/src/main/java/ch/zhaw/ads/MyList.java new file mode 100644 index 0000000..2ae6578 --- /dev/null +++ b/02/src/main/java/ch/zhaw/ads/MyList.java @@ -0,0 +1,117 @@ +package ch.zhaw.ads; + +import java.util.AbstractList; + +public class MyList extends AbstractList { + + private Object[] data; + private Node head; + private Node tail; + private int size; + + public MyList() { + clear(); + } + + public boolean add(Object obj) { + try { + if (isEmpty()) { + head = tail = new Node((T) obj); + size++; + return true; + } + + Node newNode = new Node((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 nodeToRemove = new Node((T) obj); + + for (Node 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 node) { + Node previousNode = node.getNextNode(); + Node 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 node = head; + for (int i = 0; i < index; i++) { + node = node.getNextNode(); + } + return node.getValue(); + } else { + Node 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(); + } +} diff --git a/02/src/main/java/ch/zhaw/ads/Node.java b/02/src/main/java/ch/zhaw/ads/Node.java new file mode 100644 index 0000000..e550d2a --- /dev/null +++ b/02/src/main/java/ch/zhaw/ads/Node.java @@ -0,0 +1,61 @@ +package ch.zhaw.ads; + +public class Node { + private E value; + private Node previousNode; + private Node nextNode; + + public Node(E object) { + this(object, null, null); + } + + public Node(E object, Node previousNode, Node 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 node) { + if (node == null) { + return false; + } + + return value.equals(node.value); + } + + public Node clone(){ + return new Node(value, previousNode, nextNode); + } + + public E getValue() { + return value; + } + + public void setValue(E value) { + this.value = value; + } + + public Node getPreviousNode() { + return previousNode; + } + + public void setPreviousNode(Node previousNode ) { + this.previousNode = previousNode ; + } + + public Node getNextNode() { + return nextNode; + } + + public void setNextNode(Node nextNode ) { + this.nextNode = nextNode ; + } +} diff --git a/02/src/test/java/ch/zhaw/ads/BracketServerTest.java b/02/src/test/java/ch/zhaw/ads/BracketServerTest.java index 4580460..cfe2c37 100644 --- a/02/src/test/java/ch/zhaw/ads/BracketServerTest.java +++ b/02/src/test/java/ch/zhaw/ads/BracketServerTest.java @@ -1,31 +1,40 @@ -package Praktikum_02_Code; - -import org.junit.Test; -import org.junit.Before; -import static org.junit.Assert.*; -public class BracketServerTest { - - BracketServer bs; - - @Before - public void setUp() throws Exception { - bs = new BracketServer(); - } - - private void test(String s, boolean b) { - assertEquals(s, b, bs.checkBrackets(s)); - } - - @Test - public void testBracket() { - test(")",false); - test("(",false); - test("()",true); - test("(()]",false); - test("((([([])])))",true); - test("[])",false); - test("[(3 +3)* 35 +3]* {3 +2}",true) ; - test("[({3 +3)* 35} +3]* {3 +2}",false); - } - -} +package ch.zhaw.ads; + +import org.junit.Test; +import org.junit.Before; +import static org.junit.Assert.*; + +public class BracketServerTest { + + BracketServer bs; + + @Before + public void setUp() throws Exception { + bs = new BracketServer(); + } + + private void test(String s, boolean b) { + assertEquals(s, b, bs.checkBrackets(s)); + } + + @Test + public void testBracket() { + test(")",false); + test("(",false); + test("()",true); + test("(()]",false); + test("((([([])])))",true); + test("[])",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); + } + +} diff --git a/02/src/test/java/ch/zhaw/ads/ListTest.java b/02/src/test/java/ch/zhaw/ads/ListTest.java index ce4e478..c2661e2 100644 --- a/02/src/test/java/ch/zhaw/ads/ListTest.java +++ b/02/src/test/java/ch/zhaw/ads/ListTest.java @@ -1,93 +1,93 @@ -package Praktikum_02_Code; - -import org.junit.Test; -import org.junit.Before; -import static org.junit.Assert.*; -import java.util.*; - -public class ListTest { - - List list; - - @Before - public void setUp() throws Exception { - list = new MyList(); - } - - @Test - public void testAdd() { - list.clear(); - list.add("A"); - Object o = list.get(0); - assertEquals(o, "A"); - } - - @Test - public void testAdd2() { - list.clear(); - list.add("A"); - list.add("B"); - Object o = list.get(0); - assertEquals(o, "A"); - o = list.get(1); - assertEquals(o, "B"); - } - - @Test - public void testAdd3() { - list.clear(); - list.add("A"); - list.add("B"); - list.add("C"); - Object o = list.get(0); - assertEquals(o, "A"); - o = list.get(1); - assertEquals(o, "B"); - o = list.get(2); - assertEquals(o, "C"); - } - - - - @Test - public void testSize() { - list.clear(); - assertEquals(0, list.size()); - testAdd2(); - assertEquals(2, list.size()); - } - - @Test - public void testRemove() { - list.clear(); - list.add("A"); - list.remove("A"); - assertEquals(0, list.size()); - list.add("A"); - list.remove("B"); - assertEquals(1, list.size()); - list.remove("A"); - assertEquals(0, list.size()); - } - - @Test - public void testMixed() { - list.clear(); - List list2 = new LinkedList(); - for (int i = 0; i < 100; i++) { - Character c = (char) ('A' + (Math.random()*26)); - int op = (int)(Math.random()*2); - switch (op) { - case 0 : list.add(c); list2.add(c); break; - case 1 : list.remove(c); list2.remove(c); break; - } - } - assertEquals(list2.size(), list.size()); - for (int i = 0; i < list.size(); i++) { - char c1 = (char)list.get(i); - char c2 = (char)list2.get(i); - assertEquals(c1,c2); - } - } - -} +package Praktikum_02_Code; + +import org.junit.Test; +import org.junit.Before; +import static org.junit.Assert.*; +import java.util.*; + +public class ListTest { + + List list; + + @Before + public void setUp() throws Exception { + list = new MyList(); + } + + @Test + public void testAdd() { + list.clear(); + list.add("A"); + Object o = list.get(0); + assertEquals(o, "A"); + } + + @Test + public void testAdd2() { + list.clear(); + list.add("A"); + list.add("B"); + Object o = list.get(0); + assertEquals(o, "A"); + o = list.get(1); + assertEquals(o, "B"); + } + + @Test + public void testAdd3() { + list.clear(); + list.add("A"); + list.add("B"); + list.add("C"); + Object o = list.get(0); + assertEquals(o, "A"); + o = list.get(1); + assertEquals(o, "B"); + o = list.get(2); + assertEquals(o, "C"); + } + + + + @Test + public void testSize() { + list.clear(); + assertEquals(0, list.size()); + testAdd2(); + assertEquals(2, list.size()); + } + + @Test + public void testRemove() { + list.clear(); + list.add("A"); + list.remove("A"); + assertEquals(0, list.size()); + list.add("A"); + list.remove("B"); + assertEquals(1, list.size()); + list.remove("A"); + assertEquals(0, list.size()); + } + + @Test + public void testMixed() { + list.clear(); + List list2 = new LinkedList(); + for (int i = 0; i < 100; i++) { + Character c = (char) ('A' + (Math.random()*26)); + int op = (int)(Math.random()*2); + switch (op) { + case 0 : list.add(c); list2.add(c); break; + case 1 : list.remove(c); list2.remove(c); break; + } + } + assertEquals(list2.size(), list.size()); + for (int i = 0; i < list.size(); i++) { + char c1 = (char)list.get(i); + char c2 = (char)list2.get(i); + assertEquals(c1,c2); + } + } + +}