You are going to write two classes that will work together to represent a to-do
ID: 3567798 • Letter: Y
Question
You are going to write two classes that will work together to represent a to-do list. This is the first part, and we will write the first class and test it.
a) Create a class TodoItem that represents an item on a to do list. It should be able to store the following information in its instance variables:
You need to decide the best way to represent and store the above information.
b) Write getters and setters for your instance variables in TodoItem. The setters should only set the instance variables to valid values. If the value passed to the setter is not valid (for example, if the priority of an item is represented by a number between 1 and 5, and the number 6 is passed to the setter), then the setter should print an error message and the program should exit.
c) Write a toString() method for TodoItem.
d) Make TodoItem implement the Comparable interface. To do this, you will need to write the method compareTo, which should compare two TodoItems based on their due date and priority. You can decide exactly how the comparison is made (i.e. whether the due date or the priority is more important), but document the choice in your code.
e) Write a driver to test the above code. To test the compareTo method, create three different arrays of TodoItems and sort them using Arrays.sort(...)
Explanation / Answer
Let the comments stay as they let readers understand better about which choice was made to sort the items in the Todoitem array.
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
class Todoitem implements Comparable<Todoitem>{
private String item,date;
private int day,month;
private int priority;
public Todoitem(){
}
public Todoitem(String item, String date, int priority){
setItem(item);
setDate(date);
setPriority(priority);
}
public void setItem(String item){
this.item = item;
}
public void setDate(String date){
String[] temp = date.split(" ");
day = Integer.parseInt(temp[0]);
month = Integer.parseInt(temp[1]);
//disregarding leap years
if(day > 0 && month > 0) {
if (temp.length == 2 && day <= 31 && month <= 12) {
if (month == 2 && day > 28) {
System.out.println(item + " Invalid date");
System.exit(0);
}
else
this.date = date;
}
else{
System.out.println(item + " Invalid date");
System.exit(0);
}
}
else {
System.out.println(item + " Invalid date");
System.exit(0);
}
}
public void setPriority(int priority){
this.priority = priority;
}
public String getItem(){
return item;
}
public String getDate(){
return date;
}
public int getPriority(){
return priority;
}
@Override
public String toString(){
return priority + ": " + item + ", " + day + "/" + month;
}
/**
* Compares two Todoitem objects. First Todoitems with prior date have higher importance.
* If they fall in same month, priority number is considered.
* If their priority number is same, date is considered.
* @param newone
* @return 1 if calling object has higher importance than the parameter
* -1 if calling object has lower importance than the parameter
*/
@Override
public int compareTo(Todoitem newone) {
int newMonth = Integer.parseInt(newone.getDate().split(" ")[1]);
int newDay = Integer.parseInt(newone.getDate().split(" ")[0]);;
if(month < newMonth){
return 1;
}
else if(month > newMonth)
return -1;
else{
if(day < newDay)
return 1;
else if(day > newDay)
return -1;
else{
if(priority < newone.getPriority()){
return 1;
}
else if(priority > newone.getPriority())
return -1;
else{
System.out.println("Two items can't have same date and priority");
System.exit(0);
return 0;
}
}
}
}
}
class Driver{
public static void main(String... args){
Todoitem[] myItemList1 = new Todoitem[5];
Todoitem[] myItemList2 = new Todoitem[4];
Todoitem[] myItemList3 = new Todoitem[7];
myItemList1[0] = new Todoitem("Drink","21 11",4);
myItemList1[1] = new Todoitem("Throw party","28 2",9);
myItemList1[2] = new Todoitem("Save the world","31 12",7);
myItemList1[3] = new Todoitem("Fall in love","21 9",10);
myItemList1[4] = new Todoitem("Prepare to meet T-Rex","2 9",10);
myItemList2[0] = new Todoitem("Apples","1 5",4);
myItemList2[1] = new Todoitem("Oranges","18 3",9);
myItemList2[2] = new Todoitem("Flowers","11 8",7);
myItemList2[3] = new Todoitem("Soaps","1 9",10);
myItemList3[0] = new Todoitem("Maths","2 1",4);
myItemList3[1] = new Todoitem("Science","4 12",9);
myItemList3[2] = new Todoitem("Girlfriend","1 6",7);
myItemList3[3] = new Todoitem("Buddies","3 9",10);
myItemList3[4] = new Todoitem("Call of duty: AW","7 11",1);
myItemList3[5] = new Todoitem("Zombies","7 2",12);
myItemList3[6] = new Todoitem("Megadeath","21 2",32);
Arrays.sort(myItemList1, Collections.reverseOrder());
Arrays.sort(myItemList2, Collections.reverseOrder());
Arrays.sort(myItemList3, Collections.reverseOrder());
for(Todoitem temp : myItemList1){
System.out.println(temp.toString());
}
System.out.println(" ");
for(Todoitem temp : myItemList2){
System.out.println(temp.toString());
}
System.out.println(" ");
for(Todoitem temp : myItemList3){
System.out.println(temp.toString());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.