Question
package ds.students; import ds.interfaces.List; /** * @author simont* */ public class DSList implements List { public Node head; private int size; public DSList() { head
package ds.students; import ds.interfaces.List;
/** * @author simont* */ public class DSList implements List { public Node head; private int size;
public DSList() { head = null; size = 0; }
public DSList(Node head_) { head = new Node(head_.next, head_.prev, head_.getToken()); }
public DSList(DSList other) { // Copy constructor.
}
public Token remove(int index) { if (index size()) throw new IndexOutOfBoundsException();
Node currNode = head; for (int j = 0; currNode != null && j
if (currNode.prev != null) { currNode.prev.next = currNode.next; } else { head = head.next; }
if (currNode.next != null) { currNode.next.prev = currNode.prev; } size--; return currNode.getToken(); }
public int indexOf(Token obj) { int index = 0; Node currNode = head;
while (currNode != null) { if (currNode.getToken().equals(obj)) return index;
currNode = currNode.next; index++; }
return -1; } public Node getNode(int index) { Node currNode = head; for (int j = 0; currNode != null && j
return currNode; }
public Token get(int index) { if(index = size()) { throw new IndexOutOfBoundsException(); } Node currNode = getNode(index); if(currNode == null) { return null; } else { return currNode.getToken(); }
}
//FUNCTIONS public boolean isEmpty() { return head == null; } //FUNCTIONS public int size() { return size; }
@Override public String toString() { String newString = ""; Node currNode = head;
while(head != null) { newString += currNode.toString(); head = head.next; } return newString; }
public boolean add(Token obj) {
if(head == null) { head = new Node(null, null, obj); } else { Node currNode =head; while(currNode.next != null) { currNode = currNode.next;
} Node newNode = new Node(null,currNode,obj); currNode.next = newNode; size++; return true; } return false;
}
public boolean add(int index, Token obj) { if(index size()) { throw new IndexOutOfBoundsException(); } if(obj == null) { throw new NullPointerException(); } Node currNode = head; int i = 1; while(i
}
public boolean contains(Token obj) { if(obj == null) { throw new NullPointerException(); } Node currNode = head; while (currNode != null) { if (!currNode.getToken().equals(obj)) { currNode = currNode.next;
} else { return true; } } return false;
}
public boolean remove(Token obj) { return true; }
@Override public int hashCode() { return this.hashCode(); }
@Override public boolean equals(Object other) { return true; }
}
I have started writing methods to implement the code from the List class in the screenshots but not all of my functions are working correctly can somebody please help
package ds.interfaces; import ds.students. Token; /** * @author simont * */ public interface List { ** * /* * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). * @param index Index at which to add * @param obj The object to add @return True if insertion was successful * * @throws NullPointerException if the given object is null * @throws IndexOutOfBounds Exception if the index is out of range */ public boolean add(int index, Token obj); /** * Returns true if the given object is contained in the list. * @param obj The object whose presence is to be tested * @return True if the list contains the given object * @throws NullPointerException if the specified element is null */ public boolean contains (Token obj); /** * * Remove the first instance of the given object from the list, if it exists @param obj The object to remove @return True if the object was removed * * @throws NullPointerException if the specified object is null public boolean remove(Token obj); ** * Remove the object at the specified index from the list, if it exists. * @param index The index to remove @return The object previously at the specified index 36 * * @throws IndexOutOfBounds Exception if the specified index is out of range */ public Token remove(int index); /** 36 * Get the object at the specified index, if it exists. @param index The index to retrieve @return The object at the specified index, if it exists. * * * @throws IndexOutOfBounds Exception if the specified index is out of bounds */ public Token get(int index); /** * * Returns the first index of the specified object, or -1 if the object does not exist * in the list. @param token @return the index of the specified token, or -1 if it is not contained in the list. */ public int indexOf(Token token); * ** * Appends the specified element to the end of this list. * @param obj The object to add. * @return True if the object has been added to the list. * * @throws NullPointerException if the specified object is null */ public boolean add(Token obj); /** * Returns true if this list contains no elements. * @return True if the list is empty. public boolean isEmpty(); ** * Returns the number of elements in this list. * @return The number of elements in this list. */ public int size(); /** * Returns a string containing the toString() for each object in this list. * @return The concatenated toString() for each element in this list */ @Override public String toString(); /** * Compares this list with the specified object for equality. * The equality comparison must be value-based rather than the default (reference based). * @param obj The object to compare against. * @return True if the specified object is value-comparatively equal to this list */ @Override public boolean equals(object obj); /** * * Returns the hashCode for this list. * (This method must satisfy the constraint that if List 11.equals(List 12), then 11. hashCode () == 12. hashCode () must also be true. @return the hashCode of this list. */ @Override public int hashCode(); package ds.interfaces; import ds.students. Token; /** * @author simont * */ public interface List { ** * /* * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). * @param index Index at which to add * @param obj The object to add @return True if insertion was successful * * @throws NullPointerException if the given object is null * @throws IndexOutOfBounds Exception if the index is out of range */ public boolean add(int index, Token obj); /** * Returns true if the given object is contained in the list. * @param obj The object whose presence is to be tested * @return True if the list contains the given object * @throws NullPointerException if the specified element is null */ public boolean contains (Token obj); /** * * Remove the first instance of the given object from the list, if it exists @param obj The object to remove @return True if the object was removed * * @throws NullPointerException if the specified object is null public boolean remove(Token obj); ** * Remove the object at the specified index from the list, if it exists. * @param index The index to remove @return The object previously at the specified index 36 * * @throws IndexOutOfBounds Exception if the specified index is out of range */ public Token remove(int index); /** 36 * Get the object at the specified index, if it exists. @param index The index to retrieve @return The object at the specified index, if it exists. * * * @throws IndexOutOfBounds Exception if the specified index is out of bounds */ public Token get(int index); /** * * Returns the first index of the specified object, or -1 if the object does not exist * in the list. @param token @return the index of the specified token, or -1 if it is not contained in the list. */ public int indexOf(Token token); * ** * Appends the specified element to the end of this list. * @param obj The object to add. * @return True if the object has been added to the list. * * @throws NullPointerException if the specified object is null */ public boolean add(Token obj); /** * Returns true if this list contains no elements. * @return True if the list is empty. public boolean isEmpty(); ** * Returns the number of elements in this list. * @return The number of elements in this list. */ public int size(); /** * Returns a string containing the toString() for each object in this list. * @return The concatenated toString() for each element in this list */ @Override public String toString(); /** * Compares this list with the specified object for equality. * The equality comparison must be value-based rather than the default (reference based). * @param obj The object to compare against. * @return True if the specified object is value-comparatively equal to this list */ @Override public boolean equals(object obj); /** * * Returns the hashCode for this list. * (This method must satisfy the constraint that if List 11.equals(List 12), then 11. hashCode () == 12. hashCode () must also be true. @return the hashCode of this list. */ @Override public int hashCode()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started