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

Write a Java program as follows: A set (collection of elements without duplicati

ID: 3814419 • Letter: W

Question

Write a Java program as follows:

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. All input is read from the input file and all output is written to 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
O   3
D   0   1   4
O   4
U   3   4   5
Q   5   0
Q   1   2
D   0   5   6
E   6
O   0
O   1
O   2
O   3
O   4
O   5
O   6

Explanation / Answer

Please see the classes below.

MySet.java

//MySet.java
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;


public class MySet implements Set{
  
   private LinkedList<Integer> objects ;
   //Constructor
   public MySet () {
       objects = new LinkedList();
   }

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

   @Override
   public boolean add(Object arg0) {
       Integer x = (Integer) arg0;
       if(this.isMember(x)){
           System.out.println(" Number exists");
           return false;
       }
       this.objects.add(x);
       System.out.println(" Object added successfully");
       return true;
   }


   @Override
   public boolean addAll(Collection arg0) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public void clear() {
       // TODO Auto-generated method stub
      
   }

   @Override
   public boolean contains(Object arg0) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean containsAll(Collection arg0) {
       // TODO Auto-generated method stub
       return false;
   }

   //isEmpty returns true if the set is empty, false otherwise.
   @Override
   public boolean isEmpty() {
       if(null!= this.objects && this.objects.size()>0){
           System.out.println("Not empty");
           return false;
       }
       System.out.println("Empty");
       return true;
   }

   @Override
   public Iterator iterator() {
       // TODO Auto-generated method stub
       return null;
   }

   //remove deletes an element from the set.
   public boolean remove(Object arg0) {
       Integer x= (Integer) arg0;
       this.objects.remove(x);
       System.out.println("Number removed from the set.");
       return true;
   }

   @Override
   public boolean removeAll(Collection arg0) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean retainAll(Collection arg0) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public int size() {
       // TODO Auto-generated method stub
       return 0;
   }

   @Override
   public Object[] toArray() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public Object[] toArray(Object[] arg0) {
       // TODO Auto-generated method stub
       return null;
   }

   public LinkedList<Integer> getObjects() {
       return objects;
   }
  
   public void setObjects(LinkedList<Integer> objects) {
       this.objects = objects;
   }


   ////makeEmpty creates an empty set.
   public void makeEmpty(){
       this.objects = new LinkedList<Integer>();
       System.out.println("Set cleared");
   }
   //isMember returns true if x is in the given set, false otherwise.
   public boolean isMember(int x){
       if(this.objects.contains(x)){
           System.out.println("Is a member");
           return true;
       }
       else{
           System.out.println("Is not a member");
           return false;
       }
   }

   //A union B is the set of all elements either in A or in B or both. A is this.objects
   public    LinkedList<Integer> union( LinkedList<Integer> B)
   {
       LinkedList<Integer> unionSet= new LinkedList<Integer>() ;
       for(int i=0;i<this.objects.size();i++){
           if(null != this.objects.get(i)){
               if(!isMember(unionSet.get(i))){
                   unionSet.add(this.objects.get(i));
               }
           }
       }
      
       for(int i=0;i<B.size();i++){
           if(null != B.get(i)){
               if(!isMember(unionSet.get(i))){
                   unionSet.add(B.get(i));
               }
           }
       }
      
       return unionSet;
   }
   //A intersection B is the set of all elements that are in both, A and B. A is this.objects
   public    LinkedList<Integer> intersection(LinkedList<Integer> B)
   {
       LinkedList<Integer> intersectionSet= new LinkedList<Integer>() ;
       for(int i=0;i<this.objects.size();i++){
           if(null != this.objects.get(i) && B.contains(this.objects.get(i))){
               if(!isMember(intersectionSet.get(i))){
                   intersectionSet.add(this.objects.get(i));
               }
           }
       }
      
      
       return intersectionSet;
   }
   //A difference B is the set of all elements in A but not in B. A is this.objects
   public    LinkedList<Integer> difference( LinkedList<Integer> B)
   {
       LinkedList<Integer> differenceSet= new LinkedList<Integer>() ;
       for(int i=0;i<this.objects.size();i++){
           if(null != this.objects.get(i) && !B.contains(this.objects.get(i))){
               if(!isMember(differenceSet.get(i))){
                   differenceSet.add(this.objects.get(i));
               }
           }
       }
      
      
       return differenceSet;
   }
  
   //toString returns the description of the set as a String.
   public String toString(){
       String retString = "";
       for(int i=0;i<objects.size();i++){
           retString = retString+" "+objects.get(i)+" ";
       }
      
       return retString;
   }
  
   //equals returns true if the two sets are identical (same elements), false otherwise.
   public boolean equals(LinkedList<Integer> B)
   {
       boolean equals =true;
       for(int i=0;i<this.objects.size();i++){
           if(null != this.objects.get(i) && !B.contains(this.objects.get(i))){
               equals = false;
           }
       }
       if(equals){
           for(int i=0;i<B.size();i++){
               if(null != B.get(i) && !this.objects.contains(this.objects.get(i))){
                   equals = false;
               }
           }
       }
       return equals;
      
      
   }
}


MySetTest.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class MySetTest {
   static String inputFileName = "";
   static String outputFileName ="";
   static BufferedReader br = null;
   static FileReader fr = null;
   static BufferedWriter bw = null;
   static FileWriter fw = null;
   public static void main(String [] args){

       Scanner scan = new Scanner(System.in);


       int counter=0;
       int N=0;
       MySet[] mySetArray = null;
       String outputText ="";
       String outputTextToWrite ="";


       //Reading the input and outpout file names
       System.out.println("Please enetr the input file name :");
       inputFileName = scan.nextLine();
       System.out.println("Please enetr the output file name :");
       outputFileName = scan.nextLine();

       //Reading the file and populating the list

       try {

           fr = new FileReader(inputFileName);
           String sCurrentLine;
           br = new BufferedReader(fr);
           sCurrentLine = br.readLine();
           while (sCurrentLine!= null) {

               if(counter == 0){
                   N = Integer.valueOf(sCurrentLine);
                   mySetArray = new MySet[N];//Create an array of N sets, N is the first data in the input file.
                   for(int i=0; i<N ; i++){
                       mySetArray[i] = new MySet();
                   }
               }
               else{

                   String [] commands = sCurrentLine.split(" ");
                   //A x n
                   // Insert the integer x in set S[n]
                   if(commands[0].equalsIgnoreCase( "A")){
                       Integer x= Integer.valueOf(commands[1]);
                       Integer setNum = Integer.valueOf(commands[2]);
                       MySet currSet = mySetArray[setNum];
                       currSet.add(x);
                       outputText = " Added "+x +"to Set " +setNum +"successully";
                   }
                   //R x n
                   // Delete (Remove) x from S[n]
                   else if(commands[0].equalsIgnoreCase( "R")){
                       Integer x= Integer.valueOf(commands[1]);
                       Integer setNum = Integer.valueOf(commands[2]);
                       MySet currSet = mySetArray[setNum];
                       currSet.remove(x);
                       outputText = " Removed"+x +"from Set " +setNum +"successully";
                   }
                   //U n1 n2 n3
                   // S[n3] = S[n1] union S[n2]
                   else if(commands[0].equalsIgnoreCase( "U")){
                       Integer set1 = Integer.valueOf(commands[1]);
                       Integer set2 = Integer.valueOf(commands[2]);
                       Integer set3 = Integer.valueOf(commands[3]);
                       (mySetArray[set3]).setObjects( mySetArray[set1].union((mySetArray[set2]).getObjects()));
                       outputText = " union of Set "+set1+" and Set "+set2 +"is added to Set "+set3;
                       outputText = outputText+" Set "+set3 +" is "+(mySetArray[set3]).toString();

                   }
                   //N n1 n2 n3
                   // S[n3] = S[n1] intersection S[n2]
                   else if(commands[0].equalsIgnoreCase( "N")){
                       Integer set1 = Integer.valueOf(commands[1]);
                       Integer set2 = Integer.valueOf(commands[2]);
                       Integer set3 = Integer.valueOf(commands[3]);
                       (mySetArray[set3]).setObjects( mySetArray[set1].intersection((mySetArray[set2]).getObjects()));
                       outputText = " intersection of Set "+set1+" and Set "+set2 +"is added to Set "+set3;
                       outputText = outputText+"Set "+set3 +" is "+(mySetArray[set3]).toString();
                   }
                   //D n1 n2 n3
                   // S[n3] = S[n1] difference S[n2]
                   else if(commands[0].equalsIgnoreCase("D")){
                       Integer set1 = Integer.valueOf(commands[1]);
                       Integer set2 = Integer.valueOf(commands[2]);
                       Integer set3 = Integer.valueOf(commands[3]);
                       (mySetArray[set3]).setObjects( mySetArray[set1].difference((mySetArray[set2]).getObjects()));
                       outputText = " difference of Set "+set1+" and Set "+set2 +"is added to Set "+set3;
                       outputText = outputText+"Set "+set3 +" is "+(mySetArray[set3]).toString();
                   }
                   //B x n
                   // Does x belong in set S[n] ?
                   else if(commands[0].equalsIgnoreCase("B")){
                       Integer x= Integer.valueOf(commands[1]);
                       Integer setNum = Integer.valueOf(commands[2]);
                       MySet currSet = mySetArray[setNum];
                       if(currSet.isMember(x)){
                           outputText = " "+x+" Is a Member of Set"+setNum;
                       }
                       else{
                           outputText =" "+x+" Is not a Member of Set"+setNum;
                       }
                   }
                   //O n
                   // Output the set S[n]
                   else if(commands[0].equalsIgnoreCase("O")){
                       Integer setNum = Integer.valueOf(commands[1]);
                       MySet currSet = mySetArray[setNum];
                       outputText = " Set "+setNum+" is "+ currSet.toString();
                   }
                   //E n   
                   // Is set S[n] empty?
                   else if(commands[0].equalsIgnoreCase("E")){
                       Integer setNum = Integer.valueOf(commands[1]);
                       MySet currSet = mySetArray[setNum];

                       if(currSet.isEmpty()){
                           outputText = " Set "+setNum +" is empty";
                       }
                       else{
                           outputText = " Set "+setNum +" is not empty";
                       }
                   }
                   //Q n1 n2
                   // Are two sets S[n1] and S[n2] equal?
                   else if(commands[0].equalsIgnoreCase("Q")){
                       Integer setNum1 = Integer.valueOf(commands[1]);
                       Integer setNum2 = Integer.valueOf(commands[2]);
                       System.out.println("Equal ?:");
                       System.out.println();
                       if(mySetArray[setNum1].equals(mySetArray[setNum2])){
                           outputText = " Set "+setNum1 +" and Set "+setNum2+" are equal";
                       }
                       else{
                           outputText = " Set "+setNum1 +" and Set "+setNum2+" are not equal";
                       }
                   }
               }
               sCurrentLine = br.readLine();
               counter ++;
               outputTextToWrite = outputTextToWrite + outputText;
              

           }

       } catch (IOException e) {

           e.printStackTrace();

       }

       writeToOutputFile(outputTextToWrite);


   }

   //Writing to output file
   public static void writeToOutputFile(String content){
      
       try {

           fw = new FileWriter(outputFileName);
           bw = new BufferedWriter(fw);
           bw.write(content);
           bw.flush();

           System.out.println("Done");

       } catch (IOException e) {

           e.printStackTrace();

       }
   }

}


input.txt

10
E 0
A 24 0
A 423 0
A 76 0
A 12 0
A 8 0
A 64 0
O 0


output.txt

Set 0 is empty
Added 24to Set 0successully
Added 423to Set 0successully
Added 76to Set 0successully
Added 12to Set 0successully
Added 8to Set 0successully
Added 64to Set 0successully
Set 0 is 24 423 76 12 8 64

program output:
Please enetr the input file name :
input.txt
Please enetr the output file name :
output.txt

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