Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA The SimpleSet Class: General Requirements : The SimpleSet class shall be

ID: 3745592 • Letter: I

Question

IN JAVA

The SimpleSet Class:

General Requirements:

The SimpleSet class shall be written with Generics in order to allow Sets that can hold a variety of different data types.

The SimpleSet class shall not allow duplicate items to be added to the set.

The SimpleSet class shall use Exception Handling to deal with indexes which are out of bounds, duplicate items, and sets that are full.

Use the IndexOutOfBounds exception from Java for out of bounds indices.

Create a DuplicateItemException class and use it when a duplicate item is added to the set.

Create a NotEnoughSpaceException class when an attempt is made to add an item to an already full SimpleSet or when the user tries to initialize a set with too many values.

Data Fields:

The SimpleSet class shall have a private data field which is an array of 10 elements to store each element of the Set. HINT: Take a look at the source code for ArrayList and see how they create the internal array for ArrayList. This array will be fixed at 10 and WILL NOT resize itself. We are creating a very simple set to hold only 10 items.

The SimpleSet class shall have a private data field that is an integer which holds the current size of the set. The size will increase as new items are added to the set.

The SimpleSet class shall have no other data fields.

Constructors:

The SimpleSet class shall have a no argument constructor to create a new, empty instance of SimpleSet.

The SimpleSet class shall have another constructor that will accept a comma separated list of items to initialize the set. The following would be two examples of invoking this constructor:

SimpleSet<Integer> s1 = new SimpleSet<>(1, 2, 3, 4, 5);

SimpleSet<String> s2 = new SimpleSet<>("dog", "cat", "mouse");

NOTE: The comma separated lists can include anywhere between 1 and 10 values. If you do not remember how to create a parameter of this type, do some research on "variable-length parameter lists".

The SimpleSet class shall have no other constructors.

Methods:

add: adds a new item to the end of the set.

contains: returns true or false depending on if the set already contains the item.

get: returns an item from the set at a specific index.

put: puts an item into the set at a specific index, replacing the old value.

size: returns the current size of the SimpleSet

toString: returns a String representation of a SimpleSet.

The Sorting Class:

Create a class called Sorting.

This class should be designed to prevent its instantiation.

This class has one static method called sort. This method is a generic method that should be designed to sort a SimpleSet.

This class should bound its generic to any class that uses the Comparable interface.

You may need to review how Comparable works.

The sort method should implement the following pseudocode:

while the set is not sorted:
   for each item in the set:
      if next item > current item:
         swap current item and item

Testing:

Download my main method class and use it to test your code. If my main tester does not work, your code is wrong.

Explanation / Answer

ANS:

CODE:

public class SimpleSet<E> {

@SuppressWarnings("unchecked")

private E[] elements=(E[])new Object[10];

private int currentSize=-1;

public SimpleSet()

{

}

@SafeVarargs

public SimpleSet(E...es)

{

if(es.length>10)

{

throw new NotEnoughSpaceException("Size is greater");

}

for(int i=0;i<es.length;i++)

{

boolean available=false;

for(int j=0;j<=currentSize;j++)

{

if(es[i].equals(elements[j]))

{

available=true;   

}

}

if(!available)

{

currentSize++;

elements[currentSize]=es[i];

}

else

{

throw new DuplicateItemException("Duplicate Item");

}

}

}

public void add(E element)

{

if(currentSize==9)

{

throw new NotEnoughSpaceException("Size is greater");

}

boolean available=false;

for(int i=0;i<=currentSize;i++)

{

if(element.equals(elements[i]))

{

available=true;   

}

}

if(available)

{

throw new DuplicateItemException("Duplicate Item");

}

++currentSize;

elements[currentSize]=element;

}

public boolean contains(E element)

{

boolean available=false;

for(int i=0;i<=currentSize;i++)

{

if(element.equals(elements[i]))

{

available=true;   

}

}

return available;

}

public E get(int index)

{

return elements[index];

}

public void put(int index , E element)

{

if(index<0 || index >9)

{

throw new IndexOutOfBoundsException();

}

boolean available=false;

for(int i=0;i<=currentSize;i++)

{

if(element.equals(elements[i]))

{

available=true;   

}

}

if(available)

{

throw new DuplicateItemException("Duplicate Item");

}

++currentSize;

elements[index]=element;

}

public int size()

{

return currentSize+1;

}

public String toString()

{

String result="[";

for(int i=0;i<=currentSize;i++)

{

  

result=result+elements[i].toString()+",";

}

return result.substring(0, result.length()-1)+"]";

}

}

public class Sorting {

private Sorting()

{

}

public static void sort(SimpleSet<? extends Comparable> set)

{

boolean sortedStatus=false;

while(!sortedStatus)

{

for(int i=0;i<set.size();i++)

{

if(i!=set.size()-1)

{

Comparable currentItem=set.get(i);

Comparable nextitem=set.get(i+1);

if(nextitem.compareTo(currentItem)>0)

{

Comparable temp=currentItem;

currentItem=nextitem;

nextitem=temp;

}

}

}

boolean sorted=true;

for(int j=0;j<set.size();j++)

{

if(j<set.size()-1)

{

if(set.get(j).compareTo(set.get(j+1))>0)

{

sorted=false;

}

}

}

if(sorted)

{

sortedStatus=true;

}

}

}

}

public class DuplicateItemException extends RuntimeException {

/**

*

*/

private static final long serialVersionUID = 1L;

private String message;

public DuplicateItemException(String message) {

this.message=message;

}

public String toString() {

return message;

}

}

public class NotEnoughSpaceException extends RuntimeException{

/**

*

*/

private static final long serialVersionUID = 1L;

private String message;

public NotEnoughSpaceException(String message) {

this.message=message;

}

public String toString() {

return message;

}

}