530/Handouts/notes-simple-classes.txt ------------------------------------- A 2 page summary of what Java is all about! See also http://www.u.arizona.edu/~jtmurphy/H2R/java.htm for a bit longer summary, and see the first 3-4 chapters of this online book: JavaTech -- http://www.particle.kth.se/~lindsey/JavaCourse/Book/courseMap.html ---------------- OOP - Object Oriented Programming program: collection of objects, each object of a given type (Class) objects send each other "messages" to tell them to carry out "methods" (procedures) to carry out desired computations. main() method -- special case, to get program started. objects = data + "methods" (procedures) you can ask them to carry out. Examples: Class defines a *type* of object: Car Person Ant AntEater Firm [NB: there is a static class object, too! Instance specific instances of the class One Ant instance for each Each represents a particular "entity" "ant" being modeled. Fields slots for storing data Ex: Age Height MaxSpeed Weight friendsList (store "pointer" to a list of the agents friends!) Instance variables (fields) -- specific places for data in each instance Thus each instance can represent different entity with different values. Class (static) variables (fields) -- for data stored in the Class object itself *store one value* for all instances of the class. MutationRate To Define a Class: YourClassName extends (subclass of) SomeClass default superclass: Object fields: names, data types; access control; May be static; methods: names ( parameters) and what they do! May be static constructors: special methods invoked with "new" to create new instances setters/getters: to allow other objects to access/change field values To use a class Define a "reference" variable to hold address of (pointer to) object: Ant anAnt; // variable name "anAnt" for pointers to Ant objects Use "new" to create an instance, store in the ref. variable anAnt = new Ant(); anAnt = new Ant( 10, 3, 5 ); // some constructors take parameters Send the instance messages to ask it to do things (ie execute methods). General form: addressOfObject.methodName( parameters-if-any ) Examples: double d = Math.cos( 2.1 ); // send to the class itself (static) double r = rng.nextGuassian(); // send to whatever rng points to anAnt.setWeight( 2.2 ); // send to whateveer antAnt points to Note: if no receiver object specified, default sends message to the object executing the method itself ("this" object): calculateBestMove(); This is like a simple subroutine call -- use to write short methods!! Read in JavaTech (or elsewhere) Class Definition Data Fields; Methods; Constructors Subclasses -- Class1 extends Class0 Interfaces -- Class1 implements InterfaceA Note: a method "signature" is these four parts: public void setWeight ( double wt ) ^^^^^^ ^^^^ ^^^^^^^^^ ^^^^^^^^^ access return name arguments (aka parameters) value type a method body: everything it does, between the curly braces: { ... } after the signature. Note that these variables available in a method: static (class) fields for that Class -- same value for all instances instance fields for that Class -- value for the particular instance parameter (argument) variables (passing values to the method) local variables ------------------------------------------------------------------------------ - Classes can be from: - Standard Java libraries (*lots* of useful things) - RePast libraries - Other 3rd party libraries (*lots* of these) - Your own program (and/or libraries) ------------------------------------------------------------------------ Java has the usual programming constructions which serve as the building blocks from which more complex methods can be contructed: variable types: primitive: int double float ... reference: pointers to objects! Double Bug Agent ... if ( expression ) { do some stuff if expression is true } else { do some stuff if not true } while ( expression ) { ... } for ( int i = 0; i <= maxI; ++i ) { ... } as well as other basic programming constructs. casting: tell java to convert data from one type to another, or to treat a reference (pointer) as one to a particular class. double d = 5.01; int i = (int) d; ArrayList aList = new ArrayList(); ...fill the list... Ant anAnt = (Ant) aList.get( 0 ); ------------------------------------------------------------------------- Abstract view of classes/methods: (See the Chapter 3 Supplemental material) Class as new type of data, e.g., like "int" but more complex! fields -- what data is stored int -> store an integer methods -- like operators on the data: what can be done Thus define and use these new "building blocks" in your programs. -------------------------------------------------------------------------- *** A Few Programming Tips **** - Case matters. - You have to define variables. To be used, a variable must be "in scope": - class variable usable anywhere in class always the same value for all instances - instance variable usable anywhere in class may be different values for each instance - method parameter accessible only in that method - local variable accessible only from it definition to end of enclosing block (pair of { ... } ) - The compiler is very very seldom wrong... Look for errors starting at the first method mentioned from your program Look for errors from line mentioned and then go backwards in program (error may be many lines before that!) - COMMENTS -- write them *before* you write the methods! - use it to think thru what it should do - use it as outline to guide the coding - if you don't do it then, you never will!! (and you'll be sorry later...) - Self-documenting programs: - variable names, method names that say what they are or do! - Pick a standard "style" and ALWAYS USE IT!!! Make it habitual. - indentation - case of variables, methods - horizontal, vertical space => emacs and other tools help: auto-indent, etc. - Write short methods -- fit on one screen! If you need something longer, put some of it into another method! - Start *very* simple, get something running, then add to it. DO NOT TRY TO WRITE IT ALL AT ONCE, then compile it. You will just have a zillion errors...hard to debug, and depressing! - avoid embedded constants. Use symbolic names. public static final int HOT = 0;