Question

For the code below write a public static main() method in class Student that: - creates...

For the code below write a public static main() method in class Student that:
- creates an ArrayList<Student> 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<Student> getCompByName =
   new Comparator<Student>(){

   @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<Student> getCompByDate =
   new Comparator<Student>(){

   @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;
   }
   }

   };

   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.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<Student> getCompByName =
new Comparator<Student>(){

@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<Student> getCompByDate =
new Comparator<Student>(){

@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;
}
}

};

public static void main(String args[])
{
   ArrayList<Student> al=new ArrayList<Student>();
     
   al.add(new Student("Miller",new Date(1478980869690L)));
   al.add(new Student("David",new Date(1478980867490L)));
   al.add(new Student("Jhon",new Date(1478980868950L)));
     
   al.add(new Student("Stirling",new Date(1478980876190L)));
   al.sort(getCompByName);
   System.out.println("Students list sorted by name");
   for(int i=0;i<al.size();i++)
   {
   System.out.println("Student Name:"+al.get(i).getName()+", Enrollemnt Dat:"+al.get(i).getEnrollment_date());
   }
   al.sort(getCompByDate);
   System.out.println("Students list sorted by enrollment date");
   for(int i=0;i<al.size();i++)
   {
   System.out.println("Student Name:"+al.get(i).getName()+", Enrollemnt Dat:"+al.get(i).getEnrollment_date());
   }
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
For the code below write a public static main() method in class Student that: - creates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Do the following a. Write a class Student that has the following attributes: - name:...

    1. Do the following a. Write a class Student that has the following attributes: - name: String, the student's name ("Last, First" format) - enrollment date (a Date object) The Student class provides a constructor that saves the student's name and enrollment date. Student(String name, Date whenEnrolled) The Student class provides accessors for the name and enrollment date. Make sure the class is immutable. Be careful with that Date field -- remember what to do when sharing mutable instance variables...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String...

    I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String name, major, status; private int rank;    public Student() { this.name = this.major = this.status = ""; this.rank = 0; }    public Student(String name, String major, String status) { this.name = name; this.major = major; this.status = status; } public String getName() { return name; } public String getMajor() { return major; } public String getStatus() { return status; }    public int...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • JAVA program Note: you can't change anything is already written Student.java public class Student {   ...

    JAVA program Note: you can't change anything is already written Student.java public class Student {    private String name;    private String major;    private double gpa;    public Student(String name, String major, double gpa) {        // TO-DO: Assign the given parameters to the data fields. Use the this keyword.    }    public double getGPA() {        // TO-DO: return this.gpa    }    public String getName() {        // TO-DO: return this.name    }...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT