Welcome to Spring!

Midterm Exams: 

(a) average design 78

(b) average implementation 74

Huge variation in backgrounds in C212. 

Raw scores will be posted: 100 - c where c is the cheapest cost of fix. 

Raw scores will be adjusted depending on the quality of your lab report: 

-- you can get back betwen 1/3 and 1/2 of points missed

  Written    Third      Half
    70         80        85
    40         60        70 
     0         33        50 

I want make up. Fine, make appointment:

http://silo.cs.indiana.edu:8346/cgi-bin/spr2015/schedule

I'll add more hours. 

Make up needs to be very predictable. 

Exams:

(a) read code

(b) write code 

(d) programs with classes 

(f) variation on the project 

Basic cookbook of typical programs: 

  Homework eight will be a collection of such programs

  http://www.cs.indiana.edu/classes/c212/fall2011/whatsnew.html
  
  List of problems like what's posted under Oct 17-19.

For all problems interactive templates will be provided. 

Uncover the solution or write your own. 

Make ups will be one random (or negotiable) problem from Homework Eight.

Only one problem will have no solution posted. 

What's the problem with the templates? 

Using templates is like watching a game in slow motion. 

Watching an entire game in slow motion is nuts so don't do it. 

Midterm exam: 

(a) a lot of people emulated Homework Three

(b) some people did not store the numbers

(c) those who stored the numbers used sort to find max and min

  -- array size is zero means no data

  -- otherwise max and min are the first and last

(d) there's a fascination with nextFloat

(e) we need to talk about try/catch 

(f) I have seen purley object oriented solutions

    In this case there was separation in specialized methods. 

    All these methods were instance methods. 

(g) lots of methods, static, called from main 

(h) recursive approaches like C211 (main called recursively) 

(i) I did have an answer that used a type derived from ArrayList<...>

(j) ... 

If you can't solve a problem yourself you can't hope to write a program
that would solve the problem since the program would have the steps that
you don't know. So first try to solve the problem by hand. 

Assume user gives you a sequence of numbers:

  7 7 2 5 5 5 3 3 4 1 7 7 7 5 5 6 3 3 3

We define a run to be a sequence of numbers with the same value. 

Identify the runs. 

  (7 7) (2) (5 5 5) (3 3) (4) (1) (7 7 7) (5 5) (6) (3 3 3)

Identify the longest run: 

  7 7 2 (5 5 5) 3 3 4 1 7 7 7 5 5 6 3 3 3

How about sorting the runs. 

Suppose my goal is to identify the runs. 

What is the first thing you need to do? 

How do you get started? 

Possibilities: 

  (a) what is a Run? 

--

  (a) let's use an array list of array lists

      (Kinda like the list of strings: extension of ArrayList<...>)

import java.util.*;

class Example {
  public static void main(String[] args) {
    ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>(); 
    ArrayList<Integer> b = new ArrayList<Integer>(); 
    b.add(7); 
    b.add(7); 
    a.add(b); 
    System.out.println( a ); 
    b = new ArrayList<Integer>(); 
    b.add(2); 
    a.add(b); 
    System.out.println( a ); 
    b = new ArrayList<Integer>(); 
    b.add(5); 
    b.add(5);
    b.add(5); 
    a.add(b); 
    System.out.println( a ); 

  }
}
      
Run it to get:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> run Example
[[7, 7]]
[[7, 7], [2]]
[[7, 7], [2], [5, 5, 5]]


Is this modeling effective? 


  (b) let's use a Frequency kind of object 


class Run {
  int start;
  int size;
  int value; 
}

Encode the data we received with this choice of representation: 

  7 7 2 5 5 5 3 3 4 1 7 7 7 5 5 6 3 3 3
  0 1 2 3 4 5 6 7 8 9101112131415161718

The result should be: 

  (7 7) (2) (5 5 5) (3 3) (4) (1) (7 7 7) (5 5) (6) (3 3 3)
   0 1   2   3 4 5   6 7   8   9  101112  1314  15  161718

Answer: 

 ((0, 2, 7) (2, 1, 2) (3, 3, 5) (6, 2, 3) (8, 1, 4) (9, 1, 1) ... )
  
Suppose now that I want to sort this. 

Give me some ideas which run is the biggest, longest. 

  (7 7) (2) (5 5 5) (3 3) (4) (1) (7 7 7) (5 5) (6) (3 3 3)

Give me a reason for this to be the longest run:

  (7 7) (2) (5 5 5) (3 3) (4) (1) (7 7 7) (5 5) (6) (3 3 3)
            -------

Longest run should have (a) biggest size (b) appear first 

Give me a reason for this to be the longest run:

  (7 7) (2) (5 5 5) (3 3) (4) (1) (7 7 7) (5 5) (6) (3 3 3)
                                  -------
Longest run should have (a) biggest size (b) largest value 

Give me a reason for this to be the longest run:

  (7 7) (2) (5 5 5) (3 3) (4) (1) (7 7 7) (5 5) (6) (3 3 3)
                                                    -------
Longest run should have (a) biggest size (b) smallest value 
                        (a) biggest size (b) appears last 

How do we sort collections of complex entities? 

Answer: we use Comparable and Comparators.

This is the problem in Homework Eight with no posted solution.

Example of sorting:

class Student implements Comparable<Student> {
  String name;
  int age;
  Student(String n, int a) {
    this.age = a;
    this.name = n;
  }
  public String toString() {
    return this.name + ":" + this.age; 
  }
  public int compareTo(Student other) {
    if (this.age < other.age) return -1; 
    else if (this.age == other.age) return 0;
    else return 1; 
  }
}

import java.util.*; 

class Program {
  public static void main(String[] args) {
    ArrayList<Student> a = new ArrayList<Student>(); 
    System.out.println( a ); 
    a.add(new Student("Alex", 8)); 
    a.add(new Student("Leslie", 7)); 
    a.add(new Student("Chris", 6)); 
    System.out.println( a ); 
    Collections.sort( a ); 
    System.out.println( a ); 
    
  }
}

Run this to get:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> run Program
[]
[Alex:8, Leslie:7, Chris:6]
[Chris:6, Leslie:7, Alex:8]


There arfe other ways to sort and I will get to them on Wednesday.

--