Information Hiding v Black Boxes q q v
Information Hiding v Black Boxes q q v v v Your Education o Duke: BS: Courses: Modules: Lectures: . . . Depends on your level: (and “Who’s asking? ”) Encapsulation Abstraction Object Oriented (OO) q Comp. Sci 6 Object is often the Black Box 4. 1
More on Classes v Syntax of a Class access. Specifier class Class. Name { fields constructors methods } q Have seen Chicken example in lab v Syntax Fields (define the state) /Instance Variables access. Specifier field. Type field. Name; q Examples: (access. Specifier should be private: WHY? ? ? ) private double weight; private String address; Comp. Sci 6 4. 2
More on Classes v Syntax of a Constructor access. Specifier Class. Name (parameter. List){ constructor body } q Example: (Ob. Gyn. Case) public OBGyn. Case(String a. Name) { sex = ’F’; name = a. Name; } q May have multiple constructors. v How do I know it’s a Constructor? q q Same name as class No return type (not even void!) Comp. Sci 6 4. 3
More on Classes v Syntax of a Method access. Specifier return. Type method. Name(parameter. List){ method body } Examples: public int old. Odometer(int miles. Traveled) { int miles. Displayed = miles. Traveld%100000; return miles. Displayed; } q public void reset. Timer() { hours = 0; } Comp. Sci 6 4. 4
Method Features (repeat) v Return values q v Parameters q v Methods can return information o Accessor methods require that o Have return type in header specifying type of info o Use: w = chick. get. Weight(); Methods may receive information thru parameters o Mutator methods usually require that o Method header includes parameter definition in parentheses o Use: chick. new. Name("Elsa"); May have both parameters and return values Comp. Sci 6 4. 5
Testing Classes v Use a main method and include test code q q v Can use existing special test environments q v Can have a main in every class definition for testing purposes Must then specify which main to be used when compiling Will discuss APT system later. Create your own Test Platform(s) q Test platforms can be quite elaborate in large projects Comp. Sci 6 4. 6
Variable Categories v Instance Variables (fields) q q q v Belong to an Object Are “known” throughout the class (scope) Life is same as of the object Local Variables and Parameters q q q Comp. Sci 6 Belong to a Method Scope is only that method (“know” only locally) Life is while method is active (dies on return) 4. 7
Primitive Types v Primitive Types (base types) Built-in data types; native to most hardware q Note: not objects (will use mostly first four) byte (1 byte) boolean (1 bit) short (2 bytes) int (4 bytes) q double (8 bytes) char (2 bytes) v long (8 bytes) float (4 bytes) Constants (by example): boolean f = false; int i = 32769; double d = 0. 333333; char c = ’x’; Comp. Sci 6 byte b = 33; short s = 21; long l = 289 L; float = 3. 141592 F; 4. 8
- Slides: 8