1. Create a class named Students such that: There is a public lastName field The
ID: 3725123 • Letter: 1
Question
1. Create a class named Students such that: There is a public lastName field There is a public firstName field There is a public studentID field There is a constructor that takes these values as parameters and sets the field values a. b. c. d. 2. 3. Create a Comparator for Students that orders the objects by lastName In a public static void main method a. create an array of these objects with data you choose (i.e. objects of ["mortl", "wil", "888"] & [swift", "taylor", "777"], et/ 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 create a list of these objects with data you choose (i.e. again, objects of ["morti", "wilr" 888"&[swift", "taylor", "777"), etc.] a. 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 to question 1, 2 and 3
import java.util.Arrays;
import java.util.Comparator;
public class Students {
public String firstNmae;
public String lastNmae;
public String studentId;
public Students(String firstNmae, String lastNmae, String studentId) {
this.firstNmae = firstNmae;
this.lastNmae = lastNmae;
this.studentId = studentId;
}
}
class SortbyLastName implements Comparator<Students>
{
// Used for sorting in ascending order of
// roll number
public int compare(Students a, Students b)
{
return a.lastNmae.compareTo(b.lastNmae);
}
}
class StudentsTester {
public static void main(String args[]) {
Students[] students = {new Students("Marry", "Gomes", "666"),
new Students("John", "Jackson", "444"),
new Students("Alfa", "Cian", "111"),
new Students("Piak", "Zoa", "222"),
new Students("Hella", "Hogg", "999")};
Arrays.sort(students, new SortbyLastName());
for (Students student :students) {
System.out.println(student.lastNmae + " " + student.firstNmae + " (" + student.studentId + ")");
}
}
}
================================================================
Answer to question 4, 5
import java.util.Arrays;
public class Students implements Comparable<Students> {
public String firstNmae;
public String lastNmae;
public String studentId;
public Students(String firstNmae, String lastNmae, String studentId) {
this.firstNmae = firstNmae;
this.lastNmae = lastNmae;
this.studentId = studentId;
}
@Override
public int compareTo(Students other) {
return studentId.compareTo(other.studentId);
}
}
class StudentsTester {
public static void main(String args[]) {
Students[] students = {new Students("Marry", "Gomes", "666"),
new Students("John", "Jackson", "444"),
new Students("Alfa", "Cian", "111"),
new Students("Piak", "Zoa", "222"),
new Students("Hella", "Hogg", "999")};
Arrays.sort(students);
for (Students student :students) {
System.out.println(student.lastNmae + " " + student.firstNmae + " (" + student.studentId + ")");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.