Question

In: Computer Science

JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...

JAVA

the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the 5 methods, and you may not add any new data fields to the class.

public class LinkedIntSet {
   private static class Node {
       private int data;
       private Node next;
      
       public Node(int data, Node next) {
           this.data = data;
           this.next = next;
       }
   }
  
   private Node first;

   public LinkedIntSet() {
       first = null;
   }

   public int size() {
       int counter = 0;
       for (Node current = first; current != null; current = current.next)
           counter++;
       return counter;
   }

   public boolean contains(int i) {
       for (Node current = first; current != null; current = current.next) {
           if (current.data == i)
               return true;
       }
       return false;
   }

   // Ignore this equals method. Write the code for the other equals method.
   public boolean equals(Object otherObject) {
       LinkedIntSet other = (LinkedIntSet) otherObject;
       return this.equals(other);
   }

   /***************************** NEW METHODS ************************************/

   /**
   * Adds <code>element</code> to this set if it is not already present and
   * returns <code>true</code>. If <code>element</code> is already present, the
   * set is unchanged and <code>false</code> is returned.
   *
   * @param element the element to be added
   * @return <code>true</code> if the element was added and <code>false</code>
   * otherwise.
   */
   public boolean addElement(int element) {
       // Replace the line below with your answer
       throw new RuntimeException("Not implemented");
   }

   /**
   * Removes an element from the set.
   *
   * @param element the element to be removed
   * @return <code>ture</code> if the element was removed and <code>false</code>
   * otherwise.
   */
   public boolean removeElement(int element) {
       // Replace the line below with your answer
       throw new RuntimeException("Not implemented");

}

   /**
   * Changes the set so that it is equal the union of itself and
   * <code>other</code>.
   *
   * @param other the set to union with
   */
   public void union(LinkedIntSet other) {
       // Replace the line below with your answer
       throw new RuntimeException("Not implemented");
   }

   /**
   * Changes the set so that is equal the intersection of itself and
   * <code>other</code>.
   *
   * @param other the set to intersect with
   */
   public void intersect(LinkedIntSet other) {
       // Replace the line below with your answer
       throw new RuntimeException("Not implemented");
   }
}

Solutions

Expert Solution

Code

public class LinkedIntSet {
private static class Node {
private int data;
private Node next;
  
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
  
private Node first;

public LinkedIntSet() {
first = null;
}

public int size() {
int counter = 0;
for (Node current = first; current != null; current = current.next)
counter++;
return counter;
}

public boolean contains(int i) {
for (Node current = first; current != null; current = current.next) {
if (current.data == i)
return true;
}
return false;
}

// Ignore this equals method. Write the code for the other equals method.
public boolean equals(Object otherObject) {
LinkedIntSet other = (LinkedIntSet) otherObject;
//both set are equals when both same number of elements and same elements not in exact same order
if(size()!=other.size())//if both the set has not same size return false
return false;
//check this set's every element present in other set if so then return true otherwise return fasle
for (Node current = first; current != null; current = current.next) {
if(other.contains(current.data))
return false;
}
return true;
}

/***************************** NEW METHODS ************************************/

/**
* Adds <code>element</code> to this set if it is not already present and
* returns <code>true</code>. If <code>element</code> is already present, the
* set is unchanged and <code>false</code> is returned.
*
* @param element the element to be added
* @return <code>true</code> if the element was added and <code>false</code>
* otherwise.
*/
public boolean addElement(int element) {
//first check if the element present in the set if it is then just retunr false
if(this.contains(element))
return false;
//creates the new node with element
Node newNode=new Node(element, null);
if(first==null)//check if first is nulll means set is empty then add this new node at first
{
first=newNode;
return true;
}
Node curr=first;
while(curr.next!=null)//otherwise got to last node in the set
{
curr=curr.next;
}
//and then add new node to the last node
curr.next=newNode;
return true;
}

/**
* Removes an element from the set.
*
* @param element the element to be removed
* @return <code>ture</code> if the element was removed and <code>false</code>
* otherwise.
*/
public boolean removeElement(int element) {
//first check element is present in the set or set is empty then return flase
if(!this.contains(element) || first==null)
return false;
  
//checks if the first node has same element then remove first and return true
if(first.data==element)
{
first=first.next;
return true;
}
//node curr and prev
Node curr=first;
Node prev=first;
//loops until curr.next is not null
while(curr.next!=null)
{
if(curr.data==element)//break the loop when curr.data==element
break;
prev=curr;//assing curr to prev
curr=curr.next;//and take curr to one stpe ahead in set
}
if(curr.next==null)//this conditon true whnen element present at the last node
prev.next=null;
else//else just simply skip the curr node
prev.next=curr.next;
return true;//return true
}

/**
* Changes the set so that it is equal the union of itself and
* <code>other</code>.
*
* @param other the set to union with
*/
public void union(LinkedIntSet other) {
//union means this set has all the element present in both the set
//so simply loop through other set and add other each element to this set
for (Node current = other.first; current != null; current = current.next)
this.addElement(current.data);
}

/**
* Changes the set so that is equal the intersection of itself and
* <code>other</code>.
*
* @param other the set to intersect with
*/
public void intersect(LinkedIntSet other) {
//intersect menas this set has only those elemts that are present in both the set
//so loops through this set and remove the elemtn from the this set which is not presetn in other set
//this set contains only elements that are presetn in booth the set
for (Node current = first; current != null; current = current.next)
{
if(!other.contains(current.data))
{
this.removeElement(current.data);
}
}
}
}

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
JAVA Implement a public class method named comparison on a public class Compare that accepts two...
JAVA Implement a public class method named comparison on a public class Compare that accepts two Object arguments. It should return 0 if both references are equal. 1 if both objects are equal. and -1 otherwise. (SUPER IMPORTANT) Either reference can be null, so you'll need to handle those cases carefully! Here is what I have so far: public class Compare { public static int comparison(Object a, Object b) {   if (a == null || b == null) {    return...
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT