Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate()

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 getCompByName = new 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 getCompByDate = new 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions

Question

What do the terms variable costs and fixed costs mean?

Answered: 1 week ago