I\'m trying to return a sorted version of someone\'s task list. It needs to be s
ID: 3547434 • Letter: I
Question
I'm trying to return a sorted version of someone's task list. It needs to be sorted by what priority they have it set as. I need to know the code to sort it, then print it. Thanks!
Here is this:
public class Task {
private String thingToDo;
private int priority;
public Task (){
thingToDo = "";
priority = 0;
}
public Task (String a, int b){
setThingToDo(a);
setPriority(b);
}
public String getThingToDo(){
return thingToDo;
}
public int getPriority() {
return priority;
}
public String setThingToDo(String newThingToDo){
if (newThingToDo.length() > 0)
thingToDo = newThingToDo;
return thingToDo;
}
public int setPriority(int newPriority) {
if (newPriority > 0)
priority = newPriority;
return priority;
}
public boolean equals(Task other){
if ( thingToDo.equals (other.thingToDo)
&& priority == other.priority)
return true;
else
return false;
}
public String toString(){
return "(" +priority + ") " + thingToDo + " ";
}
}
Explanation / Answer
I have written a sort function in Task class to sort ArrayList and corrected your code so that it compiles and runs correctly.
import java.util.*;
public class Task
{
private String thingToDo;
private int priority;
public Task ()
{
thingToDo = "";
priority = 0;
}
public Task (String a, int b){
setThingToDo(a);
setPriority(b);
}
public String getThingToDo(){
return thingToDo;
}
public int getPriority() {
return priority;
}
public String setThingToDo(String newThingToDo){
if (newThingToDo.length() > 0)
thingToDo = newThingToDo;
return thingToDo;
}
public int setPriority(int newPriority) {
if (newPriority > 0)
priority = newPriority;
return priority;
}
public boolean equals(Task other){
if ( thingToDo.equals (other.thingToDo)
&& priority == other.priority)
return true;
else
return false;
}
public String toString(){
return "(" +priority + ") " + thingToDo + " ";
}
//method to sort task list based on priority
public static void sort(ArrayList<Task> list)
{
Task temp;
int i=0,j=0;
for(i=0;i<list.size();i++)
{
for(j=i+1;j<list.size();j++)
{
if(list.get(i).getPriority()>list.get(j).getPriority())
{
temp=list.get(j);
list.set(j,list.get(i));
list.set(i,temp);
}
}
}
}
public static void displayMenu(){
System.out.println("Please select from the following choices: ");
System.out.println("(1) Create a new task");
System.out.println("(2) Remove an existing task");
System.out.println("(3) View current task list");
System.out.println("(4) Quit program");
}
public static void main(String[] args) {
int menuOption = 0;
ArrayList<Task> list = new ArrayList<Task>();
Task t2 = new Task ("a", 1);
Task t3 = new Task ("b", 2);
Task t4 = new Task ("c", 3);
list.add(t2);
list.add(t3);
list.add(t4);
//Display welcome message which will ONLY display at the beginning.
System.out.println("Welcome to the Task Master Program");
//Begin loop and initilize menu (located at the end of code)
do{
Scanner userInput = new Scanner(System.in);
//Contains the various cases of the switch.
displayMenu();
menuOption=userInput.nextInt();
switch (menuOption) {
//Case 1- Converts integer to binary (includes input error check)
case 1:
System.out.println("Please enter a task");
Scanner newInput = new Scanner(System.in);
String task = newInput.nextLine();
System.out.println("Please enter a priority #");
int priority = newInput.nextInt();
Task t1 = new Task(task, priority);
list.add(t1);
break;
//Case 2- Deletes a task from the list
case 2:
System.out.println("Which # would you like to delete:");
for (int j = 0; j < list.size(); j++){
Task x = list.get(j);
System.out.print(x);
}
Scanner deleteInput = new Scanner(System.in);
int deleteNumber = ((deleteInput.nextInt())-1);
Task temp = list.get(deleteNumber);
list.remove(temp);
break;
//Case 3- Quits program and prints farewell message.
case 3:
System.out.println();
System.out.println("Your Currernt Task List:");
for (int j = 0; j < list.size(); j++){
Task x = list.get(j);
System.out.print(x);
}
System.out.println();
break;
//Case 4- Quits program and prints farewell message.
case 4:
//i = 0;
System.out.println("Program Over");
break;
//Default- prints an error message for invalid integers
default:System.out.println("invalid menu option chosen");
break;
//closes loop and sets the loop's rules of looping for option 1 & 2, not 3.
}
}
while(menuOption != 4);
System.out.println("Print sorted array list");
sort(list);
System.out.println("Your Currernt Task List:");
for (int j = 0; j < list.size(); j++){
Task x = list.get(j);
System.out.print(x);
}
}
}