diff options
author | RossTheRoss <msattr@gmail.com> | 2019-12-05 10:46:37 -0600 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-12-05 10:46:37 -0600 |
commit | ffa046bfa59797e6d5a4e093899a2b149f831108 (patch) | |
tree | 0f6cbeb86d89cb8d57b9db3db04efb11d0a3fe39 /csci1913/Java | |
parent | End me (diff) | |
download | homework-ffa046bfa59797e6d5a4e093899a2b149f831108.tar homework-ffa046bfa59797e6d5a4e093899a2b149f831108.tar.gz homework-ffa046bfa59797e6d5a4e093899a2b149f831108.tar.bz2 homework-ffa046bfa59797e6d5a4e093899a2b149f831108.tar.lz homework-ffa046bfa59797e6d5a4e093899a2b149f831108.tar.xz homework-ffa046bfa59797e6d5a4e093899a2b149f831108.tar.zst homework-ffa046bfa59797e6d5a4e093899a2b149f831108.zip |
Start stuff
Diffstat (limited to '')
-rw-r--r-- | csci1913/Java/lab12_strap012.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/csci1913/Java/lab12_strap012.java b/csci1913/Java/lab12_strap012.java new file mode 100644 index 0000000..b616111 --- /dev/null +++ b/csci1913/Java/lab12_strap012.java @@ -0,0 +1,86 @@ +class PriorityQueue<Base> { + private class Node { + private Base object; + private int rank; + private Node left; + private Node right; + private Node(Base object, int rank) { + this.object = object; + this.rank = rank; + left = null; + right = null; + } + } + private Node root; // Root node of the BST. + public PriorityQueue() { + root = new Node(null, -1); + } //root is the One God + public boolean isEmpty() { + return root.right==root.left; + } + public Base dequeue() { + if (isEmpty()) { + throw new IllegalStateException(); + } else { + Node Test = root; + + return Test.object; + } + } + // Unlike the BST’s discussed in the lectures, the nodes in + //each left subtree have ranks less than or equal to the + // rank at the root. The nodes in each right subtree have + // ranks greater than the rank at the root. + // This allows two or more nodes to have the same rank. + public void enqueue(Base object, int rank) { + if (rank < 0) { + throw new IllegalArgumentException(); + } else { + + } + } + +} +// SNOBBERY. How the aristocracy behaves in a queue. 20 points. + +class Snobbery { + + // MAIN. Queue them up. + + public static void main(String[] args) { + PriorityQueue<String> queue = new PriorityQueue<String>(); + + System.out.println(queue.isEmpty()); // true 2 points + + try { + System.out.println(queue.dequeue()); + } catch (IllegalStateException ignore) { + System.out.println("Blimey!"); // Blimey! 2 points + } + + queue.enqueue("Lancelot", 5); + queue.enqueue("Fawlty", 7); + queue.enqueue("Elizabeth", 0); + queue.enqueue("Charles", 1); + queue.enqueue("Turing", 7); + + try { + queue.enqueue("Zeus", -100); + } catch (IllegalArgumentException ignore) { + System.out.println("No gods!"); // No gods! 2 points + } + + System.out.println(queue.isEmpty()); // false 2 points + + System.out.println(queue.dequeue()); // Elizabeth 2 points + System.out.println(queue.dequeue()); // Charles 2 points + System.out.println(queue.dequeue()); // Lancelot 2 points + System.out.println(queue.dequeue()); // Turing 2 points + System.out.println(queue.dequeue()); // Fawlty 2 points + + // It's OK if Fawlty comes out before Turing, but both must come out last. + + System.out.println(queue.isEmpty()); // true 2 points. + } + +} |