Howdy. 

Tonight I will post a study guide:

  (a) a list of problems
  (b) with solutions
  (c) like in Homework Seven

Exam on Monday (Help Session Sunday, Peer Tutoring Thu night) 

Seating is as for the previous exam.

Individual e-mail messages still forthcoming. 

April 21 is when we want to close the autograder. 

Everything will be collected in OnCourse from now on. 

Exam is on Monday on paper, by Wed you get them back.

In lab you make corrections if needed, collected in OnCourse. 

Homework Eight and Nine dropboxes are open. 

No good return rate: I will keep them open. 

Assume a sequence of objects that need to be sorted. 

Lab Problem One:

Assume a bunch of Players. Blackjack Players. A Player has a name
and a score. Score is somewhere between 5 and 28. It's an integer. 

Sort them. Here's what I want: better scores first. 

A score <= 21 is better than a score > 21.

For scores <= 21 a higher score is better. 

For scores > 21 a lower score is better. End of rules. 

-- 

Let's start from an array of Students. 

There are two ways to sort objects in Java.

(a) one is using the interface Comparable 

(b) the other one is using the interface Comparator

import java.util.*; 

class Student implements Comparable<Student> {
  String name; 
  double age; 
  Student(String name, double age) {
    this.name = name; 
    this.age = age; 
  } 
  public String toString() {
    return this.name + "/" + age; 
  } 
  public int compareTo(Student 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) {
    Student[] students = new Student[6]; 
    students[0] = new Student("Leslie", 18); 
    students[1] = new Student("Alex"  , 17); 
    students[2] = new Student("Chris" , 19); 
    students[3] = new Student("Laura" , 21); 
    students[4] = new Student("Gennis", 20); 
    students[5] = new Student("Frank" , 18); 
    System.out.println( Arrays.toString( students ));    
    Arrays.sort( students ); 
    System.out.println( Arrays.toString( students ));    

  }
}

It's going to be the same if the students are in array list.

Just that in that case the sort method is in Collections. 

Lab Problem Two: 

Assume a file with lines like this: 

Jason -4
Jake 10
Jason 8
Paul 6
Adrian -3
Jason 1
Adrian 7
Paul 8


Lines indicate votes. 

A vote is a numeric measure between -10 and 10. 

Read this file and sort the instructors:

(a) by votes (number of occurrences in the file) largest first

1. Jason 3
2. Adrian 2
3. Paul 2
4. Jake 1

(b) by sum of points in vote

1. Paul 14
2. Jake 10 
3. Jason 5
4. Adrian 4

(c) by average vote

1. Jake 10
2. Paul 7
3. Adrian 2
4. Jason 1.66

In this case I can't use Comparable. 

I have been told Comparator needs to be used. 

Remember that arrays are sorted with Arrays.sort and we did that once.

Array lists are sorted with Collections.sort and we should see it now. 

It's going to be interesting collecting the information from the file.

However what I should end up with is this:


import java.util.*; 

class Instructor {
  String name; 
  int points; 
  int count; 
  Instructor(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 + ")";
  }
}

class Example {
  public static void main(String[] args) {
    ArrayList<Instructor> instructors;
    instructors = new ArrayList<Instructor>(); 
    instructors.add(new Instructor("Jason" ,  5, 3)); 
    instructors.add(new Instructor("Jake"  , 10, 1)); 
    instructors.add(new Instructor("Paul"  , 14, 2)); 
    instructors.add(new Instructor("Adrian",  4, 2)); 
    System.out.println( instructors );
    Collections.sort( instructors , new One()); 
    System.out.println( instructors );

    Collections.sort( instructors , new Two()); 
    System.out.println( instructors );

    //Collections.sort( instructors , ...); 
    //System.out.println( instructors );
  }
}


class One implements Comparator<Instructor> {
  public int compare(Instructor a, Instructor b) {
    if (a.count > b.count) return -1; 
    else if (a.count < b.count) return 1; 
    else return 0; 
  }
}

class Two implements Comparator<Instructor> {
  public int compare(Instructor a, Instructor b) {
    if (a.points > b.points) return -1; 
    else if (a.points < b.points) return 1; 
    else return 0; 
  }
}

Use a hash map (hash table) to aggregate the data in the file. 

--