Solutions and rubrics for Lab 08 (File I/O, Exceptions)

This assignment was also informally known as Presidential Elections 2016. 

The following has been emailed to UIs on 11/24. 

Let's talk about: 

(a) Lab 08 Lab instructors (was due Mon Oct 22 only 4/108 submissions graded so far)

I have carefully explained how you should run this lab and emailed and posted that 
info (see Fri Oct 19 entry below and the one for Thu Oct 18)

https://www.cs.indiana.edu/classes/c212-dgerman/fall2018/whatsnew.html​

As soon as the lab was due you were supposed to follow up and grade your students' 
submissions according to what you were expecting from them based on how you taught 
that specific lab. You know the goal of our grading (we discussed this early on) 
is that we aim to post fast feedback so students can come ask during office hours 
about the grading and your feedback so you can further pick their brain during the 
discussion on how they solved the homework so you understand how they think so you 
can better adjust your teaching to more effectively help them. The main goal was 
to tell them quickly what you thought about what they submitted given what you 
taught them in lab according to my instructions which I posted at the time. And as 
I explained in the past if a specific assignment has a number of parts then assign 
the same weight to each part, carefully review what was submitted, generate quick 
and specific feedback then post grades accordingly. I do not therefore really need 
to say (following the entry quoted above) that the grade to this lab assignment 
should in fact be made of:

  20% open text file read words one by one
  20% set up a Map to store words as keys update the values for each entry as you read
  20% turn map into an array of comparable objects 
  20% for defining the comparable type used above
  20% sort the array in descending order by number of votes, 
      and lexicographically by name when same number of votes

I don't have to say this because these are the very dimensions we use to structure 
the lab which you taught. The following are sample files for a sample solution to this 
lab based on what Harrison Olinger submitted in Gracie's lab:

// I am not particularly fond of this solution but it is a genuine student solution and
// it legitimately is worth full credit so I am posting it here for your information:

public class Candidate implements Comparable<Candidate>{
  private String name;
  private int votes;
  
  public Candidate(String n, int v){
    name = n;
    votes = v;
  }
  
  public String getName(){
    return name;
  }
  public int getVotes(){
    return votes;
  }
  
  public int compareTo(Candidate other){
    if(this.votes == other.getVotes())
      return this.name.compareTo(other.getName());
    else
      return  other.getVotes() - this.votes;
  }
}

// here's the main class:

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

public class Profile{
  public static void main (String [] args){
    Scanner scan;
    File source;
    int size = -1;
    if(args.length > 1)
      size = Integer.parseInt(args[1]);
    try{
      source = new File(args[0]);
      scan = new Scanner(source);
    }
    catch(ArrayIndexOutOfBoundsException | IOException e){
      try{
        source = new File("votes.txt");
        scan = new Scanner(source);
      }catch(IOException ioe) {
        scan = new Scanner("something went wrong");
      }
    }
    Map<String, Integer> votes = new HashMap<String, Integer>();
    String next = "";
    while(scan.hasNext()){
      next = scan.next();
      if(votes.containsKey(next))
        votes.put(next, votes.get(next) + 1);
      else
        votes.put(next, 1);
    }
    ArrayList<Candidate> candidates = new ArrayList<Candidate>();
    for(String s: votes.keySet())
      candidates.add(new Candidate(s, votes.get(s)));
    Collections.sort(candidates);
    if(size == -1)
      size = candidates.size();
    for(int i = 0; i < size; i++)
      System.out.println((i +1) + ". " + candidates.get(i).getName() + "=" + candidates.get(i).getVotes());
  }
}

//--------------------------(start of votes.txt)--------------
Bernie Trump Bernie Bernie
  Trump Trump Hillary

                Jeb!

Hillary Trump Bernie Hillary
   Bernie
Putin  Putin Putin
 Hillary
   Bernie
  Hillary Hillary Hillary Trump


  Colbert

               Jeb!     Jeb!
    Trump

          Johnson
                        Stein
 Jeb!  Jeb!
//--------------------------(end of votes.txt)--------------

Sincerely,
Adrian German

--