I am having trouble with this piece of code. Basically, the main function was given, and it was asked to develop the most simple version of class CountDown, that compiles the code.
Class Main:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
CountDown a = new CountDown(3,15);
CountDown b = new CountDown(2,20);
CountDown c = new CountDown(3,15);
List<CountDown> lst = new ArrayList<CountDown>();
lst.add(a);
lst.add(b);
lst.add(c);
Set<CountDown> set = new HashSet<CountDown>();
set.addAll(lst);
lst.clear();
lst.addAll(set);
Collections.sort(lst);
for(E e : lst) {
System.out.println(e);
}
}
}
Class CountDown:
public class CountDown implements Comparable<CountDown> {
private int hour;
private int minute;
public CountDown(int hour, int minute) throws Exception {
if ((hour > 23 || hour < 0) && (minute > 59 || minute < 0)) {
throw new IllegalArgumentException("Horas ou minutos invalidos");
} else {
this.hour = hour;
this.minute = minute;
}
}
public int gethour() {
return this.hour;
}
public int getminute() {
return this.minute;
}
@Override
public int compareTo(CountDown arg0) {
int result = 0;
int minute1 = arg0.getminute();
int hour1 = arg0.gethour();
result = this.getminute() - minute1;
if(result == 0) {
result = this.gethour() - hour1;
}
return result;
}
}
My problem is that in Main function this piece of code doesn't compile, and I have no idea on how to make it work. Can someone teach me whats wrong?
for(E e : lst) {
System.out.println(e);
}
toString()method to your class.