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");
   }

Solutions

Expert Solution

SOLUTION:

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) {
Node node = new Node(element, null);
Node current = first;
// It will check if List is empty or not. If element will be stored in the first and return true
if(first==null){
first = node;
return true;
}else{
// This while loop will check if the element already exist in the list or not
// If exist it will return false and if not the element will be added to the list and return true
while(current.next!=null){
// If condition for checking element exist in the list or not
if(current.data == element){
return false;
}
current = current.next;
}
// Boundary Value Check
if(current.data==element){
return false;
}else{
// IF element doesn't exist then element will be added
current.next = node;
}
}
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) {
Node current = first;

// This while loop will check if the element exist in the list or not
// If exist it will remove the element and return true
while(current.next!=null){
if(current.next.data==element){
current = current.next.next;
return true;
}
current = current.next;
}

// condition when the element is present at the first position in the list
if(first.data == element){
first = first.next;
return true;
}

return false;
}
}


Related Solutions

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 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...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT