Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal of this assignment is to provide exposure to building a new generic data structure. Specifically, this project will involve building a new Set

The goal of this assignment is to provide exposure to building a new generic data structure. Specifically, this project will involve building a new Set structure that maintains the add counts of its unique elements, so that they can be iterated in sorted order with respect to how many times theyve been added to the set. The Set implementation will be optimized for efficient add/remove/ contains over efficient iteration.

1. [70] Create a new class OccurrenceSet that implements Set. Your OccurrenceSet implementation should create and maintain a HashMap; doing so will allow you to easily track the integer add count for each element in the set . All methods should function as specified in the Set documentation.

Additionally:

(10) The add and addAll methods will need to keep track of how many times an element has been added to the set. We are optimizing for efficient add, so add should be O(1) and addAll should be O(n) for n added elements .

(10) The remove, removeAll, and retainAll methods should remove the necessary elements from the set completely (i.e., not just decrement their counts). We are optimizing for efficient remove, so remove should be O(1) and remove All should be O(n) for n removed elements.

(10) The contains and containsAll methods should behave as documented and operate in O(1) and O(n) time (i.e., for n query elements), respectively.

(10) The size method should return the number of unique elements currently in the set (i.e., not considering add counts). This method should be O(1) . The clear and isEmpty methods should behave as documented.

(20) The iterator and toArray methods should return an Iterator or array, respectively, with elements sorted by their add counts in ascending order. We are optimizing for efficient add /contains over iteration, but these methods should still be O(n lg n). The Iterator remove method can be left blank for this assignment.

Hint: Creating a new List and using the Collections sort method is reasonable here, though note that youll need to implement a Comparator object so that the elements can be sorted by their associated counts.

(10) Add a toString method that returns a string representation of the elements in the list in sorted order ( i.e., ascending with respect to their add counts). This method should be O(n lg n).

2. [20] Create a Main class that creates a few OccurrenceSets of various types to test the functionality of your new data structure. For example, a test like:

OccurrenceSet int Set = new OccurrenceSet();

intSet.add(1);

intSet.add(3);

intSet.add(5);

intSet.add(5);

intSet.add(3);

intSet.add(3);

intSet.add(3);

System.out.println(intSet);

OccurrenceSet stringSet = new OccurrenceSet();

stringSet.add("hello");

stringSet.add("hello");

stringSet.add("world");

stringSet.add("world");

stringSet.add("world");

stringSet.add("here");

System.out.println(stringSet);

Should have the output:

[1, 5, 3]

[here, hello, world]

3. [10] Write code that is clear and efficient. Specifically, your code should be indented with respect to code blocks, avoid unnecessarily repetitive code, avoid code that is commented out or otherwise unused, use descriptive and consistent class/method/variable names, etc.

This is the assignment but I have this code, and I can't get it to run. I'm using Eclipse as a text editor and running the files in my terminal on MacOS with Java 1.8. If you could just tell me how to take the code from this page and run it thats all I need.

Here is the code I was given:

public class Main {

public static void main(String[] args) {

testStringSet(); testIntSet(); }

private static void testStringSet() { System.out.println(" ------------- stringSet ---------------"); OccurrenceSet stringSet = new OccurrenceSet<>(); System.out.println("Adding hello to stringSet. Unique entry?: " + stringSet.add("hello")); System.out.println("Adding hello to stringSet. Unique entry?: " + stringSet.add("hello")); System.out.println("Adding world to stringSet. Unique entry?: " + stringSet.add("world")); System.out.println("Adding world to stringSet. Unique entry?: " + stringSet.add("world")); System.out.println("Adding world to stringSet. Unique entry?: " + stringSet.add("world")); System.out.println("Adding here to stringSet. Unique entry?: " + stringSet.add("here")); System.out.println("\tstringSet with entry counts: " + stringSet.getMap().entrySet().toString()); System.out.println("\tstringSet toArray: " + stringSet); System.out.println("\tstringSet size: " + stringSet.size()); System.out.println(); }

private static void testIntSet() { System.out.println(" ------------ intSet ------------"); OccurrenceSet intSet = new OccurrenceSet<>(); System.out.println("Adding 1 to intSet. Unique Entry?: " + intSet.add(1)); System.out.println("Adding 3 to intSet. Unique Entry?: " + intSet.add(3)); System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("Adding 3 to intSet. Unique Entry?: " + intSet.add(3)); System.out.println("Adding 3 to intSet. Unique Entry?: " + intSet.add(3)); System.out.println("Adding 3 to intSet. Unique Entry?: " + intSet.add(3)); System.out.println("\tintSet with entry counts: " + intSet.getMap().entrySet().toString()); System.out.println("\tintSet toArray: " + intSet); System.out.println("\tintSet size: " + intSet.size()); System.out.println();

System.out.println("Removing 5 from intSet. intSet changed?: " + intSet.remove(5)); System.out.println("\tintSet with entry counts: " + intSet.getMap().entrySet().toString()); System.out.println("\tintSet toArray: " + intSet); System.out.println("\tintSet size: " + intSet.size()); System.out.println();

OccurrenceSet intSet2 = new OccurrenceSet<>(); System.out.println("duplicating to intSet2. intSet2 changed?: " + intSet2.addAll(intSet)); System.out.println("\tintSet2 with entry counts: " + intSet2.getMap().entrySet().toString()); System.out.println("\tintSet2 toArray: " + intSet2); System.out.println("\tintSet2 size: " + intSet2.size()); System.out.println();

System.out.println("Adding 7 to intSet2. Unique Entry?: " + intSet2.add(7)); System.out.println("Adding 7 to intSet2. Unique Entry?: " + intSet2.add(7)); System.out.println("\tintSet2 with entry counts: " + intSet2.getMap().entrySet().toString()); System.out.println("\tintSet2 toArray: " + intSet2); System.out.println("\tintSet2 size: " + intSet2.size()); System.out.println();

System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("Adding 5 to intSet. Unique Entry?: " + intSet.add(5)); System.out.println("\tintSet with entry counts: " + intSet.getMap().entrySet().toString()); System.out.println("\tintSet toArray: " + intSet); System.out.println("\tintSet size: " + intSet.size()); System.out.println();

System.out.println("Adding intSet2 to intSet. intSet changed?: " + intSet.addAll(intSet2)); System.out.println("\tintSet with entry counts: " + intSet.getMap().entrySet().toString()); System.out.println("\tintSet toArray: " + intSet); System.out.println("\tintSet size: " + intSet.size()); System.out.println(); } }

