IN JAVA The SimpleSet Class: General Requirements : The SimpleSet class shall be
ID: 3745563 • 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
Given below is the code for the question. Please use the Person class and Main test class provided by instructor.
I created a simple Person class in order to test. You should use the Person class given by instructor.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
SimpleSet.java
-----
public class SimpleSet<T> {
private T[] data;
private int size; // the no. of items in the set
public SimpleSet()
{
data = (T[]) new Object[10];
size = 0;
}
public SimpleSet(T... values)
{
data = (T[]) new Object[10];
size = 0;
for(int i = 0; i < values.length; i++)
add(values[i]);
}
public void add(T value)
{
if(size == data.length)
throw new NotEnoughSpaceException("Array is full");
if(contains(value))
throw new DuplicateItemException("Trying to add a duplicate value");
data[size++] = value;
}
public boolean contains(T value)
{
for(int i = 0; i < size; i++)
{
if(data[i].equals(value))
return true;
}
return false;
}
public T get(int index)
{
if(index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index " + index + " is out of range");
return data[index];
}
public void put(int index, T value)
{
if(index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index " + index + " is out of range");
data[index] = value;
}
public int size()
{
return size;
}
public String toString()
{
String s = "{";
if(size > 0)
{
s += data[0].toString();
for(int i = 1; i < size; i++)
s += ", " + data[i].toString();
}
s += "}";
return s;
}
}
Sorting.java
---------
public class Sorting {
public static <T extends Comparable<T>> void sort(SimpleSet<T> set){
for(int i = 0; i < set.size(); i++){
for(int j = i+1; j < set.size(); j++){
if(set.get(i).compareTo(set.get(j)) > 0){
T temp = set.get(i);
set.put(i, set.get(j));
set.put(j, temp);
}
}
}
}
}
Person.java (use the one given by instructor)
--------
public class Person implements Comparable<Person> {
protected String fname;
protected String lname;
public Person(String fname, String lname){
this.fname = fname;
this.lname = lname;
}
public String toString(){
return fname + " " + lname;
}
public int compareTo(Person p){
int c = lname.compareTo(p.lname);
if(c < 0)
return -1;
if(c > 0)
return 1;
else
{
c = fname.compareTo(p.fname);
if(c < 0)
return -1;
else if(c > 0)
return 1;
else
return 1;
}
}
}
output
------
Testing toString():
{java, python, c++, c#, ada, kotlin, swift}
{John Smith, Amanda Richardson, Bob Green, Sue Smith, Fred Parsons, Jane Doe}
{4, 7, 100, 23, 67, 87, 2013, 59, 29, 40}
{}
Testing add():
{java, python, c++, c#, ada, kotlin, swift, fortran}
{John Smith, Amanda Richardson, Bob Green, Sue Smith, Fred Parsons, Jane Doe, Kate Walker}
{4, 7, 100, 23, 67, 87, 2013, 59, 29, 40}
{q}
Testing contains():
true
false
false
false
true
false
true
false
Testing get():
c#
Fred Parsons
2013
q
Testing put():
{java, python, c++, objective-c, ada, kotlin, swift, fortran}
{John Smith, Jill Peters, Bob Green, Sue Smith, Fred Parsons, Jane Doe, Kate Walker}
{4, 7, 100, 23, 67, 87, 10000, 59, 29, 40}
{e}
8
7
10
1
{ada, c++, fortran, java, kotlin, objective-c, python, swift}
{Jane Doe, Bob Green, Fred Parsons, Jill Peters, John Smith, Sue Smith, Kate Walker}
{4, 7, 23, 29, 40, 59, 67, 87, 100, 10000}
{e}