01: package U2.T2.examples;
02:
03: import java.util.*;
04:
05: /**
06: * A class implementing a sorted list. The method insert is faulty.
07: */
08: public class SortedList {
09:
10: private LinkedList<Comparable> s;
11: private Comparable max;
12:
13: public String aux_laststep = "";
14: private int aux_delta = 0;
15:
16: public SortedList() {
17: s = new LinkedList<Comparable>();
18: }
19:
20: private boolean appmodel() {
21: if (aux_laststep.equals("insert"))
22: aux_delta++;
23: if (aux_laststep.equals("get"))
24: aux_delta--;
25: return aux_delta >= 0;
26: }
27:
28: public boolean classinv() {
29: assert appmodel() : "APPMODEL";
30: return s.isEmpty() || s.contains(max);
31: }
32:
33: public void insert(Comparable x) {
34:
35: int i = 0;
36: for (Comparable y : s) {
37: if (y.compareTo(x) > 0)
38: break;
39: i++;
40: }
41: s.add(i, x);
42: // i = i/0 ;
43:
44: // injecting error:
45: if (max == null || x.compareTo(max) < 0)
46: max = x;
47: }
48:
49: public Comparable get() {
50: Comparable x = max;
51: s.remove(max);
52: if (s.isEmpty())
53: max = null;
54: else
55: max = s.getLast();
56: return x;
57: }
58:
59: public Comparable get_spec() {
60:
61: assert !s.isEmpty() : "PRE";
62:
63: Comparable ret = get();
64:
65: assert s.isEmpty() || ret.compareTo(s.getLast()) >= 0 : "POST";
66:
67: return ret;
68: }
69: }
|