Comp 170-005, Spring 2009 MW 4:15-5:30 25EP-201, Lab M 3:10-4:00 or earlier Week start 1 1/12 2 1/19 MLK day 3 1/26 4 2/2 5 2/9 6 2/16 Exam 1: Wed 7 2/23 8 3/9 9 3/16 10 3/23 11 3/30 12 4/6 Exam 2: Wed?? 13 4/13 Easter: no lab, yes class 14 4/20 Week 1 intro text 2 exams Syllabus/groundrules TA: Nimisha Sharma Joshi (nsharma@luc.edu) Labs are REALLY IMPORTANT!! ======================== Who has programmed, and, if so, in what? Need for IDs and Passwords Structure of Labs ============== 1. Basics of programming variables conditions loops i: 0 sum: 0 i = i+1; sum = sum + i i = 10? Yes: done, No: go back to start java: i=0; sum =0; while (i != 10) { i = i+1; sum = sum + i; } Logically this is all you need, but in real terms it is extremely important to stay ORGANIZED. =============== 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 1990 Dodge Caravan, a specific car (an instance). Instances have properties, and can have actions applied to them. ==================================== 2. The Shapes class, and some instances. Book version lab version: replace Square with Rectangle 1.2: creating some shape objects from their classes creating instances on the object bench multiple instances 1.3: calling methods simple METHODS: makeVisible, moveRight, moveDown 1.4: methods with parameters: moveHorizontal 1.5: data types and objects: int, Circle, String 1.7: State: object inspector 1.10: source code attached to objects modifying initial color & location of a circle recompiling! compilation errors ==================================== 3. Basics of "meta"-programming: style, etc 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 HARD STUFF: actual drawing details, canvas class more programming issues compilation v execution java v bluej variables, types =============================================== 4. Getting output 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 numeric examples ================ creating a class from nothing ============================================================================== ============================================================================== Wednesday READ: Chapter 1, start TicketMachine in Chapter 2 Projects in chapter 1: picture lab-classes 0. longs Java has "long" in addition to int: long x = 20000000; x = x * 3000000; or 2000000L * 3000000 1. Classes, Objects, and primitives (no example) =================================== There are also bluej projects: one class per file, though 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. examples: rshapes class class Rect boolean isVisible String color int xPosition; It is convenient to think of programs as a bunch of operations on objects. Some of these operations require parameters. Classes are represented by java class definitions. These contain subdefinitions of data ("fields") and operations ("methods") 2. Shapes classes, source code ============================== Circles, etc have INTERNAL STATE. Look at on the object bench, and then in the class code. Look at source code for Circle (in Circle.java). Look at: fields methods Relate to graphical view methods can be called from popup menu on object bench, or from within java using the syntax x.methodName(params) methods have *signatures*: return type, and parameter types 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. Picture project: your next lab! ================================== Look at body of draw(). Note how objects are created and manipulated. (Hard part: figuring out where all these numbers come from) We called all the methods "manually", from the object bench, in Lab 0. But in real programs, objects are created and tweaked in java Circle c = new Circle(); c.moveHorizontal(100); The latter is a typical example of a *method call*. Methods (defined in classes) always act on some object. String s; s.toLowerCase(); 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. 4. 1.11-1.13: Labclass example ============================== creating objects (students) to add using the inspector object names; objects as parameters methods returning values 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. 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? 5. Use the inspector on the Labclass object to find the students (tricky) 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. 6. Modify Labclass to *create* some Student objects 7. populate(), searchStudent() What about populate()? What if populate() runs into the limit? How does searchStudent() work? What can methods do? print something change a field return a value do something (else), eg move the circle ====================================================================== ============================================================================== ==============================================================================