Source of SortStudents.java


  1: import java.util.Scanner;
  2: import java.util.List;
  3: import java.util.ArrayList;
  4: import java.util.Collections;

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

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

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

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

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

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

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

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

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