Comp 170-003 , MWF 2:45-3:35 25EP-201, Lab M 4:15-5:05 or earlier Week start 1 8/27 2 9/3* Labor Day 3 9/10 4 9/17 5 9/24 6 10/1 Exam 1: Mon 7 10/8* 8 10/15 9 10/22 10 10/29 11 11/5 12 11/12 Exam 2: Mon 13 11/19 Thanksgiving: no lab 14 11/26 15 12/3 last class Week 1 intro text 2 exams TA: Maria Saenz Syllabus/groundrules Who has programmed, and, if so, in what? Need for IDs and Passwords Structure of Labs Programming versus problem-solving actions versus objects move the car take the car and move it our book has a very strong focus on objects "Car" as a class, versus 1995 Dodge Caravan, a specific car (an instance). Instances have properties, and can have actions applied to them. The Shapes class, and some instances. ==================================== creating instances on the object bench; multiple instances simple METHODS: makeVisible, moveRight, moveDown methods with PARAMETERS: moveHorizontal data types and objects: int, Circle, String State: object inspector programming: comments, syntax, whitespace, visual style identifiers reserved words case sensitive overall format of classes; public static void println FIELDS define object state fields of square, circle, triangle programming issues compilation v execution java v bluej variables, types A simple class with some strings A class: String some instances: "hello", "peter", "loyola", "foo"+"bar" creating strings, with + The *class* defines some OPERATIONS too. Classes = data + operations the pldHello class ================== basics of string output: System.out.println() where string output goes in BlueJ hi() demo2() why some things don't work as it seems they should 1 + "hi" versus "hi"+1 ============================================================================== ============================================================================== Wednesday, August 29 READ: Chapter 1, start TicketMachine in Chapter 2 pldHello picture student In java, *almost* everything is an object. But there are "primitive" types too: int, double/float. String is a class, though. Convention: class names are capitalized, instance names are not. It is convenient to think of programs as a bunch of operations on objects. Some of these operations require parameters. Note that sometimes parameters are other objects, and sometimes it's not clear which object should be the one acted upon and which the parameter. s.compareTo(s2) Note also that some operations CHANGE the object ("mutators"), and some don't ("accessors") If change occurs sometimes and not others, it's still a mutator. String examples; pldHello class escape chars \", \n, \t, \', \\ String w, x, y, z; w = "peter" w = new String("peter"); // rare x = w; y = new String(w); w.replace('p', 'P'); w.toLowerCase(); Note last two are NOT mutators! what is a mutator?? Shapes class, subclasses, multiple instances of Circle Circles, etc have INTERNAL STATE. Look at on the object bench, and then in the class code. What about Shape itself? Making a picture Picture demo ============ In Lab 0 you created objects on the Object Bench, and positioned them "manually", by moving them. Picture: object creation within a program "automated" repositioning What happens if we create a picture, invoke setblackandwhite, and *then* draw? Try commenting something out and recompiling (esp in setblackandwhite). Second sun, red why doesn't setblackandwhite work? Enrollment example ================== creating objects (students) to add using the inspector object names; objects as parameters labclasses project: 1. create a student. What are the parameters? Just two strings? Could they be firstname and lastname? Oops... no. Why do you think student_number is a string and not an int? 2. Add some credits. 3. Call some methods that return values. End of class ====================================================================== Friday, August 31 Labclass TicketMachine Labclass object Notice that in the Shapes and Picture examples, methods did NOT return values! They just drew things. But many of the Student methods have return values. 4. Now let's create a LabClass object, and put some students into it. This illustrates the use of passing objects as parameters, using the name! (Actually, we just click) You must already have created the objects on the Object Bench. What about duplicates? LabClass project: Where is LabClass *putting* all these students? Look for the ArrayList structure. Identify where a new student is put, how we add a student, how we find out how many have been added, and how we print them all out. This last requires a loop. What about populate()? What if populate() runs into the limit? ==================================== (Normally in java you don't have the Object Bench to use to tweak individual objects; you have to write a program to do that.) =========================================================================================== =========================================================================================== Note that the Shapes and Picture programs cannot run "stand-alone"; as examples they rely on use of the Object Bench in BlueJ. Making them standalone isn't too hard; a standalone program begins with a procedure called main(). But there are a few details to sort out. ================ ============================================================================== ============================================================================== 3. Now go on to the TicketMachine. Simple one: 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 4. CORRECTION to ticketmachine: 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 ================================================================