I already have the class working. I just need to print it sorted by the length of name, and by GPA from class collections and arrays. My current code is as follows. copy2 = new LinkedList...


I already have the class working. I just need to print it sorted by the length of name, and by GPA from class collections and arrays. My current code is as follows.


}) ;<br>//looping and displaying sorted list<br>System.out.println (
copy2 = new LinkedList (list); //sort gpa Collections.sort (copy2, new Comparator () { public int compare (Student sl, Student s2) { //returnign -1 if sl has higher gpa than s2 if(sl.getGpa () > s2.getGpa () ) { return -l; //returning 1 else if(sl.getGpa () < s2.getgpa="" ()="" )="" {="" return="" 1;="" }else{="" return="" 0;="" })="" ;="" system.out.println="" ("/nstudents="" sorted="" by="" gpa="" (descending)="" :="" ");="" for="" (student="" s="" system.out.println="" (s);="" :="" copy2)="" {="" student="" array[]="new" student="" [list.size="" ()];="" list.toarray="" (array);="" arrays.sort="" (array,="" new=""> () { public int compare (Student sl, Student s2) { return sl. getName ().1ength () s2.getName ().length () ; }) ; System.out.println ("\nStudents array sorted by name length (ascending) :"); for (Student s: array) { System.out.println (s); } "/>
Extracted text: }) ; //looping and displaying sorted list System.out.println ("Students sorted by name length (ascending) :"); for (Student s : copyl) { System.out.println (s); //taking another copy of the list LinkedList copy2 = new LinkedList (list); //sort gpa Collections.sort (copy2, new Comparator () { public int compare (Student sl, Student s2) { //returnign -1 if sl has higher gpa than s2 if(sl.getGpa () > s2.getGpa () ) { return -l; //returning 1 else if(sl.getGpa () < s2.getgpa="" ()="" )="" {="" return="" 1;="" }else{="" return="" 0;="" })="" ;="" system.out.println="" ("/nstudents="" sorted="" by="" gpa="" (descending)="" :="" ");="" for="" (student="" s="" system.out.println="" (s);="" :="" copy2)="" {="" student="" array[]="new" student="" [list.size="" ()];="" list.toarray="" (array);="" arrays.sort="" (array,="" new=""> () { public int compare (Student sl, Student s2) { return sl. getName ().1ength () s2.getName ().length () ; }) ; System.out.println ("\nStudents array sorted by name length (ascending) :"); for (Student s: array) { System.out.println (s); }
import java.util.Arrays;<br>import java.util.Collections;<br>import java.util.Comparator;<br>import java.util.LinkedList;<br>public class Student {<br>private String name;<br>private double gpa;<br>public Student (String name, double gpa) {<br>this.name - name;<br>this.gpa = gpa;<br>public String getName () {<br>return name;<br>public double getGpa () {<br>return gpa;<br>public String tostring () {<br>return (
list = new LinkedList () ; list.add (new Student ("Kyle", 3.4)) : list.add (new Student ("Dog", 4.0)); list.add (new Student ("Jeff", 3.97)); list.add (new Student ("Manny", 1.9)); list.add (new Student ("wog", 5.1)); // taking a copy of list for sorting, so that sorting will not affect // the initial list LinkedList copyl = new LinkedList (1ist); // with a custom Comparator, sorting copyl list using Colle // method by name length Collections.sort (copyl, new Comparator () { // this method will return a negative value if sl n // before s2, positive value if sl needs to come af // have same order. public int compare (Student sl, Student s2) { "/>
Extracted text: import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; public class Student { private String name; private double gpa; public Student (String name, double gpa) { this.name - name; this.gpa = gpa; public String getName () { return name; public double getGpa () { return gpa; public String tostring () { return ("name," + name + " Gpa: " + gpa); public static void main (String ] args) { LinkedList list = new LinkedList () ; list.add (new Student ("Kyle", 3.4)) : list.add (new Student ("Dog", 4.0)); list.add (new Student ("Jeff", 3.97)); list.add (new Student ("Manny", 1.9)); list.add (new Student ("wog", 5.1)); // taking a copy of list for sorting, so that sorting will not affect // the initial list LinkedList copyl = new LinkedList (1ist); // with a custom Comparator, sorting copyl list using Colle // method by name length Collections.sort (copyl, new Comparator () { // this method will return a negative value if sl n // before s2, positive value if sl needs to come af // have same order. public int compare (Student sl, Student s2) {
Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here