CS100J, Spring 2001 Tues 2/13 Lecture 9 ------------------------------------------------------------------------------- Announcements: + P3 posted last night + Groups sessions: more problem solving; provide more details on lecture; practice quizzes; go over solutions together; limit < 20 people + Section nightmare -- close to resolution ------------------------------------------------------------------------------- Topics: + intro to OOP (object oriented programming) + designing a class + scope ------------------------------------------------------------------------------- Summary from Lecture 8 + static variables and static methods + scope: the visibility of a element of code (variable, method) ------------------------------------------------------------------------------- OOP: Object Oriented Programming (What it's Not) + programming languages: low level and high level - how "close" can you be to machine instructions - low level: assembly - mid level: C - high level: Java, MATLAB, Maple + why higher? closer to algorithm abstraction + avoid spaghetti code! want clarity for resuse + procedural approach: variables, control structures, methods (functions) + still have problems with spaghetti + notice trend in abstraction: - clump statements together - move clumps elsewhere ------------------------------------------------------------------------------ OOP + combine data (variables) and functions (methods) in special "clump" think "combine form with function" + helps clarify code and reduce redundancy (how?) - put related variables and methods together - called a CLASS ------------------------------------------------------------------------------ Class: + the code that you write that combines variables and methods + general syntax: modifier $class$ classname optionalstuff { -> identity variables; -> data, state methods; -> how to use, create (behavior) } + examples used so far: TokenReader, System.out, Math, your main classes + concept: blueprint for creating objects that you can use + like adding a type to a language ------------------------------------------------------------------------------ Programming with Classes: + put classes together in same project/folder/dir - Java SDK wants 1 public class per file - can have more than 1 class in file if only 1 is public (CodeWarrior allows you to break this rule!) - classes in same project can "see" eachother (think scope) - for conciseness, DIS tends to stick all classes in 1 file: class A { } class B { } public class C { public static void main(String[] args) { System.out.println("Hello, world!"); } } + How to use classes? - put vars and methods inside other classes - use dot (.) operator to access a method or var var: identifier.identifier (can have more dots) method: identifier.identifier(arguments) (can have more dots) - 2 choices: (1) make everything $static$ inside class you wish to use ex) Math.random(), Math.PI (2) use specific instances of class called OBJECTS ------------------------------------------------------------------------------ Objects: + think back: how to use a variable? type varname; varname = value; + specific instance of a class + Create objects by INSTANTIATION $new$ Classname(arguments) - gives the ADDRESS IN MEMORY (location) of the NEWLY CREATED OBJECT - location is called a REFERENCE - Why? objects too "large" to move, so instead, just pass address + declaring and assigning objects: classname var; var = new classname(arguments); ------------------------------------------------------------------------------