The lab assignment is Chapter 5 in Greenfoot book. 

Have it done by Monday noon. 

--

The rest of the notes will follow our lecture discussion today. 

Take an array of integers and sort it:

(a) ascending
(b) descending
(c) first the numbers less or equal to 21 followed by the others 
    then descending in the first group ascending in the other

interface Examiner {
  boolean betterThan(int m, int n);
}

class Ascending implements Examiner {
  public boolean betterThan(int m, int n) {
    return n < m;  
  }
}

class Descending implements Examiner {
  public boolean betterThan(int m, int n) {
    return n > m;  
  }
}

import java.util.*; 

class One {
  public static void main(String[] ags) {
    int[] a = {21, 23, 18, 22, 19, 17, 26, 25}; 
    System.out.println( Arrays.toString ( a ) ); 
    One.sort( a , new Ascending() ); // ascending order  
    System.out.println( Arrays.toString ( a ) ); 
    One.sort( a , new Descending() ); // descending order  
    System.out.println( Arrays.toString ( a ) ); 
    
  }
  public static void sort(int[] a, Examiner e) {
    for (int i = 0; i < a.length-1; i++) {
      for (int j = i; j < a.length; j++) {
        if (e.betterThan(a[i], a[j])) {
           int temp = a[j]; 
           a[j] = a[i]; 
           a[i] = temp; 
        }
      }
    }
  }
}

Please use the next five minutes to implement third ordering examiner. 

Romaan, Erin, Eric, Sara, Pascal, Earl, Ty, Mea, Celia, Scott, Dana, Chen, Jialin, 
Stephen, Peter, Geri, Rhiannon, Matt, Qiuwei, Ke, Tara, Elan, Jack, John B, Kathryn
and ... Hayley


class One {
  public static void main(String[] ags) {
    int[] a = {21, 23, 18, 22, 19, 17, 26, 25}; 

    System.out.println( Arrays.toString ( a ) ); 
    One.sort( a , new Blackjack() ); // blackjack order  
    System.out.println( Arrays.toString ( a ) ); 
    
  }
}

class Blackjack implements Examiner {
  public boolean betterThan(int m, int n) {
    if (n > 21 && m > 21) {
      return n < m;  
    } else if (n <= 21 && m <= 21) {
      return n > m; 
    } else if (n > 21) {
      return false; 
    } else { 
      return true;  
    }
  }
}

Next let's look at the complete solution: 

interface Comparable<T> {
  public int compareTo(T other); 
}

import java.util.ArrayList; 

class Collections {
  public static <T extends Comparable<T>> void sort(ArrayList<T> students) {
    boolean sorted = false; 
    while(! sorted) {
      sorted = true;
      for (int i = 0; i < students.size() - 1; i = i + 1) {
        T a = students.get(i); 
        T b = students.get(i+1); 
        if (a.compareTo(b) > 0) {
           students.set(i, b); 
           students.set(i+1, a);
           sorted = false;
           System.out.println("Swapping " + a + " with " + b);
        }
      }
    }
  }
}

We have hijacked Collections and Comparable, that is, we are providing them.

The rest is still the same: 

import java.util.*; 

class Program {
  public static void main(String[] args) {
    ArrayList<Student> a = new ArrayList<Student>(); 
    a.add(new Student("Larry", 12)); 
    a.add(new Student("Laura",  7)); 
    a.add(new Student("Alex",  9)); 
    System.out.println( a ); 
    Collections.sort( a );
    System.out.println( a ); 
  }
}

class Student implements Comparable<Student> {
  String name;
  private Integer age; 
  Student(String name, Integer age) {
    this.name = name;
    this.age = age; 
  }
  public int compareTo(Student other) {
    return this.age - other.age;  
  }
  public String toString() {
    return name + "/" + age; 
  }
}

--

If you want to sort objects in Java:

(a) make them implement Comparable or

(b) use a Comparator as the second argument to sort. 

In your readings the links are:

Sorting (in general): 

  http://silo.cs.indiana.edu:8346/cgi-bin/c212/sum2013/readings?action=next&pic=ch1213141516_files/image039.jpg

Selection sort (used above): 

  http://silo.cs.indiana.edu:8346/cgi-bin/c212/sum2013/readings?action=next&pic=ch1213141516_files/image040.jpg

Comparable and Comparators:

  http://silo.cs.indiana.edu:8346/cgi-bin/c212/sum2013/readings?action=next&pic=ch1213141516_files/image055.jpg

Generic programming: 

  http://silo.cs.indiana.edu:8346/cgi-bin/c212/sum2013/readings?action=next&pic=ch17181920_files/image029.jpg

In the online Java tutorial the links are:

  http://docs.oracle.com/javase/tutorial/collections/algorithms/index.html#sorting

  http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

Especially the second link is what you need. 

--