Note that the code in the Date object should take care of parsing the line. The
ID: 667837 • Letter: N
Question
Note that the code in the Date object should take care of parsing the line. The Date class needs a toString function that returns a String with the correct information.
To test whether one Date is before or equal to another Date, the Date class should have a compareTo function. The Date class should start with a line like:
The header for the compareTo method should have a header like:
This method should return -1 if this Date is before the other Date, 0 if they are equal, and +1 if this Date is after the other Date.
Explanation / Answer
import java.text.*;
public class Date implements Comparable
{
String s1;
Date(String d1)
{
s1=d1;
}
public int compareTo(Date other)
{
SimpleDateFormat fo = new SimpleDateFormate("dd/mm/yy");
if (fo(this.s1).equals(fo(other.s1))) return 0;
return (fo(this.s1).before(fo(other.s1)) ? -1 : 1);
}
}
class m
{
public static void main(String args[])
{
Date o = new Date("12/12/12");
Date o1 = new Date(13/12/12");
int d = o.compareTo(o1);
System.out.println(d);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.