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

***IMPORTANT: MUST INCLUDE JAVADOC DESCRIPTIONS FOR EACH METHOD**** Java data st

ID: 3586869 • Letter: #

Question

***IMPORTANT: MUST INCLUDE JAVADOC DESCRIPTIONS FOR EACH METHOD****

Java data structures. esign and implement a Set which includes the following common set operations:

make use of the generic type, and use javadoc to provide documentation of the design.

Next, create a GUI application (tester) to demonstrade the designed set, and all its supported operations. Write a description for a README file to explain (in full detail) on how to run the program and discuss the complexity of all operations.

check whether an element belongs (e) to the set or not (E) for any given sets A and B, find AuB, AnB, A-B, and check whether ACB or not. - find the cardinality of a set A -

Explanation / Answer

public class SetNode<E> {

// Instance variables

private E value;

private SetNode<E> next;

/**

* Parameterized constructor

*/

public SetNode(E value) {

this.value = value;

this.next = null;

}

/**

* Returns the value

*/

public E getValue() {

return value;

}

/**

* Returns the next Node

*/

public SetNode<E> getNext() {

return next;

}

/**

* Sets the value

*/

public void setValue(E value) {

this.value = value;

}

/**

* Sets the next Node

*/

public void setNext(SetNode<E> next) {

this.next = next;

}

/**

* Returns the value of the node

*/

@Override

public String toString() {

return "" + getValue();

}

}

import java.util.Comparator;

public class Set<E> {

// Instance variables

private SetNode<E> head;

private int count;

private Comparator<E> comparator;

/**

* Default constructor

*/

public Set() {

this.head = null;

this.count = 0;

this.comparator = null;

}

/**

* Parameterized constructor

*/

public Set(Comparator<E> comparator) {

this.head = null;

this.count = 0;

this.comparator = comparator;

}

/**

* Returns the number of elements in the set

*

* @return - number of elements in the set

*/

public int size() {

return this.count;

}

/**

* Adds the element e to the set if it is already not present.

*

* @param e

* - element to be added to the set

* @postcondition - Element e may be added to the set

* @return - Returns true if the element is added. Returns false if the

* element is already present in the set

*/

public boolean add(E e) {

// Create a new node

SetNode<E> newNode = new SetNode<E>(e);

// Check if the set has no elements

if (size() == 0) {

// Set new node as the head

this.head = newNode;

// Increment count

this.count += 1;

} else {

SetNode<E> node = this.head;

// Traverse to the last node

while (node.getNext() != null) {

// Check if element e is already present

if (this.comparator != null) {

if (comparator.compare(node.getValue(), e) == 0)

return false;

} else {

if (node.getValue() == e)

return false;

}

node = node.getNext();

}

// Set node.next as new node

node.setNext(newNode);

// Increment count

this.count += 1;

}

return true;

}

/**

* Checks whether the set contains element e.

*

* @param e

* - element to be searched

* @precondition - The set should not be empty

* @return - Returns true if the element is found, false oherwise

*/

public boolean contains(E e) {

// Check if set is not empty

if (size() > 0) {

// Get the head node

SetNode<E> node = this.head;

// Traverse to the last node

while (node != null) {

// Check node.value = e

if (this.comparator != null) {

if (comparator.compare(node.getValue(), e) == 0)

return true;

} else {

if (node.getValue() == e)

return true;

}

// Go to next node

node = node.getNext();

}

}

return false;

}

/**

* Checks whether this set is a subset of the set setB

*

* @param setB

* - The set to be checked for the elements from this set

* @return - Returns true if all elements of this set belongs to the set

* setB, false otherwise

*/

public boolean subset(Set<E> setB) {

// Check if both sets are not empty

if ((this.size() > 0) && (setB.size() > 0)) {

// Check if each element of this set belongs to setB

SetNode<E> node = this.head;

while (node != null) {

// Check if node.value belongs to setB

if (!setB.contains(node.getValue()))

return false;

// Go to next node

node = node.getNext();

}

} else

return false;

// Default return value

return true;

}

/**

* Finds the union of this set and set setB

*

* @param setB

* - Another set

*

* @return - Returns a set containing all the elements of this and set setB

* with no duplicates

*/

public Set<E> union(Set<E> setB) {

// Create new set

Set<E> set = new Set<E>(this.comparator);

// Check whether this set is not empty

if (this.size() > 0) {

// Add all elements of this set to the new set

SetNode<E> node = this.head;

while (node != null) {

// Add node.value to new set

set.add(node.getValue());

// Go to next node

node = node.getNext();

}

}

// Check whether setB is not empty

if (setB.size() > 0) {

// Add all elements of this set to the new set

SetNode<E> node = setB.head;

while (node != null) {

// Add node.value to new set

set.add(node.getValue());

// Go to next node

node = node.getNext();

}

}

return set;

}

/**

* Finds the intersection of this set and set setB

*

* @param setB

* - Another set

*

* @return - Returns a set containing all the common elements of this and

* set setB

*/

public Set<E> intersect(Set<E> setB) {

// Create new set

Set<E> set = new Set<E>(this.comparator);

// Check if both sets are not empty

if ((this.size() > 0) && (setB.size() > 0)) {

// Check if each element of this set belongs to setB

SetNode<E> node = this.head;

while (node != null) {

// Check if node.value belongs to setB

// If yes then add this value to the new set

if (setB.contains(node.getValue()))

set.add(node.getValue());

// Go to next node

node = node.getNext();

}

}

return set;

}

/**

* Finds the difference of this set from set setB

*

* @param setB

* - Another set

*

* @return - Returns a set containing all the elements of this set which are

* not in set setB

*/

public Set<E> difference(Set<E> setB) {

// Create new set

Set<E> set = new Set<E>(this.comparator);

// Check if both sets are not empty

if ((this.size() > 0) && (setB.size() > 0)) {

// Check if each element of this set belongs to setB

SetNode<E> node = this.head;

while (node != null) {

// Check if node.value does not belongs to setB

// If yes then add this value to the new set

if (!setB.contains(node.getValue()))

set.add(node.getValue());

// Go to next node

node = node.getNext();

}

}

return set;

}

/**

* Returns a string representation of the set

*/

@Override

public String toString() {

StringBuffer sb = new StringBuffer("[");

// Check if the set has elements

if (size() > 0) {

// Get head element

SetNode<E> node = this.head;

// Append head value to string buffer

sb.append(node.getValue());

// Traverse thru the rest of the set

node = node.getNext();

while (node != null) {

sb.append(", " + node.getValue());

node = node.getNext();

}

}

sb.append("]");

return sb.toString();

}

}

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.BoxLayout;

