Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the constructor and the five methods in the SetOperations class. Output Examples: Set A: [A, B, C, D, E] Set B: [E, F, G,

Complete the constructor and the five methods in the SetOperations class.

Output Examples:

Set A:         [A, B, C, D, E]

Set B:         [E, F, G, H]

Union:         [A, B, C, D, E, F, G, H]

Intersection:  [E]

SymmetricDiff: [A, B, C, D, F, G, H]

Diff A - B:    [A, B, C, D]

Diff B - A:    [F, G, H]

 

Set A:         [11, 15, 17, 19, 3, 5, 6, 8]

Set B:         [11, 15, 17, 2, 3, 8, 9]

Union:         [11, 15, 17, 19, 2, 3, 5, 6, 8, 9]

Intersection:  [11, 15, 17, 3, 8]

SymmetricDiff: [19, 2, 5, 6, 9]

Diff A - B:    [19, 5, 6]

Diff B - A:    [2, 9]

 

Main.java

import java.util.Set;
import java.util.TreeSet;
import java.util.Arrays;

class Main {

   public static void main(String[] args) {

       String[] data = new String[6];
       data[0] = "A B C D E";
       data[1] = "E F G H F";
       data[2] = "19 5 11 3 15 17 8 6";
       data[3] = "8 2 11 15 9 17 3";
       data[4] = "1 5 9 7 5 3";
       data[5] = "5 4";

       for (int i = 0; i < data.length; i += 2) {
           SetOperations setOps = new SetOperations(data[i], data[i + 1]);
           System.out.println("Set A:         " + setOps.getSetA());
           System.out.println("Set A:         " + setOps.getSetB());
           System.out.println("Union:         " + setOps.union());
           System.out.println("Intersection:  " + setOps.intersection());
           System.out.println("SymmetricDiff: " + setOps.symmetricDiff());
           System.out.println("Diff A - B:    " + setOps.diffAMinusB());
           System.out.println("Diff B - A:    " + setOps.diffBMinusA());
           System.out.println();
       }
   }
}

 

SetOperations.java

import java.util.Set;
import java.util.TreeSet;
import java.util.Arrays;

public class SetOperations {

   private Set setA;
   private Set setB;

   /**
    * Initialize the two set instance variables to TreeSets.Add elements from
    * String A to setA and elements from String B to setB
    *
    * @param A String elements to be added to setA
    * @param B String elements to be added to setB
    */
   public SetOperations(String A, String B) {

   }

   public Set getSetA() {
       return setA;
   }

   public Set getSetB() {
       return setB;
   }

   /**
    * Union
    *
    * @return Set containing all elements from both sets
    */
   public Set union() {

       return null;
   }

   /**
    * Intersection
    *
    * @return Set containing elements that occur in BOTH sets
    */
   public Set intersection() {

       return null;
   }

   /**
    * Symmetric Difference
    *
    * @return Set containing elements that occur in either but not in both
    */
   public Set symmetricDiff() {

       return null;
   }

   /**
    * Difference setA - setB
    *
    * @return Set containing elements that occur in setA but not in setB
    */
   public Set diffAMinusB() {

       return null;
   }

   /**
    * Difference setB - setA
    *
    * @return Set containing elements that occur in setB but not in setA
    */
   public Set diffBMinusA() {

       return null;
   }
}
 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

SetOperationsjava import javautilSet import javautilTreeSet import javautilArrays public class SetOperations private Set setA private Set setB Initial... 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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions