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 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...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with...

    PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with that). 2)...

  • MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a...

    MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

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
ADVERTISEMENT