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

A set (collection of elements without duplication) as a mathematical model is de

ID: 3809356 • Letter: A

Question

A set (collection of elements without duplication) as a mathematical model is defined by the java interface below.

public interface Set

{ public boolean isEmpty();          // Is the set empty?

public void makeEmpty();          // Make this set empty.

public boolean isMember(int x); // Is x a member of this set?

public void add(int x); // Insert an element x in this set.

public void remove(int y);          // Delete an element y from this set.

public void union(Set other, Set result); // result = "this" UNION other

public void intersection (Set other, Set result); // result = "this" INTERSECTION other

public void difference (Set other, Set result); // result = "this" - other

public String toString(); // Overridden toString method that returns the set description as

                                                    // a String.

public boolean equals(Object other); // Overridden equals method to check equality of two sets.

}

makeEmpty creates an empty set.

isEmpty returns true if the set is empty, false otherwise.

isMember returns true if x is in the given set, false otherwise.

add inserts an element in the set (without duplication).

remove deletes an element from the set.

A union B is the set of all elements either in A or in B or both.

A intersection B is the set of all elements that are in both, A and B.

A difference B is the set of all elements in A but not in B.

toString returns the description of the set as a String.

equals returns true if the two sets are identical (same elements), false otherwise.

You are required to implement the mathematical model Set of integers as a class that implements the above interface. You must implement this structure as a linked list of elements kept in the increasing sequence. You must implement "isMember", "add", and “intersection” methods using recursive algorithms. Others can be implemented as you wish.

To test your implementation, write the test program to do the following. As always, all input is read from the input file and all output is produced in the output file. Prompt the user to acquire the names of input and output files.

Create an array of N sets, N is the first data in the input file. // S[0] through S[N-1]

Read commands from the input file, and execute them. After each command, output what was done (like, inserted element xxx in set n) and produce the output relevant to that command. Make sure that your output is self-explanatory and paper conserving. That means, do not output the elements of the set one per line, but output many elements on the same line.

The commands are specified in the following format:

A x n                      // Insert the integer x in set S[n]

R x n                      // Delete (Remove) x from S[n]

U n1 n2 n3          // S[n3] = S[n1] union S[n2]

N n1 n2 n3          // S[n3] = S[n1] intersection S[n2]

D n1 n2 n3          // S[n3] = S[n1] difference S[n2]

B x n                      // Does x belong in set S[n] ?

O n                         // Output the set S[n]

E n                          // Is set S[n] empty?

Q n1 n2                // Are two sets S[n1] and S[n2] equal?

INPUT FILE:

10           // This is the number of Sets in the array of Sets
E   0
A   24   0
A   423   0
A   76   0
A   12   0
A   8   0
A   64   0
O   0
E   0
B   76   0
R   76   0
R   68   0
B   24   0
B   12   0
B   76   0
A   111   0
O   0
A   76   1
A   55   1
A   12   1
A   43   1
A   876   1
A   98   1
A   64   1
A   34   1
O   1
B   111   1
B   76   1
U   0   1   2
O   2
N   0   1   3

Explanation / Answer

// Set.java

import java.util.TreeSet;

public interface Set {
  
  
   public TreeSet<Integer> setList = new TreeSet<Integer>();
   public boolean isEmpty(); // Is the set empty?

   public void makeEmpty(); // Make this set empty.

   public boolean isMember(int x); // Is x a member of this set?

   public void add(int x); // Insert an element x in this set.

   public void remove(int y); // Delete an element y from this set.

   public void union(Set other1,Set other2, Set result); // result = "this" UNION other

   public void intersection(Set other1,Set other2, Set result); // result = "this"
                                                       // INTERSECTION other

   public void difference(Set other1,Set other2, Set result); // result = "this" - other

   public String toString(); // Overridden toString method that returns the set
                               // description as
                               // a String.

   public boolean equals(Object other); // Overridden equals method to check
                                           // equality of two sets.
}

// SetOfIntegers.java


public class SetofIntegers implements Set{

   //public List<LinkedList> nSets = new ArrayList<LinkedList>();
  
   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return setList.isEmpty();
   }

   @Override
   public void makeEmpty() {
       setList.clear();
      
   }

   @Override
   public boolean isMember(int x) {
       return setList.contains(x);
   }

   @Override
   public void add(int x) {
       setList.add(x);
      
   }

   @Override
   public void remove(int y) {
       setList.remove(y);
      
   }

   @Override
   public void union(Set other1, Set other2, Set result ) {
       Set temp = other2;
       for(Integer x : other1.setList){
           other2.add(x);
       }
       other2= temp;
      
      
   }

   @Override
   public void intersection(Set other1,Set other2 , Set result) {
       Set temp = other2;
       for(Integer x : other1.setList){
           if(!other2.isMember(x)){
               other2.remove(x);
           }
       }
       other2 = temp;
   }

   @Override
   public void difference(Set other1, Set other2, Set result) {
      
       Set temp = other2;
      
       for(Integer x : other2.setList){
           if(other1.isMember(x)){
               other2.remove(x);
           }
       }
       result= other2;
       other2 =temp;
   }
   public String toString(Set other1){
       String set1 = null;
       set1 = other1.toString();
       return set1;
   }
}

//SetOfIntegersTester

import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;
public class SetOfIntegersTester {
public static void main(String[] args) throws Exception {
String inf = JOptionPane.showInputDialog("d:\input.txt");
// Input file is src/<name of the file>
FileReader inFile = new FileReader(inf);
Scanner in = new Scanner(inFile);
List<Set> sets = new ArrayList<Set>();
  
  
String outf = JOptionPane.showInputDialog("d:\ooutput.txt");
PrintWriter outFile = new PrintWriter(outf);
  
while (in.hasNextLine()) {
           Set s = new SetofIntegers();
String line = in.nextLine();
String[] split = line.split("\s+");
if(split[0].equals( "A")){
   s.add(Integer.parseInt(split[1]));
   sets.add(Integer.parseInt(split[2]), s);
   line =Integer.parseInt(split[2])+" is added to the Set";
}else if( split[0].equals("R")){
  
   String jj ="";
  
   Set sa =sets.get(Integer.parseInt(split[2]));
   sa.setList.remove(Integer.parseInt(split[1]));
   line = Integer.parseInt(split[2])+" is removed from the Set";
}else if(split[0].equals("U")){
   s.union(sets.get(Integer.parseInt(split[1])),sets.get(Integer.parseInt(split[2])),sets.get(Integer.parseInt(split[3])));
   line = "Union Command Executed";
}else if(split[0].equals("N")){
    s.intersection(sets.get(Integer.parseInt(split[1])),sets.get(Integer.parseInt(split[2])),sets.get(Integer.parseInt(split[3])));
    line = "Intersection Command Executed";
}else if(split[0].equals("D")){
    s.difference(sets.get(Integer.parseInt(split[1])),sets.get(Integer.parseInt(split[2])),sets.get(Integer.parseInt(split[3])));
    line="Difference Command Executed";
}else if(split[0].equals("B")){
    s.isMember(Integer.parseInt(split[1]));
    line=Integer.parseInt(split[1])+" is checked as a member of set";
}else if(split[0].equals("E")){
    s.isEmpty();
    line = "Checked whether the Set is Empty";
}
  
outFile.println(line + " ");
}
outFile.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote