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

First, I\'m sorry that the image is blurred. I need help for fix my code, right

ID: 3807817 • Letter: F

Question

First, I'm sorry that the image is blurred. I need help for fix my code, right hand side is the assignment description, left hand side is the code I wrote that contain errors I think, can anyone help me to fix or re-write the code? Thanks!

92e esuppresswarmingst unchecked 53 verride 94 public bootean equals (Object obj) if (1 (obj instanceof List)) 95 return false: else t ss List Ex other List e (ListeEx) obi: if (other List .size 1m size return false; 108 101 TODO: Add your code ere Entry E trav head 103 fortint i 0: i size: i )f E elem a othertist, get (i ,equals (elen)) 105 286 return false l trav trav getNext 189 return true; equatst obiect obii This method has code checking that is an instance of aeutlyunkadust, and then either retums false or performs the typecast You will now need to perform a list traversal and check that the element in each Entry is equal to the element at the identical index in otherList. The algorithm you must implement (in the area labeled ADo cooE HERE) is: Set trav to the first Entry in the linked list For i 0 to site Set elem equal to the elennnt at ander 1 in athert att if rav s element and eren are equal elenmts return false End if Set trav equal to the next Entry in the list End For return true You SHOULD either use the class of test cases, boubtyunkedListtests, in Activity 5.1 ar to check your code. Remember that Web-CAT uses the last part l of submission as your grade. Be certain to submit your code once you finish so this this assignment. Each method in part l is worth 40%of the activity grade, Morth a total of 80% of the overall score.

Explanation / Answer

As full class was not given to traverse I had used my own implemntation. Hope it will help.

DoublyLinkedList.java

public class DoublyLinkedList<E> {

private Node<E> header;
private Node<E> trailer;

/**
* @return if the list is empty.
*/
public boolean isEmpty() {
return header == null;
}

/**
* @return the first element of the list.
*/
public E getFirst() {
return header != null ? header.getElement() : null;
}

/**
* @return the last element of the list.
*/
public E getLast() {
return trailer != null ? trailer.getElement() : null;
}

/**
* Adds a new Node to the beginning of the list,
* containing the specified value.
* @param value for the new first node to hold.
*/
public void addFirst(E element) {
Node<E> newNode = new Node<E>(element, null, header);
header = newNode;
if (trailer == null) {
trailer = newNode;
}
}

/**
* This method should return true if the values of this list and that are
* identical and in the same order.
*
* @param that
* list to compare this to.
* @return true if the values are identical and in the same order
*/
@SuppressWarnings("unchecked")
public boolean equals(Object that) {
if (!(that instanceof DoublyLinkedList))
return false;

DoublyLinkedList<E> other = (DoublyLinkedList<E>) that;

// if lists are empty
if (header == null) {
return other.header == null ? true : false;
}

if (!header.equals(other.header))
return false;

// Just one element
if (header == trailer) {
return true;
}

if (!trailer.equals(other.trailer))
return false;

Node<E> thisNode = header;
Node<E> otherNode = other.header;

while (thisNode.getNext() != trailer) {
thisNode = thisNode.getNext();
otherNode = otherNode.getNext();
if (!(thisNode.equals(otherNode))) {
return false;
}
}
return true;
}


/**
* Simple toString for testing purposes. Please note that solutions that use the
* .toString() to implement the .equals() method will be rejected.
*/
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("DoublyLinkedList<");

Node<E> finger = header;
while (finger != null) {
sb.append(finger.toString());
if (finger.getNext() != null) {
sb.append("-");
}
finger = finger.getNext();
}

sb.append(">");
return sb.toString();
}
}

Node.java

public class Node<E> {

private E element;
private Node<E> previous;
private Node<E> next;

public Node<E> getPrevious() {
return previous;
}

public Node<E> getNext() {
return next;
}

public E getElement() {
return element;
}

public Node(E element, Node<E> previous, Node<E> next) {
this.element = element;
this.previous = previous;
this.next = next;
}

@Override
@SuppressWarnings("unchecked")
public boolean equals(Object that) {
if (!(that instanceof Node)) {
return false;
}
Node<E> other = (Node<E>) that;
if (element == null) {
return other.element == null ? true : false;
}
return element.equals(other.element);
}

@Override
public String toString() {
return element.toString();
}
}