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

Create a class MyLinkedList that extends LinkedList<String>. Create a method in

ID: 3682245 • Letter: C

Question

Create a class MyLinkedList that extends LinkedList<String>. Create a method in the class with header

     public String findLargest()

that uses an iterator to recursively find the lexicographically largest string in the linked list (i.e., last in alphabetic order).

Use the provided file MyLinkedListDriver.java to test your file.

Recursion and Iterator Lab

Write a recursive method to find the largest value in a singly linked list. Create a MyLinkedList .java which inherits from the class LinkedList<String> and add the recursive method public String findLargest(). You will need to use the iterator to access each node of the linkedlist. You may need to add a helper method to start the recursion. Use the driver class MyLinkedListDriver.java to populate your linked list and demonstrate that your method works. Add an additional test to MyLinkedListDriver.java.

The largest in the driver class provided is Tom.

/******************************************************************************************************************************************************************/

Explanation / Answer

/**The java class MyLinkedList that inherits from LinkedList and implments the Comparable interface.*/
//MyLinkedList.java

import java.util.Iterator;
import java.util.LinkedList;
public class MyLinkedList extends LinkedList<String>
implements Comparable<String>
{

   //Create an Iterator class object to type string
   Iterator<String> iterator = null;
   //Set a max to empty
   String max="";

  
  
   /**The method findLargest that finds the largest
   * of the string in lexicographic order*/
   public String findLargest()
   {
      
       //Check if iterator is null
       if (iterator == null)
           //then get the instance of iterator object
           iterator = super.iterator();

       //Check if the iteator next element is false
       if (iterator.hasNext()==false){
           //set iterator to null
           iterator = null;
           //return max
           return max;
       }
       else
       {
           //Get value from the iterator
           String data = iterator.next();
          
           //Call compare method
           if(compareTo(data)<0){
               //update the max string
               max=data;
           }
           //call the findLargest method
           //recursively
           findLargest();
       }
      
       //return max string
       return max;
   }

  
   /*Override the compareTo method that takes */
   @Override
   public int compareTo(String temp) {      
       return max.compareTo(temp);
   }
  
}

----------------------------------------------------------------------------------

/**The java program MyLinkedListDriver that creates an instance of MyLinkedList class and add strings
* to the object and calls the findLargest method to find the largest string in lexicographic order*/
//MyLinkedListDriver.java

public class MyLinkedListDriver {
   public static void main(String[] args) {
      
      
       //Create an instance of MyLinkedList
       MyLinkedList myLinkedList = new MyLinkedList();
      
      
       //Add strings to the object, myLinkedList
       myLinkedList.add("Sarah");
       myLinkedList.add("Barbara");
       myLinkedList.add("Tom");
       myLinkedList.add("George");

       //Call findLargest method
       String largest = myLinkedList.findLargest();
       System.out.println(largest);

       //Add strings to the myLinkedList
       myLinkedList.add("Zeke");
       myLinkedList.add("Adam");
      
       //Call findLargest method
       largest = myLinkedList.findLargest();
       System.out.println(largest);
   }

}


------------------------------------------------------------------------

Sample Output:

Tom
Zeke

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