Source of SortStudents.java


  1: package comparators;

  3: import java.util.Scanner;
  4: import java.util.List;
  5: import java.util.ArrayList;
  6: import java.util.Collections;

  8: /**
  9:  * A program to sort Student objects in three ways -- defined in the Student 
 10:  * class. This program is the same for all three variations of the Student class
 11:  * (found in the folders named withClasses, anonymous and lambdas). Compare this
 12:  * version with the one in folder natural, in which the Student class defines no
 13:  * comparators.
 14:  *
 15:  * @author Mark Young (A00000000)
 16:  */
 17: public class SortStudents {

 19:     public static void main(String[] args) {
 20:         // create list of Students
 21:         List<Student> ss = new ArrayList<Student>();
 22:         Student jake = new Student("Jake");
 23:         Student angie = new Student("Angie");
 24:         Student geety = new Student("Geety");

 26:         // add grades to students and students to list
 27:         jake.setAsgnGrades(new int[]{75, 65});
 28:         angie.setAsgnGrades(new int[]{55, 65});
 29:         geety.setAsgnGrades(new int[]{95, 85});
 30:         Student.releaseAssignment(2);
 31:         ss.add(jake);
 32:         ss.add(geety);
 33:         ss.add(angie);

 35:         // report original
 36:         System.out.println("\nHere is a list of Students:");
 37:         for (Student s : ss) {
 38:             s.printStudentRecord();
 39:         }
 40:         pause();

 42:         // sort and present sorted
 43:         Collections.sort(ss, Student.BY_NAME);
 44:         System.out.println("\nHere is that same list, sorted by name:");
 45:         for (Student s : ss) {
 46:             s.printStudentRecord();
 47:         }
 48:         pause();

 50:         // sort and present sorted
 51:         Collections.sort(ss, Student.BY_GRADE);
 52:         System.out.println("\nHere is that same list, sorted by grade:");
 53:         for (Student s : ss) {
 54:             s.printStudentRecord();
 55:         }
 56:         pause();

 58:         // sort and present sorted
 59:         Collections.sort(ss, Student.BY_ANUMBER);
 60:         System.out.println("\nHere is that same list, sorted by A-#:");
 61:         for (Student s : ss) {
 62:             s.printStudentRecord();
 63:         }
 64:         pause();

 66:         // sort and present sorted
 67:         Collections.sort(ss);
 68:         System.out.println("\nHere is that same list, in its natural order:");
 69:         for (Student s : ss) {
 70:             s.printStudentRecord();
 71:         }
 72:         pause();
 73:     }

 75:     private static void pause() {
 76:         Scanner kbd = new Scanner(System.in);
 77:         System.out.print("\nPress enter...");
 78:         kbd.nextLine();
 79:         System.out.println();
 80:     }
 81: }