Create a to do list that manages tasks in a list Implement i
Create a to do list that manages tasks in a list. Implement it using the LinkedList API. (You may import ArrayList or List, but please do not import any other packages.) In particular, see the ToDoListADT interface which contains a description of the methods required in the to do list. Create a Task class that implements Comparable and contains the name of the task, the due date of the task (stored as a string, since we did not cover the Date class), the time that the task is due (also stored as a string), and an integer that indicates the importance of the task (1 being least important). Make getters and a compareTo method that passes in another Task and compares the importance level of the tasks. This method should compare the importance of the two tasks and return -1 if the first task has a higher level of importance than the second task, 0 if the length of the tasks is equal, and 1 if the first task has a lower level of importance than the second task.
All tasks are added into the to do list. The highest priority task in a to do list is the task with the highest level of importance. When removing tasks, your to do list should return the highest priority task. Tasks should be removed from the to do list when the task is completed.
import java.util.*;
public class Linkedlist {
//Main function
public static void main(String[] args) {
//Sample list of strings
LinkedList<String> strings = new LinkedList<String>();
//Adding string to list
strings.add(\"D\");
strings.add(\"H\");
strings.add(\"R\");
strings.add(\"T\");
//Calling reverse method
reverse(strings);
}
//Method that reverse the elements present in the linked list
public static void reverse(LinkedList<String> strings) {
//Printing original strings
System.out.println(\"\ Original: \ \" + strings + \" \ \");
//Variable to hold string
String temp;
//Finding length
int len = strings.size(), k;
//Variable to index from backward
k = len - 1;
//Iterate from starting index to middle index
for (int i = 0; i < len / 2; i++) {
//Taking ith element
temp = strings.get(i);
//Swapping ith element with kth element
strings.set(i, strings.get(k));
//Swapping previous ith element into kth element
strings.set(k, temp);
//Decrementing k
k--;
}
//Printing list in reverse order
System.out.println(\"\ Reverse: \ \" + strings + \" \ \");
}
}
static class Task implements Comparable<Task> {
//TODO: complete class
}
static interface ToDoListADT {
/**
* Adds one task to the to do list.
* @param t the task to be added to the to do list.
*/
public void add(Task t);
/**
* Removes and returns the highest priority task in the to do list.
* @return the highest priority task in the buffer.
*/
public Task remove();
/**
* Returns true if this to do list contains no elements.
* @return true if this to do list is empty
*/
public boolean isEmpty();
/**
* Returns the number of elements in this to do list.
* @return the integer representation of the size of the to do list.
*/
public int size();
}
static class ToDoList implements ToDoListADT {
//TODO: complete class
}
public static void main(String[] args) {
ToDoList toDoList = new ToDoList();
Task t1 = new Task(\"Unit 11 Programming Assignment\", \"02/26/2016\", \"11:59 PM\", 2);
Task t2 = new Task(\"Mastering Physics: Chapter 5 Assignment\", \"02/24/2016\", \"11:59 PM\", 3);
Task t3 = new Task(\"Dentist Appointment\", \"03/05/2017\", \"2:30PM\", 1);
toDoList.add(t1);
toDoList.add(t2);
toDoList.add(t3);
System.out.println(toDoList.remove());
System.out.println(toDoList.remove());
System.out.println(toDoList.remove());
}
}
Solution
public class Task implements Comparable<Task>{
private String taskName;
private String date;
private String time;
private int importance;
public Task(String taskName, String date, String time, int importance) {
super();
this.taskName = taskName;
this.date = date;
this.time = time;
this.importance = importance;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getImportance() {
return importance;
}
public void setImportance(int importance) {
this.importance = importance;
}
@Override
public int compareTo(Task t) {
if(importance==t.importance)
return 0;
else if(importance<t.importance)
return 1;
else
return -1;
}
@Override
public String toString() {
return \"Task [taskName=\" + taskName + \", date=\" + date + \", time=\" + time + \", importance=\" + importance + \"]\";
}
}
===============================================================================
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class ToDOTasks {
private static List<Task> tasks = new LinkedList<Task>();
public static void main(String[] args) {
Task t1 = new Task(\"Unit 11 Programming Assignment\", \"02/26/2016\", \"11:59 PM\", 2);
Task t2 = new Task(\"Mastering Physics: Chapter 5 Assignment\", \"02/24/2016\", \"11:59 PM\", 3);
Task t3 = new Task(\"Dentist Appointment\", \"03/05/2017\", \"2:30PM\", 1);
tasks.add(t1);
tasks.add(t2);
tasks.add(t3);
Collections.sort(tasks);
System.out.println(\"Tasks sorted in importance order\");
for (Task t : tasks)
System.out.println(t);
System.out.println(\"Removing highest importance task \" + removeTask());
}
// deleting task with highest importance
private static Task removeTask() {
Collections.sort(tasks);
return tasks.remove(0);
}
}



