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

Need help with my JAVA assignment, please! Thanks. Part 1 - addFirst: Write the

ID: 3846494 • Letter: N

Question

Need help with my JAVA assignment, please! Thanks.

Part 1 - addFirst: Write the clear(), addFirst(), and toString() methods for LinkedList.java. Do not attempt to modify Tester.java. Check the output file (output.txt) to see what should be produced from running the driver. Note: there are two fields (head and size) within the LinkedList class. The type passed into addFirst must be Comparable.

Part 2 - addLast: Write the addLast() method for LinkedListA.java. Do not attempt to modify Tester.java. Do NOT delete LinkedList$Node.class, or LinkedList.class from the folder. Check the output file (output.txt) to see what should be produced from running the driver. The type passed into addLast must be Comparable.

Part 4 - remove: Write the remove() method for LinkedListc.java. The method takes an int index as a parameter. It should remove the element at the specified position in this list. It should return the element previously at the specified position. If index is out of range then throw an IndexOutOfBoundsException. The first node in the list is index 0. Do NOT attempt to modify Tester.java. Do NOT delete any of the .class files from the folder. Check the output file (output.txt) to see what should be produced from running the driver.

Tester for each part:

Part1:

public class Tester

{

   public static void main ( String[] args )

   {

String[] alpha = {"Apple", "Banana", "Carrot", "Drum stick",

"Eclair", "Flan", "Grape", "Hash browns",

"Ice cream", "Jicama", "Kiwi fruit", "Lettuce",

"Mango", "New potatoes", "Olives", "Papaya",

"Quiche", "Romaine lettuce", "Steak", "Tomatoes",

"Umble pie", "V-8", "Wheat bread", "Xmas cookies",

"Yellowtail", "Zwieback" };

                               

LinkedList list = new LinkedList();

  

      // Add to the list

   for (int x = 0; x < alpha.length; x++ )

   list.addFirst(alpha[x]);

          

       // Print out number added

System.out.println ("After adding " + alpha.length + " items ");

       System.out.println();

                              

       // Print out the list  

       System.out.println("Call on a list with " + list.size() + " items");                  

       System.out.println(list);

       System.out.println();

       // Clear the list

list.clear();

      

       // Print out empty list

       System.out.println("Call on an empty list: ");

       System.out.println(list);

       System.out.println();

      

System.out.println("Finished");

   }

}

Part 2:

public class Tester

{

   public static void main ( String[] args )

   {

String[] food =    {"Apple", "Banana", "Carrot", "Drum stick",

"Eclair", "Flan", "Grape", "Hash browns",

"Ice cream", "Jicama", "Kiwi fruit", "Lettuce",

"Mango", "New potatoes", "Olives", "Papaya",

"Quiche", "Romaine lettuce", "Steak", "Tomatoes",

"Umble pie", "V-8", "Wheat bread", "Xmas cookies",

"Yellowtail", "Zwieback" };

                              

LinkedListA list = new LinkedListA();

for (int x = 0; x < food.length; x++ )

   list.addLast(food[x]);

          

System.out.println ("After adding " + food.length + " items");

      

System.out.println(list);

  

System.out.println("Finished");

      

   }// end main

  

}// end class

Part 4:

public class Tester

{

   public static void main ( String[] args )

   {

String[] array   = {"Apple", "Banana", "Carrot", "Drum stick",

"Eclair", "Flan", "Grape", "Hash browns",

"Ice cream", "Jicama", "Kiwi fruit", "Lettuce",

"Mango", "New potatoes", "Olives", "Papaya",

"Quiche", "Romaine lettuce", "Steak", "Tomatoes",

"Umble pie", "V-8", "Wheat bread", "Xmas cookies",

"Yellowtail", "Zwieback" };

                              

       int index = array.length / 2;

      

LinkedListC list = new LinkedListC();

       //java.util.LinkedList list = new java.util.LinkedList();

      

for (int x = 0; x < array.length; x++ )

           list.addLast(array[x]);

          

System.out.println ("After adding " + array.length + " items " +

"the list size is " + list.size());

         System.out.println();

                              

       System.out.println ("Attempting to remove " + array[index]);

     System.out.println ("Removed " + list.remove(index));

       System.out.println ("The list size is " + list.size());

       System.out.println();

              

       index = 0;

       System.out.println ("Attempting to remove " + array[index]);

System.out.println ("Removed " + list.remove(index));

       System.out.println ("The list size is " + list.size());

       System.out.println();

      

       index = list.size() - 1;

       System.out.println ("Attempting to remove " + array[array.length - 1]);

System.out.println ("Removed " + list.remove(index));

       System.out.println ("The list size is " + list.size());

       System.out.println();

      

      

       try

       {

               index = list.size();

               System.out.println ("Attempting to remove index not in the list");

       System.out.println ("Removed " + list.remove(index));

               System.out.println ("The list size is " + list.size());

               System.out.println();

       }// end try

       catch (IndexOutOfBoundsException e)

       {

           System.out.println("Not removed - " + e.getMessage());

           System.out.println();

       }// end catch

      

       try

       {

               index = -1;

               System.out.println ("Attempting to remove index not in the list");

       System.out.println ("Removed " + list.remove(index));

               System.out.println ("The list size is " + list.size());

               System.out.println();

       }// end try

       catch (IndexOutOfBoundsException e)

       {

           System.out.println("Not removed - " + e.getMessage());

           System.out.println();

       }// end catch

      

       list.clear();

      

       try

       {

               index = 0;

               System.out.println ("Attempting to remove on empty list");

       System.out.println ("Removed " + list.remove(index));

               System.out.println ("The list size is " + list.size());

               System.out.println();

       }// end try

       catch (IndexOutOfBoundsException e)

       {

           System.out.println("Not removed - " + e.getMessage());

           System.out.println();

       }// end catch

  

       System.out.println("Finished");

      

   }// end main

  

}// end class

