[infostudents] SWT Blatt 4: A1, A2 a)

  • From: Michael Leukert <leukert@xxxxxxxxxxxxxxxxxxxxxxxxxx>
  • To: infostudents@xxxxxxxxxxxxx
  • Date: Thu, 05 Jun 2008 22:56:11 +0200

ka obs stimmt. nur damit mal was da draussen rumschwirrt. wie wärs wenn mehr leute wieder teilen würden?

mfg,

mike
//wir haben es so gemacht wie auf folie28, 
//da es in der vorlesung mehrere möglichkeiten 
//gab, diese aufgabe zu lösen
/* A stack with a fixed maximum capacity */
//klasseninvariante: content!=null
public class Stack<X> {
    
    int topIx;            // index in content of the top element
    final X[] content;    // array that stores the elements of the stack
    
    @post {this.content.length>0}
    public Stack(int capacity) {
        this.content = (X[]) new Object[capacity];
        this.topIx = -1;
    }
    @pre {capacity>0}
    
    @post {content.length=oldcontent.length+1}
    public X top() {
        return this.content[this.topIx];
    }
    @pre {content!=null}        
    
    @post {topIx=oldtopIx-1 && content.length=oldcontent.length}
    public X pop() {
        X res = this.content[this.topIx];
        this.topIx--;
        return res;       
    }
    @pre {content.length>0}
    
    @post {content.length=oldcontent.length+1}
    public void push(X x) {
        this.topIx++;
        this.content[this.topIx] = x;
    }
    @pre {content!=null}
    
    @post {topIx=-1 && content!=null}
    public boolean isEmpty() {
        return this.topIx == -1;
    }
    @pre {content!=null}
    
    @post {topIx=content.length-1 && content!=null}
    public boolean isFull() {
        return this.topIx == (this.content.length - 1);
    }
    @pre {content.length>0}

    public static void main(String[] args) {
        // some tests
        Stack<String> stack = new Stack<String>(2);
        System.out.println(stack.isEmpty());
        System.out.println(stack.isFull());
        stack.push("1");
        stack.push("2");
        System.out.println(stack.isEmpty());
        System.out.println(stack.isFull());
        System.out.println(stack.pop());
        stack.push("3");
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
        System.out.println(stack.isFull());
        stack.push("4");
        System.out.println(stack.pop());       
    }
}
a)

interface Map<K,V> {

        @post {key!=null && key is element in Map}
        boolean containsKey(K key);
        @pre {key!=null}

        @post {key!=null && (containsKey(key) OR return null)}
        V get(K key);
        @pre {key!=null}

        @post {containsKey(key)}
        void put(K key, V value);
        @pre {key!=null}

}

Other related posts:

  • » [infostudents] SWT Blatt 4: A1, A2 a)