Hi there.

Exam on Monday. 

Study guide to be posted tonight: problems with solutions. 

Topic: programs like in Homework Seven. 

Homework Eight and Nine will remain open until significant response rate. 

April 21 is when we want to close autograder. 

Everything is now being collected in OnCourse. 

Assigned seating for exam.

Lab Assignment for this week:

(a) Assume a collection of Player objects. Each player has
a number of points (integer, between 5 and 28). Please sort
these players with the better scores in front. Here are the
rules for what's better: 

  1. scores <= 21 are better than scores > 21
  2. within scores <= 21 higher scores are better
  3. within scores > 21 lower scores are better

Use Arrays.sort or Collections.sort and whatever else is needed.

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

http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html

import java.util.*;

class Person implements Comparable<Person> {
  String name; 
  int age; 
  Person(String name, int age) {
    this.name = name;
    this.age = age; 
  }  
  public String toString() {
    return this.name + "/" + this.age; 
  } 
  public int compareTo(Person other) {
    if (this.age < other.age) return -1;
    else if (this.age > other.age) return 1; 
    else return this.name.compareTo(other.name);     
  }
}

class Example {
  public static void main(String[] args) {
    ArrayList<Person> people; 
    people = new ArrayList<Person>(); 
    people.add(new Person("Laura" , 18)); 
    people.add(new Person("Alex"  , 19)); 
    people.add(new Person("Chris" , 21)); 
    people.add(new Person("Leslie", 20)); 
    people.add(new Person("Jordan", 18)); 
    people.add(new Person("Sam"   , 20)); 
    people.add(new Person("Adrian", 19)); 
    System.out.println( people ); 
    Collections.sort( people );
    System.out.println( people ); 
    
  } 
}

(b) Assume a file with lines like this

Jordan 7
Sam 4
Leslie 9
Sam -6
Sam -3
Jordan 4

Write a program that reads this file and sorts these people:

  1. Sort by number of votes (occurrences in file): 
   
     Sam    3
     Jordan 2
     Leslie 1

  2. Sort by sum:
     
     Jordan 11
     Leslie  9
     Sam    -5

  3. Sort by the average vote:

     Leslie 9
     Jordan 5.5
     Sam -1.66

I can't use Comparable because I have three different orders. 

So what do I use? I will use Comparators.  

Here's the answer:

import java.util.*; 

class Candidate {
  String name; 
  int points;
  int count; 
  Candidate(String name, int points, int count) {
    this.name = name; 
    this.points = points; 
    this.count = count; 
  }
  public String toString() {
    return this.name + "(" + this.points + "/"  + this.count + ")"; 
  }
  double getAverage() {
    return (double) this.points / this.count; 
  }
}

class Example {
  public static void main(String[] args) {
    Candidate[] people = new Candidate[3];
    people[0] = new Candidate( "Jordan", 11, 2 ); 
    people[1] = new Candidate( "Leslie",  9, 1 ); 
    people[2] = new Candidate( "Sam"   , -5, 3 ); 
    System.out.println( Arrays.toString( people ) );

    Arrays.sort( people, new One() ); 
    System.out.println( Arrays.toString( people ) );

    Arrays.sort( people, new Two() ); 
    System.out.println( Arrays.toString( people ) );

    Arrays.sort( people, new Three() ); 
    System.out.println( Arrays.toString( people ) );


  }
}


class One implements Comparator<Candidate> {
  public int compare(Candidate one, Candidate two) {
    if (one.count > two.count) return -1; 
    else if (one.count < two.count) return 1;
    else return 0;
  }
}

class Two implements Comparator<Candidate> {
  public int compare(Candidate one, Candidate two) {
    if (one.points > two.points) return -1; 
    else if (one.points < two.points) return 1;
    else return 0;
  }
}

class Three implements Comparator<Candidate> {
  public int compare(Candidate one, Candidate two) {
    if (one.getAverage() > two.getAverage()) return -1; 
    else if (one.getAverage() < two.getAverage()) return 1;
    else return 0;
  }
}
 

You have to create the Candidate objects from the file. 

You might find hashtables or hashmaps useful. 

--