A java question. In this problem you will do a better implementation of the to d
ID: 3840041 • Letter: A
Question
A java question. In this problem you will do a better implementation of the to do list. Write a class ToDoList that maintains a priority queue to manage the items to be done.
Write a class ToDoItem. ToDoItem has a description and a priority. The constructor takes these as parameters in that order.
ToDoItem implements Comparable interface and has these methods:
1. get methods(accessors) for priority and description
2. equals method. Two items are equal if and only if they have both the same priority and description
3. compareTo method orders by priority. If priorities are equal, order by description.
4. toString returns a String representation of the ToDoItem in this format:(!!!)
Write the class ToDoList. Its constructor initializes an empty PriorityQueue.
ToDoList has these methods:
1). public void add(ToDoItem item) Adds an item for this ToDoList
2). public ToDoItem nextItem() removes and returns the next item to do. (The one with the priority closest to 1). Note that this also violates the rule about mutators not returning values
3). public boolean hasNext() returns true if there is at least one item left to do otherwise false
4). public ToDoItem peek() returns the next item to do but does not remove it from the list
Please show the full code and your output, and please separate "ToDoRunner.java", "ToDoItem.java", and "ToDoList.java" as three files. Thank you!
-------------------------------------------------------------------------------------------------
The ToDoRunner is given as follow:
ToDoRunner.java
Explanation / Answer
Hi Below is your code: -
ToDoRunner.java
public class ToDoRunner {
public static void main(String[] args) {
ToDoList todo = new ToDoList();
todo.add(new ToDoItem("sleep", 12));
todo.add(new ToDoItem("study", 3));
todo.add(new ToDoItem("see friends", 1));
todo.add(new ToDoItem("eat", 5));
todo.add(new ToDoItem("watch a movie", 4));
todo.add(new ToDoItem("eat", 5));
todo.add(new ToDoItem("see a movie", 3));
System.out.println("hasNext: " + todo.hasNext());
System.out.println("peek when queue has an item: " + todo.peek());
// remove and print
System.out.println("Remove all items and print:");
while (todo.hasNext()) {
System.out.println(todo.nextItem());
}
System.out.println("peek when queue is empty: " + todo.peek());
System.out.println("hasNext: " + todo.hasNext());
}
}
ToDoItem.java
public class ToDoItem implements Comparable<ToDoItem> {
private String description;
private int priority;
public ToDoItem(String description, int priority) {
super();
this.description = description;
this.priority = priority;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public int compareTo(ToDoItem o) {
if (this.priority > o.getPriority()) {
return 1;
} else if (this.priority < o.getPriority()) {
return -1;
} else {
return this.description.compareTo(o.description);
}
}
//hashcode needs to be overridden because equals is overridden.
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + priority;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ToDoItem other = (ToDoItem) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (priority != other.priority)
return false;
return true;
}
public String toString() {
return "ToDoItem [description=" + description + ", priority=" + priority + "]";
}
}
ToDoList.java
import java.util.PriorityQueue;
public class ToDoList {
PriorityQueue<ToDoItem> queue;
public ToDoList() {
queue = new PriorityQueue<>();
}
public void add(ToDoItem item) {
queue.add(item); // method to add the item in the queue
}
public ToDoItem nextItem() {
return queue.poll(); // method to remove first element from the queue
// and return it.
}
public boolean hasNext() {
if(queue.isEmpty()) { // method to check if queue is still having element
return false;
} else {
return true;
}
}
public ToDoItem peek() {
return queue.peek(); // method to return first element but not remove it
}
}
Sample Run: -
hasNext: true
peek when queue has an item: ToDoItem [description=see friends, priority=1]
Remove all items and print:
ToDoItem [description=see friends, priority=1]
ToDoItem [description=see a movie, priority=3]
ToDoItem [description=study, priority=3]
ToDoItem [description=watch a movie, priority=4]
ToDoItem [description=eat, priority=5]
ToDoItem [description=eat, priority=5]
ToDoItem [description=sleep, priority=12]
peek when queue is empty: null
hasNext: false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.