------------------------------------------------------------------------------------------------------------

public class OccurrenceSet implements Set {

private HashMap _map;

public OccurrenceSet() { _map = new HashMap<>(); }

public HashMap getMap() { return _map; }

@Override public int size() { return _map.size(); }

@Override public boolean isEmpty() { return _map.isEmpty(); }

@Override public boolean contains(Object o) { return _map.containsKey(o); }

@Override public Iterator iterator() { return _map.keySet().iterator(); }

@Override public Object[] toArray() { ArrayList list = new ArrayList<>(); Stream> stream = _map.entrySet().stream(); stream.sorted(Comparator.comparing(Map.Entry::getValue)).forEachOrdered(entry -> list.add(entry.getKey())); return list.toArray(); }

@Override @SuppressWarnings("unchecked") public E[] toArray(E[] a) { if (a.length < _map.size()) return (E[]) Arrays.copyOf(toArray(), _map.size(), a.getClass()); System.arraycopy(toArray(), 0, a, 0, _map.size()); if (a.length > _map.size()) a[_map.size()] = null; return a; }

@Override public boolean add(T t) { return _map.put(t, _map.get(t) == null ? 1 : _map.get(t) + 1) == null; }

@Override public boolean remove(Object o) { return _map.remove(o) != null; }

@Override public boolean containsAll(Collection c) { if (c.size() > 0) { for (Object entry : c) { if (!contains(entry)) { return false; } } return true; } return false; }

@Override public boolean addAll(Collection c) { if (c.size() > 0) { // if adding an instance of OccurrenceSet, adds the multiple occurrences of each entry if (c instanceof OccurrenceSet) { for (T entry : c) { for (int i = 0; i < (int) ((OccurrenceSet) c).getMap().get(entry); i++) { add(entry); } } return true; // otherwise, just adds each entry } else { for (T entry : c) { add(entry); } return true; } } return false; }

@Override public boolean retainAll(Collection c) { if (c.size() > 0) { boolean setChanged = false; for (Map.Entry entry : _map.entrySet()) { if (!c.contains(entry)) { remove(entry); setChanged = true; } } return setChanged; } return false; }

@Override public boolean removeAll(Collection c) { if (c.size() > 0) { for (Object entry : c) { remove(entry); } return true; } return false; }

@Override public void clear() { _map.clear(); }

@Override public String toString() { String outputString = "["; for (Object element : toArray()) { outputString += element.toString() + ", "; } outputString = outputString.substring(0, outputString.length() - 2) + "]"; return outputString; }

@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; OccurrenceSet that = (OccurrenceSet) o; return _map.equals(that._map); }

@Override public int hashCode() { return _map.hashCode(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Let f(x) = 1/2, 0 Answered: 1 week ago

Answered: 1 week ago

Question

NMR ANALYSIS

Answered: 1 week ago

Question

1. Traditional and modern methods of preserving food Articles ?

Answered: 1 week ago

Question

What is sociology and its nature ?

Answered: 1 week ago

Question

What is liquidation ?

Answered: 1 week ago

Question

Explain the different types of Mergers.

Answered: 1 week ago