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

ListInterface.java /** An interface for the ADT list. Entries in the list have p

ID: 3831338 • Letter: L

Question

ListInterface.java

/**

An interface for the ADT list.

Entries in the list have positions that begin with 1.

@author Frank M. Carrano

@version 3.0

*/

public interface ListInterface<T>

{

/** Adds a new entry to the end of this list.

Entries currently in the list are unaffected.

The listÕs size is increased by 1.

@param newEntry the object to be added as a new entry */

public void add(T newEntry);

/** Adds a new entry at a specified position within this list.

Entries originally at and above the specified position

are at the next higher position within the list.

The listÕs size is increased by 1.

@param newPosition an integer that specifies the desired

position of the new entry

@param newEntry the object to be added as a new entry

@return true if the addition is successful, or

false if newPosition < 1, or newPosition > getLength() + 1 */

public boolean add(int newPosition, T newEntry);

/** Removes the entry at a given position from this list.

Entries originally at positions higher than the given

position are at the next lower position within the list,

and the listÕs size is decreased by 1.

@param givenPosition an integer that indicates the position of

the entry to be removed

@return a reference to the removed entry or null, if either

the list was empty, givenPosition < 1, or

givenPosition > getLength() */

public T remove(int givenPosition);

/** Removes all entries from this list. */

public void clear();

/** Replaces the entry at a given position in this list.

@param givenPosition an integer that indicates the position of

the entry to be replaced

@param newEntry the object that will replace the entry at the

position givenPosition

@return true if the replacement occurs, or false if either the

list is empty, givenPosition < 1, or

givenPosition > getLength() */

public boolean replace(int givenPosition, T newEntry);

/** Retrieves the entry at a given position in this list.

@param givenPosition an integer that indicates the position of

the desired entry

@return a reference to the indicated entry or null, if either

the list is empty, givenPosition < 1, or

givenPosition > getLength() */

public T getEntry(int givenPosition);

/** Sees whether this list contains a given entry.

@param anEntry the object that is the desired entry

@return true if the list contains anEntry, or false if not */

public boolean contains(T anEntry);

/** Gets the length of this list.

@return the integer number of entries currently in the list */

public int getLength();

/** Sees whether this list is empty.

@return true if the list is empty, or false if not */

public boolean isEmpty();

/** Retrieves all entries that are in this list in the order in which

they occur in the list. */

public T[] toArray();

public int countEntry(T anEntry);

public boolean removeSecond(T anEntry);

} // end ListInterface

LList.java

import java.io.*;

/** An linked implementation of the ADT List.
*
* This code is from Chapter 14 of
* Data Structures and Abstractions with Java 3/e
*      @author Frank M. Carrano
*
* Modifications were made by Charles Hoot:
* The toString method is overwritten to give a nice display of the items in
* the list in this format { <1> <2> <3> <4> }
*
* An alternate display method has been created to print the list one item
* to a line along with the index
*
* Stubs were added for the methods needed to complete Lab 13
*
//- * @version 3.0
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>       
* @version 3.1 (Modified for the Solution)
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
*/
class LList<T> implements ListInterface<T> {

    private Node firstNode; // reference to first node
    private int numberOfEntries;

    public LList() {
        clear();
    } // end default constructor

    public final void clear() // note the final method
    {
        firstNode = null;
        numberOfEntries = 0;
    } // end clear

    public void add(T newEntry) {
        Node newNode = new Node(newEntry);
        if (isEmpty()) {
            firstNode = newNode;
        } else // add to end of nonempty list
        {
            Node lastNode = getNodeAt(numberOfEntries);
            lastNode.next = newNode; // make last node reference new node
        } // end if

        numberOfEntries++;
    } // end add

    public boolean add(int newPosition, T newEntry) {
        boolean isSuccessful = true;
        if ((newPosition >= 1) && (newPosition <= numberOfEntries + 1)) {
            Node newNode = new Node(newEntry);
            if (newPosition == 1) // case 1
            {
                newNode.setNextNode(firstNode);
                firstNode = newNode;
            } else // case 2: list is not empty
            { // and newPosition > 1
                Node nodeBefore = getNodeAt(newPosition - 1);
                Node nodeAfter = nodeBefore.getNextNode();
                newNode.setNextNode(nodeAfter);
                nodeBefore.setNextNode(newNode);
            } // end if
            numberOfEntries++;
        } else {
            isSuccessful = false;
        }
        return isSuccessful;
    } // end add

    public T remove(int givenPosition) {
        T result = null; // return value
        if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
            assert !isEmpty();
            if (givenPosition == 1) // case 1: remove first entry
            {
                result = firstNode.getData(); // save entry to be removed
                firstNode = firstNode.getNextNode();
            } else // case 2: not first entry
            {
                Node nodeBefore = getNodeAt(givenPosition - 1);
                Node nodeToRemove = nodeBefore.getNextNode();
                Node nodeAfter = nodeToRemove.getNextNode();
                nodeBefore.setNextNode(nodeAfter);
                result = nodeToRemove.getData(); // save entry to be removed
            } // end if
            numberOfEntries--;
        } // end if
        return result; // return removed entry, or
        // null if operation fails
    } // end remove

    public boolean contains(T anEntry) {
        boolean found = false;
        Node currentNode = firstNode;
        while (!found && (currentNode != null)) {
            if (anEntry.equals(currentNode.getData())) {
                found = true;
            } else {
                currentNode = currentNode.getNextNode();
            }
        } // end while
        return found;
    } // end contains

    public T getEntry(int givenPosition) {
        T result = null; // result to return
        if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
            assert !isEmpty();
            result = getNodeAt(givenPosition).getData();
        } // end if
        return result;
    } // end getEntry

    public boolean replace(int givenPosition, T newEntry) {
        boolean isSuccessful = true;
        if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
            assert !isEmpty();
            Node desiredNode = getNodeAt(givenPosition);
            desiredNode.setData(newEntry);
        } else {
            isSuccessful = false;
        }
        return isSuccessful;
    } // end replace

    public int getLength() {
        return numberOfEntries;
    }

    public boolean isEmpty() {
        boolean result;
        if (numberOfEntries == 0) // or getLength() == 0
        {
            assert firstNode == null;
            result = true;
        } else {
            assert firstNode != null;
            result = false;
        } // end if
        return result;
    } // end isEmpty

    public T[] toArray() {
// the cast is safe because the new array contains null entries
        @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++;
        } // end while
        return result;
    } // end toArray

    private Node getNodeAt(int givenPosition) {
        assert (firstNode != null)
                && (1 <= givenPosition) && (givenPosition <= numberOfEntries);
        Node currentNode = firstNode;
// traverse the chain to locate the desired node
        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 bag
        private Node next; // link to next node

        private Node(T dataPortion) {
            this(dataPortion, null);
        } // end constructor

        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;
        } // end setNextNode
    } // end Node

    /** Build a string representation of the list
     *
     * @return a string showing the state of the list
     */
    public String toString() {
        String result = "{ ";
        Node currentNode = firstNode;
        while (currentNode != null) {

            result = result + "<" + currentNode.data + "> ";
            currentNode = currentNode.next;

        }
        result = result + "}";

        return result;
    }

    /** Display the list with indices one to a line
     * This will correctly display an infinite list,
     * whereas the toString() method will never return
     *
     */
    public void display() {
        int index = 1;
        Node currentNode = firstNode;

        while (currentNode != null) {
            System.out.println(index + ":" + currentNode.getData());

            currentNode = currentNode.getNextNode();
            index++;
        }

    } // end display

   
   /** Check to see if two lists are the same.
    * @param aList another linked list to check this list against
    * @return true if all the items in this list and the other list are equals
    */

    public boolean equals(LList<T> aList) {
        boolean isEqual = false; // result of comparison of lists

        Node currOne = firstNode;
        Node currTwo = aList.firstNode;
        int counter;

        if (numberOfEntries == aList.numberOfEntries) {
            // lists have equal lengths, so traverse both and compare items as you go:
            // (NOTE: loop is skipped if lists are empty)

            while ((currOne != null) && (currOne.getData()).equals(currTwo.getData())) {
                currOne = currOne.getNextNode();
                currTwo = currTwo.getNextNode();
            } // end while

            isEqual = currOne == null;
        } else // lists have unequal lengths
        {
            isEqual = false;
        }

        return isEqual;
    } // end equals

   
       
    /*********************************************************************
     *
     * METHODS TO BE COMPLETED
     *
     ***********************************************************************/

    public int countEntry(T anEntry) {
        int count = 0;
  (for int count= 0; count < numEntries; i++)
  {
   
  }

  

  return count;
    }

    public boolean removeSecond(T anEntry) {
        boolean found = false;
  
        if (previousNode ! = null)
        { periousNode.setLink(NodeToRemove.getLink());
        }
       
        }

  return found;
    }
}

Write an implementation of these methods for the class LList. a. Create an Eclipse project FinalTest. Download the files LList List Interface.java from Blackboard/Assignments/FinalTest and include project. b. Write a method countEntry in the provided class LList. c. Write a method remove second in the provided class LList. d. Create a new class ListTest to test your countEntry and methods. In the main method of the ListTest class, create a list stringsLis the following strings: "hip", "hop", "pip", pop", "pit", "pot", "pop", "hop Print the elements of the list stringsList. Apply the method countEntry ("pop") to the list stringsList and Apply removeSecond ("pop") to the list stringsList Print the elements of the resulting list. Submit the classes LList, and ListTest to FinalTest in the Blackboard.

Explanation / Answer

import java.io

public int countEntry(T anEntry) {
        int count = 0;
  (for int count= 0; count < numEntries; i++)
  {
   
  }


class LList<T> implements ListInterface<T> {

    private Node firstNode; // reference to first node
    private int numberOfEntries;

    public LList() {
        clear();
    } // end default constructor

    public final void clear() // note the final method
    {
        firstNode = null;
        numberOfEntries = 0;
    } // end clear

    public void add(T newEntry) {
        Node newNode = new Node(newEntry);
        if (isEmpty()) {
            firstNode = newNode;
        } else // add to end of nonempty list
        {
            Node lastNode = getNodeAt(numberOfEntries);
            lastNode.next = newNode; // make last node reference new node
        } // end if

        numberOfEntries++;
    } // end add

    public boolean add(int newPosition, T newEntry) {
        boolean isSuccessful = true;
        if ((newPosition >= 1) && (newPosition <= numberOfEntries + 1)) {
            Node newNode = new Node(newEntry);
            if (newPosition == 1) // case 1
            {
                newNode.setNextNode(firstNode);
                firstNode = newNode;
            } else // case 2: list is not empty
            { // and newPosition > 1
                Node nodeBefore = getNodeAt(newPosition - 1);
                Node nodeAfter = nodeBefore.getNextNode();
                newNode.setNextNode(nodeAfter);
                nodeBefore.setNextNode(newNode);
            } // end if
            numberOfEntries++;
        } else {
            isSuccessful = false;
        }
        return isSuccessful;
    } // end add

    public T remove(int givenPosition) {
        T result = null; // return value
        if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
            assert !isEmpty();
            if (givenPosition == 1) // case 1: remove first entry
            {
                result = firstNode.getData(); // save entry to be removed
                firstNode = firstNode.getNextNode();
            } else // case 2: not first entry
            {
                Node nodeBefore = getNodeAt(givenPosition - 1);
                Node nodeToRemove = nodeBefore.getNextNode();
                Node nodeAfter = nodeToRemove.getNextNode();
                nodeBefore.setNextNode(nodeAfter);
                result = nodeToRemove.getData(); // save entry to be removed
            } // end if
            numberOfEntries--;
        } // end if
        return result; // return removed entry, or
        // null if operation fails
    } // end remove

    public boolean contains(T anEntry) {
        boolean found = false;
        Node currentNode = firstNode;
        while (!found && (currentNode != null)) {
            if (anEntry.equals(currentNode.getData())) {
                found = true;
            } else {
                currentNode = currentNode.getNextNode();
            }
        } // end while
        return found;
    } // end contains

    public T getEntry(int givenPosition) {
        T result = null; // result to return
        if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
            assert !isEmpty();
            result = getNodeAt(givenPosition).getData();
        } // end if
        return result;
    } // end getEntry

    public boolean replace(int givenPosition, T newEntry) {
        boolean isSuccessful = true;
        if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
            assert !isEmpty();
            Node desiredNode = getNodeAt(givenPosition);
            desiredNode.setData(newEntry);
        } else {
            isSuccessful = false;
        }
        return isSuccessful;
    } // end replace

    public int getLength() {
        return numberOfEntries;
    }

    public boolean isEmpty() {
        boolean result;
        if (numberOfEntries == 0) // or getLength() == 0
        {
            assert firstNode == null;
            result = true;
        } else {
            assert firstNode != null;
            result = false;
        } // end if
        return result;
    } // end isEmpty

    public T[] toArray() {
// the cast is safe because the new array contains null entries
        @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++;
        } // end while
        return result;
    } // end toArray

    private Node getNodeAt(int givenPosition) {
        assert (firstNode != null)
                && (1 <= givenPosition) && (givenPosition <= numberOfEntries);
        Node currentNode = firstNode;
// traverse the chain to locate the desired node
        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 bag
        private Node next; // link to next node

        private Node(T dataPortion) {
            this(dataPortion, null);
        } // end constructor

        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;
        } // end setNextNode
    } // end Node

    /** Build a string representation of the list
     *
     * @return a string showing the state of the list
     */
    public String toString() {
        String result = "{ ";
        Node currentNode = firstNode;
        while (currentNode != null) {

            result = result + "<" + currentNode.data + "> ";
            currentNode = currentNode.next;

        }
        result = result + "}";

        return result;
    }

    /** Display the list with indices one to a line
     * This will correctly display an infinite list,
     * whereas the toString() method will never return
     *
     */
    public void display() {
        int index = 1;
        Node currentNode = firstNode;

        while (currentNode != null) {
            System.out.println(index + ":" + currentNode.getData());

            currentNode = currentNode.getNextNode();
            index++;
        }

    } // end display

   
   /** Check to see if two lists are the same.
    * @param aList another linked list to check this list against
    * @return true if all the items in this list and the other list are equals
    */

    public boolean equals(LList<T> aList) {
        boolean isEqual = false; // result of comparison of lists

        Node currOne = firstNode;
        Node currTwo = aList.firstNode;
        int counter;

        if (numberOfEntries == aList.numberOfEntries) {
            // lists have equal lengths, so traverse both and compare items as you go:
            // (NOTE: loop is skipped if lists are empty)

            while ((currOne != null) && (currOne.getData()).equals(currTwo.getData())) {
                currOne = currOne.getNextNode();
                currTwo = currTwo.getNextNode();
            } // end while

            isEqual = currOne == null;
        } else // lists have unequal lengths
        {
            isEqual = false;
        }

        return isEqual;
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote