Question: Hello can someone pls help me trouble shoot the code and fix it. I have been debugging this code and I don't know why I

Hello can someone pls help me trouble shoot the code and fix it. I have been debugging this code and I don't know why I keep getting error. If possible pls explain how to fix it. The code is in java.

https://codeshare.io/GqdOdA - this code is the driver class, which have the main.

https://codeshare.io/2WynyW- this code is the interface.

This is the LList class and the one that I wanted to get fix. Code can't run with out the driver and the interface. Thank you!

public class LList implements ListInterface {

private Node firstNode; // Reference to first node of chain private int numberOfEntries;

public LList() { initializeDataFields(); }

// Initializes the class's data fields to indicate an empty list. private void initializeDataFields() { firstNode = null; numberOfEntries = 0; } // end initializeDataFields

// Returns a reference to the node at a given position. // Precondition: The chain is not empty; // 1 <= givenPosition <= numberOfEntries. private Node getNodeAt(int givenPosition) { assert !isEmpty() && (1 <= givenPosition) && (givenPosition <= numberOfEntries); Node currentNode = firstNode;

// Traverse the chain to locate the desired node // (skipped if givenPosition is 1) for (int counter = 1; counter < givenPosition; counter++) currentNode = currentNode.getNextNode();

assert currentNode != null;

return currentNode; } // end getNodeAt

private class Node { private T data; // Entry in list

private Node next; // Link to next node

private Node(T dataPortion) { data = dataPortion; next = null; }

private Node(T dataPortion, Node nextNode) {

data = dataPortion;

next = nextNode;

} // end constructor

private T getData() {

return data;

} // end getData

private void setData(T newData) {

data = newData;

} // end setData

private Node getNextNode() {

return next;

} // end getNextNode

private void setNextNode(Node nextNode) {

next = nextNode;

} }

public void add(T newEntry) { // TODO Auto-generated method stub Node newNode = new Node(newEntry);

if (isEmpty())

firstNode = newNode;

else // Add to end of non-empty list

{

Node lastNode = getNodeAt(numberOfEntries);

lastNode.setNextNode(newNode); // Make last node reference new node

} // end if

numberOfEntries++;

}

public void add(int newPosition, T newEntry) { // TODO Auto-generated method stub if ((newPosition >= 1) && (newPosition <= numberOfEntries + 1)) { Node newNode = new Node(newEntry); if (newPosition == 1) { newNode.setNextNode(firstNode); firstNode = newNode; } else { Node nodeBefore = getNodeAt(newPosition - 1); Node nodeAfter = nodeBefore.getNextNode(); newNode.setNextNode(nodeAfter); nodeBefore.setNextNode(newNode); } numberOfEntries++; } else throw new IndexOutOfBoundsException("Illegal position give to add operation.");

}

public T remove(int givenPosition) { // TODO Auto-generated method stub T result = null; if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); if (givenPosition == 1) { result = firstNode.getData(); firstNode = firstNode.getNextNode();

} else { Node nodeBefore = getNodeAt(givenPosition - 1); Node nodeToRemove = nodeBefore.getNextNode(); result = nodeToRemove.getData(); Node nodeAfter = nodeToRemove.getNextNode(); nodeBefore.setNextNode(nodeAfter); } numberOfEntries++; return result; } else throw new IndexOutOfBoundsException("Illegal postion given to remove operaiton."); }

public void clear() { // TODO Auto-generated method stub initializeDataFields();

}

public T replace(int givenPosition, T newEntry) { // TODO Auto-generated method stub if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { Node desireNode = getNodeAt(givenPosition); T originalEntry = desireNode.getData(); desireNode.setData(newEntry); return originalEntry; } else throw new IndexOutOfBoundsException("Illegal postion given to remove operaiton.");

}

public T getEntry(int givenPosition) {

if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {

assert !isEmpty();

return getNodeAt(givenPosition).getData();

} else

throw new IndexOutOfBoundsException("Illegal position given to getEntry operation.");

}

public boolean contains(T anEntry) { boolean found = false; Node currNode = firstNode;

while(!found && (currNode != null)) { if (anEntry.equals(currNode.getData())) found = true; else currNode = currNode.getNextNode(); } return found; }

public int getLength() { return numberOfEntries; }

public boolean isEmpty() { // TODO Auto-generated method stub boolean result; if (numberOfEntries == 0) { assert firstNode == null; result = true; } else { assert firstNode != null; result = false; } return result; }

public T[] toArray() { // TODO Auto-generated method stub @SuppressWarnings("unchecked") T[] result = (T[])new Object[numberOfEntries];

int index = 0; Node currentNode = firstNode; while((index < numberOfEntries) && (currentNode != null)) { result[index] = currentNode.getData(); currentNode = currentNode.getNextNode(); index++; }

return result; } public void display() {

System.out.println("Displaying all the Nodes data: ");

Node currentNode = firstNode;

while (currentNode != null) {

System.out.print(currentNode.getData() + " ");

currentNode = currentNode.getNextNode();

} // end while

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!