1. Design a Java interface called Priority that includes two methods: setPriorit
ID: 667680 • 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.
I've already finished 1 but now I need to do this,
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.
Explanation / Answer
import java.io.*;
interface Priority
{
void setPriority(String ob, int pro);
void getPriority();
}
class Task implements Priority, Comparable
{
String obj;
int p;
public void getPriority()
{
System.out.println("Object name is " + this.obj);
System.out.println("Object Priority is " + this.p);
}
public void setPriority(String oo, int pp)
{
this.obj=oo;
this.p=pp;
}
public void compareTo(Object obj1)
{
if(this.p>obj1.p)
{
System.out.println(this.obj);
System.out.println(obj1.obj);
}
else
{
System.out.println(obj1.obj);
System.out.println(this.obj);
}
}
}
class driver
{
public static void main(String args[])
{
Task tk = new Task();
Task tk1 = new Task();
tk.setPriority("Pen",2);
tk1.setPriority("Pencil",1);
tk.getPriority();
tk1.getPriority();
tk1.compareTo(tk2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.