Question

can i please get help on this? i don't know how to do it and I'm...

can i please get help on this? i don't know how to do it and I'm so confused. This is in java. This is what I need to do. Thank you so much.

In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers
(with no tail reference and no dummy head node)
, but it need not be sorted.
The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the linked list. It should provide the following public methods:
IntegerSet(int size): this constructor method creates a new set of size integers by prompting the user to enter size elements for the set on the keyboard.
int size(): this method returns the cardinality of the set.
boolean isEmpty(): this method returns true if the set has no elements, false otherwise.
boolean isMember(int item): this method returns true if item is an element of the set, false otherwise.
boolean add(int item): if item is not in the set, this method adds it to the set and returns true; otherwise, the set is unchanged and the method returns false.
boolean remove(int item): if item is in the set, this method removes it from the set and returns true; otherwise, the set is unchanged and the method returns false.
boolean isSubset(IntegerSet set2): this method returns true if every element in set2 is also in the calling set, false otherwise.
IntegerSet intersection(IntegerSet set2): this method returns a new set that is the intersection of the calling set and set2. An integer is an element of the intersection set if it is in both the calling set and set2.
IntegerSet union(IntegerSet set2): this method returns a new set that is the union of the calling set and set2. An integer is an element of the union set if it is in either the calling set or set2, or both. Keep in mind that the union set formed should not contain any duplicates.
IntegerSet difference(IntegerSet set2): this method returns a new set that is the difference between the calling set and set2 (in this order). An integer is an element of the difference set if it is in the calling set but not in set2.
void display(): this method prints all elements of the set on the screen.
In addition to the IntegerSet class, you are also required to write a test driver that serves as the test harness for the IntegerSet class. This test harness should provide the user with options to explicitly test each method in the IntegerSet class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//IntegerSet.java

import java.util.Scanner;

public class IntegerSet {

      

       class Node

       {

             public int data;

             public Node next;

       }

      

       private int size;

       private Node head;

      

       public IntegerSet(int size)

       {

             this.size = 0;

             head = null;

             if(size > 0)

                    inputData(size);

       }

      

       private void inputData(int size)

       {

             int num;

             Scanner scan = new Scanner(System.in);

            

                    System.out.println("Enter "+size+" elements of the set :");

                    for(int i=0;i<size;i++)

                    {

                           System.out.print("Enter element : ");

                           num = scan.nextInt();

                           if(!add(num))

                           {

                                 System.out.println(num+" exists in Set");

                                 i--;

                           }

                          

                    }

       }

      

       public int getSize()

       {

             return size;

       }

      

       public boolean isEmpty()

       {

             return(size == 0);

       }

      

       public boolean isMember(int item)

       {

             if(isEmpty())

                    return false;

             Node curr = head;

             while(curr != null)

             {

                    if(curr.data == item)

                           return true;

                    curr = curr.next;

             }

            

             return false;

       }

      

       public boolean add(int item)

       {

             if(isMember(item))

                    return false;

             Node node = new Node();

             node.data = item;

             node.next = head;

             head = node;

             size++;

             return true;

       }

      

       public boolean remove(int item)

       {

             if(isMember(item))

             {

                    if(head.data == item)

                    {

                           head = head.next;

                           size--;

                           return true;

                    }

                    else

                    {

                           Node curr = head;

                           Node prev = null;

                           while(curr != null)

                           {

                                 if(curr.data == item)

                                 {

                                        prev.next = curr.next;

                                        size--;

                                        return true;

                                 }

                                 prev = curr;

                                 curr = curr.next;

                           }

                    }

             }

            

             return false;

       }

      

       public boolean isSubset(IntegerSet set2)

       {

             Node curr = set2.head;

             while(curr != null)

             {

                    if(!isMember(curr.data))

                           return false;

                    curr = curr.next;

             }

            

             return true;

       }

       public IntegerSet intersection(IntegerSet set2)

       {

             IntegerSet result = new IntegerSet(0);

             Node node = set2.head;

             while(node != null)

             {

                    if(isMember(node.data))

                           result.add(node.data);

                    node = node.next;

             }

            

             return result;

       }

      

       public IntegerSet union(IntegerSet set2)

       {

             IntegerSet result = new IntegerSet(0);

             Node node = head;

             while(node != null)

             {

                    result.add(node.data);

                    node = node.next;

             }

            

             node = set2.head;

             while(node != null)

             {

                    result.add(node.data);

                    node = node.next;

             }

            

             return result;

       }

      

       public IntegerSet difference(IntegerSet set2)

       {

             IntegerSet result = new IntegerSet(0);

             Node node = head;

             while(node != null)

             {

                    if(!set2.isMember(node.data))

                           result.add(node.data);

                    node = node.next;

             }

            

             return result;

       }

      

