Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class Student that has the following attributes: name: String, the stude

ID: 3794202 • Letter: W

Question

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 Student(String name, Date whenEnrolled) that saves the student's name and enrollment date. 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 -- issued discussed in Chapter 3 (class Employee). Write contracts for all methods: preconditions/postconditions. Write the class invariant in the class javadoc comment. Implement a static method in class Student public static Comparator getCompByHame() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. Implement a static method in class Student public static Comparator getCompByDate() that returns a new comparator object for Student that compares 2 Students objects by their enrollment date.

Explanation / Answer

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

   };

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote