================================================================ Week 2 Wed, Sept 5 ====================================== What are classes? fields + methods What are objects: Class definition + "instance data", or "state", for the fields Objects are very much like DATABASE RECORDS, except for this public/private stuff. How are objects created? with new() and a "constructor" Fig 1.6 Data types: int, String, classes TicketMachine 2: how do you tell everything apart? _ convention?? methods calling other methods =============== 5. picdemo multiple ctors adding extra ctors to circle how they appear in bluej can one ctor call the other? sort of. First look at LOOPS meteor lawnzip -- oops, this probably isn't what we meant lawngrow Math.random() rain randombubbles Note use of RANDOM here that makes rain easier than lawngrow. ====================== accessors v mutators if, if-else, if-else if - else conditional Boolean ClockDisplay example ch 3: clock-display example. Abstraction two kinds of diagrams Can see the display updated by each timeTick by using the inspector. methods that call other methods object creation modularization & abstraction First, we want to build a display that increments correctly as "timeTick()" events (1-minute interval signals) arrive. We need hours:minutes. Why don't we make "hours" and "minutes" be NumberDisplay? Pros: good modularization/abstraction Cons: Maybe we can just *do* the thingie with numbers? (but leading 0's are tricky) Consider a numberdisplay object, with an increment() method. Note: % operator (read as "modulo"), and &&, and how to return a string from a number. ClockDisplay object Note that we'll have different limits for the two NumberField thingies in a ClockDisplay. Numberdisplay: modulo operator "0" + num "" + num Now on to ClockDisplay. 1. Note updateDisplay() is *private*. Also, it's the first method we've had that is called by other methods of the same class. 2. Objects creating subobjects look at the ctor initially "hours" and "minutes" are null 3. Two different NumberDisplay subobjects, with different rolloverlimits 4. multiple ctors for ClockDisplay ================================================================================ ================================================================================ Friday, Sept 7 =================================================================== Examples: picdemo (pld's example, not a book example) train Picture meteor lawngrow MailSystem NoteBook Structure of simple while loop: n=0; while (n<100) { do stuff do more stuff ... probably depends on n; may not though n=n+1; } used in lawngrow, rain, bubbles, meteor Some TABLES of numbers, using loops triangletable ============================================ Demo of MailSystem Note that it would be quite hard to simulate multiple users without the BlueJ object bench. Look at MailItem Don't look at MailServer (why?) MailClient: "this" keyword: useful for dealing with name conflicts demo of creating three users and several messages, and using the Inspector 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 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; } }