CS 100, Summer 2001 Wednesday, 7/11 Lecture 9 ----------------------------------------------------------------------- Announcements: + Exercise 6 due 7/12 + drop deadline is Friday + will post grades today, please check them + reading: Savitch 4.2, 5.6 ----------------------------------------------------------------------- Topics: + scope + methods + OOP (object oriented programming) + designing a class + objects + constructors ----------------------------------------------------------------------- ---------------------------------------------------------------------- + we covered a LOT of material yesterday. Today we're going to slow down and look at it more closely. I'm more interested in having you really understand this than rushing to get to every possible topic related to computer programming! ---------------------------------------------------------------------- + Scope: - what portion of the code is visible to another portion - for classes, you will see that classes can be used from other classes when the classes are located in the same project - actually, you have already seen this (SavitchIn.java) EXAMPLE: scope.java ----------------------------------------------------------------------- + Method Terminology: + Formal parameters: - variable names written inside method header - these become local variables for the method + Actual parameters: - whatever is actually passed into the method + Call/pass by value: - variable "binding" - value of actual parameter copied into formal parameter - since it's a copy, the original is not changed - EXAMPLE: TensComplement.java: "number" is not changed even if "n" is assigned to something else. - Also, see the link to "pass by value" --------------------------------------------------------------------- --------------------------------------------------------------------- OOP + OOP philosophy: group related information together - OOP allows you to model real-world objects and interactions + doesn't add new functionality, but makes a LOT of things easier! + combine data (variables) and functions (methods) together + helps clarify code and reduce redundancy (how?) - this is called a CLASS EXAMPLES: + BunchOfGrapes without objects: several variables and methods that the programmer has to manage (keep consistent, remember how many there are, etc.) + BunchOfGrapes with objects: programmer only has to describe the collection of variables and methods ONCE. Easy to update! + Keywords: - class: defines a "pattern" that can later be used to make objects - new: creates an object of the specified class type - static: indicates that the associated variable or method goes with the class, instead of with an object of the class type - this will make more sense later! + suggest re-reading 5.1, 5.2, 5.3 now that you have an idea what classes are ---------------------------------------------------------------------- Class: (section 4.3) + 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) } - for now, "modifier" is just "public" and we don't have any "optionalstuff" yet. + examples used so far: System.out, Math, SavitchIn, your main classes + concept: blueprint/pattern for creating objects that you can use + like adding a type to a language - this is very powerful! EXAMPLES: BunchOfGrapes.java, Spaceship.java Design note: + Spaceship.java comes with "stubs" (Savitch p. 332) or placeholders for the methods you'll implement. This allows the tester program (also known as a driver program), SpaceshipTester.java, to compile and run even if you've only implemented some of the methods. ---------------------------------------------------------------------- Objects: + think back: how to use a variable? type varname; varname = value; + specific instance of a class + declaring and assigning objects: classname var; var = new classname(arguments); + we've already seen examples of this, with String objects String s = "Hello!" is the same as String s = new String("Hello!"); + class vs. object: - class is a pattern or template, description - object is an INSTANTIATION of that pattern into a real thing --------------------------------------------------------------------- Constructors + specifies what happens when you say BunchOfGrapes bunch1 = new BunchOfGrapes(); + fills in any object variables with default values - a constructor with no parameters is a "default constructor" + other constructors are possible BunchOfGrapes bunch2 = new BunchOfGrapes(36, true); - assuming a constructor is defined that takes in an int and a boolean, this will create a new bunch of grapes and set variables as indicated inside the constructor method - Note that parameters (as always) are order-sensitive (so new BunchOfGrapes(true, 36) is a different call!) EXAMPLE: Constructor in Spaceship.java --------------------------------------------------------------------- PICK UP Graded Exercise 5, handed out Exercise 6