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

PLEASE USE WHAT I HAVE ALREADY STARTED for SetOfIntegers.java The methods that a

ID: 3814136 • Letter: P

Question

PLEASE USE WHAT I HAVE ALREADY STARTED for SetOfIntegers.java

The methods that are empty are the ones i am stuck on. (UNION, INTERSECTION, DIFFERENCE, etc.)

A set (collection of elements without duplication) as a mathematical model is defined by the java interface below. Implement the mathematical model Set of integers as a class that implements the below 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.

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.

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?

Tester Class:

import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class SetOfIntegersTester {

public static void main(String[] args) throws Exception {

String inf = JOptionPane.showInputDialog("Input file?");
// Input file is src/
FileReader inFile = new FileReader(inf);
Scanner in = new Scanner(inFile);
String outf = JOptionPane.showInputDialog("Output file?");
PrintWriter outFile = new PrintWriter(outf);

while (in.hasNextLine()) {


String line = in.nextLine();
String[] split = line.split("\s+");

  
  
outFile.println(line + " ");
}
outFile.close();
}
}

Here is what i have so far:

//SetOfIntegers Class

import java.util.LinkedList;

public class SetOfIntegers implements Set {

    private Node first;

    public SetOfIntegers() {
        LinkedList list = new LinkedList();
        list.add(first);
    }

    @Override
    public boolean isEmpty() {
        return first == null;
    }

    @Override
    public void makeEmpty() {
        first = null;
    }

    @Override
    public boolean isMember(int x) {
      
    }

    public int getSize(){
        int count = 0;
        Node p = first;
        while(p!=null){
            count++;
            p = p.next;
        }
        return count;
    }
  
    @Override       //Need to make this recursive
    public void add(int x) {
        Node q = new Node(x);
        q.next = first;
        first = q;
    }

    @Override
    public void remove(int y) {
        if(isEmpty())
            return;
        first = first.next;
    }

    @Override
    public void union(SetOfIntegers other1, SetOfIntegers other2, SetOfIntegers result) {
      
    }

    @Override
    public void intersection(SetOfIntegers other1, SetOfIntegers other2, SetOfIntegers result) {
        SetOfIntegers set1 = new SetOfIntegers();
        if(other1.isEmpty()||other2.isEmpty()){
            set1 = other1;
        }for(int i = 0; i < set1.getSize(); i++){
            if()
        }
      
    }

    @Override
    public void difference(SetOfIntegers other1, SetOfIntegers other2, SetOfIntegers result) {

    }

    @Override
    public String toString() {
        Node p = first;
        String out = " ";
        while(p!=null){
            out = out + p.data + " ";
            p = p.next;
        }
        return out;
    }

    @Override
    public boolean equals(Object other) {

    }

}

//Node Class

public class Node {

    public int data;
    public Node next = null;

    public Node(int data) {
        this.data = data;
    }
}

//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

// I have made implemented recursive strategy for add(), isMember() and intersection()
// I left out the union and difference, because their interfaces are not proper. There is no way of getting elements of a set.


import java.io.FileReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class SetOfIntegersTester {

   public static void main(String[] args) throws Exception {

       String inf = JOptionPane.showInputDialog("Input file?");
       // Input file is src/
       FileReader inFile = new FileReader(inf);
       Scanner in = new Scanner(inFile);
       String outf = JOptionPane.showInputDialog("Output file?");
       PrintWriter outFile = new PrintWriter(outf);

       while (in.hasNextLine()) {

           String line = in.nextLine();
           String[] split = line.split("\s+");

           outFile.println(line + " ");
       }
       outFile.close();
       in.close();
   }
}

// SetOfIntegers Class

class SetOfIntegers implements Set {

   private Node first;

   @Override
   public boolean isEmpty() {
       return first == null;
   }

   @Override
   public void makeEmpty() {
       first = null;
   }

   @Override
   public boolean isMember(int x) {
       return recursiveIsMember(x, first);
   }
  
   private boolean recursiveIsMember(int x, Node current) {
       if (current == null) {
           return false;
       }
      
       if (current.data == x) {
           return true;
       } else {
           return recursiveIsMember(x, current.next);
       }
   }
  
   public int getSize() {
       int count = 0;
       Node p = first;
       while (p != null) {
           count++;
           p = p.next;
       }
       return count;
   }

   @Override
   public void add(int x) {
       first = addRecursive(x, first);
   }

   private Node addRecursive(int x, Node current) {
      
        if (current == null) {
           Node newNode = new Node(x);
           return newNode;
        }
        if (current.data == x) {
           return current;
        }  
        if (current.data > x) {
           Node newNode = new Node(x);
           newNode.next = current;
           return newNode;
        }
        else
           current.next = addRecursive(x, current.next);
           return current;
        }
      
  

   @Override
   public void remove(int y) {
       if (isEmpty())
           return;
       first = first.next;
   }

   @Override
   public String toString() {
       Node p = first;
       String out = " ";
       while (p != null) {
           out = out + p.data + " ";
           p = p.next;
       }
       return out;
   }

   @Override
   public void intersection(Set other, Set result) {
       recursiveIntersection(first, other, result);
   }
  
   public void recursiveIntersection(Node current, Set other, Set result) {
       if (result == null) {
           result = new SetOfIntegers();
       }
      
       if (current != null && other != null) {
           if (other.isMember(current.data)) {
               result.add(current.data);
           }
           else {
               recursiveIntersection(current.next, other, result);
           }
       }
   }

   @Override
   public void union(Set other, Set result) {
       // TODO Design proper interface
       // There is no way of getting the elements of Set 'other'
   }


   @Override
   public boolean equals(Object other) {
       return false;   //TODO
   }

   @Override
   public void difference(Set other, Set result) {
       // TODO Design proper interface
       // There is no way of getting the elements of Set 'other'
   }

}

class Node {

   public int data;
   public Node next = null;

   public Node(int data) {
       this.data = data;
   }
}

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.
}

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