solved s2 ex2
This commit is contained in:
parent
514e94147b
commit
4f31b86af4
52
02/src/main/java/ch/zhaw/ads/BracketServer.java
Normal file
52
02/src/main/java/ch/zhaw/ads/BracketServer.java
Normal 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.
|
||||
}
|
72
02/src/main/java/ch/zhaw/ads/ListStack.java
Normal file
72
02/src/main/java/ch/zhaw/ads/ListStack.java
Normal 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;
|
||||
}
|
||||
}
|
117
02/src/main/java/ch/zhaw/ads/MyList.java
Normal file
117
02/src/main/java/ch/zhaw/ads/MyList.java
Normal 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();
|
||||
}
|
||||
}
|
61
02/src/main/java/ch/zhaw/ads/Node.java
Normal file
61
02/src/main/java/ch/zhaw/ads/Node.java
Normal 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 ;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user