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

/ Instructions 1. Create a class named Students such that a. There is a public l

ID: 3725124 • Letter: #

Question

/ Instructions 1. Create a class named Students such that a. There is a public lastName field b. There is a public firstName field c. There is a public studentID field d. There is a constructor that takes these values as parameters and sets the field values 2. 3. Create a Comparator for Students that orders the objects by lastName In a public static void main method create an array of these objects with data you choose (i.e. objects of ["mortl", "wil", "888"] & [swift, "taylor", "777"1,etc.] a. i. you must have at least 5 elements in the array they should initially be out of order ii. b. use Array.sort to sort them using your comparator 4. 5. Modify Students to implement Comparable that orders objects based upon studentiD In a public static void main method a. create a list of these objects with data you choose (i.e. again, objects of "mortl", "will", "888"& [swift", "taylor, "777"1, etc.] i. ii. you must have at least 5 elements in the collection you should add them to be out of order b. use Collection.sort to sort them (this will use the Comparable interface)

Explanation / Answer

// Answer for first 3 question

//Student.java class

package com.studentComparatorPackage;

import java.util.Comparator;

public class Student implements Comparator{
  
   // Instance variable defined as per stated in problem statement
   public String firstName;
   public String lastName;
   public String studentId;
  
   //Parametrized Constructor
   public Student(String firstName, String lastName, String studentId) {
       super();
       this.firstName = firstName;
       this.lastName = lastName;
       this.studentId = studentId;
   }
  
   // Default constructor
   public Student() {
      
   }

   // Getter and setter of instance variable
   public String getFirstName() {
       return firstName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   public String getStudentId() {
       return studentId;
   }
   public void setStudentId(String studentId) {
       this.studentId = studentId;
   }
  
  
   // To implement Comparator we need to provide implementation of compare method
  
   @Override
   public int compare(Object student1, Object student2) {
       String lastName1 = ((Student) student1).getLastName();
         
            String lastName2 = ((Student) student2).getLastName();
         

            if (!(lastName1.equalsIgnoreCase(lastName2)))
              
              return lastName1.compareTo(lastName2);
          
           return 0;
      
   }
  

}

//comparatorTest.java class

package com.mainPackage;

import java.util.Arrays;
import java.util.Comparator;

import com.sun.xml.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
import com.studentComparatorPackage.Student;

public class comparatorTest {
  
  
   public static void main(String [] args){
      
      
       // creation of student array
       Student[] arrayOfStudents=new Student[5];
      
       Student s1=new Student("Rosy", "shah", "N1");
       Student s2=new Student("Tom","agarwal","N2");

       Student s3=new Student("Richel", "sid", "N3");
       Student s4=new Student("Hary","surname","N4");
      

       Student s5=new Student("Rose", "test", "N5");
      
       arrayOfStudents[0]=s1;
       arrayOfStudents[1]=s2;
       arrayOfStudents[2]=s3;
       arrayOfStudents[3]=s4;
       arrayOfStudents[4]=s5;
      
      
       // Array Sort method for sorting based on comparator
       Arrays.sort(arrayOfStudents,new Student());
  
      
      
      
      
   }

}

//answer of 4 the question

// Here we need to provide implemetation of comparable interface

// To implement Comparable we need to provide implementation of compareTo
   // method

   @Override
   public int compareTo(Object student) {
      
       int studentIdCompare=((Student)student).getStudentId();
      
       // To sort in ascending order
          return this.studentId-studentIdCompare;

   }