The following should be in JAVA.... JAVA Create a class called Sets for positive
ID: 3820940 • Letter: T
Question
The following should be in JAVA....
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
Sets.java:
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
public class Sets {
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
ArrayList<Integer> list = new ArrayList<>();
// returns false if element already exist
public boolean addElement(int i) {
if (i <= 0 || set.contains(Integer.valueOf(i))) {
return false;
} else {
set.add(i);
list.add(i);
return true;
}
}
public Integer getElement(int pos) {
if (pos < list.size()) {
return list.get(pos);
} else {
return null;
}
}
public int getSize() {
return list.size();
}
public Set<Integer> getSet() {
return set;
}
public void removeElement(int elem) {
if(list.contains(elem)) {
list.remove(Integer.valueOf(elem));
set.remove(elem);
}
}
public boolean isSubset(Sets b) {
Set<Integer> setB = b.getSet();
for(Integer i: list) {
if(!setB.contains(i)) {
// if element of our set is not in setsB, then it is not subset
return false;
}
}
return true;
}
public boolean isProper(Sets b) {
if(isSubset(b) && (list.size() < b.getSize()))
return true;
else
return false;
}
public void printOrderedPairs(Sets b) {
for(int i: list) {
for(int j: b.getSet()) {
System.out.print("(" + i + "," + j + "), ");
}
}
System.out.println();
}
public void printSet() {
System.out.print("Set: {");
for(int i: list) {
System.out.print(i + ", ");
}
System.out.println();
}
public static void main(String args[]) {
Sets s1 = new Sets();
Sets s2 = new Sets();
// s1: 2, 4, 5, 7
// s2: 2, 4, 5, 7
s1.addElement(2);
s1.addElement(4);
s1.addElement(5);
s1.addElement(7);
s1.addElement(-4); // will not be added
s2.addElement(2);
s2.addElement(4);
s2.addElement(5);
s2.addElement(7);
s1.addElement(0); // will not be added
System.out.println("Set A:");
s1.printSet();
System.out.println("Set B:");
s2.printSet();
System.out.println(" Printing ordered pairs of S1 and S2");
s1.printOrderedPairs(s2);
System.out.println(" SetA subset of SetB: " + s1.isSubset(s2));
System.out.println("SetA proper subset of SetB: " + s1.isProper(s2));
System.out.println(" After removing 5 from Set A");
s1.removeElement(5);
System.out.println(" SetA subset of SetB: " + s1.isSubset(s2));
System.out.println("SetA proper subset of SetB: " + s1.isProper(s2));
}
}
Sample Output:
Set A:
Set: {2, 4, 5, 7,
Set B:
Set: {2, 4, 5, 7,
Printing ordered pairs of S1 and S2
(2,2), (2,4), (2,5), (2,7), (4,2), (4,4), (4,5), (4,7), (5,2), (5,4), (5,5), (5,7), (7,2), (7,4), (7,5), (7,7),
SetA subset of SetB: true
SetA proper subset of SetB: false
After removing 5 from Set A
SetA subset of SetB: true
SetA proper subset of SetB: true
i have provided some extra functions just to debug the code better.