Presentation is loading. Please wait.

Presentation is loading. Please wait.

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan 24 2006

Similar presentations


Presentation on theme: "University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan 24 2006"— Presentation transcript:

1 University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan 24 2006 http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr based on slides by Paul Carter

2 Reading This Week n Chap 3

3 String firstname = "Alphonse"; char thirdchar = firstname.charAt(2); object method parameter Recap: Methods and Parameters n Methods are how objects are manipulated n pass information to methods with parameters n inputs to method call n tell charAt method which character in the String object we're interested in n methods can have multiple parameters n API specifies how many, and what type n two types of parameters n explicit parameters given between parens n implicit parameter is object itself

4 Recap: Return Values n Methods can have return values n Example: charAt method result n return value, the character 'n', is stored in thirdchar String firstname = "kangaroo"; char thirdchar = firstname.charAt(2); n Not all methods have return values n No return value indicated as void return value object method parameter

5 Recap: Constructors and Parameters n Many classes have more than one constructor, taking different parameters n use API docs to pick which one to use based on what initial data you have animal = new String(); animal = new String("kangaroo");

6 Recap: Keyboard Input n Want to type on keyboard and have Java program read in what we type n store it in variable to use later n Scanner class does the trick n java.util.Scanner n nicer than System.in, the analog of System.out

7 Recap: Importing Packages n Collections of related classes grouped into packages n tell Java which packages to keep track of with import statement n again, check API to find which package contains desired class n No need to import String, System.out because core java.lang packages automatically imported

8 import java.util.Scanner; public class Echo { public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text: "); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } Recap: Scanner Class Example n Print out the message on the display

9 Escape Characters n How can you make a String that has quotes? n String foo = “oh so cool”; n String bar = “oh so \”cool\”, more so”; n Escape character: backslash n general principle

10 Objectives n understand principles of abstraction and encapsulation n understand how to design new classes using these principles n understand how to implement new classes in Java

11 Creating Classes and Objects n So far you’ve seen how to use classes created by others n Now let’s think about how to create our own n Example: rolling dice n doesn’t exist already in Java API n we need to design n we need to implement n Start with two design principles

12 Abstraction n Abstraction: process whereby we n hide non-essential details n provide a view that is relevant n Often want different layers of abstraction depending on what is relevant

13 Encapsulation n Encapsulation: process whereby n inner workings made inaccessible to protect them and maintain their integrity n operations can be performed by user only through well-defined interface. n aka information hiding n Cell phone example n inner workings encapsulated in hand set n cell phone users can’t get at them n intuitive interface makes using them easy n without understanding how they actually work

14 Approach n Apply principles of abstraction and encapsulation to classes we design and implement n same idea as examples from daily life n only in software

15 Designing Die Class n Blueprint for constructing objects of type Die n Think of manufacturing airplanes n build one blueprint n manufacture many instances from it n Consider two viewpoints n client programmer: want to use Die object in a program n designer: creator of Die class

16 Client Programmer n What operations does client programmer need? n what methods should we create for Die ?

17 Designer n Decide on inner workings n implementation of class n Objects need state n attributes that distinguish one instance from another n many names for these n state variables n fields n attributes n data members n what fields should we create for Die ?

18 Information Hiding n Hide fields from client programmer n maintain their integrity n allow us flexibility to change them without affecting code written by client programmer n Parnas' Law: n "Only what is hidden can by changed without risk."

19 Public vs Private n public keyword indicates that something can be referenced from outside object n can be seen/used by client programmer n private keyword indicates that something cannot be referenced from outside object n cannot be seen/used by client programmer n Let’s fill in public/private for Die class

20 Public vs. Private Example Die myDie = new Die(); myDie. //not allowed!

21 Unified Modeling Language n Unified Modeling Language (UML) provides us with mechanism for modeling design of software n critical to separate design from implementation (code) n benefits of good software design n easy to understand, easy to maintain, easy to implement n What if skip design phase and start implementing (coding)? n code difficult to understand, thus difficult to debug n We’ll use UML class diagrams represent design of our classes n Once the design is completed, could be implemented in many different programming languages n Java, C++, Python,...

22 UML for Die n UML diagram representing Die class design

23 Encapsulation Diagram n Illustrate principle of encapsulation for Die A Die object client programmer

24 Implementing Die public class Die { }

25 Implementing RollDice public class RollDice { public static void main ( String [] args) { }


Download ppt "University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan 24 2006"

Similar presentations


Ads by Google