================================================================ Week 3 Mon, Jan 26 loops in Picture.java, numeric loops Clockdisplay MailSystem Notebook reprise: naming objects Lab-classes picdemo: more loops numeric examples ====================== accessors v mutators if, if-else, if-else if - else conditional Boolean Examples: ============================================ public void meteor(){ Circle m = new Circle(); ... int i = 0; while(i<150){ m.moveHorizontal(2); m.moveVertical(1); i=i+1; } } public void rain(){ Circle droplet; Random rand = new Random(); int i=0; while( i<100){ int x = rand.nextInt(150); int y = rand.nextInt(200); droplet = new Circle(x,y); // pld: new ctor droplet.changeColor("black"); droplet.changeSize(4); droplet.makeVisible(); i += 1; } } public void lawnzip() { Triangle t = new Triangle(); ... while (i<24) { t.moveHorizontal(10); i=i+1; } } public void lawngrow() { Triangle t; int i = 0; while (i<24) { t = new Triangle(); t.moveVertical(200); t.moveHorizontal(i*10); t.changeColor("green"); t.changeSize(20,10); t.makeVisible(); i=i+1; } } public void numtable() { // Table of cubes int n=0; while(n<=20) { System.out.println(n + " " + n*n*n); n = n+1; } } Numeric example public bool isPrime(int n) { int div = 2; int top = n-1; // top = (int) Math.sqrt(n) while (div < top) { if (n % div == 0) return false; div ++; } return true; } now *print* primes from 1 to 100: for (int i = 1; i<=100; i++) { if (isPrime(i)) { System.out.println("" + i + " is prime"); } } ========================================================================================== for loops / nested loops: OMIT FOR NOW public void triangletable() { int row,col; for (row = 1; row <= 15; row++) { for (col = 0; col loops: howManyMailItems(string who) for:each loop getNextMailItem(String who): Iterator loop in original version; could also be while loop do the while version forgot i++ used <= instead of < demo of reading messages, and watching the inspector view change Demo of sending a message and setting a breakpoint in printNextMessage(), and running the debugger. (This crashed a couple times for me) 5. Class diagrams v object diagrams class arrow: NOT just for subobjects What do the arrows mean for object diagrams? "Uses" relationship =========================================================================== =========================================================================== Wednesday 1. Different types on object bench: if they are to interact, their object-bench names MUST be used! Labclasses MailSystem For that matter, two objects of the *same* type on the object bench must have their names used somehow, in order to interact. 2. Notebook & chapter 4 3. Lab-classes revisited ================================================ The Notebook example will be made available to you during the first exam, as a source of "standard" examples. Notebook makes use of ArrayList, which can hold an arbitrary number of notes. Notebook example: Notebook1: demo of storeNote, numberOfNotes, showNote look at code: ArrayList get/size inspector Problem: what if we forget which note we want? Notebook2: A *loop* to show all notes while version for-each version (for loop, or "classic for loop", is also a possibility) ============================================================================== Notebook search loop 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 ================================================== Where else would this help? LabClass project ==============================================================================