aboutsummaryrefslogtreecommitdiffstats
path: root/OLD/csci1913/Java/lab10_strap012.java
diff options
context:
space:
mode:
authorRossTheRoss <msattr@gmail.com>2020-02-09 08:46:52 -0600
committerRossTheRoss <msattr@gmail.com>2020-02-09 08:46:52 -0600
commitbd1d350c376b45e334697851e29b2f79fee0c956 (patch)
treeeb242032a64442caef7a35434400eb0818911e9a /OLD/csci1913/Java/lab10_strap012.java
parentDo micro prelab (diff)
parentRename file (diff)
downloadhomework-bd1d350c376b45e334697851e29b2f79fee0c956.tar
homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.gz
homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.bz2
homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.lz
homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.xz
homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.zst
homework-bd1d350c376b45e334697851e29b2f79fee0c956.zip
Merge branch 'master' of github.com:RosstheRoss/TestingFun
Diffstat (limited to 'OLD/csci1913/Java/lab10_strap012.java')
-rw-r--r--OLD/csci1913/Java/lab10_strap012.java148
1 files changed, 148 insertions, 0 deletions
diff --git a/OLD/csci1913/Java/lab10_strap012.java b/OLD/csci1913/Java/lab10_strap012.java
new file mode 100644
index 0000000..06c0dfa
--- /dev/null
+++ b/OLD/csci1913/Java/lab10_strap012.java
@@ -0,0 +1,148 @@
+class AssociationList<Key, Value> {
+ private class Node {
+ private Key key;
+ private Value value;
+ private Node next;
+ private Node(Key k, Value v, Node n) {
+ key=k; value=v; next=n;
+ }
+ }
+ private Node head;
+ public AssociationList() {
+ head = new Node(null, null, null);
+ }
+ public void delete(Key key) {
+ Node left = head;
+ Node right = head.next;
+ while (right!=null) {
+ if (isEqual(right.key, key)) {
+ left.next=right.next;
+ break;
+ } else {
+ left=right; right=right.next;
+ }
+ }
+
+ }
+ public Value get(Key key) {
+ Node where=head.next;
+ while (where!=null) {
+ if (isEqual(where.key,key)) {
+ return where.value;
+ } else {
+ where=where.next;
+ }
+ } //No matching key found
+ throw new IllegalArgumentException();
+ }
+ private boolean isEqual(Key leftKey, Key rightKey) {
+ if (leftKey == null || rightKey == null) {
+ return leftKey == rightKey;
+ } else {
+ return leftKey.equals(rightKey);
+ }
+ }
+ public boolean isIn(Key key) {
+ Node where = head.next;
+ while (where != null) {
+ if (isEqual(where.key, key)) {
+ return true;
+ } else {
+ where = where.next;
+ }
+ } // No matching key found
+ return false;
+ }
+ public void put(Key key, Value value) {
+ Node where=head.next;
+ boolean found=false;
+ while (where!=null){
+ if(isEqual(key, where.key)) {
+ where.value=value;
+ found=true;
+ break;
+ } else {
+ where=where.next;
+ }
+ } //End of while
+ if (!found) {
+ Node temp=head.next;
+ head.next= new Node(key, value, temp);
+ }
+ }
+}
+
+//
+// Tests for CSci 1913 Lab 10
+// James Moen
+// 08 Apr 19
+//
+// The TRY-CATCH statements catch exceptions thrown by ASSOCIATION LIST's
+// methods, so that the program can continue to run even if a method fails.
+//
+// Each test has a comment that shows what it should print and how many points
+// it is worth, for a total of 40 points.
+
+// HOGWARTS. The Hogwarts dating service.
+
+class Hogwarts {
+
+ // MAIN. Make an instance of ASSOCIATION LIST and test it.
+
+ public static void main(String[] args) {
+ AssociationList<String, String> list = new AssociationList<String, String>();
+
+ System.out.println(list.isIn(null)); // false 2 points.
+
+ try {
+ System.out.println(list.get(null));
+ } catch (IllegalArgumentException ignore) {
+ System.out.println("No null"); // No null 2 points.
+ }
+
+ list.put(null, "Wormtail");
+ list.put("Ron", "Lavender");
+ list.put("Voldemort", null);
+ list.put("Dean", "Ginny");
+
+ System.out.println(list.isIn("Dean")); // true 2 points.
+ System.out.println(list.isIn("Ginny")); // false 2 points.
+ System.out.println(list.isIn("Ron")); // true 2 points.
+ System.out.println(list.isIn("Voldemort")); // true 2 points.
+ System.out.println(list.isIn(null)); // true 2 points.
+ System.out.println(list.isIn("Joanne")); // false 2 points.
+
+ System.out.println(list.get("Ron")); // Lavender 2 points.
+ System.out.println(list.get("Dean")); // Ginny 2 points.
+ System.out.println(list.get("Voldemort")); // null 2 points.
+ System.out.println(list.get(null)); // Wormtail 2 points.
+
+ try {
+ System.out.println(list.get("Joanne"));
+ } catch (IllegalArgumentException ignore) {
+ System.out.println("No Joanne"); // No Joanne 2 points.
+ }
+
+ list.delete(null);
+
+ System.out.println(list.isIn(null)); // false 2 points.
+
+ list.put(null, null);
+ list.put("Harry", "Ginny");
+ list.put("Ron", "Hermione");
+
+ System.out.println(list.isIn(null)); // true 2 points.
+ System.out.println(list.get(null)); // null 2 points.
+ System.out.println(list.get("Harry")); // Ginny 2 points.
+ System.out.println(list.get("Dean")); // Ginny 2 points.
+ System.out.println(list.get("Ron")); // Hermione 2 points.
+
+ list.delete("Dean");
+
+ try {
+ System.out.println(list.get("Dean"));
+ } catch (IllegalArgumentException ignore) {
+ System.out.println("No Dean"); // No Dean 2 points.
+ }
+ }
+} \ No newline at end of file