Sorting a sequence, array, collection of objects. Start with this (Example.java): class Student { } class Example { public static void main(String[] args) { Student a = new Student(); Student b = new Student(); Student c = new Student(); } } ----------------------- Let's flesh out our model of Student: class Student { String name; // instance variables (differ from object to object) int age; } class Example { public static void main(String[] args) { Student a = new Student(); a.name = "Kevin"; a.age = 16; Student b = new Student(); b.name = "Jessica"; b.age = 18; Student c = new Student(); c.name = "Leslie"; c.age = 15; } } ------------------------ We make the Students report: class Student { String name; int age; void talk() { System.out.println("Howdy!"); } } class Example { public static void main(String[] args) { Student a = new Student(); a.name = "Kevin"; a.age = 16; a.talk(); Student b = new Student(); b.name = "Jessica"; b.age = 18; b.talk(); Student c = new Student(); c.name = "Leslie"; c.age = 15; c.talk(); } } -------------------- They become more specific: class Student { String name; int age; void talk() { System.out.println(this.name + " " + this.age); } } class Example { public static void main(String[] args) { Student a = new Student(); a.name = "Kevin"; a.age = 16; a.talk(); Student b = new Student(); b.name = "Jessica"; b.age = 18; b.talk(); Student c = new Student(); c.name = "Leslie"; c.age = 15; c.talk(); } } ------------------------ Let's pretend for a moment that the name and age are private. Direct access from outside is strictly forbidden. class Student { private String name; private int age; void talk() { System.out.println(this.name + " " + this.age); } Student(String name, int age) { this.name = name; this.age = age; } } class Example { public static void main(String[] args) { Student a = new Student("Kevin", 16); a.talk(); Student b = new Student("Jessica", 18); b.talk(); Student c = new Student("Leslie", 15); c.talk(); } } -------------------------------------------- class Student { private String name; private int age; void talk() { System.out.println(this.name + " " + this.age); } Student(String name, int age) { this.name = name; this.age = age; } Student() { } } class Example { public static void main(String[] args) { Student a = new Student(); a.talk(); Student b = new Student("Jessica", 18); b.talk(); Student c = new Student("Leslie", 15); c.talk(); } } ---------------------------- import java.util.*; class Student { String name; int age; void talk() { System.out.println(this.name + " " + this.age); } Student(String name, int age) { this.name = name; this.age = age; } } class Example { public static void main(String[] args) { Scanner input = new Scanner(System.in); String what, who, age, line; System.out.print("type> "); line = input.nextLine(); while (! line.equals("finished")) { System.out.print("type> "); line = input.nextLine(); } System.out.println("Print sorted collection of students here."); } } ---------------------------------- import java.util.*; class Student { String name; int age; void talk() { System.out.println(this.name + " " + this.age); } Student(String name, int age) { this.name = name; this.age = age; } } class Example { public static void main(String[] args) { Student[] students = new Student[10]; Scanner input = new Scanner(System.in); String what, who, age, line; System.out.print("type> "); line = input.nextLine(); int index = 0; while (! line.equals("finished")) { students[index] = new Student("Student no. ", (index + 1)); index = index + 1; System.out.print("type> "); line = input.nextLine(); } for (int i = 0; i < students.length; i++) if (students[i] != null) students[i].talk(); } } ------------------------------- import java.util.*; class Student { String name; int age; void talk() { System.out.println(this.name + " " + this.age); } Student(String name, int age) { this.name = name; this.age = age; } } class Example { public static void main(String[] args) { ArrayList students = new ArrayList(); Scanner input = new Scanner(System.in); String what, who, age, line; System.out.print("type> "); line = input.nextLine(); while (! line.equals("finished")) { students.add(new Student("Student no. ", (students.size() + 1))); System.out.print("type> "); line = input.nextLine(); } for (int i = 0; i < students.size(); i++) students.get(i).talk(); } } ------------------------------------------------------- import java.util.*; class Student { String name; int age; void talk() { System.out.println(this.name + " " + this.age); } Student(String name, int age) { this.name = name; this.age = age; } } class Example { public static void main(String[] args) { ArrayList students = new ArrayList(); Scanner input = new Scanner(System.in); String what, who, age, line; System.out.print("type> "); line = input.nextLine(); while (! line.equals("finished")) { students.add(new Student("Student no... ", (students.size() + 1))); System.out.print("type> "); line = input.nextLine(); } System.out.println( students ); } } --------------------------------- import java.util.*; class Student { String name; int age; public String toString() { return " (" + this.name + " " + this.age + ") "; } Student(String name, int age) { this.name = name; this.age = age; } } class Example { public static void main(String[] args) { ArrayList students = new ArrayList(); Scanner input = new Scanner(System.in); String what, who, age, line; System.out.print("type> "); line = input.nextLine(); while (! line.equals("finished")) { students.add(new Student("Student no. ", (students.size() + 1))); System.out.print("type> "); line = input.nextLine(); } System.out.println( students ); Collections.sort(students); System.out.println( students ); } } Collections.sort(...) public static void sort(List list) Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list). -------------------------------------- import java.util.*; class Student implements Comparable { String name; int age; public String toString() { return " (" + this.name + " " + this.age + ") "; } Student(String name, int age) { this.name = name; this.age = age; } public int compareTo(Object other) { return this.age - ((Student)other).age; } } class Example { public static void main(String[] args) { ArrayList students = new ArrayList(); Scanner input = new Scanner(System.in); String what, who, age, line; System.out.print("type> "); line = input.nextLine(); while (! line.equals("finished")) { students.add(new Student("No. " + (students.size() + 1), (int) (Math.random() * 26 + 12))); System.out.print("type> "); line = input.nextLine(); } System.out.println( students ); Collections.sort(students); System.out.println( students ); } } ---------------------------------------------------- So on Monday we discuss ArrayList and parameterized types.