================================================================ Week 2 Wed, Jan 20 ====================================== 0. 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 (4th ed, Page 10) Data types: int, String, classes How do you tell everything apart? This can be tricky _ convention (field names begin with _)?? B&K don't use this methods calling other methods ======================================================== 1. Now go on to the TicketMachine. Version 1: variables: private data public/private ctor getPrice getBalance insertMoney printTicket Ticketmachine "invariant": balance & total are always updated properly. Exercises 2.7/2.8: change to "class public Ticketmachine", leave out public entirely Fields/Ctors/Methods Note that the ctor does NOT return a value! You can write functions that don't return a value by putting "void" there. Look at fields through the Inspector Formal v Actual params in ctor Which variable is which? Figuring out what the fields are accessors & mutators Make TicketMachine print a serial # on each ticket private int serialNum Add a method ticketCount() What if we don't put in enough money? What if we put in too much? What if we insert -100000 cents? What if the initial ticket price is negative? This brings us to if-statements: p 36 2. TicketMachine Version 2 ========================== improved logic to printTicket(): make sure enough money was inserted class vars v local vars _ convention? Overview of ch 2: fields, ctors, methods (accessor/mutator), parameters, assignment, return, if review structure of a class: fields almost always private ctors usually public; no return type methods usually public; must have return type (or "void") accessors generally return a value mutators may or may not. At least as far as fields go, an object looks like a DATABASE RECORD. ISOM 370 registrants take note: methods calling other methods ================================================================ =============== 3. picdemo picdemo (pld's example, not a book example) train Picture meteor lawngrow multiple ctors adding extra ctors to circle how they appear in bluej can one ctor call the other? sort of. See commented out this(20,60) (but ignore for now) 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. Structure of simple NUMERIC 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 An even simpler form of loop is the FOR-EACH loop, where we do something to each element of a list (eg print it). However, the simplicity is perhaps a touch deceptive. did not finish all loop examples above; see 03.text