We need to do three things today: 

(a) clarify the lab assignment

(b) answer the question that ended the lecture

(c) describe and use HashMaps in a meaningful example

Let's get started: 

(a) https://www.cs.indiana.edu/~dgerman/sum2012/hfjse.pdf

Look through chapter 12 then decide to google 

     o'reilly head first java source code

Maybe it helps.

http://www.headfirstlabs.com/books/hfjava/

Things the chapter develops: 

(a) TwoButtons.java

(b) SimpleAnimation.java

(c) MiniMusicPlayer3.java

(d) Animate.java

I download DrJava and while I wait I ask the following. 

Question: 

What is the fundamental unit of compilation in Java? 

Answer: not the class. 

Answer: the package is. 

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\akuhlens>cd Desktop

C:\Users\akuhlens\Desktop>javac
'javac' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\akuhlens\Desktop>PATH=%PATH%;"C:\Program Files\Java\jdk1.6.0_33\bin"

C:\Users\akuhlens\Desktop>dir TwoButtons.java
 Volume in drive C has no label.
 Volume Serial Number is FC3D-A6A6

 Directory of C:\Users\akuhlens\Desktop

11/15/2012  01:54 PM             1,892 TwoButtons.java
               1 File(s)          1,892 bytes
               0 Dir(s)  39,320,571,904 bytes free

C:\Users\akuhlens\Desktop>javac -d . TwoButtons.java

C:\Users\akuhlens\Desktop>dir
 Volume in drive C has no label.
 Volume Serial Number is FC3D-A6A6

 Directory of C:\Users\akuhlens\Desktop

11/15/2012  01:56 PM    <DIR>          .
11/15/2012  01:56 PM    <DIR>          ..
11/15/2012  01:56 PM    <DIR>          cabbage
11/15/2012  01:40 PM    <DIR>          chap12
11/15/2012  01:39 PM    <DIR>          chap13
11/15/2012  01:48 PM        12,239,948 drjava-stable-20120818-r5686.jar
11/15/2012  01:32 PM        50,356,835 hfjse.pdf
11/15/2012  01:54 PM             1,892 TwoButtons.java
               3 File(s)     62,598,675 bytes
               5 Dir(s)  39,320,494,080 bytes free

C:\Users\akuhlens\Desktop>dir cabbage
 Volume in drive C has no label.
 Volume Serial Number is FC3D-A6A6

 Directory of C:\Users\akuhlens\Desktop\cabbage

11/15/2012  01:56 PM    <DIR>          .
11/15/2012  01:56 PM    <DIR>          ..
11/15/2012  01:56 PM               647 MyDrawPanel.class
11/15/2012  01:56 PM               607 TwoButtons$ColorButtonListener.class
11/15/2012  01:56 PM               644 TwoButtons$LabelButtonListener.class
11/15/2012  01:56 PM             1,454 TwoButtons.class
               4 File(s)          3,352 bytes
               2 Dir(s)  39,320,494,080 bytes free

C:\Users\akuhlens\Desktop>java cabbage.TwoButtons

C:\Users\akuhlens\Desktop>

You have the code. 

Now Chapter 13. But that one seems to develop only one program. 

Nice. 

--

Question: if we model a Run, what's the difference between

class Run extends ArrayList<Integer> {

}

and 

class Run {
  int start, end, value; 

}

Which of these two models can help us more in Homework Nine. 

Answer: the second one knows about its context. 

The first one doesn't. 

When it comes to determining the largest run the second model is more useful. 

--

Now, here's a situation in Perl:

-bash-4.1$ ls -ld data
-rw-r--r-- 1 dgerman faculty 259 Nov 15 14:48 data
-bash-4.1$ ls -ld one
-rwxr--r-- 1 dgerman faculty 237 Nov 15 14:56 one
-bash-4.1$ cat data
trump
trump
barack hussein obama ii
barack hussein obama ii
calvin and hobbes
trump
mitt romney
trump
barack hussein obama ii
mitt romney
barack hussein obama ii
calvin and hobbes
mitt romney
calvin and hobbes
mitt romney
calvin and hobbes
calvin and hobbes

-bash-4.1$ cat one
#!/usr/bin/perl

open (AB, $ARGV[0]);
while ($x = <AB>) {
  print $x;
  $x =~ s/^\s*//g;
  $x =~ s/\s*$//g;
  $dictionary{$x} += 1;
}
close(AB);

foreach $key (keys %dictionary) {
  print $key, " occurs ", $dictionary{$key}, "\n";
}
-bash-4.1$ ./one
-bash-4.1$ ./one data
trump
trump
barack hussein obama ii
barack hussein obama ii
calvin and hobbes
trump
mitt romney
trump
barack hussein obama ii
mitt romney
barack hussein obama ii
calvin and hobbes
mitt romney
calvin and hobbes
mitt romney
calvin and hobbes
calvin and hobbes

 occurs 1
barack hussein obama ii occurs 4
trump occurs 4
mitt romney occurs 4
calvin and hobbes occurs 5
-bash-4.1$

Can we do the same in Java? 

Even simpler, considering a text like:

  A man a plan a canal: Panama. Man, man! Plan.  

Can you tell me how many times each word occurs? 


-bash-4.1$ cat One.java
import java.util.*;
import java.io.*;

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

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

    System.out.println( d );

    Scanner a = new Scanner(new File(args[0]));
    while (a.hasNext()) {
      String token = a.next();
      System.out.println( token );

      if (d.containsKey( token )) {
        d.put(token, d.get(token) + 1);
      } else {
        d.put(token, 1);
      }

      System.out.println( d );
    }
  }
}
-bash-4.1$ cat data.txt
romney obama letterman trump obama romney
ryan oprah letterman
romney obama obama clinton
romney
ryan trump calvin hobbes hobbes
-bash-4.1$ javac One.java
-bash-4.1$ java One data.txt
{}
romney
{romney=1}
obama
{romney=1, obama=1}
letterman
{romney=1, obama=1, letterman=1}
trump
{romney=1, obama=1, trump=1, letterman=1}
obama
{romney=1, obama=2, trump=1, letterman=1}
romney
{romney=2, obama=2, trump=1, letterman=1}
ryan
{romney=2, ryan=1, obama=2, trump=1, letterman=1}
oprah
{oprah=1, romney=2, ryan=1, obama=2, trump=1, letterman=1}
letterman
{oprah=1, romney=2, ryan=1, obama=2, trump=1, letterman=2}
romney
{oprah=1, romney=3, ryan=1, obama=2, trump=1, letterman=2}
obama
{oprah=1, romney=3, ryan=1, obama=3, trump=1, letterman=2}
obama
{oprah=1, romney=3, ryan=1, obama=4, trump=1, letterman=2}
clinton
{oprah=1, romney=3, clinton=1, ryan=1, obama=4, trump=1, letterman=2}
romney
{oprah=1, romney=4, clinton=1, ryan=1, obama=4, trump=1, letterman=2}
ryan
{oprah=1, romney=4, clinton=1, ryan=2, obama=4, trump=1, letterman=2}
trump
{oprah=1, romney=4, clinton=1, ryan=2, obama=4, trump=2, letterman=2}
calvin
{oprah=1, romney=4, clinton=1, calvin=1, ryan=2, obama=4, trump=2, letterman=2}
hobbes
{oprah=1, hobbes=1, romney=4, clinton=1, calvin=1, ryan=2, obama=4, trump=2, letterman=2}
hobbes
{oprah=1, hobbes=2, romney=4, clinton=1, calvin=1, ryan=2, obama=4, trump=2, letterman=2}
-bash-4.1$

--