From bbb5a689edc9d8a6d5f4044e8c5b6963644c924a Mon Sep 17 00:00:00 2001 From: deebrecke Date: Wed, 8 Mar 2023 14:19:29 -0800 Subject: [PATCH 1/4] Cloned project and testing git --- src/edu/greenriver/sdev333/MathSet.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/edu/greenriver/sdev333/MathSet.java b/src/edu/greenriver/sdev333/MathSet.java index 4273aba..e5b24fa 100644 --- a/src/edu/greenriver/sdev333/MathSet.java +++ b/src/edu/greenriver/sdev333/MathSet.java @@ -1,6 +1,8 @@ package edu.greenriver.sdev333; /** + * Dee Brecke + * 3/8/23 * A MathSet represents a finite mathematical set. * Sets have a collection of unique elements (keys) - no duplicate keys allowed. * Set operations include contains, size, union, intersection, and difference. From 0f548d68ea6c41e5cce68712768cbe4f3ffbb16a Mon Sep 17 00:00:00 2001 From: deebrecke Date: Wed, 8 Mar 2023 15:34:58 -0800 Subject: [PATCH 2/4] Created BSTSet.java and HashSet.java. Added methods to BSTSet --- .idea/vcs.xml | 6 + src/Main.java | 1 + src/edu/greenriver/sdev333/BSTSet.java | 198 ++++++++++++++++++++++++ src/edu/greenriver/sdev333/HashSet.java | 97 ++++++++++++ src/edu/greenriver/sdev333/Queue.java | 2 + 5 files changed, 304 insertions(+) create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/BSTSet.java create mode 100644 src/edu/greenriver/sdev333/HashSet.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 3e59c38..3e44342 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,6 @@ public class Main { public static void main(String[] args) { System.out.println("Hello world!"); + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java new file mode 100644 index 0000000..67a5e51 --- /dev/null +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -0,0 +1,198 @@ +package edu.greenriver.sdev333; +import java.util.Iterator; +public class BSTSet> implements MathSet{ + + private Node root; + + private class Node{ + private KeyType key; + private Node left; + private Node right; + private int count; + + public Node(KeyType key, int count){ + this.key = key; + this.count = count; + } + } + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + root = put(root, key); + } + + private Node put(Node current, KeyType key){ + if(current==null){ + return new Node(key, 1); + } + int compare = key.compareTo(current.key); + if(compare > 0){ + current.left = put(current.left, key); + } else if (compare < 0) { + current.right = put(current.right, key); + }else{ + current.key = key; + } + current.count = size(current.left) + size(current.right) + 1; + return current; + } + + /** + * Is the key in the set? + * + * @param key key to check + * @return true if key is in the set, false otherwise + * + */ + @Override + public boolean contains(KeyType key) { + return get(key) !=null; + } + + public KeyType get(KeyType key) { + return get(root, key); + } + private KeyType get(Node current, KeyType key){ + if(current==null){ + return null; + } + int compare = key.compareTo(current.key); + if(compare < 0){ + //go left + return get(current.left, key); + }else if(compare> 0){ + return get(current.right, key); + }else { + return current.key; + } + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return size() == 0; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return size(root); + } + + private int size(Node current){ + if(current == null){ + return 0; + }else { + return current.count; + } + } + + /** + * Determine the union of this set with another specified set. + * Returns A union B, where A is this set, B is other set. + * A union B = {key | A.contains(key) OR B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to union + * @return the union of this set with other + */ + @Override + public MathSet union(MathSet other) { + MathSet result = new BSTSet(); + //walk through this and see if they are in other if not put in result + //for (item: this) { + Iterator itr = (Iterator) this.keys(); + while (itr.hasNext()){ + KeyType currentKey = itr.next(); + result.add(currentKey); + } + return result; + } + + /** + * Determine the intersection of this set with another specified set. + * Returns A intersect B, where A is this set, B is other set. + * A intersect B = {key | A.contains(key) AND B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to intersect + * @return the intersection of this set with other + */ + @Override + public MathSet intersection(MathSet other) { + MathSet result = new BSTSet(); + //walk through this and see if they are in other if not put in result + //for (item: this) { + Iterator itr = (Iterator) this.keys(); + while (itr.hasNext()){ + KeyType currentKey = itr.next(); + if(other.contains(currentKey)){ + result.add(currentKey); + } + } + return result; + } + + /** + * Determine the difference of this set with another specified set. + * Returns A difference B, where A is this set, B is other set. + * A difference B = {key | A.contains(key) AND !B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to difference + * @return the difference of this set with other + */ + @Override + public MathSet difference(MathSet other) { + MathSet result = new BSTSet(); + //walk through this and see if they are in other if not put in result + //for (item: this) { + Iterator itr = (Iterator) this.keys(); + while (itr.hasNext()){ + KeyType currentKey = itr.next(); + if(!other.contains(currentKey)){ + result.add(currentKey); + } + } + return result; + } + + /** + * Retrieves a collection of all the keys in this set. + * + * @return a collection of all keys in this set + * + * carry over from SymbolTables/BST.java + */ + @Override + public Iterable keys() { + //empty queue to hold results + Queue queue = new Queue<>(); + //start the recursion collecting results in teh queue + inOrder(root, queue); + return queue; + } + + private void inOrder(Node current, Queue q){ + if(current == null){ + //do nothing -- just exit + return; + } + inOrder(current.left, q); + q.enqueue(current.key); + inOrder(current.right, q); + } +} diff --git a/src/edu/greenriver/sdev333/HashSet.java b/src/edu/greenriver/sdev333/HashSet.java new file mode 100644 index 0000000..cec760c --- /dev/null +++ b/src/edu/greenriver/sdev333/HashSet.java @@ -0,0 +1,97 @@ +package edu.greenriver.sdev333; + +public class HashSet implements MathSet{ + + /** + * Puts the specified key into the set. + * + * @param key key to be added into the set + */ + @Override + public void add(KeyType key) { + + } + + /** + * Is the key in the set? + * + * @param key key to check + * @return true if key is in the set, false otherwise + */ + @Override + public boolean contains(KeyType key) { + return false; + } + + /** + * Is the set empty? + * + * @return true if the set is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Number of keys in the set + * + * @return number of keys in the set. + */ + @Override + public int size() { + return 0; + } + + /** + * Determine the union of this set with another specified set. + * Returns A union B, where A is this set, B is other set. + * A union B = {key | A.contains(key) OR B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to union + * @return the union of this set with other + */ + @Override + public MathSet union(MathSet other) { + return null; + } + + /** + * Determine the intersection of this set with another specified set. + * Returns A intersect B, where A is this set, B is other set. + * A intersect B = {key | A.contains(key) AND B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to intersect + * @return the intersection of this set with other + */ + @Override + public MathSet intersection(MathSet other) { + return null; + } + + /** + * Determine the difference of this set with another specified set. + * Returns A difference B, where A is this set, B is other set. + * A difference B = {key | A.contains(key) AND !B.contains(key)}. + * Does not change the contents of this set or the other set. + * + * @param other specified set to difference + * @return the difference of this set with other + */ + @Override + public MathSet difference(MathSet other) { + return null; + } + + /** + * Retrieves a collection of all the keys in this set. + * + * @return a collection of all keys in this set + */ + @Override + public Iterable keys() { + return null; + } +} diff --git a/src/edu/greenriver/sdev333/Queue.java b/src/edu/greenriver/sdev333/Queue.java index 638d71c..143e50f 100644 --- a/src/edu/greenriver/sdev333/Queue.java +++ b/src/edu/greenriver/sdev333/Queue.java @@ -3,6 +3,8 @@ import java.util.Iterator; /** + * Dee Brecke + * 3/8/23 * FIFO queue, page 151 of the red book */ public class Queue implements Iterable { From ae3a501cf02290c22525b218d180ad53d2b1d4bf Mon Sep 17 00:00:00 2001 From: deebrecke Date: Wed, 8 Mar 2023 16:01:06 -0800 Subject: [PATCH 3/4] Tested methods, some don't work, some do --- src/Main.java | 33 +++++++++++++++++++++++++- src/edu/greenriver/sdev333/BSTSet.java | 16 +++++++------ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/Main.java b/src/Main.java index 3e44342..caf5d6a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,37 @@ +import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.MathSet; + +import java.util.Scanner; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + String inputString = "S E A R C H E X A M P L E"; + String newString = "O T H E R S E T"; + + Scanner input = new Scanner(inputString); + Scanner otherInput = new Scanner(newString); + + MathSet set = new BSTSet<>(); + MathSet otherSet = new BSTSet<>(); + + while (input.hasNext()) { + String key = input.next(); + set.add(key); + } + + while (otherInput.hasNext()) { + String key = otherInput.next(); + otherSet.add(key); + } + + System.out.println(set.contains("E"));//should be true so something is wrong with my contains method + System.out.println(set.isEmpty());//false as it should be + System.out.println(set.size());//10 + System.out.println(otherSet.isEmpty());//false as it should be + System.out.println(otherSet.size());//6--correct + //System.out.println(set.intersection(otherSet));//ClassCastException error + //System.out.println(set.union(otherSet));//ClassCastException error + //System.out.println(set.difference(otherSet));/ClassCastException error } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java index 67a5e51..8d1d196 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -57,6 +57,7 @@ public boolean contains(KeyType key) { public KeyType get(KeyType key) { return get(root, key); } + private KeyType get(Node current, KeyType key){ if(current==null){ return null; @@ -136,13 +137,14 @@ public MathSet intersection(MathSet other) { MathSet result = new BSTSet(); //walk through this and see if they are in other if not put in result //for (item: this) { - Iterator itr = (Iterator) this.keys(); - while (itr.hasNext()){ - KeyType currentKey = itr.next(); - if(other.contains(currentKey)){ - result.add(currentKey); - } - } + +// Iterator itr = (Iterator) this.keys(); +// while (itr.hasNext()){ +// KeyType currentKey = itr.next(); +// if(other.contains(currentKey)){ +// result.add(currentKey); +// } +// } return result; } From 8235503a004fbeb57da1be6435ade9edab57d519 Mon Sep 17 00:00:00 2001 From: deebrecke Date: Tue, 14 Mar 2023 10:19:21 -0700 Subject: [PATCH 4/4] Final version: BSTSet, HashSet complete. FlightRoutesGraph written as demoed in class. --- src/Main.java | 22 +++- src/edu/greenriver/sdev333/BSTSet.java | 77 +++++++++---- .../greenriver/sdev333/FlightRoutesGraph.java | 109 ++++++++++++++++++ src/edu/greenriver/sdev333/HashSet.java | 105 +++++++++++++++-- .../sdev333/SequentialSearchSet.java | 72 ++++++++++++ 5 files changed, 347 insertions(+), 38 deletions(-) create mode 100644 src/edu/greenriver/sdev333/FlightRoutesGraph.java create mode 100644 src/edu/greenriver/sdev333/SequentialSearchSet.java diff --git a/src/Main.java b/src/Main.java index caf5d6a..3f563a5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,5 @@ import edu.greenriver.sdev333.BSTSet; +import edu.greenriver.sdev333.HashSet; import edu.greenriver.sdev333.MathSet; import java.util.Scanner; @@ -11,27 +12,36 @@ public static void main(String[] args) { Scanner input = new Scanner(inputString); Scanner otherInput = new Scanner(newString); - MathSet set = new BSTSet<>(); - MathSet otherSet = new BSTSet<>(); + //This is currently being used to test HashSet, but can be changed to BSTSet to test that class + MathSet set = new HashSet<>(); + MathSet otherSet = new HashSet<>(); while (input.hasNext()) { String key = input.next(); set.add(key); } + //BST should print out in order A C E H L M P R S X + System.out.println("Set " + set);// Hash: in no particular order: S E A R C H X M P L + while (otherInput.hasNext()) { String key = otherInput.next(); otherSet.add(key); } - System.out.println(set.contains("E"));//should be true so something is wrong with my contains method + System.out.println("other Set = " + otherSet);// O T H E R S + + System.out.println(set.contains("E"));//should be true System.out.println(set.isEmpty());//false as it should be System.out.println(set.size());//10 System.out.println(otherSet.isEmpty());//false as it should be System.out.println(otherSet.size());//6--correct - //System.out.println(set.intersection(otherSet));//ClassCastException error - //System.out.println(set.union(otherSet));//ClassCastException error - //System.out.println(set.difference(otherSet));/ClassCastException error + //These should print in order for BST, not for Hash but still contain the same letters + System.out.println("intersection " + set.intersection(otherSet));//should be E H R S + System.out.println("union " + set.union(otherSet));// should be A C E H L M O P R S T X + System.out.println("difference " + set.difference(otherSet)); // A C L M P X + System.out.println("other difference " + otherSet.difference(set)); //O T + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/BSTSet.java b/src/edu/greenriver/sdev333/BSTSet.java index 8d1d196..1e00908 100644 --- a/src/edu/greenriver/sdev333/BSTSet.java +++ b/src/edu/greenriver/sdev333/BSTSet.java @@ -1,5 +1,15 @@ package edu.greenriver.sdev333; -import java.util.Iterator; + +/** + * Binary Search Tree Set Class + * + * * Author: Dee Brecke + * * This class uses a binary search tree set to process data and compare two sets + * * using math methods of intersect, union and difference + * * The iterator uses a queue to store data + * + * @param comparable data type to be compared + */ public class BSTSet> implements MathSet{ private Node root; @@ -31,12 +41,13 @@ private Node put(Node current, KeyType key){ return new Node(key, 1); } int compare = key.compareTo(current.key); - if(compare > 0){ + if(compare < 0){ current.left = put(current.left, key); - } else if (compare < 0) { + } else if (compare > 0) { current.right = put(current.right, key); - }else{ - current.key = key; + }else{//this is unnecessary code but a good reminder that if the key is there, no need to do anything + //current.key = key; + //do nothing } current.count = size(current.left) + size(current.right) + 1; return current; @@ -54,10 +65,16 @@ public boolean contains(KeyType key) { return get(key) !=null; } + /** + * getter used in other methods to check if a given key is in the set + * @param key + * @return key if it is in the set + */ public KeyType get(KeyType key) { return get(root, key); } + //private helper method for getter private KeyType get(Node current, KeyType key){ if(current==null){ return null; @@ -66,7 +83,7 @@ private KeyType get(Node current, KeyType key){ if(compare < 0){ //go left return get(current.left, key); - }else if(compare> 0){ + }else if(compare > 0){ return get(current.right, key); }else { return current.key; @@ -93,6 +110,7 @@ public int size() { return size(root); } + //helper method private int size(Node current){ if(current == null){ return 0; @@ -113,11 +131,13 @@ private int size(Node current){ @Override public MathSet union(MathSet other) { MathSet result = new BSTSet(); - //walk through this and see if they are in other if not put in result - //for (item: this) { - Iterator itr = (Iterator) this.keys(); - while (itr.hasNext()){ - KeyType currentKey = itr.next(); + //walk through this and put in result + for (KeyType currentKey: this.keys()) { + result.add(currentKey); + } + //then walk through other and put in result + //because it's a set, it won't add duplicates + for(KeyType currentKey : other.keys()){ result.add(currentKey); } return result; @@ -135,16 +155,12 @@ public MathSet union(MathSet other) { @Override public MathSet intersection(MathSet other) { MathSet result = new BSTSet(); - //walk through this and see if they are in other if not put in result - //for (item: this) { - -// Iterator itr = (Iterator) this.keys(); -// while (itr.hasNext()){ -// KeyType currentKey = itr.next(); -// if(other.contains(currentKey)){ -// result.add(currentKey); -// } -// } + //walk through this and see if they are in other if so put in result + for (KeyType currentKey: this.keys()) { + if(other.contains(currentKey)){ + result.add(currentKey); + } + } return result; } @@ -161,10 +177,7 @@ public MathSet intersection(MathSet other) { public MathSet difference(MathSet other) { MathSet result = new BSTSet(); //walk through this and see if they are in other if not put in result - //for (item: this) { - Iterator itr = (Iterator) this.keys(); - while (itr.hasNext()){ - KeyType currentKey = itr.next(); + for (KeyType currentKey: this.keys()) { if(!other.contains(currentKey)){ result.add(currentKey); } @@ -188,6 +201,7 @@ public Iterable keys() { return queue; } + //helper method for iterator private void inOrder(Node current, Queue q){ if(current == null){ //do nothing -- just exit @@ -197,4 +211,17 @@ private void inOrder(Node current, Queue q){ q.enqueue(current.key); inOrder(current.right, q); } + + /** + * Method to print out results as a string instead of an address + * useful for testing + * @return string version of data in set + */ + public String toString(){ + String output =""; + for(KeyType key: keys()){ + output += key + ", "; + } + return output; + } } diff --git a/src/edu/greenriver/sdev333/FlightRoutesGraph.java b/src/edu/greenriver/sdev333/FlightRoutesGraph.java new file mode 100644 index 0000000..3830ee0 --- /dev/null +++ b/src/edu/greenriver/sdev333/FlightRoutesGraph.java @@ -0,0 +1,109 @@ +package edu.greenriver.sdev333; + +/** + * Author: Dee Brecke (demoed by Kendrick Hang) + * This class represents a basic graph of airports connected + * by flight paths. + * It is an introductory class which only contains a way to build + * the graph and find the nodes directly connected. We will be + * going into greater depth (or breadth, lol) on this concept next quarter + */ +public class FlightRoutesGraph { + //two sets need to model a graph (network) + //1. a set of vertices (points, nodes) --airports + //2. a set of edges (connections, lines, routes, relationships)--route between airports + + //helper class + private class Edge{ + private String node1; + private String node2; + + public Edge(String from, String to){ + node1 = from; + node2 = to; + } + } + //airports + private MathSet nodes; + //fight paths + private MathSet edges; + + /** + * default constructor + */ + public FlightRoutesGraph(){ + nodes = new BSTSet<>(); //BST okay because strings are comparable + edges = new HashSet<>();//must use hash here because edges are not comparable + } + + /** + * add a node + * @param city airport node + */ + public void addNode(String city){ + nodes.add(city); + } + + /** + * add a route from one city to another + * @param city1 one of the connecting cities + * @param city2 the other connecting city + */ + public void addEdge(String city1, String city2){ + Edge connection = new Edge(city1, city2); + edges.add(connection); + } + + /** + * find all cities that are connected to given city via an edge (flight path) + * @param city airport to check + * @return airports directly connected to airport to check + */ + public MathSet getNeighbors(String city){ + //create an empty set to hold the results + MathSet neighbors = new BSTSet<>(); + //loop through edges and check + //is city either in node1 or node2? + for(Edge e : edges.keys()){ + if (e.node1.equals(city)) { + neighbors.add(e.node2); + } else if (e.node2.equals(city)) { + neighbors.add((e.node1)); + } + } + return neighbors; + } + + //explore graph with BFS--breadth first search + //or with DFS--depth first search (use recursion or stack) + //shortest path algorithm + + /** + * Tester method (to keep Main class clean for testing BSTSet and HashSet only) + * @param args + */ + public static void main(String[] args){ + FlightRoutesGraph g = new FlightRoutesGraph(); + + //add all cities first + g.addNode("JFK"); + g.addNode("ORD"); + g.addNode("ATL"); + g.addNode("MCO"); + g.addNode("SEA"); + g.addNode("DEN"); + + //add connections between cities + g.addEdge("JFK","MCO"); + g.addEdge("ATL","MCO"); + g.addEdge("DEN","ORD"); + g.addEdge("ORD","ATL"); + g.addEdge("SEA","DEN"); + + //look for direct flights from MCO + MathSet directFromMCO = g.getNeighbors("MCO"); + MathSet directFromATL = g.getNeighbors(("ATL")); + System.out.println("Direct flights from MCO: " + directFromMCO); + System.out.println("Direct flights from ATL: " + directFromATL); + } +} diff --git a/src/edu/greenriver/sdev333/HashSet.java b/src/edu/greenriver/sdev333/HashSet.java index cec760c..03df2be 100644 --- a/src/edu/greenriver/sdev333/HashSet.java +++ b/src/edu/greenriver/sdev333/HashSet.java @@ -1,7 +1,39 @@ package edu.greenriver.sdev333; +/** + * Hash Set class + * + * Author: Dee Brecke + * This class uses a hashset to process data and compare two sets + * using math methods of intersect, union and difference + * The constructor uses Sequential Search Set (code borrowed from SequentialSearchST + * from a previoous project) and iterator uses a queue to store data + * @param Any data type to be added to the set + */ public class HashSet implements MathSet{ + private SequentialSearchSet[] listArray; //referred to as st in previous project + private int buckets; //referred to as M in previous project and book + + //default constructor + public HashSet(){ + this(1000); + } + + //parameterized constructor (Code from previous project used variables "M" and "st" but I think these are more descriptive + public HashSet(int buckets){ + this.buckets = buckets; + listArray = new SequentialSearchSet[buckets]; + + for(int i = 0; i < buckets; i++){ + listArray[i] = new SequentialSearchSet<>(); + } + } + + private int hash(KeyType key){ + return (key.hashCode() & 0x7fffffff) % buckets; + } + /** * Puts the specified key into the set. * @@ -9,7 +41,19 @@ public class HashSet implements MathSet{ */ @Override public void add(KeyType key) { + int index = hash(key); + listArray[index].put(key); + } + /** + * accessor method used in other methods + * @param key item to be checked + * @return item if it is in the set + */ + public KeyType get(KeyType key) { + int index = hash(key); //find array index/bucket + //check in the bucket to see if it's there + return listArray[index].get(key); } /** @@ -20,7 +64,7 @@ public void add(KeyType key) { */ @Override public boolean contains(KeyType key) { - return false; + return get(key) !=null; } /** @@ -30,7 +74,7 @@ public boolean contains(KeyType key) { */ @Override public boolean isEmpty() { - return false; + return size() == 0; } /** @@ -40,7 +84,11 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + int sum = 0; + for (int i = 0; i < buckets; i++) { + sum+= listArray[i].size(); + } + return sum; } /** @@ -54,7 +102,17 @@ public int size() { */ @Override public MathSet union(MathSet other) { - return null; + MathSet result = new HashSet(buckets); + //walk through this and put in result + for (KeyType currentKey: this.keys()) { + result.add(currentKey); + } + //then walk through other and put in result + //because it's a set, it won't add duplicates + for(KeyType currentKey : other.keys()){ + result.add(currentKey); + } + return result; } /** @@ -68,7 +126,14 @@ public MathSet union(MathSet other) { */ @Override public MathSet intersection(MathSet other) { - return null; + MathSet result = new HashSet(buckets); + //walk through this and see if they are in other if so put in result + for (KeyType currentKey: this.keys()) { + if(other.contains(currentKey)){ + result.add(currentKey); + } + } + return result; } /** @@ -82,7 +147,14 @@ public MathSet intersection(MathSet other) { */ @Override public MathSet difference(MathSet other) { - return null; + MathSet result = new HashSet(buckets); + //walk through this and see if they are in other if not put in result + for (KeyType currentKey: this.keys()) { + if(!other.contains(currentKey)){ + result.add(currentKey); + } + } + return result; } /** @@ -92,6 +164,25 @@ public MathSet difference(MathSet other) { */ @Override public Iterable keys() { - return null; + Queue collector = new Queue<>(); + for (int i = 0; i < buckets; i++) { + for(KeyType key : listArray[i].keys()){ + collector.enqueue(key); + } + } + return collector; + } + + /** + * Method to print out results as a string instead of an address + * useful for testing + * @return string version of data in set + */ + public String toString(){ + String output =""; + for(KeyType key: keys()){ + output += key + ", "; + } + return output; } } diff --git a/src/edu/greenriver/sdev333/SequentialSearchSet.java b/src/edu/greenriver/sdev333/SequentialSearchSet.java new file mode 100644 index 0000000..7fc7549 --- /dev/null +++ b/src/edu/greenriver/sdev333/SequentialSearchSet.java @@ -0,0 +1,72 @@ +package edu.greenriver.sdev333; + +public class SequentialSearchSet { + private Node first; //we usually call this head but the book uses first so that's what I did this time + + private int count; + private class Node{ + KeyType key; + Node next; + + /** + * Constructor for Nodes being used int this class + * @param key key + * @param next the next node in the linked list + */ + public Node(KeyType key, Node next){ + this.key = key; + this.next = next; + } + } + + /** + * Given a key value pair, add the node to the structure + * @param key key + */ + public void put(KeyType key) { + for(Node current = first; current!=null; current = current.next){ + if(key.equals(current.key)){ + return; + } + }first=new Node(key, first); + count++; + } + + /** + * Find the value at a given key + * @param key key + * @return value at given key + */ + + public KeyType get(KeyType key) { + for(Node current = first; current !=null; current = current.next){//the book uses x instead of current + if(key.equals(current.key)){ + return current.key; + } + } + return null; + } + + /** + * This method counts how many nodes are in the structure + * @return integer number of nodes in structure + */ + public int size() { + return count; + } + + /** + * iterator to go through the structure and put the nodes into a queue + * @return queue of nodes + */ + + public Iterable keys() { + Queue queue = new Queue<>(); + Node current = first; + while (current !=null){ + queue.enqueue(current.key); + current = current.next; + } + return queue; + } +}