******************************************************************************* 0) Last time- anyone not get emailed by me? any technical problems? questions? 1) Administrative - codelab ch2 "additional" due Wed 2/4, read 3.1-3.3 - A0 is released, due Fri 2/6 (handout) - a lot at once, but none of them are huge 2) Class format experiment: lecturing is not very effective in the long run. - first person in each group (alphabetically by last name) has job of summarizing & presenting concepts of 3.1-3.3 to group next time - should take about 15 minutes; include definitions, rules, and some examples of the concepts- just take notes as you read, then organize - everyone else at least skim it, then help out and be supportive 3) (Primitive) Data: single values that you can store and use A. Store: using identifiers- any name that follows naming rules * numbers, letters, $, _, can't start with a number, no reserved words - identifier has a specific type: tells what kind of data it can hold * int, double, boolean, char - declaration: state the existence of the identifier and its type * int x; boolean t_or_f; char myLetter; - assignment: use an assignment statement, stores right into left * x = 5; t_or_f = false; myLetter = 'd'; * can combine declaration/assignment into one line: int x = 5; * can put several together: int x=5, y=6; - variables: you can re-assign to them (t_or_f = true;) * to make this illegal, change declaration to: final boolean t_or_f; * this makes it a constant and will cause an error if you re-assign * by convention, capitalize constants: T_OR_F (mixed-case for variables) B. Use: the same way you can use variables in algebraic expressions * int y = 2*x; x = x + 1; * think about what equals usually means- how is this different? * can use literal values w/o storing (2, true, 'd'): constant literals C. Non-primitive types: reference types hold addresses instead of data * they're like arrows pointing to places in memory (objects) ** Questions?? 4) Methods: sets of instructions that you can declare and then use A. Declare: header and body ( ) { } public static int f ( int val ) { return 3*val*val + 2*val + 1; // calculates equation } * parameter: declaration of a variable, passed a value by caller * return type: type of data that we'll return to caller (or void) B. Use: method call (from another place in the code, like main) ( ); // if it doesn't return anything System.out.println("Hello"); = ( ); // using a return value x = f(1); // jumps over to f, sets val=1, gets back 6, sets x * arguments get assigned into corresponding parameters C. Why? -write once, use often: more efficient and fewer mistakes * also makes main simpler (use descriptive method names) * example (don't copy) ** Questions?? 5) Objects: collections of data and associated methods that you declare and use A. Declare: using identifiers again, but this time they're reference types * state existence of identifier and its type SketchPad pad; JFrame fr; * create the object for the identifer to point to (using a constructor) pad = new SketchPad(); // as with methods, could pass parameters * before this, the value in "pad" is special value "null" (nowhere) * after, the value is a reference to a chunk of memory for object's data B. Use: dot notation to send it messages (call its methods) fr.setSize(50, 100); // tells fr to set its size C. Type of an object: its class, short for "classification" * the class defines what data and methods belong to this kind of object * objects of "JFrame" type have "setSize" method, data "width" & "height" * two different instances might have different values for width, though * class is blueprint that defines structure; object is house made from it * JFrame class is pre-defined in Java, but you can define your own too class { // members: data and methods } public class JFrame { // tiny part of the JFrame class private int width; private int height; // two data member declarations public void setSize (int w, int h) { width = w; height = h; // one method declaration } } * body is not instructions, but declarations (of data and methods) * added part of data declarations: they get modifers too * data are usually called "private", methods "public" - about security * other possible modifiers: "final", "static" * what types of data? primitive or reference (ints, SketchPads, etc.) D. Categories of members * instance data: like width and height, each object can have diff values * class data ("static"): all objects of the class share one value private static int min_width = 0; // imaginary, but possible * instance methods: normal ones * class methods ("static"): ones that can only work with static data E. Class diagram parts (see A0 handout) F. Why? - once JFrame is written it's easy to use * abstraction: get something to happen w/o worrying about details (car) ** Questions?? 6) Summary - data, methods, and objects are building blocks of programs - all of them are declared and used - objects are collections of data and methods - a class defines what one kind of object has *******************************************************************************