Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java The given class SetInterface.java is : public interface SetInterface { /** * Gets the current number of entries in this set. * *

Using Java

The given class SetInterface.java is :

public interface SetInterface {

/**

* Gets the current number of entries in this set.

*

* @return The integer number of entries currently in the set.

*/

public int getSize();

/**

* 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 one unspecified entry from this set, if possible.

*

* @return Either the removed entry, if the removal was successful, or null.

*/

public T remove();

/** 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);

/**

* Returns true if one set is equivalent to another (contains the same

* elements), and false otherwise.

*

* @param anotherSet another set

* @return true if this set contains the same elements as the other set, or

* false if not.

*/

public boolean isEquivalentTo(SetInterface anotherSet);

/**

* Returns true if all of the elements of this set are contained in the other

* set, and false otherwise.

*

* @param anotherSet another set

* @return true if all of the elements of this set are contained in the other

* set, and false otherwise.

*/

public boolean isSubsetOf(SetInterface anotherSet);

/**

* Computes the union of this set with a given set

*

* @param anotherSet another set

* @return the union of this set with anotherSet

*/

public SetInterface union(SetInterface anotherSet);

/**

* Computes the intersection of this set with a given set

*

* @param anotherSet another set

* @return the intersection of this set with anotherSet

*/

public SetInterface intersection(SetInterface anotherSet);

/**

* Retrieves all entries that are in this set.

*

* @return A newly allocated array of all the entries in the set, where the size

* of the array is equal to the number of entries in the set.

*/

public T[] toArray();

} // end SetInterface

1. (63 points) Create a class ResizableArraySet that implements the methods in SetInterface.java file that you have been provided (do not modify the given interface file). There should be just one constructor, with no parameters, that creates a Set containing no elements (but the underlying array should have an initial length of 5). Methods should avoid unnecessary code duplication, using private helper methods to reduce repeated code. Double the size of the array when the array is full. After removing an element, if the array is less than half full, then cut the size of the array in half. However, the size of the array should never be less than 5. Write a toString() method that returns a comma-separated list of the data in the set, surrounded by curly braces (such as "{A, C, B}"). (write early so you can test)

2. (See sample run at the end of this document) In a separate class, Tester, create a void method named setTester() that has no parameters and: (10 points) Creates an array consisting of several strings, including some repeated strings, and then adds them to one of your sets, displaying information about your set as it goes. Then, remove the elements you added, in the same order that you added them. The idea is that this will help you show yourself (and us) that all the methods in your Set class are working. (10 points) Creates another set and adds 20 random integers in the range 0 to 99. Each time a number is added, print the number that was added, along with the contents of the set. Note that if duplicates are added, then the set size after adding 20 numbers would actually be less than 20. After adding the numbers to the set, use a loop to remove all even numbers from the set, showing the number that was removed, and the resulting set. Only show the removal if the number is actually in the set.

The final testing file:

import java.awt.Point;

public class BaselineTester {

public static void main(String[] args) {

testResizableArraySet();

testNodeClass();

testTester();

}

// Verifies that the proper methods and constructor exist in ResizableArraySet

// If this tester contains compiler errors, it indicates a problem with your code.

// Do not modify this tester. Instead, modify your ResizableArraySet code.

public static void testResizableArraySet() {

// Is it possible to create different kinds of sets?

SetInterface set1 = new ResizableArraySet<>();

SetInterface set2 = new ResizableArraySet<>();

SetInterface set3 = new ResizableArraySet<>();

// Test the add and remove methods:

boolean b = set1.add("cat");

b = set1.remove("cat");

String s = set1.remove();

set1.clear();

int i = set1.getSize();

String[] arr = set1.toArray();

SetInterface set4 = new ResizableArraySet<>();

SetInterface set5 = new ResizableArraySet<>();

SetInterface set6 = set4.union(set5);

set6 = set4.intersection(set5);

b = set6.contains(new Point());

b = set6.isEquivalentTo(set5);

b = set6.isSubsetOf(set5);

b = set6.isEmpty();

}

// This makes sure that your Node class exists and has package-level instance variables

// named data and next, and a constructor that has an int parameter.

public static void testNodeClass() {

Node n = new Node(5);

int i = n.data;

Node n2 = n.next;

}

// This makes sure that you have a class named Tester and that the class has static

// methods with the proper names.

public static void testTester() {

Tester.setTester();

Tester.nodeTester();

}

}

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

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions