Complete the implementations of the abstract class LinkedList.java, which implements the List.java interface and the class LinkedOrderedList.java, which extends the abstract class LinkedList.java and implements theOrderedList.java interface. LinkedList methods to implement: removeFirst removeLast remove contains LinkedOrderedList method to implement: add Recommended order of development: add to empty list to end of list to front of list to middle of list contains removeFirst removeLast remove Write a test class that tests all of the methods you implemented. Note: You only need to test LinkedOrderedList, because all of the methods are implemented in that class or inherited from LinkedList.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Complete the implementations of

  • the abstract class LinkedList.java, which implements the List.java interface and
  • the class LinkedOrderedList.java, which extends the abstract class LinkedList.java and implements theOrderedList.java interface.

LinkedList methods to implement:

  • removeFirst
  • removeLast
  • remove
  • contains

LinkedOrderedList method to implement:

  • add

Recommended order of development:

  • add
    • to empty list
    • to end of list
    • to front of list
    • to middle of list
  • contains
  • removeFirst
  • removeLast
  • remove

Write a test class that tests all of the methods you implemented. Note: You only need to test LinkedOrderedList, because all of the methods are implemented in that class or inherited from LinkedList.

Although you are not required to submit the test class, you might be asked to do so if you are having difficulties getting your implementation to work.

Submit the completed LinkedList.java and LinkedOrderedList.java.

LinkedList.java code

import java.util.NoSuchElementException;
import java.util.StringJoiner;

public abstract class LinkedList<E> implements List<E> {
    protected class Node {
        E element;
        Node next;

        Node(E element) {
            this(element, null);
        }

        Node(E element, Node next) {
            this.element = element;
            this.next = next;
        }
    }

    protected int count;
    protected Node head;
    protected Node tail;

    /**
     * Creates an empty list.
     */
    public LinkedList() {
        count = 0;
        head = null;
        tail = null;
    }

    /**
     * Removes and returns the first element from the list.
     *
     * @return the first element of the list
     * @throws IllegalStateException if the list is empty
     */
    @Override
    public E removeFirst() {
        if (isEmpty()) {
            throw new IllegalStateException("The LinkedList is empty.");
        }

        return null;
    }

    /**
     * Removes and returns the last element from the list.
     *
     * @return the last element in the list
     * @throws IllegalStateException if the list is empty    
     */
    @Override
    public E removeLast() {
        if (isEmpty()) {
            throw new IllegalStateException("The LinkedList is empty.");
        }

        return null;
    }

    /**
     * Removes and returns the specified element from the list.
     *
     * @param  targetElement the element to be removed from the list
     * @return the removed element
     * @throws IllegalStateException if the list is empty
     * @throws NoSuchElementException if the target element is not found
     */
    @Override
    public E remove(E targetElement) {
        if (isEmpty()) {
            throw new IllegalStateException("The LinkedList is empty.");
        }

        return null;
    }

    /**
     * Returns the first element in the list without removing it.
     * 
     * @return the first element in the list
     * @throws IllegalStateException if the list is empty
     */
    @Override
    public E first() {
        if (isEmpty()) {
            throw new IllegalStateException("The LinkedList is empty.");
        }

        return head.element;
    }

    /**
     * Returns the last element in the list without removing it. 
     *
     * @return the last element in the list  
     * @throws IllegalStateException if the list is empty
     */
    @Override
    public E last() {
        if (isEmpty()) {
            throw new IllegalStateException("The LinkedList is empty.");
        }

        return tail.element;
    }

    /**
     * Returns true if the specified element is found in the list.
     *
     * @param  targetElement the element that is sought in the list
     * @return true if the element is found in the list
     * @throws IllegalStateException if the list is empty
     */
    @Override
    public boolean contains(E targetElement) {
        if (isEmpty()) {
            throw new IllegalStateException("The LinkedList is empty.");
        }

        return false;
    }

    /**
     * Returns true if the list contains no elements.
     *
     * @return true if the list contains no elements
     */
    @Override
    public boolean isEmpty() {
        return (count == 0);
    }

    /**
     * Returns the number of elements in the list.
     *
     * @return the number of elements in the list
     */
    @Override
    public int size() {
        return count;
    }

    /**
     * Returns a string representation of the list.
     *
     * @return a string representation of the list    
     */
    @Override
    public String toString() {
        StringJoiner result = new StringJoiner(", ", "[", "]");

        Node current = head;
        while (current != null) {
            result.add(current.element.toString());
            current = current.next;
        }

        return result.toString();
    }
 }

Thank you for any help/suggestions!

*/
public class LinkedOrdered List<E> extends LinkedList<E> implements Ordered List<E> {
/**
* Creates an empty list.
*/
public LinkedOrdered List() {
super();
}
/**
* Adds the specified element to this list at the location determined by
* the element's natural ordering.
*
* @param element the element to be added to this list
* @throws IllegalStateException if the element is not of a Comparable type
*/
@Override
public void add (E element) {
if (!(element instanceof Comparable)) {
throw new IllegalStateException ("LinkedOrdered List requires Comparable elements.");
}
var comparableElement = (Comparable<E>) element;
Transcribed Image Text:*/ public class LinkedOrdered List<E> extends LinkedList<E> implements Ordered List<E> { /** * Creates an empty list. */ public LinkedOrdered List() { super(); } /** * Adds the specified element to this list at the location determined by * the element's natural ordering. * * @param element the element to be added to this list * @throws IllegalStateException if the element is not of a Comparable type */ @Override public void add (E element) { if (!(element instanceof Comparable)) { throw new IllegalStateException ("LinkedOrdered List requires Comparable elements."); } var comparableElement = (Comparable<E>) element;
public interface List<E> {
/**
* Removes and returns the first element from the list.
}
*
* @return the first element of the list
*/
public E removeFirst();
/**
* Removes and returns the last element from the list.
*
* @return the last element in the list
*/
public E removeLast();
/**
* Removes and returns the specified element from the list.
*
* @param targetElement the element to be removed from the list
* @return the removed element
*/
public E remove(E element);
/**
* Returns the first element in the list without removing it.
*
* @return the first element in the list
*/
public E first();
/**
* Returns the last element in the list without removing it.
*
* @return the last element in the list
*/
public E last();
/**
* Returns true if the specified element is found in the list.
*
* @param targetElement the element that is sought in the list
* @return true if the element is found in the list
*/
public boolean contains (E targetElement);
/**
* Returns true if the list contains no elements.
*
* @return true if the list contains no elements
*/
public boolean isEmpty();
/**
* Returns the number of elements in the list.
*
* @return number of elements in the list
*/
public int size();
/**
* Returns a string representation of the list.
*
* @return a string representation of the list
*/
public String toString();
Transcribed Image Text:public interface List<E> { /** * Removes and returns the first element from the list. } * * @return the first element of the list */ public E removeFirst(); /** * Removes and returns the last element from the list. * * @return the last element in the list */ public E removeLast(); /** * Removes and returns the specified element from the list. * * @param targetElement the element to be removed from the list * @return the removed element */ public E remove(E element); /** * Returns the first element in the list without removing it. * * @return the first element in the list */ public E first(); /** * Returns the last element in the list without removing it. * * @return the last element in the list */ public E last(); /** * Returns true if the specified element is found in the list. * * @param targetElement the element that is sought in the list * @return true if the element is found in the list */ public boolean contains (E targetElement); /** * Returns true if the list contains no elements. * * @return true if the list contains no elements */ public boolean isEmpty(); /** * Returns the number of elements in the list. * * @return number of elements in the list */ public int size(); /** * Returns a string representation of the list. * * @return a string representation of the list */ public String toString();
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education