This lab uses a Student class with the following fields: private final String firstName; private final String lastName; private final...

80.2K

Verified Solution

Question

Programming

This lab uses a Student class with the following fields:

private final StringfirstName;
private final StringlastName;
private final String major;
private final int zipcode;
private final StringstudentID;
private final double gpa;

A TestData class has been provided that contains acreateStudents() method that returns an array of populated Studentobjects.

Assignmen

The Main method prints the list of Students sorted by last name.It uses the Arrays.sort() method and an anonymous Comparator objectto sort the array by the student’s last name.

Using the Arrays.sort() method and anonymous Comparator objectsprint out the array of students using the following three sortingcriteria:

  1. Sorted in order of major (A-Z)
  2. Sorted by zip code (increasing)
  3. Sorted by Grade Point Average (GPA) (decreasing)

Note: when sorting by GPA, you will need to be clever tocorrectly sort GPA’s such as 3.1, 3.2, 3.4 in the correctorder.

Note: In this assignment the Comparator class will beinstantiated as:

               new Comparator()

The type argument means that the Comparatoroperates on Student objects. You will learn more about this in thenext lesson on Generics.

Example Output

Students Sorted By LastName

First Name   Last Name   Major             Zip Code    GPA        

Penny       Adiyodi     Finance           90304       3.1        

Marina      Andrieski   Marketing         76821       3.4        

Quentin     Coldwater   Biology           43109       2.7        

Henry       Fogg        Botany            49022       3.8        

Margo       Hanson      Psychology        56231       2.91       

Josh        Hoberman    Astronomy         33021       2.5        

Kady         Orloff-DiazEnglish           65421       3.2        

Alice       Quinn       Math              89123       4.0        

Eliot       Waugh       Chemistry         12345       2.1        

Julia       Wicker       ComputerScience   43210       4.0        

==============================================================

Students Sorted By Major

First Name   Last Name   Major             Zip Code    GPA        

Josh        Hoberman    Astronomy         33021       2.5        

Quentin     Coldwater   Biology           43109       2.7        

Henry       Fogg        Botany            49022       3.8        

Eliot       Waugh       Chemistry         12345       2.1        

Julia       Wicker       ComputerScience   43210       4.0        

Kady         Orloff-DiazEnglish           65421       3.2        

Penny       Adiyodi     Finance           90304        3.1  

==============================================================

Students Sorted By Zip Code

First Name   Last Name   Major             Zip Code    GPA        

Eliot       Waugh       Chemistry         12345       2.1        

Josh        Hoberman    Astronomy         33021       2.5        

Quentin     Coldwater   Biology           43109       2.7        

Julia       Wicker       ComputerScience   43210       4.0        

Henry       Fogg        Botany            49022       3.8        

Margo       Hanson      Psychology        56231       2.91       

Kady         Orloff-DiazEnglish           65421       3.2        

==============================================================

Students Sorted By GPA

First Name   Last Name   Major             Zip Code    GPA        

Julia       Wicker       ComputerScience   43210       4.0        

Alice       Quinn       Math              89123       4.0        

Henry       Fogg        Botany            49022       3.8        

Marina      Andrieski   Marketing         76821       3.4        

Kady         Orloff-DiazEnglish           65421        3.2  

Coding:

package home;

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

public class Main {

public static void main(String[] args) {
Student[] students = TestData.createStudents();
Arrays.sort(students,new Comparator() {
public int compare(Student s1,Student s2) {
String lastname1 = s1.getLastName();
String lastname2 = s2.getLastName();
return lastname1.compareTo(lastname2);
}
});
printStudentList(\"Students Sorted By LastName\",students);

// TODO - sort students by major
printStudentList(\"Students Sorted By Major\",students);

// TODO - sort students by zip code
printStudentList(\"Students Sorted By Zip Code\",students);

// TODO - sort students by GPA
printStudentList(\"Students Sorted By GPA\",students);
}

public static void printStudentList(String title,Student[] list){
final String format = \"%-12s %-12s %-18s %-12s %-12s\n\";
System.out.println(title);
System.out.printf(format,\"First Name\",\"Last Name\",\"Major\",\"ZipCode\",\"GPA\");
for (Student s : list) {
System.out.printf(format,s.getFirstName(),s.getLastName(),s.getMajor(),s.getZipcode(),s.getGpa());
}
System.out.println(\"==============================================================\n\");
}
}

package home;

/**
* Student class (immutable)
*/
public final class Student {
private final String firstName;
private final String lastName;
private final String major;
private final int zipcode;
private final String studentID;
private final double gpa;

public Student(String firstName, String lastName, String major,int zipcode, String studentID, double gpa) {
this.firstName = firstName;
this.lastName = lastName;
this.major = major;
this.zipcode = zipcode;
this.studentID = studentID;
this.gpa = gpa;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getMajor() {
return major;
}

public int getZipcode() {
return zipcode;
}

public String getStudentID() {
return studentID;
}

public double getGpa() {
return gpa;
}
}

package home;

public class TestData {
public static Student[] createStudents() {
Student[] students = {
new Student(\"Julia\",\"Wicker\",\"ComputerScience\",43210,\"A0123\",4.0),
newStudent(\"Quentin\",\"Coldwater\",\"Biology\",43109,\"D3902\",2.7),
new Student(\"Eliot\",\"Waugh\",\"Chemistry\",12345,\"Z0101\",2.1),
new Student(\"Penny\",\"Adiyodi\",\"Finance\",90304,\"M2030\",3.1),
newStudent(\"Margo\",\"Hanson\",\"Psychology\",56231,\"L9832\",2.91),
new Student(\"Alice\",\"Quinn\",\"Math\",89123,\"U8932\",4.0),
newStudent(\"Kady\",\"Orloff-Diaz\",\"English\",65421,\"K3949\",3.2),
new Student(\"Henry\",\"Fogg\",\"Botany\",49022,\"R9392\",3.8),
new Student(\"Josh\",\"Hoberman\",\"Astronomy\",33021,\"H3021\",2.5),
newStudent(\"Marina\",\"Andrieski\",\"Marketing\",76821,\"J3491\",3.4)
};
return students;
}
}

Answer & Explanation Solved by verified expert
4.5 Ratings (635 Votes)
The comparators have been explained using commentsPlease let me know if there is any problem CodeStudentjavapackage home Student class immutable public final class Student private final String firstName private final String lastName private final String major private final int zipcode private final String studentID private final double gpa public StudentString firstName String lastName String major int zipcode String studentID double gpa thisfirstName firstName thislastName lastName thismajor major thiszipcode zipcode thisstudentID studentID thisgpa gpa public String getFirstName return firstName public String getLastName return lastName public String getMajor    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students