aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913/Java/lab8_strap012.java
blob: 5335f79cef3c99a042202669ad5fec4dca6618c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class RunnyStack<Base> {
    private int numRuns;
    private int numDepth;
    private Run top;
    //Constructors
    private class Run {
        private Base base;
        private int l;
        private Run next;
        private Run(Base base, Run next) {
            this.base=base;
            this.l=l+1;
            this.next=next;
        }
    }
    public RunnyStack() {
        top=null;
    }
    //Methods
    private boolean isEqual(Run b1, Base b2){
        if (b1 == null || b2 == null) {
            return b1 == b2;
        } else {
            return b1.base.equals(b2);
        }
    }
    public boolean isEmpty() {
        return top==null;
    }
    public int depth() {
        return numDepth;
    }
    public Base peek() {
        if (isEmpty()) {
            throw new IllegalStateException();
        } else {
            return top.base;
        }
    }
    public void pop() {
        if (isEmpty()) {
            throw new IllegalStateException();
        } else {
            top.l--;
            numDepth--;
            if (top.l == 0) {
                top=top.next;
                numRuns--;
            }
        }
    }
    public void push(Base base) {
        numDepth++;
        if (isEqual(top, base)) {
            top.l++;
        } else {
            numRuns++;
            top=new Run(base, top);
        }
    }
    public int runs() {
        return numRuns;
    }
   
}

//
//  Tests for CSci 1913 Lab 8
//  James Moen
//  20 Mar 17
//
//  The TRY-CATCH statements catch exceptions thrown by RUNNY STACK's methods,
//  so that the program can continue to run even if a method fails. We still
//  haven't talked about TRY-CATCH'es in the lectures yet.
//
//  Most tests have comments that show what they should print, and how many
//  points they are worth, for a total of 40 points.
//
//  Camembert is a soft French cheese. It may be runny. It can be stacked.
//

class Camembert {
    public static void main(String[] args) {
        RunnyStack<String> s = new RunnyStack<String>();

        System.out.println(s.isEmpty()); // true 1 point
        System.out.println(s.depth()); // 0 1 point
        System.out.println(s.runs()); // 0 1 point

        try {
            s.pop();
        } catch (IllegalStateException ignore) {
            System.out.println("No pop"); // No pop 1 point
        }

        try {
            System.out.println(s.peek());
        } catch (IllegalStateException ignore) {
            System.out.println("No peek"); // No peek 1 point
        }

        s.push("A");
        System.out.println(s.peek()); // A 1 point
        System.out.println(s.depth()); // 1 1 point
        System.out.println(s.runs()); // 1 1 point

        System.out.println(s.isEmpty()); // false 1 point

        s.push("B");
        System.out.println(s.peek()); // B 1 point
        System.out.println(s.depth()); // 2 1 point
        System.out.println(s.runs()); // 2 1 point

        s.push("B");
        System.out.println(s.peek()); // B 1 point
        System.out.println(s.depth()); // 3 1 point
        System.out.println(s.runs()); // 2 1 point

        s.push("B");
        System.out.println(s.peek()); // B 1 point
        System.out.println(s.depth()); // 4 1 point
        System.out.println(s.runs()); // 2 1 point

        s.push("C");
        System.out.println(s.peek()); // C 1 point
        System.out.println(s.depth()); // 5 1 point
        System.out.println(s.runs()); // 3 1 point

        s.push("C");
        System.out.println(s.peek()); // C 1 point
        System.out.println(s.depth()); // 6 1 point
        System.out.println(s.runs()); // 3 1 point

        s.pop();
        System.out.println(s.peek()); // C 1 point
        System.out.println(s.depth()); // 5 1 point
        System.out.println(s.runs()); // 3 1 point

        s.pop();
        System.out.println(s.peek()); // B 1 point
        System.out.println(s.depth()); // 4 1 point
        System.out.println(s.runs()); // 2 1 point

        s.pop();
        System.out.println(s.peek()); // B 1 point
        System.out.println(s.depth()); // 3 1 point
        System.out.println(s.runs()); // 2 1 point

        s.pop();
        s.pop();
        System.out.println(s.peek()); // A 1 point
        System.out.println(s.depth()); // 1 1 point
        System.out.println(s.runs()); // 1 1 point

        s.pop();
        System.out.println(s.isEmpty()); // true 1 point
        System.out.println(s.depth()); // 0 1 point
        System.out.println(s.runs()); // 0 1 point

        try {
            System.out.println(s.peek());
        } catch (IllegalStateException ignore) {
            System.out.println("No peek"); // No peek 1 point
        }
    }
}