Unformatted text preview:

CompSci 64.1Information Hidingÿ Black Boxes  Your Educationo Duke: BS: Courses: Modules: Lectures: . . . Depends on your level: (and “Who’s asking?”)ÿ Encapsulationÿ Abstractionÿ Object Oriented (OO) Object is often the Black BoxCompSci 64.2More on Classesÿ Syntax of a Class accessSpecifierclassClassName{fieldsconstructorsmethods} Have seen Chicken example in labÿ Syntax Fields (define the state) /Instance VariablesaccessSpecifier fieldType fieldName; Examples: (accessSpecifier should be private: WHY???)private double weight;private String address;CompSci 64.3More on Classesÿ Syntax of a Constructor accessSpecifier ClassName (parameterList){constructor body} Example: (ObGynCase)public OBGynCase(String aName) {sex=’F’;name = aName;} May have multiple constructors.ÿ How do I know it’s a Constructor? Same name as class No return type (not even void!)CompSci 64.4More on Classesÿ Syntax of a MethodaccessSpecifier returnType methodName(parameterList){method body} Examples: public int oldOdometer(int milesTraveled) {int milesDisplayed = milesTraveld%100000;return milesDisplayed;}public void resetTimer() {hours = 0;}CompSci 64.5Method Features (repeat)ÿ Return values  Methods can return informationo Accessor methods require thato Have return type in header specifying type of infoo Use: w = chick.getWeight();ÿ Parameters Methods may receive information thru parameterso Mutator methods usually require that o Method header includes parameter definition in parentheseso Use: chick.newName("Elsa");ÿ May have both parameters and return valuesCompSci 64.6Testing Classesÿ Use a main method and include test code Can have a main in every class definition for testing purposes Must then specify which main to be used when compilingÿ Can use existing special test environments Will discuss APT system later.ÿ Create your own Test Platform(s) Test platforms can be quite elaborate in large projectsCompSci 64.7Variable Categoriesÿ Instance Variables (fields) Belong to an Object Are “known” throughout the class (scope) Life is same as of the objectÿ Local Variables and Parameters Belong to a Method Scope is only that method (“know” only locally) Life is while method is active (dies on return)CompSci 64.8Primitive Typesÿ Primitive Types (base types) Built-in data types; native to most hardware Note: not objects (will use mostly first four)booleanintdoublecharÿ Constants (by example):boolean f = false;int i = 32769;double d = 0.333333;char c = ’x’;byteshortlongfloatbyte b = 33;short s = 21;long l = 289L;float =


View Full Document

Duke CPS 006 - Information Hiding

Download Information Hiding
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Information Hiding and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Information Hiding 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?