Question: -Program for SetDictionary.java key is the item, value is a boolean set to true SetDictionary.java uses (instance of class TreeMap) CODE THAT NEEDS TO BE
-Program for SetDictionary.java
key is the item, value is a boolean set to true
SetDictionary.java uses (instance of class TreeMap)
CODE THAT NEEDS TO BE IMPLEMENTED
public class SetDictionary> implements SetInterface, Iterable
{ private TreeMap items; public SetDictionary() { // TODO Project 1 } // end default constructor public boolean add(K newEntry) { // TODO Project 1 return false; } // end add public boolean remove(K anEntry) { // TODO Project 1 return false; } // end remove public void clear() { // TODO Project 1 } // end clear public boolean contains(K anEntry) { // TODO Project 1 return false; } // end contains public int getCurrentSize() { // TODO Project 1 return 0; } // end getCurrentSize public boolean isEmpty() { // TODO Project 1 return false; } // end isEmpty public boolean equals(Object o) { // TODO Project 1 return false; } // equals public Iterator getIterator() { // TODO Project 1 return null; } // end getIterator public Iterator iterator() { // TODO Project 1 return null; } // end iterator public K[] toArray() { // TODO Project 1 // the cast is safe because the new array contains null entries @SuppressWarnings("unchecked") K[] result = (K[]) new Comparable[getCurrentSize()]; // unchecked cast //MUST BE IMPLEMENTED WITH ITERATOR return result; } // end toArray public SetInterface union(SetInterface otherSet) { // TODO Project 1 SetInterface result = new SetDictionary<>(); //MUST BE IMPLEMENTED WITH ITERATORS USING forEach lambda CONSTRUCT // AS SHOWN IN LectureDictionary EXAMPLES return result; } // end union public SetInterface intersection(SetInterface otherSet) { // TODO Project 1 SetInterface result = new SetDictionary<>(); //MUST BE IMPLEMENTED WITH ITERATORS // UTILIZE TRY_CATCH BLOCK // try // { // while (true) // { // // // // // // } // end while // } catch (NoSuchElementException nsee) // { // System.out.println("Finished creating intersection."); // } return result; } // end intersection public static void main(String args[]) { System.out.println("CREATING set1"); SetInterface set1 = new SetDictionary<>(); set1.add(1); set1.add(3); set1.add(2); set1.add(0); set1.add(-1); System.out.println("--> set1 has " + set1.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set1.toArray())); System.out.println(); System.out.println("--> contains for -1 yields " + set1.contains(-1)); System.out.println("--> contains for -2 yields " + set1.contains(-2)); System.out.println("--> contains for 3 yields " + set1.contains(3)); System.out.println("--> contains for 4 yields " + set1.contains(4)); set1.add(1); System.out.println(" --> Added 1 again to the set1, should be the same"); System.out.println("--> set1 has " + set1.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set1.toArray())); System.out.println(); System.out.println("--> Iterating over set1 utilizing getIterator method"); Iterator kIter = set1.getIterator(); while (kIter.hasNext()) { System.out.println("\t" + kIter.next()); } // end while System.out.println("--> Iterating over set1 utilizing iterator method"); kIter = ((SetDictionary) set1).iterator(); while (kIter.hasNext()) { System.out.println("\t" + kIter.next()); } // end while System.out.println("--> Iterating over set1 utilizing forEach lambda"); ((SetDictionary) set1).items.forEach((k,v) -> System.out.println("\t " + k)); System.out.println(" --> Removing -1 20 3 from set1:"); boolean result = set1.remove(-1); if (result) System.out.println("---> -1 was removed - CORRECT"); else System.out.println("---> -1 was not removed - INCORRECT"); result = set1.remove(20); if (result) System.out.println("---> 20 was removed - INCORRECT"); else System.out.println("---> 20 was not removed - CORRECT"); result = set1.remove(3); if (result) System.out.println("---> 3 was removed - CORRECT"); else System.out.println("---> 3 was not removed - INCORRECT"); System.out.println("--> Should just have 0 1 and 2 now"); System.out.println("--> set1 has " + set1.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set1.toArray())); System.out.println(); System.out.println("CREATING set2"); SetInterface set2 = new SetDictionary<>(); set2.add(1); set2.add(3); set2.add(2); set2.add(-1); set2.add(5); set2.add(8); System.out.println("--> set2 has " + set2.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set2.toArray())); System.out.println(); if (set1.equals(set2)) System.out.println("set1 and set2 are the same - INCORRECT"); else System.out.println("set1 and set2 are NOT the same - CORRECT"); System.out.println(); System.out.println("CREATING UNION OF set1 and set2"); SetInterface testUnion = set1.union(set2); System.out.print("--> The union of set1 and set2 has " + testUnion.getCurrentSize() + " items: "); System.out.println(Arrays.toString(testUnion.toArray())); System.out.println(); System.out.println("--> set1 should be unchanged"); System.out.println("--> set1 has " + set1.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set1.toArray())); System.out.println(); System.out.println("--> set2 should be unchanged"); System.out.println("--> set2 has " + set2.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set2.toArray())); System.out.println(); System.out.println("CREATING UNION OF set1 and set1"); testUnion = set1.union(set1); System.out.print("--> The union of set1 and set1 has " + testUnion.getCurrentSize() + " items: "); System.out.println(Arrays.toString(testUnion.toArray())); System.out.println(); if (set1.equals(testUnion)) System.out.println("set1 and testUnion are the same - CORRECT"); else System.out.println("set1 and testUnion are NOT the same - INCORRECT"); System.out.println(); System.out.println("CREATING INTERSECTION OF set1 and set2"); SetInterface testIntersection = set1.intersection(set2); System.out.print("--> The intersection of set1 and set2 has " + testIntersection.getCurrentSize() + " items: "); System.out.println(Arrays.toString(testIntersection.toArray())); System.out.println(); System.out.println("--> set1 should be unchanged"); System.out.println("--> set1 has " + set1.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set1.toArray())); System.out.println(); System.out.println("--> set2 should be unchanged"); System.out.println("--> set2 has " + set2.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set2.toArray())); System.out.println(); System.out.println("CREATING INTERSECTION OF set2 and set1"); testIntersection = set2.intersection(set1); System.out.print("--> The intersection of set2 and set1 has " + testIntersection.getCurrentSize() + " items: "); System.out.println(Arrays.toString(testIntersection.toArray())); System.out.println(); System.out.println("--> set1 should be unchanged"); System.out.println("--> set1 has " + set1.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set1.toArray())); System.out.println(); System.out.println("--> set2 should be unchanged"); System.out.println("--> set2 has " + set2.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set2.toArray())); System.out.println(); System.out.println("CREATING INTERSECTION OF set2 and set2"); testIntersection = set2.intersection(set2); System.out.print("--> The intersection of set2 and set2 has " + testIntersection.getCurrentSize() + " items: "); System.out.println(Arrays.toString(testIntersection.toArray())); System.out.println(); System.out.println("--> set1 should be unchanged"); System.out.println("--> set1 has " + set1.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set1.toArray())); System.out.println(); System.out.println("--> set2 should be unchanged"); System.out.println("--> set2 has " + set2.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set2.toArray())); System.out.println(); if (set2.equals(testIntersection)) System.out.println("set2 and testIntersection are the same - CORRECT"); else System.out.println("set2 and testIntersection are NOT the same - INCORRECT"); System.out.println(); System.out.println("CREATING INTERSECTION OF testUnion and set2"); testIntersection = testUnion.intersection(set2); System.out.print("--> The intersection of testUnion and set2 has " + testIntersection.getCurrentSize() + " items: "); System.out.println(Arrays.toString(testIntersection.toArray())); System.out.println(); System.out.println("--> testUnion should be unchanged"); System.out.println("--> testUnion has " + testUnion.getCurrentSize() + " items: "); System.out.println(Arrays.toString(testUnion.toArray())); System.out.println(); System.out.println("--> set2 should be unchanged"); System.out.println("--> set2 has " + set2.getCurrentSize() + " items: "); System.out.println(Arrays.toString(set2.toArray())); System.out.println(); if (set2.equals(testIntersection)) System.out.println("set2 and testIntersection are the same - INCORRECT"); else System.out.println("set2 and testIntersection are NOT the same - CORRECT"); System.out.println(); } // end main }
SET INTERFACE
public interface SetInterface { /** * Gets the current number of entries in this set. * * @return The integer number of entries currently in the set. */ public int getCurrentSize(); /** * Sees whether this set is empty. * * @return True if the set is empty, or false if not. */ public boolean isEmpty(); /** * Adds a new entry to this set, avoiding duplicates. * * @param newEntry The object to be added as a new entry. * @return True if the addition is successful, or * false if the item already is in the set. */ public boolean add(T newEntry); /** * Removes a specific entry from this set, if possible. * * @param anEntry The entry to be removed. * @return True if the removal was successful, or false if not. */ public boolean remove(T anEntry); /** * Removes all entries from this set. */ public void clear(); /** * Tests whether this set contains a given entry. * * @param anEntry The entry to locate. * @return True if the set contains anEntry, or false if not. */ public boolean contains(T anEntry); /** * Tests whether this set is the same as the other set. * * @param o The set to compare this set with. * @return True if the set contains anEntry, or false if not. */ public boolean equals(Object o); /** * Retrieves all entries that are in this set. * * @return A newly allocated array of all the entries in the set. */ public T[] toArray(); /** * Creates an iterator that traverses all entries in this set. * * @return An iterator that provides sequential access to the entries * in the set. */ public Iterator getIterator(); /** * Creates a new set that combines the contents of this set and another set. * * @param anotherSet The second set. * @return A set that is the union of the two sets. */ public SetInterface union(SetInterface anotherSet); /** * Creates a new set that contains those items that occur * in both this set and another set. * * @param anotherSet The second set. * @return A set that is the intersection of the two sets. */ public SetInterface intersection(SetInterface anotherSet); } // end SetInterface Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
