Question: Project Setup: Create a new project in eclipse called Homework03 . In your src folder, create a package called hw03 . Right-click the src folder
Project Setup:
Create a new project in eclipse called Homework03.
In your src folder, create a package called hw03.
Right-click the src folder and choose new -> package.
All of your source code files for this project will go in the hw03 package.
Right-click the package and choose new -> Class.
Create three classes in your hw03 package, and name them:
MySet
MySetUtils
MySetMain
These three classes are described below.
The MySet Class:
Be sure to name your class exactly MySet.
This class shall always keep the values in sorted order. Whenever a value is added to the set, you will need to resort the data.
HINT: Sorting seems scary, but you are allowed to use the Arrays.sort() method to keep your sets sorted. Review the material from Chapter 07 of your textbook on the Arrays class (see above for exact section.)
The class shall always keep the values in the set in sorted order. This means that any time the values of a set are changed, you will need to re-sort the values.
HINT: Take a look at the Arrays.sort() method that already exists in Java. You may use this method in your implementation, but you may not use any other methods from this class.
Reminder: Be sure to use this throughout this class for good coding practices.
Data Fields:private int[] set
Your class shall have a data field called set.
This data field shall be an array of integer values.
This data field shall also be private with no getters or setters.
public static int numSets
Your class shall have a data field called numSets.
This data field shall be an integer.
This data field shall also be public and static.
This data field will count how many sets have been instantiated.
This data field does not need any getters or setters.
HINT: Be sure to increase the value of numSets by 1 every time any constructor is called.
No other data fields are necessary / allowed in this class. All other variables must be local variables.
Constructors:public MySet()
Your class shall define a default constructor to create an empty set.
This constructor should initialize the data field array to be an array of size zero.
HINT: You would do something like: this.set = new int[0]
We do this so that later on when we call our add method we avoid any NullPointerExceptions from having an uninitialized array.
This constructor shall also increase the numSets count by one.
Instead of saying this.numSets, since this is a static data field we would instead say MySet.numSets to access this data field. We do this because this is used for instance things, and ClassName.thingName is used for static things.
public MySet(int ... values)
Your class shall have an additional constructor which will accept a comma separated list of integers as input to the constructor or an array of integers.
For this special type of parameter, you will want to review variable-length parameter lists from CS2011 (see review materials above).
REMINDER: A set cannot contain duplicate values. You may assume that when I test this constructor, I will NOT include duplicates in the comma-separated list of input values. Keep in mind that you would normally need to account for this, but to make the assignment easier, all values entered into the constructor will be non-duplicates.
HINT: The following examples show how this constructor would be invoked:
CS2012Set set1 = new CS2012Set(23, 1, 2, 4, 78, 9);
CS2012Set set2 = new CS2012Set(4, 10, 29, 77, 34, 42, 17, 89, 47);
CS2012Set set3 = new CS2012Set(myArray); //myArray is an existing array of integers.
No other constructors are needed or allowed.
Methods:public boolean existsInSet(int value):
This method shall be a public method which accepts one integer as an argument.
This method shall return true or false depending on whether or not the given value already exists within your set.
You will want to implement this method first since it will help you to implement some of the methods to follow.
NOTE: If the set is empty (has a length of 0) you can return false.
public void add(int value):
This method shall be a public method which accepts one integer as an argument.
The integer shall be added to the set, provided that the value does not already exist within the set.
If the value already exists, display an error message: "The value already exists in the set.".
Don't forget to sort the data after the new value has been added. (Using Arrays.sort())
The following HINTS may help if you are stuck:
HINT 1: Use the existsInSet() method that you wrote previously to help implement this method.
HINT 2: Since we cannot change the size of an existing array, you will need to create a new array that is the size of the current array + 1. Copy the data from the old array to the new array (Using System.arraycopy). Don't forget to update the set data field to refer to the new set instead of the old one.
public void delete(int value):
This method shall be a public method which accepts one integer as an argument.
This integer shall be a value that will be removed from the set. (Note: This is not the index to remove, but the actual value you want to remove.)
The following HINTS may help if you are stuck:
HINT 1: Make sure the value to be removed actually exists. If it does not, display an error message to the user and do not remove anything. Use the existsInSet method that you wrote previously to help implement this method.
HINT 2: Since we cannot change the size of an existing array, you will need to create a new array with the current size - 1, and then copy the existing values to the new array, but exclude the value to be removed. Be sure to update your set data field to refer to the new array and not the old one.
public int get(int index):
This method shall be a public method which accepts one integer as an argument.
This integer shall be an index position in the set.
This method will return the integer stored at the given index.
Assume that the given integer index will be in bounds.
Later in the semester, we will see how to use Exception Handling to deal with out of bounds errors.
public int size():
This method shall be a public method which has no parameters.
This method shall returns the size (number of elements), in the set.
public void printSet():
This method shall print a String representation of your set.
The printed set should look like the following format: {1, 2, 6, 10, 42}
If the set is empty, print an empty pair of curly braces {}
The MySetUtils Class:
Be sure to name your class exactly MySetUtils.
This class shall only contain static methods.
NOTE:
To invoke (call) any methods from this class you will not instantiate the class as in MySetUtils msu = new MySetUtils().
Since all methods in this class are static, you will invoke them by using MySetUtils.methodName(), similar to how you use the Math class.
Data Fields:
This class shall have no data fields.
Constructors:
This class shall have a no-arg (default) constructor which should have private visibility. This is to prevent the class from being instantiated.
HINT: The constructor should look like private MySetUtils() { }
Methods:public static MySet union(MySet set1, MySet set2):
This method shall take two MySet objects as parameters.
This method shall return a new MySet object which will be the union of the two parameters sets.
If you don't remember how to perform the union of two sets, please see the review materials above.
Example: The union of the sets {1, 2, 3, 4, 5} and {1, 3, 5, 6, 10} is {1, 2, 3, 4, 5, 6, 10}.
You should use the methods from your MySet class to help implement this method.
public static MySet intersection (MySet set1, MySet set2):
This method shall take two MySet objects as parameters.
This method shall return a new MySet object which will be the intersection of the two parameters sets.
If you don't remember how to perform the intersection of two sets, please see the review materials above.
Example: The intersection of the sets {1, 2, 3, 4, 5} and {1, 3, 5, 6, 10} is {1, 3, 5}.
If there is no intersection, then the result is the empty set.
The MySetMain Class:
This class shall contain your main method.
You should use this class to demonstrate that all functionality of your MySet and MySetUtils classes works.
Create several MySet objects and demonstrate that all of the methods from the MySet class are fully functional and correct.
Also demonstrate that all methods from the MySetUtils class are fully functional and correct.
CAUTION: Any methods / requirements which are not demonstrated, will receive no credit regardless of whether or not they are actually correct.
Your main method should show that you put thought into testing your code and not just a few lines. If I feel you did not test your code well enough, you will also lose points.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
