Week 4: Week of Sept 17 Monday: Overview of notebook.pld listReverse totallength longestNote note need to record POSITION of longest note Thus, a for:each loop could not be used. relation to min date (year or year/month) MailServer: Iterator loop example [??] Notebook: iterator loop (below) Not on exam in any form. Advantage: Search loop again Loop types ================================================= Notebook SEARCH loops: javadoc and String page; find note.contains(s) "return loop": use return statement to terminate when found isPrime was an example of this so was LabClass.searchStudent "print all" loop: terminate at end of list, regardless of data found Notebook.searchNotes is an example "find first" loop: compare to example on page 94 ======================================================= Iterator loop: p 95 Iterator it = notes.iterator(); while(it.hasNext()) { totallen += it.next().length(); } MailServer iterator loop: Use iterator (or while) because we can remove an item from the list being processed. public MailItem getNextMailItem1(String who) { Iterator it = items.iterator(); while(it.hasNext()) { MailItem item = it.next(); if(item.getTo().equals(who)) { it.remove(); return item; } } return null; } ================================================= max loop: finding longest note loop patterns: processing a list processing a range of numbers 1..N processing N times printing or other repeated action sum search max ================================================= ================================================= Wednesday: Auction system: more min/max loops Trivial classes: Person, Bid Simple classes: Lot List class: Auction Lots of use of "null" and of anonymous objects ================================================== Auction system use of null to indicate unused fields what the fields do mostly record-keeping 1. "Trivial" classes Person and Bid Could we use a String for Person? What about Bid? 2 Lot: description, lotnum, highbid. Lotnum = position in Auction.lots 4. Anonymous objects: 4.9.4 =================================================== Numeric example with max repeatedly calculate rand.nextInt(100) until we reach 1 Count the # of iterations count the max Do it 1000 times and find max length =================================================== Friday Notes on labs: Over a third of you turned in the wrong file, or didn't attach a file! Ctor in Membership: nobody asked about "throws IllegalArgumentException"! That's probably a good thing, in that this "weird stuff" didn't confuse too much. If Alice was the first member, you didn't check the month if (m.getYear() < minYear) { versus: if (m.getYear() < minYear || (m.getYear() == minYear && m.getMonth() < minMonth)) min loop: ALWAYS need to update min_so_far depending on circumstances, may also need to keep track of position of min_so_far in the list, or of entire min_so_far object ArrayList v ArrayList, "autoboxing" 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 ===================================================