Question: Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by
Using Java programming language, build a class called IntegerSet.
Instructions
- Create class IntegerSet
- An IntegerSet object holds integers in the range 0-100
- Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise
- Create these constructors and methods for the class
IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()
- The constructor (no arguments) initializes the array to represent the "empty set" (i.e. no integers in the set)
- Method union creates and returns a new set that is the set-theoretic union of the two existing sets (the calling object and the parameter). An element is in the union if it's in either of the starting two sets
- Method intersection creates and returns a new set that is the set-theoretic intersection of the two existing sets (the calling object and the parameter). An element is in the intersection if it's in BOTH of the starting two sets
- Method insertElement adds the argument (an integer) to the set (the calling object), and also should return that set (so that calls can be cascaded)
- Method deleteElement removes the argument (an integer) from the set (the calling object), and also should return that set (so that calls can be cascaded)
- Method isEqualTo determines whether two sets are equal (i.e. they have all the same elements), returning a true or false indication
- Method toString returns a string containing the set elements as a list of numbers, in ascending order, separated by spaces. Include only elements present in the set. Use "---" to represent an empty set.
Here's the sample run:
After set1.insertElement(10), set1 = 0 2 8 10 default IntegerSet is = --- set1 = 0 2 4 6 8 10 12 95 100 set2 = 0 3 6 9 12 set1.union(set2) = 0 2 3 4 6 8 9 10 12 95 100 set1.intersection(set2) = 0 6 12 set1.deleteElement(2) = 0 4 6 8 10 12 95 100 set1.isEqualTo(set1) = true set1.isEqualTo(set2) = false
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
