Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// To complete this lab you must: // 1.) fill out the union() method // 2.) fill out the intersection() method // 3.) fill out

image text in transcribedimage text in transcribedimage text in transcribed

// To complete this lab you must: // 1.) fill out the union() method // 2.) fill out the intersection() method // 3.) fill out the complement() method // 4.) fill out the subtraction() method with only union, intersection, and/or complement // 5.) fill out the complement() method // 6.) demonstrate to your TA public class SetOperations { //Declaring 2 sets Set setA; Set setB; public SetOperations() { setA = new Set(); setB = new Set(); //setting up Set A in the constructor setA.addInt(1); setA.addInt(3); setA.addInt(5); setA.addInt(6); setA.addInt(9); setA.addInt(10); //setting up Set B in the constructor setB.addInt(0); setB.addInt(1); setB.addInt(4); setB.addInt(5); setB.addInt(10); setB.addInt(7); setB.addInt(5); } //TO BE FILLED OUT public Set union(Set set1, Set set2) { Set unionSet = new Set(); // Union is similar to || (or) // If setA || setB have 4, then the union has 4 // If both setA and setB do not have 5, the union does not have 5 //Example: // if(set1.checkInt(1) || set2.checkInt(1)) // unionSet.addInt(1); return unionSet; } //TO BE FILLED OUT public Set intersection(Set set1, Set set2) { Set intersectionSet = new Set(); // Intersection is similar to && // If setA && setB have 4, then the intersection has 4 // If setA has 3 but setB does not, the intersection does not return intersectionSet; } //TO BE FILLED OUT public Set complement(Set set) { Set complementSet = new Set(); // Complement means opposite // So if set A has 1, the complement does not have 1 // If set A does not have 2, the complement does have 2 return complementSet; } //the following methods can only be filled out using a combination of union, intersection, and/or complement //TO BE FILLED OUT public Set subtraction(Set set1, Set set2) { //fill out using a combination of union, intersection, and/or complement only Set subtractionSet = new Set(); //Example //(subtracts all values from one set to another) //SetA = {1, 3, 5} //SetB = {2, 4, 5} //A - B = {1, 3} *because 5 exists in both sets, it is removed return subtractionSet; } //TO BE FILLED OUT public Set symmetricDifference(Set set1, Set set2) { //fill out using a combination of union, intersection, complement, and/or subtraction only Set symSet = new Set(); //similar to XOR logic, one or the other but not both //an easy way to think about this is: we want all of the values in both A and B. Then minus what A and B have in common. return symSet; } public void run() { System.out.println("Set A:\t\t"+setA.toString()); System.out.println("Set B:\t\t"+setB.toString()); System.out.println("Union:\t\t"+union(setA, setB).toString()); System.out.println("Intersect:\t"+intersection(setA, setB).toString()); System.out.println("A Complement:\t"+complement(setA).toString()); System.out.println("B Complement:\t"+complement(setB).toString()); System.out.println("Neither A or B:\t"+intersection(complement(setA), complement(setB)).toString()); System.out.println("A subtract B:\t"+subtraction(setA, setB).toString()); System.out.println("Symmetric Difference of A and B:\t"+symmetricDifference(setA, setB).toString()); } public static void main(String[] args) { SetOperations a = new SetOperations(); a.run(); } } 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 Set.java: A class that simulates a mathematical set of integers ranging from 0-99. This set can add, remove, and check if a specific integer is a member of the set. * */ // Note: The set class contains a boolean array of 11. If an integer is a member of the set, the index of that integer is set to true, otherwise it is false. public class Set { //define array size and array public static final int ARRAY_SIZE = 11; private boolean[] set = new boolean[ARRAY_SIZE]; //method that adds an integer to the set public void addInt(int number) { try { set[number] = true; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Invalid number"); } } //a method that can remove and integer from the set public void removeInt(int number) { try { set[number] = false; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Invalid number"); } } //a method that returns the status of an integer (true/false) public boolean checkInt(int index) { try { boolean result = set[index]; return result; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Invalid number"); return false; } } //method that prints the set public void println() { for(int i = 0; i   CIS 181 - Lab4 Set Operations Overview Today you will be working with sets. These sets can only contain numbers 0 to 10, and may not include duplicates. Lucky for you, the Set class has already been filled out. The set class is pretty basic. You can add integers to the set with the addInt () method, remove integers from the set with the removeInt () method, or check if an set has a specific integer with the checkInt() method. This class utilizes a boolean array with a size of 11 (range of 0-10). If the set contains a number, the element at that index is set to true. Otherwise, it will be false Example To demonstrate, look at a smaller example, say a set of integers from 0...4. We will call this SetA. We create the empty set, as shown in Figure 1. Notice that every index is false. This is ause the set has no numbers in it. Now let's pretend we have used our addInt () method to add 2 and 4 to our set. SetA.addInt (2) SetA.addInt (4) Figure 2 Now look at Figure 2. Since we have added the numbers 2 and 4 to our set, the elements at indices 2 and 4 have been set to true. Elements at 0, 1, and 3 remain false because those numbers are still not included in the set. So, you are asked to finish the main class called SetOperations. Since we are developing a class for the use of sets, it should allow for the usual set operations. Provided is a file with the structure for the class, including the method stubs. Goals  Write methods according to the lab specifications. * Resolve problem understanding issues prior to task initiation by formulating appropriate questions Tasks 1. You may form a group with your classmates, and review tasks with questions and answers 2. Individually meet with the TA/mentor to resolve issues. 3. In Eclipse, create a new Java project called "Lab 4", and import the 2 files (Set.java and SetOperations.java) Complete the following methods in the SetOperations class: 4. a. b. c. public Set union(Set setl, Set set2) public Set intersection(Set setl, Set set2) public Set complement(Set set) 5. Complete the following methods in the SetOperations class by only using a combination of union, intersection, and/or complement a. public Set subtraction(Set setl, Set set2) b. public Set symmetricDifference(Set setl, Set set2) *you may use subtraction) During the method implementation, you need to design test cases to verify if your implementation is correct. Test cases should be recorded in the file TestlntSet.java. When completed and checked-off by TA, only the source file SetOperations.java must be uploaded to myCourses. 6. Reference For examples, sets will only have a possible range of 1-5 Union: (similar to OR logic) SetA- 1,3,5 SetB (2, 4,5 A (union) B-,2, 3, 4, 5 Intersect: (similar to AND logic) SetA ,3, 5 SetB (2,4, 5 Complement: (similar to NOT logic) SetA 1, 3, 5 A- 12,4 Subtraction (also known as set difference): (subtracts all values from one set to another) SetA- 1,3, 5 SetB (2, 4,5 A-B-{ l, 3} *because 5 exists in both sets, it is removed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

For Exercise 15.33 give all aliases for the six main effects.

Answered: 1 week ago

Question

3. How can we confi rm both ourselves and others?

Answered: 1 week ago

Question

2. In what ways can confl ict enrich relationships?

Answered: 1 week ago