aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913/Java
diff options
context:
space:
mode:
authorRossTheRoss <mstrapp@protonmail.com>2019-11-06 17:29:49 -0600
committerRossTheRoss <mstrapp@protonmail.com>2019-11-06 17:29:49 -0600
commite768e3c0d204233bdb6b72e23765f21acbd34176 (patch)
tree8392d13f4642fa747a496ab4203caf5ac7c3f542 /csci1913/Java
parentDon't commit in matlab kiddos (diff)
downloadhomework-e768e3c0d204233bdb6b72e23765f21acbd34176.tar
homework-e768e3c0d204233bdb6b72e23765f21acbd34176.tar.gz
homework-e768e3c0d204233bdb6b72e23765f21acbd34176.tar.bz2
homework-e768e3c0d204233bdb6b72e23765f21acbd34176.tar.lz
homework-e768e3c0d204233bdb6b72e23765f21acbd34176.tar.xz
homework-e768e3c0d204233bdb6b72e23765f21acbd34176.tar.zst
homework-e768e3c0d204233bdb6b72e23765f21acbd34176.zip
Finish lab
Diffstat (limited to 'csci1913/Java')
-rw-r--r--csci1913/Java/lab8_strap012.java167
1 files changed, 167 insertions, 0 deletions
diff --git a/csci1913/Java/lab8_strap012.java b/csci1913/Java/lab8_strap012.java
new file mode 100644
index 0000000..35f9106
--- /dev/null
+++ b/csci1913/Java/lab8_strap012.java
@@ -0,0 +1,167 @@
+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 {
+ boolean foo = b1.base.equals(b2);
+ return foo;
+ }
+ }
+ 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
+ }
+ }
+}