Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips 367 1.

Similar presentations


Presentation on theme: "COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips 367 1."— Presentation transcript:

1 COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips 367 1

2 Announcements Late days Do you have any left? 2

3 Questions? What is the difference = ==.equals() 3

4 Class and Primitive Types In memory Primitive types store value Class types store address to data Why? 4

5 How big is a String? int 4 bytes float 4 bytes double 8 bytes String ? bytes “hello” “How are you doing today” 5

6 Types in memory 5 3.14159 2078 1056 “hello” “A string” int num float num2 String s1 String s2 s1 = s2; s1 = “how are you?” System.out.print(s2); “How are you?” 6

7 Today in COMP 110 Writing a class with good programming practices Monopoly 7

8 Why classes? boardwalk.buyHouse(); if(!hotelOwned || !mortgaged) { if (numHouses == 4) { hotelOwned = true; numHouses = 0; } if (numHouses < 4) numHouses++; } 8

9 Information Hiding Method can be used without understanding detail of code A.K.A. - abstraction Programmer’s job becomes simpler Code is easy to read 9

10 Before You Start Define the problem Write a class for a property in Monopoly UML diagram 10

11 - name: String - cost: int - morgageRate: int - houseCost: int - rent: int - house1: int - house2: int - house3: int - house4: int - hotel: int - owner: String - currentRent: int - numHouses: int - hotelOwned: boolean - mortgaged: boolean Property Class Name Instance Variables Instance variables should be private 11

12 - calculateCurrentRent(): void + setOwner(String s): void + setNumHouse(int i): void + buyHotel(): void + buyHouse(): void + mortgageProperty(): void + buyProperty(): void + getRent(): int + inquire(): void Property Class Name Methods Most methods are public but some are private 12

13 Comments Precondition - everything that needs to be true before method Postcondition - describes effect of method call 13

14 /** Precondition: The instance variables of the calling objects have values. Postcondition: The number of houses the user owns have been updated by one if the owner had fewer than 4 houses and the property has not been mortgaged. If the owner had 4 houses, they bought a hotel and now have zero houses. */ 14

15 When to use Pre and Post You can omit for obvious methods get (accessor), set (mutator) … All other methods need pre and post conditions If you are unsure, write pre and post! 15

16 Accessor Method Read data from instance variable getRent(); 16

17 Mutator Methods Change data in private instance variable setOwner(String s); 17

18 Write methods mortgageProperty(); calculateCurrentRent(); 18

19 Constructors Method designed to initialize instance variables public Property No return value Same name as class Monopoly property: Name, cost, mortgage rate, cost for each house, rent, rent for each house, rent for hotel 19

20 Encapsulation Hiding details of a class that are not necessary to understand how objects of the class are used User Interface Implementation 20

21 User Interface What programmer needs to know to use the class Headings for public methods public type_or_void Method_Name(Parameter_List) Defined constants public static final Type Variable = Constant; Comments for using public methods /* Precondition: Postcondition: */ 21

22 Implementation All private elements of class definition Private instance variables private Type Variable; Definitions of private and public methods body of the methods 22

23 Implementation: Private instance variables Private constants Private Methods Bodies of all methods Method definitions Well Encapsulated Interface: Comments Headings of public methods Public defined constants Programmer 23

24 Guidelines Comments before class definition (this is your header) Instance variables are private Provide public accessor and mutator methods Pre and post comments before methods Make helping methods private /**/ for user-interface comments and // for implementation comments 24

25 Global vs. Local Variables are local to methods Instance variables are Global for all methods in a class 25

26 Wednesday Finish 4.3 Start 5.1 26


Download ppt "COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips 367 1."

Similar presentations


Ads by Google