Chapter 5 Part 2 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Chapter 5 Part 2

Description:

Discuss the idea of a class interface, and how we use the tools ... as a component in another, more complicated program (who is familiar with Wheel of Fortune? ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 15
Provided by: rosss8
Category:
Tags: chapter | fortune | of | part | wheel

less

Transcript and Presenter's Notes

Title: Chapter 5 Part 2


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 weve 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 youve 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 dont 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 didnt
    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 makes your programs easier for you to
understand, too!
  • 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.

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\DoubleO
ut.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.j
ava
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
Write a Comment
User Comments (0)
About PowerShow.com