solved ex2

This commit is contained in:
navid.sassan 2020-09-28 15:16:28 +02:00
parent 68ce7543f4
commit f6699b861c
2 changed files with 79 additions and 0 deletions

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 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,7 @@
package ch.zhaw.ads;
public class StackOverflowException extends Exception {
public StackOverflowException(String errorMessage) {
super(errorMessage);
}
}