Final Exam is in two stages: 

  (a) extract a representative example from each one of the chapters in ZyBooks
  Alternatively make it up. Explain briefly what the chapter wanted to teach and how the code demonstrates it. 

  (b) submit it in GitHub under a folder named "Final Exam" by Wednesday night @10pm. 

So you need to have 20 examples from the text:

 1. Introduction  
 2. Basic Objects
 3. Basic Methods + Classes
 4. Data Types 
 5. Branches
 6. Loops 
 7. Arrays 
 8. Methods Continued
 9. Classes Continued 
10. Inheritance 
11. Abstract Classes and Interfaces 
12. Generics
13. Collections 
14. GUI
15. JavaFX
16. Input/Output
17. Exceptions 
18. Recursion 
19. Memory Management
20. Searching and Sorting Algorithms 

As an example: 13 contains info about List, Map, Set, Queue, Dequeue. 

Choose one of these (for example Map) as the most representative example. 

--

Homework 09 assumes you have a file with names (no typos, one token per person). 

-----------(start of file: votes.txt)-----------------------------------
                                 

      Daohui 
Ben                     Nariman Chu Mary 
   Chase 


                 Grant 

Murun Dustin 
Zack Sai Qi Namit Sunghyun Chase
     Zack

   Michael               
             LaBrian 
                      Jiongran Michael Michael 
JeVante Grant
                 StephenK
Emma PeterW
           YoungHwan 


      LeBrian Michael StephenK

Nariman Sai 

      Mary      

                     Dustin Zack 

   Sai 

-----------(end of file)-----------------------------------

Our goal is to list descending by how many occurrences in the file these people. 

public class Election {
  public static void main(String[] args) {
    String fileName = args[0]; 
    System.out.println("File is: " + fileName); 
  }
}

-----------------------------------------------------------

import java.io.File; 
import java.util.Scanner; 

public class Election {
  public static void main(String[] args) throws Exception {
    String fileName = args[0]; 
    File file = new File(fileName); 
    Scanner read = new Scanner(file); 
    // https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#Scanner-java.io.File-
    while (read.hasNext()) {
      String word = read.next(); 
      System.out.println( word ); 
    }
  }
}

-----------------------------------------------------------

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Map<String, String> m = new HashMap<String, String>();
Static Error: Undefined class 'Map'
> import java.util.Map; // auto-import
Map<String, String> m = new HashMap<String, String>();
Static Error: Undefined class 'HashMap'
> import java.util.HashMap; // auto-import
import java.util.Map; // auto-import
Map<String, String> m = new HashMap<String, String>();
> m
{}
> m.set("gru", "n0thing")
Static Error: No method in Map<String, String> has name 'set'
> m.put("gru", "n0thing")
null
> m
{gru=n0thing}
> m.put("kevin", "s0mething")
null
> m
{kevin=s0mething, gru=n0thing}


-----------------------------------------------------------

import java.io.File; 
import java.util.Scanner; 
import java.util.Map; 
import java.util.HashMap; 

public class Election {
  public static void main(String[] args) throws Exception {
    String fileName = args[0]; 
    File file = new File(fileName); 
    Scanner read = new Scanner(file); 
    Map<String, Integer> m = new HashMap<String, Integer>(); 
    // https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#Scanner-java.io.File-
    while (read.hasNext()) {
      String word = read.next(); 
      System.out.println( word ); 
      m.put(word, 1); 
    }
    System.out.println( m ); 
  }
}

-----------------------------------------------------------

import java.io.File; 
import java.util.Scanner; 
import java.util.Map; 
import java.util.HashMap; 

public class Election {
  public static void main(String[] args) throws Exception {
    String fileName = args[0]; 
    File file = new File(fileName); 
    Scanner read = new Scanner(file); 
    Map<String, Integer> m = new HashMap<String, Integer>(); 
    // https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#Scanner-java.io.File-
    while (read.hasNext()) {
      String word = read.next(); 
      // System.out.println( word ); 
      if (m.get(word) == null) {
        m.put(word, 1);  
      } else { 
        m.put(word, 1 + m.get(word)); 
      }
    }
    System.out.println( m ); 
  }
}

-----------------------------------------------------------

So far:

  (a) we learned to open a file and read one token at a time
  (b) we learned to create a map and update it one input at a time

We want: 

  (c) to sort the entries 

We wrote a sort function to emulate Arrays.sort(...) 

We don't do this any more, instead use Exam 03 knowledge.

Ben says: use Comparable. 

-----------------------------------------------------------

import java.io.File; 
import java.util.Scanner; 
import java.util.Map; 
import java.util.HashMap; 
import java.util.ArrayList;
import java.util.Collections;

public class Election {
  public static void main(String[] args) throws Exception {
    String fileName = args[0]; 
    File file = new File(fileName); 
    Scanner read = new Scanner(file); 
    Map<String, Integer> m = new HashMap<String, Integer>(); 
    // https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#Scanner-java.io.File-
    while (read.hasNext()) {
      String word = read.next(); 
      // System.out.println( word ); 
      if (m.get(word) == null) {
        m.put(word, 1);  
      } else { 
        m.put(word, 1 + m.get(word)); 
      }
    }
    System.out.println( m ); 
    ArrayList<Candidate> a = new ArrayList<Candidate>(); 
    // https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#keySet--
    for (String key : m.keySet()) {
      System.out.println( key + ": " + m.get(key) );  
      a.add(new Candidate( key , m.get(key) )); 
    }
    System.out.println(a); 
    Collections.sort(a); 
    System.out.println(a); 
  }
}

-----------------------------------------------------------

public class Candidate implements Comparable<Candidate> {
  public int compareTo(Candidate other) {
    return 0; 
  }
  public Candidate(String name, int votes) {
    this.name = name;
    this.votes = votes;
  }
  private String name;
  private int votes; 
  public String toString() {
    return this.name + ":" + this.votes; 
  }
}

And check one more time the text of the assignment for the extra (optional) argument. 

--