import javax.swing.ButtonGroup;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

public class SetGUIPanel extends JPanel implements ActionListener {

// Instance variables

private Set<Integer> s1;

private Set<Integer> s2;

private JLabel resultLbl;

private JRadioButton unionRB;

private JRadioButton intersectRB;

private JRadioButton subsetRB;

private JRadioButton diffRB;

/**

* Default constructor

*/

public SetGUIPanel() {

super();

// Set layout

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

// Define sets

s1 = new Set<Integer>();

s1.add(1);

s1.add(2);

s1.add(1);

s1.add(23);

s1.add(4);

s2 = new Set<Integer>();

s2.add(25);

s2.add(26);

s2.add(1);

s2.add(23);

s2.add(4);

// Add components

addComponents();

}

/**

* Adds components to the panel

*/

private void addComponents() {

addInputLabels();

addRadioButtons();

}

/**

* Displays the set elements

*/

private void addInputLabels() {

// Create label panel

JPanel panel = new JPanel(new GridBagLayout());

panel.setBorder(BorderFactory.createTitledBorder("Set"));

// Create grid bag constraints

GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.weightx = 1.0;

gbc.weighty = 0;

// Add label for set 1 to the panel

gbc.gridx = 0;

gbc.gridy = 0;

panel.add(new JLabel("Set A:"), gbc);

gbc.gridx = 1;

gbc.gridy = 0;

panel.add(new JLabel("" + s1), gbc);

// Add label for set 2 to the panel

gbc.gridx = 0;

gbc.gridy = 1;

panel.add(new JLabel("Set B:"), gbc);

gbc.gridx = 1;

gbc.gridy = 1;

panel.add(new JLabel("" + s2), gbc);

// Add label for result to the panel

gbc.gridx = 0;

gbc.gridy = 2;

panel.add(new JLabel("Result:"), gbc);

gbc.gridx = 1;

gbc.gridy = 2;

resultLbl = new JLabel();

panel.add(resultLbl, gbc);

// Add uiPanel to the main panel

add(panel);

}

/**

* Displays the radiobuttons to perform set operations

*/

private void addRadioButtons() {

// Init radio buttons

unionRB = new JRadioButton(" A union B");

intersectRB = new JRadioButton(" A intersection B");

diffRB = new JRadioButton(" A difference B");

subsetRB = new JRadioButton(" A subset of B");

// Add action listener

unionRB.addActionListener(this);

intersectRB.addActionListener(this);

diffRB.addActionListener(this);

subsetRB.addActionListener(this);

// Create button group

ButtonGroup grp = new ButtonGroup();

// Add all radio buttons ot the group

grp.add(unionRB);

grp.add(intersectRB);

grp.add(diffRB);

grp.add(subsetRB);

// Create panel for radio buttons

JPanel panel = new JPanel(new GridBagLayout());

panel.setBorder(BorderFactory.createTitledBorder("Set operations"));

// Create grid bag constraints

GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.weightx = 1.0;

gbc.weighty = 0;

// Add label for set 1 to the panel

gbc.gridx = 0;

gbc.gridy = 0;

panel.add(unionRB, gbc);

gbc.gridx = 1;

gbc.gridy = 0;

panel.add(intersectRB, gbc);

// Add label for set 2 to the panel

gbc.gridx = 0;

gbc.gridy = 1;

panel.add(diffRB, gbc);

gbc.gridx = 1;

gbc.gridy = 1;

panel.add(subsetRB, gbc);

// Add uiPanel to the main panel

add(panel);

}

/**

* Performs action as per button click

*/

@Override

public void actionPerformed(ActionEvent ae) {

// Get radio button clicked

JRadioButton rb = (JRadioButton) ae.getSource();

// Create resiult set

Set<Integer> resultSet = new Set<Integer>();

if (rb == unionRB)

resultSet = s1.union(s2);

else if (rb == intersectRB)

resultSet = s1.intersect(s2);

else if (rb == diffRB)

resultSet = s1.difference(s2);

else if (rb == subsetRB) {

resultLbl.setText(s1.subset(s2) ? "True" : "False");

return;

}

// Set result label as set result

resultLbl.setText("" + resultSet);

}

}

import javax.swing.JFrame;

public class SetGUI extends JFrame {

private static final int WIDTH = 300;

private static final int HEIGHT = 300;

/**

* Default constructor

*/

public SetGUI() {

super("Set Demo");

// Set size

setSize(WIDTH, HEIGHT);

// Set resizable

// setResizable(false);

// Center the Jframe on the screen

setLocationRelativeTo(null);

// Set action when frame is close

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Add panel

add(new SetGUIPanel());

// Set visible

setVisible(true);

}

public static void main(String args[]) {

new SetGUI();

}

}