Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1,Implement the compareTo method according to the commented description in the source code. The current (stub) version of compareTo is shown below. public class Student

1,Implement the compareTo method according to the commented description in the source code. The current (stub) version of compareTo is shown below.

public class Student implements Comparable { private String fname; private String lname; private int section; /** Creates a new student. */ public Student(String last, String first, int sec) { lname = last; fname = first; section = sec; } /** Returns this student's first name. */ public String getFirstName() { return fname; } /** Returns this student's last name. */ public String getLastName() { return lname; } /** Returns this student's section. */ public int getSection() { return section; } /** * Implement compareTo so that students are ordered in the * following way: in ascending order of last name, then in * ascending order of first name, and then in ascending order * of section. */ @Override public int compareTo(Student s) { //question is here return -99; } /** Returns a string representation of this student. */ @Override public String toString() { return section + ", " + lname + ", " + fname; } }

2,Implement the CompareStudentsBySection comparator according to the commented description in the source code. The current (stub) version is shown below.

import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; /** * ClassRoll.java. * A class to illustrate storing and sorting * a list of Students. */ public class ClassRoll { /** Drives execution. */ public static void main(String[] args) { List roll = Arrays.asList( new Student("Knuth", "Don", 2), new Student("Turing", "Alan", 2), new Student("Hoare", "Tony", 2), new Student("von Neumann", "John", 1), new Student("Dijkstra", "Jimmy", 1), new Student("Church", "Alonzo", 1), new Student("McCarthy", "John", 2), new Student("Turing", "Alan", 1), new Student("Dijkstra", "Edsger", 2)); System.out.println("Original Roll: "); roll.forEach(System.out::println); System.out.println(); // Sort the roll in natural order. Collections.sort(roll); System.out.println("Natural Order: "); roll.forEach(System.out::println); System.out.println(); // Shuffle the roll into a random order. // Use the Collections.shuffle() method. // *** Replace this line with your solution. System.out.println("Random Order: "); roll.forEach(System.out::println); System.out.println(); // Sort the roll in ascending order of section Comparator orderBySection = new CompareStudentsBySection(); Collections.sort(roll, orderBySection); System.out.println("Ascending Order of Section: "); roll.forEach(System.out::println); System.out.println(); // Sort the roll in descending order of section. // Use either Collections.reverseOrder() or // Comparator.reversed() in conjunction with // Collections.sort(). // *** Replace this line with your solution. System.out.println("Descending Order of Section: "); roll.forEach(System.out::println); System.out.println(); } /** * Implement this Comparator so that Student objects are compared * in ascending order of section. That is, compare(s1, s2) must * return a negative integer if s1's section is less than s2's section, * zero if s1 and s2 have the same section, and a postive integer * if s1's section is greater than s2's section. */ public static class CompareStudentsBySection implements Comparator { /** Compares s1 to s2 in with respect to the defined total order. */ public int compare(Student s1, Student s2) { //question is here return -99; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions