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

Lab 02 To Do List Objective: Implement a system that keeps tracks of tasks you h

ID: 3873249 • Letter: L

Question

Lab 02 To Do List Objective: Implement a system that keeps tracks of tasks you have to do! Write a class ToDoList with the following Internal class ListNode which has o Instance Variables . data of type String link of type ListNode o Constructors . Default Parameterized Instance Variables o head: a ListNode which always points to the beginning of the linked list o current: a ListNode which moves to point at different items in the list o previous: a ListNode which points to the item behind current. . Constructor o A default constructor that initializes head to an empty ListNode and sets current and previous to point at the head. Methods o goToNext: This moves the current node forward in the list by one node. It doesn't move forward if that node is null o gotoPrev: This moves the current node backwards in the list by one node. It does not move backwards if the current node is the head. o getDataAtCurrent: returns the data at the current node as long as the current isn't null o setDataAtCurrent: takes in a parameter of type String and sets the data at the current node to that value as long as current is not null o addItem: This method adds a new node at the end of the list (HINT: Look for first null element using a loop). If the list is empty (thus head is null) then it starts the list. o insertAfterCurrent: creates a new node based on data that is passed in by a parameter and puts that node after the current position o deleteCurrentNode: removes the current node from the list by resetting the links o showList: prints out the contents of the list line-by-line Finally write a driver that implements an instance of GrocerList and demonstrates each of the methods work.

Explanation / Answer

PROGRAM CODE:

ToDoList.java

package linkedlist;

public class ToDoList {

   //internal class

   class ListNode

   {

       //data for storing the string value

       String data;

       //link for storing the next node

       ListNode link;

      

       //constructor

       public ListNode() {

           data = "";

           link = null;

       }

      

       //parameterized constructor

       public ListNode(String data) {

           this.data = data;

           link = null;

       }

   }

  

   //instance variables to hold the list

   ListNode head;

   ListNode current;

   ListNode previous;

  

   //constructor

   public ToDoList() {

       head = new ListNode();

       current = head;

       previous = head;

   }

  

   //move forward

   public void goToNext()

   {

       if(current != null && current.link != null)

       {

           previous = current;

           current = current.link;

       }

   }

  

   //move backward

   public void goToPrevious()

   {

       if(head != current)

       {

           current = previous;

       }

   }

  

   public String getDataAtCurrent()

   {

       if(current != null)

           return current.data;

       else return "";

   }

  

   //change the data at current node

   public void setDataAtCurrent(String data)

   {

       if(current != null)

           current.data = data;

   }

  

   //insert an item into the list

   public void addItem(String data)

   {

       ListNode node = new ListNode(data);

       if(head == null)

           head = node;

       else

       {

           ListNode temp = head;

           while(temp.link != null)

               temp = temp.link;

           temp.link = node;

       }

   }

  

   //insert an item after the current node

   public void insertAfterCurrent(String data)

   {

       ListNode node = new ListNode(data);

       node.link = current.link;

       current.link = node;

       current = current.link;

   }

  

   //delete the current node from the list

   public void deleteCurrentNode()

   {

       ListNode temp = head;

       while(temp.link != null)

       {

           if(temp.link.data.equals(current.data))

           {

               temp.link = temp.link.link;

               break;

           }

           else temp = temp.link;

       }

   }

  

   // display the list on the console

   public void showList()

   {

       ListNode temp = head;

       System.out.println("Printing List");

       int counter = 1;

       while(temp.link != null)

       {

           System.out.println(counter++ + ". " + temp.link.data);

           temp = temp.link;

       }

   }

}

  

GroceryList.java

package linkedlist;

public class GroceryList {

   public static void main(String[] args) {

       ToDoList list = new ToDoList();

       System.out.println("To Do List Tester!");

       System.out.println("Adding Five Tasks To Do");

       list.addItem("Buy Ground Beef");

       list.addItem("Buy Cheese");

       list.addItem("Buy Taco Shells");

       list.addItem("Make Tacos");

       list.addItem("Eat Tacos");

       list.showList();

       System.out.println("I forgot to get salsa. Let me add that after step 2.");

       list.goToNext();

       list.goToNext();

       list.insertAfterCurrent("Buy Salsa");

       list.showList();

       System.out.println("On second thought I'm in a spicy mood so let's change salsa to hot sauce.");

       list.setDataAtCurrent("Buy Hot Sauce");

       list.showList();

       System.out.println("Do people get guacamole on tacos? I'll add it after step 3.");

       list.insertAfterCurrent("Buy Guacamole");

       list.showList();

       System.out.println("On second thought I don't think they do let me tak that out");

       list.deleteCurrentNode();

       list.showList();

       System.out.println("Now I have tested the perfect taco related list!");

   }

}

OUTPUT:

To Do List Tester!

Adding Five Tasks To Do

Printing List

1. Buy Ground Beef

2. Buy Cheese

3. Buy Taco Shells

4. Make Tacos

5. Eat Tacos

I forgot to get salsa. Let me add that after step 2.

Printing List

1. Buy Ground Beef

2. Buy Cheese

3. Buy Salsa

4. Buy Taco Shells

5. Make Tacos

6. Eat Tacos

On second thought I'm in a spicy mood so let's change salsa to hot sauce.

Printing List

1. Buy Ground Beef

2. Buy Cheese

3. Buy Hot Sauce

4. Buy Taco Shells

5. Make Tacos

6. Eat Tacos

Do people get guacamole on tacos? I'll add it after step 3.

Printing List

1. Buy Ground Beef

2. Buy Cheese

3. Buy Hot Sauce

4. Buy Guacamole

5. Buy Taco Shells

6. Make Tacos

7. Eat Tacos

On second thought I don't think they do let me tak that out

Printing List

1. Buy Ground Beef

2. Buy Cheese

3. Buy Hot Sauce

4. Buy Taco Shells

5. Make Tacos

6. Eat Tacos