Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

public class A5Tests { /** The pass/fail status of the tests. */ private boolean allTestsPassed = true; /** The List used to test the outcomes.

 

Node (E obj): takes 1 argument to create a node where item is set to obj and next is set to null. setItem(): takes 1 paramete  


 

public class A5Tests {

   /** The pass/fail status of the tests. */
   private boolean allTestsPassed = true;
 
   /** The List used to test the outcomes. */
   private LinkedList movieList;
 
   /**
      The starting point for the test program.
     
      @param args command line arguments - ignored in this program
   */
   public static void main(String[] args) {
      new A5Tests().runTests();
   }
 
   /**
      Runs the tests of the various LinkedList methods.
   */
   private void runTests() {
      setup();
     
      // Display the original list
      System.out.println("movieList : " + movieList);
     
      // Test all methods
      testInsert();     // test various add methods
      testReverse();    // test reverse method
      testRemove();     // test different remove options

      if (allTestsPassed) {
         System.out.println("All tests passed!");
      }
   }
 
   /**
      Initializes the LinkedList that will be used in the tests.
   */  
   private void setup() {
      movieList = new LinkedList();
     
      // Add few movies to the LinkedList
      movieList.add("Frozen");
      movieList.add("The Lion King");
      movieList.add("Moana");
      movieList.add("Onward");
      movieList.add("Toy Story");
      movieList.add("Aladdin");
      movieList.add("Your Name");
   }
 
   /**
      Test various add methods.
   */
   public void testInsert()   {
      // Add at the begining of the list
      movieList.add(0, "Spider-man");
   
      if (!movieList.get(0).equals("Spider-man")) {
         allTestsPassed = false;
         System.out.println("Test add at index 0 failed!");
      }
     
      // Add movie at the end of the list
      movieList.add("Zootopia");
     
      if (!movieList.get(movieList.getSize() - 1).equals("Zootopia")) {
         allTestsPassed = false;
         System.out.println("Test add at end of list failed!");
      }
       
      // Add a movie at the k index
      movieList.add(4, "Inside Out");  
     
      if (!movieList.get(4).equals("Inside Out")) {
         allTestsPassed = false;
         System.out.println("Test add at index k failed!");
      }
   }
 
   /**
      Test reverse method.
   */
   public void testReverse() {
      // Create a reverse of the original list
      List reverseList = movieList.duplicateReversed();  
   
      // Loop to check if reverse is correct
      for (int i = 0; i < movieList.getSize(); i++) {
         if (!movieList.get(i).equals(reverseList.get(reverseList.getSize() - (i + 1)))) {
            allTestsPassed = false;
            System.out.println("Test of the duplicateReversed() method failed!");
         }
      }
   }
 
   /**
      Test remove methods.
   */
   public void testRemove()   {
      // Create a duplicate list
      List duplicateList = movieList.duplicate();  
     
      // Remove from the begining of the list
      duplicateList.remove(0);
   
      if (!duplicateList.get(0).equals(movieList.get(1))) {
         allTestsPassed = false;
         System.out.println("Test remove at index 0 failed!");
      }
     
      // Remove movie at the end of the list
      duplicateList.remove(duplicateList.getSize() - 1);
           
      if (!duplicateList.get(duplicateList.getSize() - 1).equals(movieList.get(movieList.getSize() - 2))) {
         allTestsPassed = false;
         System.out.println("Test remove at end of the list failed!");
      }  
       
      // Remove movie based on movie title
      duplicateList.remove("Inside Out");
      if (duplicateList.indexOf("Inside Out") != -1) {
         allTestsPassed = false;
         System.out.println("Test remove based on title failed!");
      }
   }

}

***************************************************************************************************

public interface List {
 
   /**
      Return true if the list is empty; false otherwise.
 
      @return true if the list is empty; false otherwise
   */  
   boolean isEmpty();
 
   /**
      Returns the number of items in the list.
     
      @return the number of items in the list
   */
   int getSize();
   
   /**
      Adds an item to the end of the list.
     
      precondition: none
      postcondition: item is added at the end of the list
     
      @param item the element to add to the end of the list
   */
   void add(E item);
 
   /**
      Adds an item to the list at the given index.
     
      precondition: none
      postcondition: item is added at the given index
   
      @param index the position at which to add the item
      @param item the element to add to the list
      @throws IndexOutOfBoundsException if index is out of bounds
   */
   void add(int index, E item);

   /**
      Removes the item from the list at the given index.
     
      precondition: none
      postcondition: removes the item from the list at the given index
     
      @param index the position from which to remove an element
      @throws NoSuchElementException if the Linked List is empty
      @throws IndexOutOfBoundsException if index is out of bounds
   */
   void remove(int index);
 
   /**
      Removes an item from the list.
     
      precondition: none
      postcondition: removes the first element in the list whose equal method
      matches that of the given item
   
      @param item the element to remove
      @throws NoSuchElementException if the Linked List is empty
   */
   void remove(E item);
 
   /**
      Returns the item at the specified index.
     
      precondition: none
      postcondition: returns the element at specified index
     
      @param index the position from which to return an element
      @return the item at the specified index
      @throws IndexOutOfBoundsException if index is out of bounds
   */
   E get(int index);

   /**
      Returns the index of the first item in the list whose equal method
      matches that of the given item, or -1 if no match is found.
     
      precondition: none
      postcondition: returns the index of specified item
     
      @param item the element to search for
      @return the index of the first item that matches given item or -1 if no match is found
   */
   int indexOf(E item);

   /**
      Creates a duplicate of the list.
     
      precondition: none
      postcondition: returns a copy of the linked list

      @return a copy of the linked list
   */
   List duplicate();
 
   /**
      Creates a duplicate of the list with the nodes in reverse order.
     
      precondition: none
      postcondition: returns a copy of the linked list with the nodes in
      reverse order
     
      @return a copy of the linked list in reverse order
   */  
   List duplicateReversed();

}


 

In this assignment you will implement a singly linked list very similar to the one described in detail in chapter 16. Your task is to implement your own LinkedList class. Do NOT to usc any class from the Java Collections Framework (do not import anything from java.util). The linked list that you will implement is a variation briefly described on page 1013 that uses a ummy head node, as shown in the following figure: item next item next item next head size = 2 -3 17 Dummy header node index 0 index 1 LinkedList class instance fields Fiqure 1: Linked List with a dummy head node Notes about the Node class: Complete the implementation of the following Node class: public class Node // fields private E item; private Node next: // methods public Node (E obj) public void setItem (E obj) public void setNext (Node temp) public E getItem() public Node getNext () eOverride public String tostring () where, item is a private field of generic type E, and next is a private field reference to the next Node in the list.

Step by Step Solution

3.54 Rating (147 Votes )

There are 3 Steps involved in it

Step: 1

solution Nodejava public class Node private E item private Node next public NodeE item thisitem item public E getItem return item public void setItemE ... blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Algorithms

Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

3rd edition

978-0262033848

More Books

Students explore these related Programming questions

Question

relatively high socioeconomic development

Answered: 3 weeks ago