Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA The SimpleSet Class: General Requirements : The SimpleSet class shall be written with Generics in order to allow Sets that can hold a

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 s1 = new SimpleSet<>(1, 2, 3, 4, 5);

SimpleSet 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.

 import person.Person; public class Main { public static void main(String[] args) { //SimpleSet error1 = new SimpleSet<>(1, 2, 2, 3, 4); //This line should cause an exception. //SimpleSet error2 = new SimpleSet<>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); //Testing Constructors SimpleSet strings = new SimpleSet<>("java", "python", "c++", "c#", "ada", "kotlin", "swift"); SimpleSet people = new SimpleSet<>(new Person("John", "Smith"), new Person("Amanda", "Richardson"), new Person("Bob", "Green"), new Person("Sue","Smith"), new Person("Fred", "Parsons"), new Person("Jane", "Doe")); SimpleSet nums = new SimpleSet<>(4, 7, 100, 23, 67, 87, 2013, 59, 29, 40); SimpleSet chars = new SimpleSet<>(); //Testing toString(); System.out.println("Testing toString():"); System.out.println(strings); System.out.println(people); System.out.println(nums); System.out.println(chars); System.out.println(); System.out.println(); //Testing add System.out.println("Testing add():"); //strings.add("java"); //This line should fail. //people.add(new Person("John", "Smith")); //This line should fail. //nums.add(42)// This line should fail. strings.add("fortran"); people.add(new Person("Kate", "Walker")); chars.add('q'); System.out.println(strings); System.out.println(people); System.out.println(nums); System.out.println(chars); System.out.println(); System.out.println(); //Testing contains System.out.println("Testing contains():"); System.out.println(strings.contains("java")); System.out.println(strings.contains("sql")); System.out.println(people.contains(new Person("Bob", "Green"))); System.out.println(people.contains(new Person("Jimmy", "Reed"))); System.out.println(nums.contains(2013)); System.out.println(nums.contains(1000)); System.out.println(chars.contains('q')); System.out.println(chars.contains('i')); System.out.println(); System.out.println(); //Testing get() System.out.println("Testing get():"); //System.out.println(chars.get(6)); //This line should fail. //System.out.println(people.get(9)); //This line should fail. System.out.println(strings.get(3)); System.out.println(people.get(4)); System.out.println(nums.get(6)); System.out.println(chars.get(0)); System.out.println(); System.out.println(); //Testing put(): System.out.println("Testing put():"); //strings.put(3, "java"); //This line should fail. //people.put(5, new Person("Sue", "Smith")); //This line should fail. //nums.put(10, 57); //This line should fail. //chars.put(2, 'c'); //This line should fail. strings.put(3, "objective-c"); people.put(0, new Person("John", "Smith")); //This line should be ok. people.put(1, new Person("Jill", "Peters")); nums.put(6, 10000); chars.put(0, 'e'); System.out.println(strings); System.out.println(people); System.out.println(nums); System.out.println(chars); System.out.println(); System.out.println(); //Testing size(): System.out.println(strings.size()); System.out.println(people.size()); System.out.println(nums.size()); System.out.println(chars.size()); System.out.println(); System.out.println(); //Testing sort(): Sorting.sort(strings); Sorting.sort(people); Sorting.sort(nums); Sorting.sort(chars); System.out.println(strings); System.out.println(people); System.out.println(nums); System.out.println(chars); System.out.println(); System.out.println(); } }

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

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

More Books

Students also viewed these Databases questions