Given: public class IntegerSet {boolean []set=new boolean[101]; public IntegerSe
ID: 3528730 • Letter: G
Question
Given: public class IntegerSet {boolean []set=new boolean[101]; public IntegerSet(int a[]) {int i; for(i=0;i<101;i++) {set[i]=false; } for(i=0;i<101;i++) set[a[i]]=true; } public IntegerSet() {int i; for(i=0;i<101;i++); set[i]=false; } public void setSet(int a[]) {int i; for(i=0;i<101;i++) {set[i]=false; } for(i=0;i<101;i++) set[a[i]]=true; } public boolean[] getSet() {return set; } public void insertElement(int k) {set[k]=true; } public void InsertElement(int k) {if(k<0|| k>100) System.out.println("element out of range"); else set[k]=true; } public void DeleteElement(int k) {if(k<0|| k>100) System.out.println("element out of range"); else set[k]=false; } public void Print() {System.out.print("{"); boolean first=true; for(int i=0;i<101;i++) if(set[i]) if(!first) System.out.print(","+i); else {System.out.print(i); first=false; } System.out.println("}"); } public IntegerSet Union(IntegerSet y) {IntegerSet t=new IntegerSet(); {for(int i=0;i<101;i++) t.set[i]=set[i]||y.set[i]; } return t; } } Make the following modifications in Java Coding: Provide an Intersection(IntegerSet y) member method which creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set's array is set to false if that element is false in either or both of the existing sets, and an element of the third set's array is set to true if that element is true in both of the existing sets). NOTE: Of the two existing sets one set exists within the object whose Intersection method is being called and the other set is given to the Intersection method as argument (y). The Intersection method returns the third set that it creates inside it to represent the intersection of the two existing sets. Provide an IsEqualTo(IntegerSet y) member method that determines whether two existing sets are equal. NOTE: Of the two existing sets one set exists within the object whose IsEqualTo method is being called and the other set is given to the IsEqualTo method as argument (y). The IsEqualTo method returns true or false. Now write a driver program to test your IntegerSet class. Instantiate several IntegerSet objects. Test that all your constructors and member methods work properly.Explanation / Answer
public class IntegerSet
{
boolean []set=new boolean[101];
public IntegerSet(int a[])
{
int i;
for(i=0;i<101;i++)
{
set[i]=false;
}
for(i=0;i<101;i++)
set[a[i]]=true;
}
public IntegerSet()
{
int i;
for(i=0;i<101;i++)
set[i]=false;
}
public void setSet(int a[])
{
int i; for(i=0;i<101;i++)
{
set[i]=false;
}
for(i=0;i<101;i++)
set[a[i]]=true;
}
public boolean[] getSet()
{
return set;
}
public void insertElement(int k)
{
set[k]=true;
}
public void InsertElement(int k)
{
if(k<0|| k>100)
System.out.println("element out of range");
else set[k]=true;
}
public void DeleteElement(int k)
{
if(k<0|| k>100)
System.out.println("element out of range");
else set[k]=false;
} public void Print()
{System.out.print("{");
boolean first=true;
for(int i=0;i<101;i++)
if(set[i])
if(!first)
System.out.print(","+i);
else {System.out.print(i);
first=false;
}
System.out.println("}");
}
public IntegerSet Union(IntegerSet y)
{
IntegerSet t=new IntegerSet();
{
for(int i=0;i<101;i++)
t.set[i]=set[i]||y.set[i];
}
return t;
}
public IntegerSet Intersection(IntegerSet y)
{
IntegerSet t=new IntegerSet();
{
for(int i=0;i<101;i++)
t.set[i]=set[i] && y.set[i];
}
return t;
}
public boolean IsEqualTo(IntegerSet y)
{
for(int i=0;i<101;i++)
{
if(set[i] != y.set[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
IntegerSet set1=new IntegerSet();
IntegerSet set2=new IntegerSet();
set1.insertElement(10);
set2.insertElement(20);
System.out.println(" is set1 is equal to set2 " + set1.IsEqualTo(set2));
IntegerSet inter_set = set1.Intersection(set2);
IntegerSet union_set = set1.Union(set2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.