I have a homework assignment that I have to complete This in
I have a homework assignment that I have to complete. This includes the details as well as an example of the output of the solution. This has to be done using JAVA.
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
Instance Variables:
- data of type String
- link of type ListNode
Constructors:
- Default
- Parameterized
Instance Variables:
- head: a ListNode which always points to the beginning of the linked list
- current: a ListNode which moves to point at different items in the list
- previous: a ListNode which points to the item behind current.
Constructor:
- A default constructor that initializes head to an empty ListNode and sets current and previous to point at the head.
Methods:
- goToNext: This moves the current node forward in the list by one node. It doesn’t move forward if that node is null
- 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.
- getDataAtCurrent: returns the data at the current node as long as the current isn’t null
- 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
- 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.
- insertAfterCurrent: creates a new node based on data that is passed in by a parameter and puts that node after the current position
- deleteCurrentNode: removes the current node from the list by resetting the links
- showList: prints out the contents of the list line-by-line
*Finally write a driver that implements an instance of TodoList and demonstrates each of the methods work.
Example Dialog:
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 put 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 take that out.
1. Buy Ground Beef
2. Buy Cheese
3. Buy Hot Sauce
4. Buy Taco Shells
5. Make Tacos
6. Eat Tacos
Now I have tested the perfect taco related list!
Solution
CODE:
// ToDoList class
class ToDoList{
// Create an ListNode inner class
class ListNode{
// Create two instance variables - data and link.
protected String data;
protected ListNode link;
// default constructor
public ListNode()
{
link = null;
data = \"\";
}
// Parameterised Constructor
public ListNode(String d, ListNode n)
{
data = d;
link = n;
}
}
// Create instance variables - head, current and previous.
protected ListNode head;
protected ListNode current;
protected ListNode previous;
// default constructor
public ToDoList(){
head = null;
current = head;
previous = head;
}
// Parameterised Constructor
public void goToNext(){
if (current.link != null)
current = current.link;
}
// gotoPrev() method implemented
public void gotoPrev(){
if (current != head)
current = previous;
}
// Get data at current node
public String getDataAtCurrent(){
if(current != null)
return current.data;
else
return null;
}
// Set data at current node
public void setDataAtCurrent(String val){
if(current != null)
current.data = val;
}
// Add item to the linux-modtest
public void addItem(String data){
// check if the head is null and insert.
if (head == null){
head = new ListNode(data,null);
}
else{
current = head;
previous = current;
// Loop to find the last element.
while(current.link != null){
previous = current;
current = current.link;
}
// insert element at the last.
current.link = new ListNode(data,null);
// after inserting the node, adjust the pointers.
previous = current;
current = current.link;
}
}
// insert the node after the current node.
public void insertAfterCurrent(String val){
ListNode nd = new ListNode(val,null);
current.link = nd;
previous = current;
current = current.link;
}
// delete the current node
public void deleteCurrentNode(){
// adjust the links before deleting the node.
previous.link = current.link;
current = null;
}
// Show the list.
public void showList(){
if(head == null)
return;
System.out.println(\"Printing List\");
ListNode temp = head;
while(temp != null){
System.out.println(temp.data);
temp = temp.link;
}
}
}
// Test Driver class to check ToDoList
public class TestDriver{
public static void main(String[] args){
ToDoList tdl = new ToDoList();
System.out.println(\"To Do List Tester!\");
System.out.println(\"Adding elements to the List\");
tdl.addItem(\"Hello\");
tdl.addItem(\"world\");
tdl.addItem(\"First Program\");
tdl.addItem(\"Second Program\");
tdl.addItem(\"Third Program\");
tdl.showList();
System.out.println(\"---------------------------------\");
System.out.println(\"Get data at Current node\");
System.out.println(tdl.getDataAtCurrent());
System.out.println(\"---------------------------------\");
System.out.println(\"Set data at Current node\");
tdl.setDataAtCurrent(\"First program\");
tdl.showList();
System.out.println(\"---------------------------------\");
System.out.println(\"Insert data after Current node\");
tdl.insertAfterCurrent(\"Fourth Program\");
tdl.showList();
System.out.println(\"---------------------------------\");
System.out.println(\"Delete Current Node\");
tdl.deleteCurrentNode();
tdl.showList();
}
}
OUTPUT:
To Do List Tester!
Adding elements to the List
Printing List
Hello
world
First Program
Second Program
Third Program
---------------------------------
Get data at Current node
Third Program
---------------------------------
Set data at Current node
Printing List
Hello
world
First Program
Second Program
First program
---------------------------------
Insert data after Current node
Printing List
Hello
world
First Program
Second Program
First program
Fourth Program
---------------------------------
Delete Current Node
Printing List
Hello
world
First Program
Second Program
First program