       public void display()

       {

             Node curr = head;

             if(head == null)

                    System.out.println("Empty Set");

             else

             {

                    while(curr != null)

                    {

                           System.out.print(curr.data+" ");

                           curr = curr.next;

                    }

                    System.out.println();

             }

       }

      

}

//end of IntegerSet.java

//IntegerSetDemo.java

import java.util.Scanner;

public class IntegerSetDemo {

             

       public static void main(String[] args) {

      

             IntegerSet set1 = new IntegerSet(5);

             IntegerSet set2 = new IntegerSet(5);

             System.out.println("Set 1 : ");

             set1.display();

             System.out.println("Set 2 : ");

             set2.display();

             IntegerSet result;

             int choice, num;

             Scanner scan = new Scanner(System.in);

             do

             {

                    System.out.println("1. Set Empty");

                    System.out.println("2. Check Member");

                    System.out.println("3. Add an item");

                    System.out.println("4. Get the size");

                    System.out.println("5. Remove an element");

                    System.out.println("6. Check Subset");

                    System.out.println("7. Intersection of Set1 and Set2");

                    System.out.println("8. Union of Set1 and Set2");

                    System.out.println("9. Difference of Set1 and Set2");

                    System.out.println("10. Exit");

                    System.out.print("Enter your choice(1-9) : ");

                    choice = scan.nextInt();

                    switch(choice)

                    {

                    case 1:

                           System.out.println("Set 1 Empty : "+set1.isEmpty());

                           break;

                    case 2:

                           System.out.print("Enter an integer : ");

                           num = scan.nextInt();

                           System.out.println(num+" present in Set1 : "+set1.isMember(num));

                           break;

                    case 3:

                           System.out.print("Enter an integer : ");

                           num = scan.nextInt();

                           if(set1.add(num))

                                 System.out.println(num+" added in Set1");

                           else

                                 System.out.println(num+" already present in Set1");

                           set1.display();

                           break;

                    case 4:

                           System.out.println("Size of Set1 : "+set1.getSize());

                           break;

                    case 5:

                           System.out.print("Enter an integer : ");

                           num = scan.nextInt();

                           if(set1.remove(num))

                                 System.out.println(num+" removed in Set1");

                           else

                                 System.out.println(num+" not present in Set1");

                           set1.display();

                           break;

                    case 6:

                           if(set1.isSubset(set2))

                                 System.out.println("Set 1 is subset of Set2");

                           else

                                 System.out.println("Set 1 is not a subset of Set2");

                           break;

                    case 7:

                           result = set1.intersection(set2);

                           result.display();

                           break;

                    case 8:

                           result= set1.union(set2);

                           result.display();

                           break;

                    case 9:

                           result = set1.difference(set2);

                           result.display();

                           break;

                    case 10:

                           break;

                                

                    }

             }while(choice !=10);

             scan.close();

       }

}

//end of IntegerSetDemo.java

Add a comment
Know the answer?
Add Answer to:
can i please get help on this? i don't know how to do it and I'm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • Use C++ programing language Please don't use other Chegg solved solutions, make it new thanks. Assignment...

    Use C++ programing language Please don't use other Chegg solved solutions, make it new thanks. Assignment requirements Pre-requisite: Please read zyBook Chapter 6 Sets and understand the three operations of Sets (union, intersection, and difference) . You will be creating your custom Set (think a list of unique elements) class in C++ or a language of your choice. Please do NOT use STL, or any pre-defined library for this assignment. 1. The data type of the set collection: array (or...

  • Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName...

    Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName */ public class IntegerSet {    /** * Creates a new instance of IntegerSet    */ // TODO: implement the constructor    /** * Return a new IntegerSet containing the union of the two IntegerSet objects * passed as arguments */ // TODO: implement the union method    /** * Return a new IntegerSet containing the intersection of the two IntegerSet objects * passed...

  • Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation...

    Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...

  • import java.util.*; /** * A class that implements the ADT set by using a linked bag....

    import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */ public class LinkedSetWithLinkedBag> implements SetInterface { private LinkedBag setOfEntries; /** * Creates a set from a new, empty linked bag. */ public LinkedSetWithLinkedBag() { //TODO Project1 } // end default constructor public boolean add(T newEntry) { //TODO Project1 // new node is at beginning of chain if(this.setOfEntries.isEmpty()) { if (!this.setOfEntries.contains(newEntry)) this.setOfEntries.add(newEntry); } return true; //...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding...

    Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding functionality Set A contains Set B if every element in Set B is also in Set A. We will compare three ways to determine whether Set A contains Set B. Approach 1: Write code in the main method in a test class Add code to the main of ArraySetTester to create SetA and SetB, fill them with data and write a contains operation that...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT