You need prompt feedback on your assignments. 

Makeups with dgerman in Lh204 make appointments.

Lab Assignment 08 due Friday in lab. 

login as: mrstalba
mrstalba@silo.cs.indiana.edu's password:
Last login: Thu Sep  1 16:44:32 2016 from 149-160-145-163.dhcp-bl.indiana.edu


*******************************************************************
**   Indiana University School of Informatics and Computing      **
**             ** For Authorized Use Only **                     **
*******************************************************************
**  For general SoIC computing information, please see:          **
**      http://help.soic.indiana.edu/                            **
**                                                               **
**  To submit a problem report or question, please see:          **
**      http://help.soic.indiana.edu/request                     **
*******************************************************************


[mrstalba@silo ~]$ ls
bin  README
[mrstalba@silo ~]$ mkdir c212-workspace
[mrstalba@silo ~]$ cd c212-workspace/
[mrstalba@silo c212-workspace]$ pwd
/u/mrstalba/c212-workspace
[mrstalba@silo c212-workspace]$ mkdir lab08
[mrstalba@silo c212-workspace]$ cd lab08
[mrstalba@silo lab08]$


  [Kirk Mitchell S Mitch]

  [Alex Nicholas Jordan Josh Vaishali Menghan] 

  [Chatan Drake Yuanyuan Younghun Tianqi] 

  [Blake Sarah Magdalena Elizabeth Tobias Yixuan Rui] 

  [Chris Ben Max Chris Levi] 

  [Olivia Nova Graham Kendall Yicheng] 

  [Kyle Kyle Justin Glenn Derek Yiqing Jiahao] 


[mrstalba@silo lab08]$ pwd
/u/mrstalba/c212-workspace/lab08
[mrstalba@silo lab08]$ nano -w data.txt
[mrstalba@silo lab08]$ ls -l
total 4
-rw------- 1 mrstalba students 20 Oct 19 09:57 data.txt
[mrstalba@silo lab08]$ cat data.txt
This
  is a
test

.
[mrstalba@silo lab08]$ wc data.txt
 5  5 20 data.txt
[mrstalba@silo lab08]$ nano -w data.txt
[mrstalba@silo lab08]$ cat data.txt
T his
  is a
test

..
[mrstalba@silo lab08]$ wc data.txt
 5  6 22 data.txt
[mrstalba@silo lab08]$

So we first examine how we could write something like this. 

[mrstalba@silo lab08]$ ls -l
total 4
-rw------- 1 mrstalba students 22 Oct 19 09:59 data.txt
[mrstalba@silo lab08]$ man wc
[mrstalba@silo lab08]$ nano -w WordCount.java
[mrstalba@silo lab08]$ cat WordCount.java
import java.io.*;
import java.util.*;

class WordCount {
  public static void main(String[] args) throws Exception {

    Scanner a = new Scanner(new File(args[0]));

    System.out.println(a); // print the Scanner, it's possible, so: why not?

    int lines, // count the number of lines in the file
        words = 0, // count the number of words (tokens) in the file
        nonBlankChars = 0, // counts the number of non-blank characters
        allChars = 0; // counts all characters in file including space

    for (lines = 0; a.hasNextLine(); ) { // line by line

      String line = a.nextLine(); // get the line
      allChars += line.length(); // count the characters on the line

      lines = lines + 1; // count the line

      allChars += 1; // count the new lines

      System.out.println( lines + ". ***(" + line + ")***"); // some feedback

      Scanner b = new Scanner(line); // let's get the tokens out of this line, one by one
      while (b.hasNext()) { // while I see a token
        String word = b.next(); // get it
        words += 1; // cont it
        nonBlankChars += word.length(); // count its characters
      }
    }

    System.out.println("I see " + lines         + " lines in this file.");
    System.out.println("I see " + words         + " words (token) in this file.");
    System.out.println("I see " + nonBlankChars + " non-blank characters in this file.");
    System.out.println("I see " + allChars      + " all characters in this file.");
  }
}
[mrstalba@silo lab08]$ javac WordCount.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[mrstalba@silo lab08]$ java WordCount data.txt
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
1. ***(T his)***
2. ***(  is a)***
3. ***(test)***
4. ***()***
5. ***(..)***
I see 5 lines in this file.
I see 6 words (token) in this file.
I see 13 non-blank characters in this file.
I see 22 all characters in this file.
[mrstalba@silo lab08]$ wc data.txt
 5  6 22 data.txt
[mrstalba@silo lab08]$

So here you learned to open a file, read lines, tokens (words) and count them. 

[mrstalba@silo lab08]$ nano -w One.java
[mrstalba@silo lab08]$ javac One.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[mrstalba@silo lab08]$ java One dogs
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{d=1}
{d=1, o=1}
{d=1, g=1, o=1}
{s=1, d=1, g=1, o=1}
[mrstalba@silo lab08]$ java One puppies
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{p=1}
{p=1, u=1}
{p=2, u=1}
{p=3, u=1}
{p=3, u=1, i=1}
{p=3, u=1, e=1, i=1}
{p=3, s=1, u=1, e=1, i=1}
[mrstalba@silo lab08]$ java One abbaacbcca
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{a=1}
{a=1, b=1}
{a=1, b=2}
{a=2, b=2}
{a=3, b=2}
{a=3, b=2, c=1}
{a=3, b=3, c=1}
{a=3, b=3, c=2}
{a=3, b=3, c=3}
{a=4, b=3, c=3}
[mrstalba@silo lab08]$ java One abbaacbccabb
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{a=1}
{a=1, b=1}
{a=1, b=2}
{a=2, b=2}
{a=3, b=2}
{a=3, b=2, c=1}
{a=3, b=3, c=1}
{a=3, b=3, c=2}
{a=3, b=3, c=3}
{a=4, b=3, c=3}
{a=4, b=4, c=3}
{a=4, b=5, c=3}
[mrstalba@silo lab08]$ java One abbaacbccabbccccc
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{a=1}
{a=1, b=1}
{a=1, b=2}
{a=2, b=2}
{a=3, b=2}
{a=3, b=2, c=1}
{a=3, b=3, c=1}
{a=3, b=3, c=2}
{a=3, b=3, c=3}
{a=4, b=3, c=3}
{a=4, b=4, c=3}
{a=4, b=5, c=3}
{a=4, b=5, c=4}
{a=4, b=5, c=5}
{a=4, b=5, c=6}
{a=4, b=5, c=7}
{a=4, b=5, c=8}
[mrstalba@silo lab08]$ java One abbaacbccabbccccczzzzzz
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{a=1}
{a=1, b=1}
{a=1, b=2}
{a=2, b=2}
{a=3, b=2}
{a=3, b=2, c=1}
{a=3, b=3, c=1}
{a=3, b=3, c=2}
{a=3, b=3, c=3}
{a=4, b=3, c=3}
{a=4, b=4, c=3}
{a=4, b=5, c=3}
{a=4, b=5, c=4}
{a=4, b=5, c=5}
{a=4, b=5, c=6}
{a=4, b=5, c=7}
{a=4, b=5, c=8}
{a=4, b=5, c=8, z=1}
{a=4, b=5, c=8, z=2}
{a=4, b=5, c=8, z=3}
{a=4, b=5, c=8, z=4}
{a=4, b=5, c=8, z=5}
{a=4, b=5, c=8, z=6}
[mrstalba@silo lab08]$ java One abbaacbccabbccccczzzzzzmmmmmmmm
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{a=1}
{a=1, b=1}
{a=1, b=2}
{a=2, b=2}
{a=3, b=2}
{a=3, b=2, c=1}
{a=3, b=3, c=1}
{a=3, b=3, c=2}
{a=3, b=3, c=3}
{a=4, b=3, c=3}
{a=4, b=4, c=3}
{a=4, b=5, c=3}
{a=4, b=5, c=4}
{a=4, b=5, c=5}
{a=4, b=5, c=6}
{a=4, b=5, c=7}
{a=4, b=5, c=8}
{a=4, b=5, c=8, z=1}
{a=4, b=5, c=8, z=2}
{a=4, b=5, c=8, z=3}
{a=4, b=5, c=8, z=4}
{a=4, b=5, c=8, z=5}
{a=4, b=5, c=8, z=6}
{a=4, b=5, c=8, z=6, m=1}
{a=4, b=5, c=8, z=6, m=2}
{a=4, b=5, c=8, z=6, m=3}
{a=4, b=5, c=8, z=6, m=4}
{a=4, b=5, c=8, z=6, m=5}
{a=4, b=5, c=8, z=6, m=6}
{a=4, b=5, c=8, z=6, m=7}
{a=4, b=5, c=8, z=6, m=8}
[mrstalba@silo lab08]$ java One uuuuuuuuuabbaacbccabbccccczzzzzzmmmmmmmm
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
{}
{u=1}
{u=2}
{u=3}
{u=4}
{u=5}
{u=6}
{u=7}
{u=8}
{u=9}
{a=1, u=9}
{a=1, b=1, u=9}
{a=1, b=2, u=9}
{a=2, b=2, u=9}
{a=3, b=2, u=9}
{a=3, b=2, c=1, u=9}
{a=3, b=3, c=1, u=9}
{a=3, b=3, c=2, u=9}
{a=3, b=3, c=3, u=9}
{a=4, b=3, c=3, u=9}
{a=4, b=4, c=3, u=9}
{a=4, b=5, c=3, u=9}
{a=4, b=5, c=4, u=9}
{a=4, b=5, c=5, u=9}
{a=4, b=5, c=6, u=9}
{a=4, b=5, c=7, u=9}
{a=4, b=5, c=8, u=9}
{a=4, b=5, c=8, u=9, z=1}
{a=4, b=5, c=8, u=9, z=2}
{a=4, b=5, c=8, u=9, z=3}
{a=4, b=5, c=8, u=9, z=4}
{a=4, b=5, c=8, u=9, z=5}
{a=4, b=5, c=8, u=9, z=6}
{a=4, b=5, c=8, u=9, z=6, m=1}
{a=4, b=5, c=8, u=9, z=6, m=2}
{a=4, b=5, c=8, u=9, z=6, m=3}
{a=4, b=5, c=8, u=9, z=6, m=4}
{a=4, b=5, c=8, u=9, z=6, m=5}
{a=4, b=5, c=8, u=9, z=6, m=6}
{a=4, b=5, c=8, u=9, z=6, m=7}
{a=4, b=5, c=8, u=9, z=6, m=8}
[mrstalba@silo lab08]$

http://silo.cs.indiana.edu:8346/c212/fall2016/week09/map.phps

[mrstalba@silo lab08]$ nano -w One.java
[mrstalba@silo lab08]$ javac One.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[mrstalba@silo lab08]$ cat One.java
import java.io.*;
import java.util.*; // why do we need this?

class One {
  public static void main(String[] args) {

    Map<String, Integer> d = new HashMap<String, Integer>();

    System.out.println( d );

    for (int i = 0; i < args[0].length(); i++) {

      String letter = args[0].substring(i, i+1);

      if (d.containsKey( letter )) {

        d.put(letter, d.get(letter) + 1);

      } else {

        d.put(letter, 1);

      }
      System.out.println( d );
    }
  }
}
[mrstalba@silo lab08]$

Sorting an array(list) of objects. 

[mrstalba@silo lab08]$ cat Example.java
import java.util.*;

public class Example {
  public static void main(String[] args) {
    ArrayList<Student> a = new ArrayList<Student>();
    a.add(new Student("James" ,  8));
    a.add(new Student("Jim"   ,  9));
    a.add(new Student("Nick"  ,  8));
    a.add(new Student("Nancy" ,  9));
    a.add(new Student("Jay"   ,  6));
    a.add(new Student("Alex"  ,  8));
    a.add(new Student("Lynn"  , 12));
    a.add(new Student("Leslie", 12));
    a.add(new Student("Laura" , 12));
    System.out.println( a );
    Collections.sort( a );
    System.out.println( a );
  }
}
[mrstalba@silo lab08]$ cat Student.java
public class Student implements Comparable<Student> {
  String name;
  int age;
  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }
  public String toString() {
    return this.name + " " + this.age;
  }
  public int compareTo(Student other) {
    if (other.age > this.age) return 1;
    else if (other.age < this.age) return -1;
    else return this.name.compareTo(other.name);
  }
}
[mrstalba@silo lab08]$ javac Example.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[mrstalba@silo lab08]$ java Example
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[James 8, Jim 9, Nick 8, Nancy 9, Jay 6, Alex 8, Lynn 12, Leslie 12, Laura 12]
[Laura 12, Leslie 12, Lynn 12, Jim 9, Nancy 9, Alex 8, James 8, Nick 8, Jay 6]
[mrstalba@silo lab08]$

So the last example shows how to convert a hash table into an array list of entries for sorting. 

[mrstalba@silo lab08]$ nano -w Profile.java
[mrstalba@silo lab08]$ javac Profile.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[mrstalba@silo lab08]$ java Profile aaabbbccccc
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[b=3, a=3, c=5]
[c=5, a=3, b=3]
[mrstalba@silo lab08]$ java Profile uuuuuaaabbbccccc
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[b=3, a=3, u=5, c=5]
[c=5, u=5, a=3, b=3]
[mrstalba@silo lab08]$ java Profile uuuuuuuaaabbbccccc
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
[b=3, a=3, u=7, c=5]
[u=7, c=5, a=3, b=3]
[mrstalba@silo lab08]$


[mrstalba@silo lab08]$ cat Profile.java
import java.util.*;

public class Profile {
  public static void main(String[] args) {

    String word = args[0];

    Hashtable<Character, Integer> h = new Hashtable<Character, Integer>();

    for (int i = 0; i < word.length(); i++)
      if (h.containsKey(word.charAt(i)))
        h.put(word.charAt(i), h.get(word.charAt(i)) + 1);
      else h.put(word.charAt(i), 1);

    ArrayList<Pair> p = new ArrayList<Pair>();

    for (Enumeration<Character> keys = h.keys(); keys.hasMoreElements(); ) {
      Character key = keys.nextElement();
      p.add(new Pair(key, h.get(key)));
    }
    System.out.println( p );
    Collections.sort( p );
    System.out.println( p );
  }
}

class Pair implements Comparable<Pair> {
  char c;
  int count;
  Pair(char c, int count) {
    this.c = c;
    this.count = count;
  }
  public int compareTo(Pair another) {
    if (this.count > another.count) return -1;
    else if (this.count < another.count) return 1;
    else return (c + "").compareTo(another.c + "");
  }
  public String toString() {
    return c + "=" + count;
  }
}
[mrstalba@silo lab08]$

Next Homework 05.

Use BigBang and World to compile and run Ripples and Circle. 

Study them, then write variations of them for Homework 05. 

To be continued on Monday. 

--