Comp 170 Week 5 -- Feb 9, 2009

Study guide now available
solutions too: partial solutions for book exercises, full solutions for mine.

club and printing members:
    System.out.println(m)

returning lists

Auction system




Club search that returns a list
We also need a way to print a list!

    public ArrayList<Membership> search2(String s) {
        ArrayList<Membership> memsfound = new ArrayList<Membership>();
        for (Membership m : members) {
            if (m.getName().contains(s)) {
                memsfound.add(m);
            }
        }
        return memsfound;
    }

Loops:
    some variable is the "marker", or cursor, that moves through the list.
    For while loops, it's "i" or "index" or whatever is tested & incremented
    For for-each loops, it's done automatically.

Loop forms:
    for each element of list:
Note that (often) the longer the expression, the shorter the result
    members
    members.get(i)
    members.get(i).getName();
    members.get(i).getDonations();


Auction system: more min/max loops

Trivial classes: Person, Bid. "Trivial" is used in a technical sense here:
Could we use a String for Person? What about Bid?

Simple classes: Lot
    description, lotnum, highbid. Lotnum = position in the list Auction.lots.

List class: Auction

Lots of use of null and of anonymous objects (4.9.4).
Anonymous objects (created "in place" with new()) are pretty common.
Basic use:
Auction methods:

Wednesday
    Arrays
    Weblog-analyzer
    ... weblog_graph
    ArrayList internal growth
    NumList
    Phonebook

Arrays

logfile analyzer: how would we do it with an ArrayList?
Allocating arrays v allocating array elements
classic for-loops:
classic for-loops v for:each loops
was the classic for-loop really designed for arrays?
for:each loop: no access to the numeric position in the list!
classic-for / while equivalence
for:each and arrays

Weblog-analyzer

Just started Monday
Basic operation:
    create LogAnalyzer object
    run analyzeHourlyData()
   
========================

arrays v ArrayList:
Compare:
            hourCounts[hour]++;
versus the ArrayList version:
        int count = h2Counts.get(hour);
        count++;
        h2Counts.set(hour, count);

weekday counts:
    int weekday = (day+6) % 7
Why 6? Because May 1, 2005, was a Sunday, which is day 0.
    may 2   day 1
    may 3   day 2
    ...
    may 7   day 6   (saturday)
    may 8   day 0   sunday again


Chart version:
    Do analyzeHourlyData() first!
    drawData(): rectangles grow downwards!
    drawData2(): fixed

======================

How new Foo(...) works: memory view of object creation and of arrays


ArrayList growth

Inspector example demoing how "real" ArrayLists handle growth
Sizes: 10, 16, 25, 38, 58, 88, 133, 200
next size = floor(1.5*prev_size) + 1;

Use my NoteDemo project, with the addBunch() method.

NumList

    A class in my version of weblog: like an ArrayList
    using it in the program
    Definition of get(), set()
    How add() works
    How add2() works
    fillRandom();
    What happens when we execute
          NumList nlist = new NumList(5);
    How does that compare with new ArrayList<String>(10)?

Monday lab: Phonebook, implemented using StringMap (demo not available online!)
    Basic operations:
       lookup: what does it do if not found
       insert: what does it do if that key is already there?
       update
       search: problems with returning "first" instance; return a list instead?