1. Design a Java interface called Priority that includes two methods: setPriorit
ID: 3889268 • Letter: 1
Question
1. Design a Java interface called Priority that includes two methods: setPriority and getPriority. The interface should define a way to establish numeric priority among a set of objects. Design and implement a class called Task that represents a task (such as on a todo list) that implements the Priority interface. Create a driver class to exercise some Task objects
2. Modify the Task class from the project in question 1 so that it also implements the Comparable interface from the Java standard class library. Implement the interface such that the tasks are ranked by priority. Create a driver class whose main method shows these new features of Task objects.
Ive got the first one working but cant seem to figure out number 2. HELP IS NEEDED THANK YOU. please provide working program.
public interface Priority {
//Setters & getters methods
public void setPriority(int priority);
public int getPriority();
int priority=1; //default number for a task
}
// end of priority interface
**************************************************************
public abstract class Task implements Priority {
String taskName;
int priority;
//Instance Variables
public Task(String taskName) {
this.taskName = taskName; //sets
priority = Priority.priority; //sets default of the task
}
//default constructor
public Task(String taskName, int priority) {
this.taskName=taskName; //set task name
setPriority(priority); //calling setPriority method
}
@Override
public final void setPriority (int prior) {
this.priority=prior; //method to set the priority number of the task
}
@Override
public int getPriority() {
return priority; //method that returns the priority number of the task
}
public String getTaskName() {
return taskName; //method that returns the task
}
@Override
public String toString() {
String taskContent="";
taskContent="Task Name: "+getTaskName()+
" Priority Name: "+getPriority()+" ";
return taskContent;
}
}
**************************************************************
public class TaskDriver {
public static void main(String[] args) {
Task task1=new Task("Clean Room", 3) {};
Task task2=new Task("Make Bed") {};
Task task3=new Task("Eat Breakfast", 2) {};
//creates and instantiates task objects
System.out.println(task1);
System.out.println(task2);
System.out.println(task3);
}
}
*********************************************** END PROGRAM.
Explanation / Answer
public interface Priority {
//Setters & getters methods
public void setPriority(int priority);
public int getPriority();
int priority=1; //default number for a task
}
----------------------------------------------------------------------------------------
//Task.java
//The java program implements the Comparable interface
public abstract class Task implements Priority,Comparable<Task> {
String taskName;
int priority;
//Instance Variables
public Task(String taskName) {
this.taskName = taskName; //sets
priority = Priority.priority; //sets default of the task
}
//Override compareTo method
public int compareTo(Task o) {
if(this.priority<o.getPriority())
return -1;
else if(this.priority>o.getPriority())
return 1;
else
return 0;
}
//default constructor
public Task(String taskName, int priority) {
this.taskName=taskName; //set task name
setPriority(priority); //calling setPriority method
}
@Override
public final void setPriority (int prior) {
this.priority=prior; //method to set the priority number of the task
}
@Override
public int getPriority() {
return priority; //method that returns the priority number of the task
}
public String getTaskName() {
return taskName; //method that returns the task
}
@Override
public String toString() {
String taskContent="";
taskContent="Task Name: "+getTaskName()+
" Priority Name: "+getPriority()+" ";
return taskContent;
}
}
----------------------------------------------------------------------------------------
/*
* Testing compareTo method of Task class
* */
//TaskDriver.java
public class TaskDriver {
public static void main(String[] args) {
Task task1=new Task("Clean Room", 3) {};
Task task2=new Task("Make Bed") {};
Task task3=new Task("Eat Breakfast", 2) {};
//creates and instantiates task objects
if(task1.compareTo(task2)>0)
System.out.println("Task1 priority is graeter than task2");
else if(task1.compareTo(task2)<0)
System.out.println("Task1 priority is less than task2");
else
System.out.println("Task1 and Task2 are equal.");
if(task2.compareTo(task3)>0)
System.out.println("Task2 priority is graeter than task3");
else if(task2.compareTo(task3)<0)
System.out.println("Task2 priority is less than task3");
else
System.out.println("Task2 and Task3 are equal.");
if(task3.compareTo(task1)>0)
System.out.println("Task3 priority is graeter than task1");
else if(task3.compareTo(task1)<0)
System.out.println("Task3 priority is less than task1");
else
System.out.println("Task3 and Task1 are equal.");
}
}
----------------------------------------------------------------------------------------
Sample Output:
Task1 priority is graeter than task2
Task2 priority is less than task3
Task3 priority is less than task1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.