Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Part 2 COSI 11a 2006-11-08 Prepared by Ross Shaull.

Similar presentations


Presentation on theme: "Chapter 5 Part 2 COSI 11a 2006-11-08 Prepared by Ross Shaull."— Presentation transcript:

1 Chapter 5 Part 2 COSI 11a 2006-11-08 Prepared by Ross Shaull

2 Objectives for Today Review encapsulation. Discuss the idea of a class interface, and how we use the tools we have learned to create a useful one. Learn about packages. Write more code in GuessWord.java.

3 Encapsulation We sometimes call it information hiding. Who can give me some intuitions or definitions for encapsulation? Can the tools we’ve learned in chapters 4 and 5 help us create a well encapsulated class? (yes!)

4 Friendly Interface Put yourselves in the shoes of another programmer who is going to use the class you’ve written. You want to give this programmer an interface to your class that is: –Safe –Easy to understand –Easy to use

5 Safe Constructors that either… –Provide parameters to set necessary values corresponding to internal state (i.e. instance variables); or, –Set reasonable defaults for internal state not provided as argument to constructor. Private variables and final variables –How do these improve safety?

6 Easy to Understand Overloaded methods reduce the number of method names that programmers have to remember. Proper return values and parameters make it clear what a method does. Good comments that can be extracted using javadoc.

7 Easy to Use Reduce keystrokes! Static methods help because you don’t have to instantiate an object to use them.

8 Why? Programs are most useful when they can be used multiple times, perhaps in contexts you didn’t even think of. For example, the GuessGame class that we are writing might be useful as a component in another, more complicated program (who is familiar with Wheel of Fortune?).

9 Why? This idea of reusing code is part of the core philosophy behind Object Oriented design (remember that term from the first lecture?). When you keep these principles in mind, your classes become more useful for other programmers. This makes your programs easier for you to understand, too!

10 Packages Get organized! Keep code in multiple directories. Resolve name clashes. Works hand-in-hand with good OO design to make your classes easier to use for other programmers (and you). Page 384.

11 Package Declaration package general.utilities; public class DoubleOut { // stuff here… } This file must be stored in a directory “ general/utilities ” general/utilities can be put in a variety of library paths! Remember the CLASSPATH variable?

12 Package Example (Windows) C:\myjavastuff\libraries\general\utilities\DoubleOut.java CLASSPATH = “C:\myjavastuff\libraries;.” import general.utilities.DoubleOut; public class DoubleOutTest { // … use DoubleOut in here } The “;.” is very important!

13 Package Example (OS X) /Users/ross/java/lib/general/utilities/DoubleOut.java CLASSPATH = “/Users/ross/java/lib/;.” import general.utilities.DoubleOut; public class DoubleOutTest { // … use DoubleOut in here } The “;.” is here again!

14 Summary (Ask Questions!) Invoking methods Method parameters Static methods Constructors Information Leakage Testing techniques Packages


Download ppt "Chapter 5 Part 2 COSI 11a 2006-11-08 Prepared by Ross Shaull."

Similar presentations


Ads by Google