Question
1. Write a generic class named ConstrainedSet which has the following private attributes: [ 1 Pt ] smallest (which is final and of some type
1. Write a generic class named ConstrainedSet which has the following private attributes: [1 Pt]
- smallest (which is final and of some type that has Comparable as an upper bound): Cannot be null
- elements (HashSet whose elements are of the same type as the type of the attribute smallest)
2. Provide a one-argument constructor that initializes smallest. This constructor initializes elements to an empty HashSet [0.5 Pt]
3. Provide the getter method getSmallest() to return smallest [0.5 Pt]
4. Write a method named add() that takes an element and adds it to the HashSet elements if and only if this element is greater than or equal to smallest (follows smallest in the natural order or equals it) [1.5 Pts]
5. Write a method named subset() that takes an argument named elem of the same type as the type of smallest and returns an object of type ConstrainedSet containing all the elements in the HashSet elements that are greater than or equal to the argument elem. [1.5 Pt]
6. Override the method toString() such that it returns a string of the form: [0.5 Pt]
{elem1, ., elemN,}
where elem1 and elemN are the first element and the last element in the HashSet elements.
For example if the elements of the HashSet elements is: 5, 3, 10, 15, it returns the string:
{5, 3, 10, 15,}
7. Provide a method named testConstrainedSet() where you will: [2 Pts]
- Create an object of ConstrainedSet having Integer elements and pass 20 to the constructor (20 is the smallest element).
- Add the elements: 15, 100, 49, 20, 18, to the ConstrainedSet Object created in the previous step.
- Print the elements of the ConstrainedSet Object.
- Call the method subset of the ConstrainedSet object with the argument 45 (to return you a subset containing the elements that are greater than or equal to 45) and then print the elements of this subset.
- Create another object of ConstrainedSet having String elements and pass Se to the constructor (this is to only allow elements that come after Se alphabetically to be added to this object).
- Add the elements: Turki, Salem, Ahmed, Mohammed, Ziyad, Selim, to this object.
Print the elements of the ConstrainedSet object after adding the elements in the previous step.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started