Explanation / Answer

PROGRAM CODE:

Part 1 Code:

LinkedList.java

package list;

public class LinkedList<T extends Comparable<T>> {
   private class Node
   {
       Comparable<T> data;
       Node next;
         
       public Node(Comparable<T> data) {
           this.data = data;
           next = null;
       }
         
       public void setNext(Node next)
       {
           this.next = next;
       }
         
       public void setData(Comparable<T> data)
       {
           this.data = data;
       }
   }
  
   private Node head;
   private int size;
  
   public LinkedList() {
       clear();
   }

   public void addFirst(Comparable<T> data) {
       if(head == null)
           head = new Node(data);
       else
       {
           Node newNode = new Node(data);
           newNode.setNext(head);
           head = newNode;
       }
       size++;
   }

   public int size() {
       return size;
   }

   public void clear() {
       head = null;
       size = 0;
   }
  
   @Override
   public String toString() {
       String result = "";
       Node temp = head;
      
       while(temp != null)
       {
           result += temp.data + " ";
           temp = temp.next;
       }
       return result;
   }
}

OUTPUT:

After adding 26 items

Call on a list with 26 items
Zwieback
Yellowtail
Xmas cookies
Wheat bread
V-8
Umble pie
Tomatoes
Steak
Romaine lettuce
Quiche
Papaya
Olives
New potatoes
Mango
Lettuce
Kiwi fruit
Jicama
Ice cream
Hash browns
Grape
Flan
Eclair
Drum stick
Carrot
Banana
Apple


Call on an empty list:


Finished

Part 2 Code:

LinkedListA.java

package list;


public class LinkedListA<T> {
   private class Node
   {
       Comparable<T> data;
       Node next;
         
       public Node(Comparable<T> data) {
           this.data = data;
           next = null;
       }
         
       public void setNext(Node next)
       {
           this.next = next;
       }
         
       public void setData(Comparable<T> data)
       {
           this.data = data;
       }
   }
  
   private Node head;
   private int size;
  
   public LinkedListA() {
       clear();
   }


   public int size() {
       return size;
   }

   public void clear() {
       head = null;
       size = 0;
   }
  
   @Override
   public String toString() {
       String result = "";
       Node temp = head;
      
       while(temp != null)
       {
           result += temp.data + " ";
           temp = temp.next;
       }
       return result;
   }

   public void addLast(Comparable<T> data) {
       if(head == null)
           head = new Node(data);
       else
       {
           Node newNode = new Node(data);
           Node temp = head;
          
           while(temp.next != null)
           {
               temp = temp.next;
           }
           temp.next = newNode;
       }
       size++;
      
   }
}

OUTPUT:

After adding 26 items
Apple
Banana
Carrot
Drum stick
Eclair
Flan
Grape
Hash browns
Ice cream
Jicama
Kiwi fruit
Lettuce
Mango
New potatoes
Olives
Papaya
Quiche
Romaine lettuce
Steak
Tomatoes
Umble pie
V-8
Wheat bread
Xmas cookies
Yellowtail
Zwieback

Finished

Part 4 Code:

package list;


public class LinkedListC<T> {
   private class Node
   {
       Comparable<T> data;
       Node next;
         
       public Node(Comparable<T> data) {
           this.data = data;
           next = null;
       }
         
       public void setNext(Node next)
       {
           this.next = next;
       }
         
       public void setData(Comparable<T> data)
       {
           this.data = data;
       }
   }
  
   private Node head;
   private int size;
  
   public LinkedListC() {
       clear();
   }


   public int size() {
       return size;
   }

   public void clear() {
       head = null;
       size = 0;
   }
  
   @Override
   public String toString() {
       String result = "";
       Node temp = head;
      
       while(temp != null)
       {
           result += temp.data + " ";
           temp = temp.next;
       }
       return result;
   }

   public void addLast(Comparable<T> data) {
       if(head == null)
           head = new Node(data);
       else
       {
           Node newNode = new Node(data);
           Node temp = head;
          
           while(temp.next != null)
           {
               temp = temp.next;
           }
           temp.next = newNode;
       }
       size++;
      
   }


   public Comparable<T> remove(int index) {
       if(index>=size || index<0)
           throw new IndexOutOfBoundsException();
       else
       {
           int counter = 0;
           Comparable<T> removedData = null;
           Node temp = head;
           if(index == 0)
           {
               removedData = head.data;
               head = head.next;
           }
           else
           {
               while(counter < index-1)
               {
                   temp = temp.next;
                   counter++;
               }
               removedData = temp.next.data;
               temp.next = temp.next.next;
           }
           size--;
          
           return removedData;
       }
   }
}

OUTPUT:

After adding 26 items the list size is 26

Attempting to remove New potatoes
Removed New potatoes
The list size is 25

Attempting to remove Apple
Removed Apple
The list size is 24

Attempting to remove Zwieback
Removed Zwieback
The list size is 23

Attempting to remove index not in the list
Not removed - null

Attempting to remove index not in the list
Not removed - null

Attempting to remove on empty list
Not removed - null

Finished

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