***JAVA problem IntegerSet The purpose of this assignment is to practice JAVA im
ID: 3679952 • Letter: #
Question
***JAVA problem
IntegerSet
The purpose of this assignment is to practice JAVA implementation for a generic class: Set, an unordered container of integer values.
Part I: Generic implementation (70pts)
Given a integer k and an IntegerSet S, the following operations should be defined:
The following program is a trivial example of how your class will be tested.
Part II: Union/Intersection (30pts)
Given two IntegerSet object A and B, the following operations should be defined:
S.contains(k) returns true iff k is one of the integer values contained in S S.insert(k) adds k to the set of integer values contained in S S.remove(k) removes k from the set of integers contained in S S.empty() returns true iff S is the empty set S.size() returns the cardinality of the set S (or |S|)Explanation / Answer
package org.students;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class IntegerSet
{
Set<Integer> S = new HashSet<Integer>();
Set<Integer> union ;
Set<Integer> intersection;
public boolean contains(int k)
{
return S.contains(k);
}
public boolean empty()
{
return S.isEmpty();
}
public void insert(int k)
{
S.add(k);
}
public void remove(int k)
{
S.remove(k);
}
public int size()
{
return S.size();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
package org.students;
import java.util.HashSet;
import java.util.Set;
import org.students.*;
public class Test1{
public static void main(String[] args)
{
IntegerSet is=new IntegerSet();
//Check whether this hash set is empty or not.
System.out.println("Is The IntegerSet is Emply:"+is.empty());
//insert element into hash set object
is.insert(5);
is.insert(4);
is.insert(9);
is.insert(3);
is.insert(6);
System.out.println("Size of this IntegerSet |S| is:"+is.size());
// remove element from hash set
is.remove(9);
System.out.println(is.contains(4) ? "IntegerSet(is) contains 4":"is does not contain 4");
System.out.println(is.contains(3) ? "IntegerSet(is) contains 3":"is does not contain 3");
}
}
--------------------------------------------------------------------------------------------------------------------------------------
output:
Is The Set is Emply:true
Size of this HasSet5
|S| = 4
Set(is) contains 4
Set(is) contains 3
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.