Hello.

Minute paper:

  (a) your name

  (b) your team's name

  (c) your teammates' names

Goal: sort objects. 

import java.util.ArrayList; 

public class Example {
  public static void main(String[] args) {
    Player a = new Player("Leslie", 20);
    ArrayList<Player> players = new ArrayList<Player>();
    players.add( a );
    players.add( new Player("Laura", 21 )); 
    players.add( new Player("Lee"  , 28 ));     
    players.add( new Player("Larry", 19 )); 
    players.add( new Player("Alex" , 19 )); 
    players.add( new Player("Louie", 22 )); 
    System.out.println( players );
  }
}

public class Player {
  private String name; 
  private int points; 
  public Player(String name, int points) {
    this.name = name;
    this.points = points;
  }
  public String toString() {
    return this.name + ":" + this.points; 
  }
}

--

import java.util.ArrayList; 
import java.util.Collections; 

public class Example {
  public static void main(String[] args) {
    Player a = new Player("Leslie", 20);
    ArrayList<Player> players = new ArrayList<Player>();
    players.add( a );
    players.add( new Player("Laura", 21 )); 
    players.add( new Player("Lee"  , 28 ));     
    players.add( new Player("Larry", 19 )); 
    players.add( new Player("Alex" , 19 )); 
    players.add( new Player("Louie", 22 )); 
    System.out.println( players );
    Collections.sort(players); 
    System.out.println( players );     
  }
}

public class Player implements Comparable<Player> {
  private String name; 
  private int points; 
  public Player(String name, int points) {
    this.name = name;
    this.points = points;
  }
  public String toString() {
    return this.name + ":" + this.points; 
  }
  // descending by points
  public int compareTo(Player other) {
    if (this.points > other.points) return -1; // I go first because more points
    else if (this.points < other.points) return 1; 
    else return this.name.compareTo(other.name); // lexicographic by name 
  }
}

Team exercise: 

  (a) modify compareTo in Player such that
  (b) scores <= 21 come first in descending order 
  (c) followed by scores > 21 in ascending order 

Only compareTo changes. 

import java.util.ArrayList; 
import java.util.Collections; 

public class Example {
  public static void main(String[] args) {
    Player a = new Player("Leslie", 20);
    ArrayList<Player> players = new ArrayList<Player>();
    players.add( a );
    players.add( new Player("Laura", 21 )); 
    players.add( new Player("Lee"  , 28 ));     
    players.add( new Player("Larry", 19 )); 
    players.add( new Player("Alex" , 19 )); 
    players.add( new Player("Louie", 22 )); 
    System.out.println( players );
    Collections.sort(players); 
    System.out.println( players );     
    Collections.sort(players, new What()); 
    System.out.println( players );     
    
  }
}


public class Player implements Comparable<Player> {
  private String name; 
  private int points; 
  public int getPoints() {
    return this.points;  
  } 
  public String getName() {
    return this.name;  
  }
  public Player(String name, int points) {
    this.name = name;
    this.points = points;
  }
  public String toString() {
    return this.name + ":" + this.points; 
  }
  // descending by points
  public int compareTo(Player other) {
    if (this.points > other.points) return -1; // I go first because more points
    else if (this.points < other.points) return 1; 
    else return this.name.compareTo(other.name); // lexicographic by name 
  }
}


import java.util.Comparator;

public class What implements Comparator<Player> {
  public int compare(Player one, Player two) {
    if (one.getPoints() <= 21 && two.getPoints() <= 21) {
      if (one.getPoints() > two.getPoints()) return -1;
      else if (one.getPoints() < two.getPoints()) return 1; 
      else return one.getName().compareTo(two.getName());
    } else if (one.getPoints() > 21 && two.getPoints() > 21) {
      if (one.getPoints() > two.getPoints()) return 1; // swap 
      else if (one.getPoints() < two.getPoints()) return -1; 
      else return one.getName().compareTo(two.getName());
    } else if (one.getPoints() <= 21 && two.getPoints() > 21) {
      return -1;
    } else { // if (one.points > 21 && two.points <= 21) {
      return 1; 
    }
  }
}

So that gives us two orders:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Example
[Leslie:20, Laura:21, Lee:28, Larry:19, Alex:19, Louie:22]
[Lee:28, Louie:22, Laura:21, Leslie:20, Alex:19, Larry:19]
[Laura:21, Leslie:20, Alex:19, Larry:19, Louie:22, Lee:28]


And you know how to do the assignment. 

--