I am working on a task list in java in which I have my tasks and their initial priorities stated in the driver. As the initial priority numbers get passed through the Tasks class it goes through the method setPriority, where it first gets priority from the initial priority then it goes through the if statement in the setPriority method, where it is given a priority from low to high based, or if no priority was initially set a statement reads out 'no priority was set'.
public interface Priority {
//getPriority method
public int getPriority();
//setPriority method
public String setPriority(int priority);
}
public class Tasks implements Priority {
//variables
private String task;
private int priority;
private String newPriority;
//set variables
public Tasks(String task, int priority) {
this.task = task;
this.priority = priority;
}
//get task
public String getTask() {
return task;
}
//use from the Priority interface getPriority
@Override
public int getPriority() {
return priority;
}
//use from the Priority interface setPriority
//if statement to change int priority to newPriority
@Override
public String setPriority(int priority) {
if (priority >= 3) {
newPriority = ("LOW");
} else if (priority >= 4 && priority <= 7) {
newPriority = ("MED");
} else if (priority >=8) {
newPriority = ("HIGH");
} else {
newPriority = ("There is no priority set");
}
return newPriority;
}
//return task and new Priority
public String ranking()
{
return("Task: " task " Priority: " newPriority);
}
}
public class Driver {
public static void main(String[] args)
{
//tasks listed
Tasks[] task = new Tasks[4];
task[0] = new Tasks( "biking", 3 );
task[1] = new Tasks( "school work", 9 );
task[2] = new Tasks( "taxes", 10 );
task[3] = new Tasks( "cooking", 5 );
//call method from end of Tasks class to print end result
//task.ranking();
//iterate over tasks
for ( int j=0; j <= 4; j )
System.out.print( task[j].ranking());
//the new list of tasks is put in order
}
}
CodePudding user response:
If your problematic line is this commented-out line:
//task.ranking();
then ranking
needs to be called on a Tasks
object.
task
is not a Tasks
, it is an array of Tasks
. You could call task[0].ranking()
.
Editorial: your names are a confusion of singular and plural. An object of your Tasks
class looks more like it is a task. Your task
variable is 4 Tasks (each of which is a task).
I recommend you rename class Tasks
as Task
to reflect what is is. I recommend you rename variable task
as tasks
for the same reason.
It remains unclear to me what ranking
is supposed to do.
If your problem line is this one:
System.out.print( task[j] task.ranking());
then the same observation about you needing to call ranking
on a single Tasks object, not an array of them. Additionally, ranking
returns nothing -- it has a return type of void
-- so you can't do a string concatenation on that return value.
And lastly (I hope), ranking
takes two arguments, but you're calling it with none. I repeat, it's unclear what you're trying to accomplish with it.
CodePudding user response:
When you attempt to print the rankings, you call them like this:
for ( int j=0; j <= 4; j )
System.out.print( task[j] task.ranking());
First of all, the task variable is an array, which means you're trying to call the .ranking() function on an array. What you wanted to type there was probably
System.out.print( task[j] task[j].ranking());
However, the problem here is that your ranking method is a void function that doesn't return anything.
public void ranking(String task, String newPriority){
System.out.println("Task: " task "Priority: " newPriority);
}
In order to correctly output the rankings, you need to change your ranking function to:
//return task and new Priority
public String ranking()
{
return "Task: " task "Priority: " priority;
}
and the output function to:
for ( int j=0; j <= 4; j )
System.out.println(task[j].ranking());
As a side note, if you rename the ranking function to toString(), you'll override Java's internal toString() function, which means you'll be able to print the task without needing to call the ranking function.
@Override
public String toString()
{
return "Task: " task "Priority: " priority;
}
....
for ( int j=0; j <= 4; j )
System.out.println(task[j]);