Question
For the code below write a public static main() method in class Student that: - creates an ArrayList object called students - adds 4 new
For the code below write a public static main() method in class Student that: - creates an ArrayList
import java.util.Comparator; import java.util.Date;
public final class Student {
private final String name; private final Date enrollment_date;
/*@invariant name != null && name.length>0; @*/ //class invariant /*@invariant enrollment_date != null; @*/ //class invariant public Student(String name, Date enrollment_date) { super(); assert name != null : "Precondition: name != null"; assert enrollment_date != null : "Precondition: enrollment_date != null"; assert name.indexOf(",") != -1 : "Precondition: name.indexOf(\",\") != -1"; this.name = name; this.enrollment_date = enrollment_date; assert getName() == name : "Postcondition: getName() == name"; assert getEnrollment_date() == enrollment_date : "Postcondition: getEnrollment_date() == enrollment_date"; }
public String getName() { return name; }
public Date getEnrollment_date() { return enrollment_date; }
public static Comparator
@Override public int compare(Student o1, Student o2) {
assert o1.name != null : "Precondition: o1.name != null"; assert o2.name != null : "Precondition: o2.name != null"; return o1.name.compareTo(o2.name); }
};
public static Comparator
@Override public int compare(Student p, Student q) {
if (p.getEnrollment_date().before(q.getEnrollment_date())) { return -1; } else if (p.getEnrollment_date().after(q.getEnrollment_date())) { return 1; } else { return 0; } }
};
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started