Presentation is loading. Please wait.

Presentation is loading. Please wait.

XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.

Similar presentations


Presentation on theme: "XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more."— Presentation transcript:

1 XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more serious for the class! Understand lecture AT CLASS Practice at home (do homework) and at lab sessions. Self study for term project  Term project teams  Project topics Game Algorithm animation  Project proposal Next Week !

2 Programming Fundamentals I Java Programming Spring 2007 XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 10

3 Encapsulation 1. Overview 2. Encapsulation 3. Information Hiding 4. Composition 5. Singleton

4 XuanTung Hoang 4 What’s next after midterm … ? We have learned almost every basics of Java  We can write java programs solve almost every computing problem. We will learn HOW TO ORGANIZE your program in a clever way.  For understanding  For reusability

5 XuanTung Hoang 5 Specifically, what will we learn? Writing Java program …  Creating class: Rules Tricks And patterns  If you follow (or use) those rules/tricks/patterns: You will write very good Java code!!! You will be able to USE classes provided by others effectively  Using class: Java Collection Framework Java I/O Java GUI Java Multithreading ….

6 XuanTung Hoang 6 Rule 1: Encapsulation Group related information into a class  Use instance variables  Provide related actions to process those variables Group related information together

7 XuanTung Hoang 7 Rule 1.1: Information Hiding Instance variable should be private Standardizing:  the way to change instance variables (with set methods)  the way to represent instance variables with get methods with predicate methods Expose necessary methods with public modifier 1.Don’t allow instance variables to be changed arbitrarily 2.Only expose necessary methods

8 XuanTung Hoang 8 Trick 1.2: Composition Bottom up design ! Example:  Define six classes Input, Output, Memory, CPU, ALU, Storage  Make Computer class that contains the above 6 classes You can combine classes to define another class

9 XuanTung Hoang 9 Case Study: Time Class (1) Validate parameter values before setting instance variables format strings, check JavaAPI manual for details private instance variables

10 XuanTung Hoang 10 Case Study: Time Class (2) Create a Time1 object Call setTime method Call setTime method with invalid values Call toString method Call toUniversalString method

11 XuanTung Hoang 11 Notes from the Case Study … hour, minute, second are private. They cannot be accessed/modified from other classes  Access/modify via set and get methods (e.g. setTime method)

12 XuanTung Hoang 12 Making hour, minute, second public is allowed, but it isn ’ t a good idea …  Hour, minute, second should store valid values. If they can be set freely, their values are very likely invalid. If hour, minute, second are private with appropriate set metho ds we can make sure that their values are always valid  appropriate set methods: Integrate validity checking in set method Other classes (clients) just call the set method without worries about invalidity. Complex detailed implementation related to private fields are hidden (information hiding)?

13 XuanTung Hoang 13 set, get, and predicate methods Data/information hiding is a property of class instance Typical classes has set, get, and predicate methods  set methods (mutators): validate and set the new values to instance  variables  get methods (accessors): obtain values of instance variables and control the format of the returned values E.g.: toUniversalString() can be considered as an get method  Predicate methods: test whether a certain condition on the object is true or false E.g.: we can add to class Time1 method is21Century() that tests whether the date is in the 21 st century or not. Great feature of OOP

14 XuanTung Hoang 14 Data abstraction and ADTs (1) Thanks to information hiding, clients care only what functionalities a class offers and don’t concern with how those functionalities are implemented. This concept is called data abstraction Data is not just the information like in common sense, data is the information and operations that can be performed on the information  Abstract Data Types (ADTs) ADTs = Data representation + Operations  In ADTs, data representation can be hidden

15 XuanTung Hoang 15 Data abstraction and ADTs (2) ADT Is a term that can refer to any type in current programming languages Java:  ADTs can be primitive types. E.g.: int (data representa-tion is int eger number; operations are arithmetic operations)  ADTs can be classes. E.g. Time1 class, Employee class Read section 8.15, text book

16 XuanTung Hoang 16 Pattern 1.3: Singleton In an application, if you have ONLY ONE instance of a class, use Singleton pattern. Singleton = Universal public Instance

17 XuanTung Hoang 17 Singleton pattern In a singleton class:  1 private static instance variable of singleton class (sInstance) Initialize it to null  1 public static method to: create object of Singleton class ONLY ONE  Guarantee ONLY ONE object creation by checking sInstance and return the singleton instance

18 XuanTung Hoang 18 Singleton Pattern

19 XuanTung Hoang 19 Examples of Singleton Universal Random number generator Universal Sequence number generator In CardGame project


Download ppt "XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more."

Similar presentations


Ads by Google