I HAVE THE FOLLOWING ASSIGNMENT WHICH REQUIRES ME TO ADD WHAT IS ASKED BELOW TO
ID: 3844177 • Letter: I
Question
I HAVE THE FOLLOWING ASSIGNMENT WHICH REQUIRES ME TO ADD WHAT IS ASKED BELOW TO A CODE THAT I ALREADY HAVE......PLEAE HELP AND MAKE COMMENTS AS ADDING THE CONSTRUCTORS AND METHOD. THIS IS FOR DISCRETE..... JAVA PLZ. (don't copy and paste plz)
REQUIREMENT:
Extend the Sets class from lab 1.
Remember that has sets for positive integers that you will need the following functions:
addElement , will take a positive integer and add it to the set
Note: you need to change it so that it will not add a duplicate element
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
(You can keep other functions in if need be).
Add the functions of:
unionOps, that takes a Sets object and does a union with the current set
intersection, that takes a Sets object and does an intersection with the current set
printSet, will print the Sets object contents
It is recommended to add a constructor that will take an array and add its elements to the current set.
Define a set a with certain values, like 1, 2, 3, 4 and set b with certain values like 3, 4, 5, 6
Have main create three objects: setA with set a, setB with set b, and setC with set a.
Print the setA, setB, setC
Do an union of setA with setB (setA will be changed) then print setA
Do an intersection of setC (setC will be changed) with setB then print setC...
-------COPY THIS CODE AND ADD ON TO IT WHAT IS LISTED ABOVE----------------------------------------------------------
import java.util.*;
public class Sets
{
//Hash set & Array list for integers
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;
}
//additional method added will remove
public void removeElement(int elem) {
if(list.contains(elem)) {
list.remove(Integer.valueOf(elem));
set.remove(elem);
}
}
//isSubset
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;
}
//isProper
public boolean isProper(Sets b) {
if(isSubset(b) && (list.size() < b.getSize()))
return true;
else
return false;
}
//printOrderedPairs
public void printOrderedPairs(Sets b) {
for(int i: list) {
for(int j: b.getSet()) {
System.out.print("(" + i + "," + j + "), ");
}
}
System.out.println();
}
//print set
public void printSet() {
System.out.print("Set: {");
for(int i: list) {
System.out.print(i + ", ");
}
System.out.println();
}
}
////////////////////Main///////////////////
public class Main{
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));
}
}
Explanation / Answer
Hi,
Please see the updated classes. Please comment for any queries/feedbacks.
Thanks.
Sets.java
import java.util.*;
public class Sets
{
//Hash set & Array list for integers
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
public Sets(){
super();
}
/**
* constructor that will take an array and add its elements to the current set.
* @param inputArr
*/
public Sets(int[] inputArr){
for(int i: inputArr){
list.add(i);
}
}
// 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 -1;//return -1 if bad position
}
}
/**
* return the size of the set
* @return
*/
public int getSize() {
return list.size();
}
public Set<Integer> getSet() {
return set;
}
//additional method added will remove
public void removeElement(int elem) {
if(list.contains(elem)) {
list.remove(Integer.valueOf(elem));
set.remove(elem);
}
}
//isSubset
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;
}
//isProper
public boolean isProper(Sets b) {
if(isSubset(b) && (list.size() < b.getSize()))
return true;
else
return false;
}
//printOrderedPairs
public void printOrderedPairs(Sets b) {
for(int i: list) {
for(int j: b.getSet()) {
System.out.print("(" + i + "," + j + "), ");
}
}
System.out.println();
}
/**
* will print the Sets object contents
*/
public void printSet() {
System.out.print("Set: {");
for(int i: list) {
System.out.print(i + ", ");
}
System.out.println("}");
System.out.println();
}
/**
* takes a Sets object and does a union with the current set
* @param b
*/
public void unionOps(Sets b){
for(int i: b.getSet()) {
if(!list.contains(i)){
list.add(i);
}
}
}
/**
* takes a Sets object and does an intersection with the current set
* @param b
* @return
*/
public Sets intersection(Sets b){
Sets intersection = new Sets();
for(int i: b.getSet()) {
if(list.contains(i)){
intersection.addElement(i);
}
}
return intersection;
}
}
Main.java
////////////////////Main///////////////////
public class Main{
public static void main(String args[]) {
Sets setA = new Sets();
Sets setB = new Sets();
Sets setC = new Sets();
//Define a set a with certain values, like 1, 2, 3, 4 and set b with certain values like 3, 4, 5, 6
// setA: 1, 2, 3, 4
// setB: 3, 4, 5, 6
setA.addElement(1);
setA.addElement(2);
setA.addElement(3);
setA.addElement(4);
setB.addElement(3);
setB.addElement(4);
setB.addElement(5);
setB.addElement(6);
setC.addElement(5);
setC.addElement(6);
setC.addElement(8);
setC.addElement(9);
//Print the setA, setB, setC
System.out.println("Set A:");
setA.printSet();
System.out.println("Set B:");
setB.printSet();
System.out.println("Set C:");
setC.printSet();
//Do an union of setA with setB (setA will be changed) then print setA
setA.unionOps(setB);
System.out.println("After union of Set A with Set B");
setA.printSet();
//Do an intersection of setC (setC will be changed) with setB then print setC...
setC = setC.intersection(setB);
System.out.println("After intersection of Set C with Set B");
setC.printSet();
// System.out.println(" Printing ordered pairs of S1 and S2");
// setA.printOrderedPairs(setB);
//
// System.out.println(" SetA subset of SetB: " + setA.isSubset(setB));
// System.out.println("SetA proper subset of SetB: " + setA.isProper(setB));
//
// System.out.println(" After removing 5 from Set A");
// setA.removeElement(5);
//
// System.out.println(" SetA subset of SetB: " + setA.isSubset(setB));
// System.out.println("SetA proper subset of SetB: " + setA.isProper(setB));
}
}
Sample output:
Set A:
Set: {1, 2, 3, 4, }
Set B:
Set: {3, 4, 5, 6, }
Set C:
Set: {5, 6, 8, 9, }
After union of Set A with Set B
Set: {1, 2, 3, 4, 5, 6, }
After intersection of Set C with Set B
Set: {5, 6, }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.