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:
- print it
- print it conditionally
- extract a number from it, and add it to sum
- extract a number from it, and keep a running max (or min)
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:
- All fields are set by the constructor
- There is a field accessor for every field
- There are no mutators
(no methods even potentially changing the object)
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.
- use of null to indicate unused fields
- what the various fields do
- mostly record-keeping
Basic use:
- On the object bench, create a Person and an Auction.
- Add some lots to the Auction with enterLot().
- Use showLots()
to view them.
- Add some bids with bidFor();
Auction methods:
- enterLot: adds to list "lots"
- showLots():
for-each loop to print each lot
toString() example
- getLot(): some checks and balances
- bidFor():
gets a list element and UPDATES it.
Note that we do NOT in any sense "put the lot back"
Wednesday
Arrays
Weblog-analyzer
... weblog_graph
ArrayList internal growth
NumList
Phonebook
Arrays
- fixed size
- most efficient (but don't obsess about that)
- underlying basis for ArrayList
- A[i] notation
logfile analyzer: how would we do it with an ArrayList?
Allocating arrays v allocating array elements
classic for-loops:
- initializing an array of ints to 0
- adding the elements of an array
- finding the max of an array
- counting selected elements of an array
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
- overview of program
input: how much do we really have to know?
- how would we do it with an ArrayList?
- Adding Weekly counts
- LogfileReader: lots of stuff done in ctor, including opening and reading the file weblog.txt
- LogEntry: fields stored in an array, this is somewhat weird but it does make compareTo() simpler
Basic operation:
create LogAnalyzer object
run analyzeHourlyData()
========================
arrays v ArrayList:
- could we do the weblog-analyzer with an ArrayList?
- the ArrayList get/set interface is awkward.
For many lists, though,we NEVER use it:
we insert using .add(item), and
access the elements using for:each loops
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.
- What happens when we fill the ArrayList with addBunch() and then add one more?
- What happens as we repeat this?
- What happens as we do this while watching the array in the inspector?
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?