Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Create a class called Sets for positive integers that will have the followi

ID: 3818085 • Letter: J

Question

JAVA

Create a class called Sets for positive integers that will have the following functions:

addElement , will take a positive integer and add it to the set

getElement, will take a position number and return the element at the position (return -1 if bad position)

getSize, return the size of the set

isSubset, takes a sets object (call it b) and see if the current set (call it a) is a subset of b. If so, return a Boolean true, else return a Boolean false

isProper, takes a sets object (call it b) and see if the current set (call it a) is a proper subset of b. Note, use the isSubset function first then see if b has at least one more element than a

printOrderedPairs, takes a sets object (call it b) and takes the current set (call it a) and prints the ordered pairs of of A X B

Have main create two objects: setA and setB.

Input the values into setA (end with a 0 or negative) and input the values into setB (end with 0 or negative).

Print the ordered pairs using printOrderedPairs.

Print if setA is a subset of setB.

Then print if setA is a proper subset of setB.

Explanation / Answer

Answer -

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

package my_chegg_package;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Sets {
   Set<Integer> test = new HashSet<Integer>();

  
void addElement(Integer x)
{
   test.add(x);
  
}
int getElement(int index)
{
   Iterator<Integer> it = test.iterator();
   int i = 0;
   Integer current = null;
   while(it.hasNext() && i < index) {
   current = it.next();
   i++;
   }
   return current;
}
   int getSize()
   {
       int size =test.size();
       return size;
   }
   boolean contains(Set<Integer> s) {
   if (this.test.containsAll(s))
   return true;
   else
   return false;
   }
     
   public void printOrderedPairs(Sets a,Sets b)
   {
         
       int len1=a.test.size();
       int len2=b.test.size();
       System.out.println("Ordered Pairs ");
       for(int i=1;i<=(len1*len2);i++)
       {
           for(int j=1;j<=len2;j++)
           System.out.println("["+a.getElement(i)+","+b.getElement(j)+"]");
             
       }
       System.out.println(" ");
   }
   public static void main(String args[]) {
       Sets setA = new Sets();   
       Sets setB = new Sets();
       setA.addElement(3);
       setA.addElement(5);
       setA.addElement(8);
       setB.addElement(3);
       setB.addElement(4);
       Sets obj=new Sets();
       obj.printOrderedPairs(setA,setB);
       if(setA.contains(setB.test))
       {
           System.out.println("Set B is subset Of Set A ");
       }
       else
       {
           System.out.println("Set B is not subset Of A ");
             
       }
   }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT -

Ordered Pairs

[3,3]
[3,4]
[5,3]
[5,4]
[8,3]
[8,4]
[8,3]
[8,4]
[8,3]
[8,4]
[8,3]
[8,4]


Set B is not subset Of A