Chapter 9 - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 9

Description:

A class represents a single concept from the problem domain ... a pay check is more useful than only designing a class that seems to compute a pay ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 64
Provided by: david603
Category:
Tags: apay | chapter

less

Transcript and Presenter's Notes

Title: Chapter 9


1
Chapter 9 Designing Classes
2
Goals
  • Learning to design classes
  • Preconditions and postconditions for methods
  • More details about static/class methods and
    fields
  • Creating packages

3
Choosing Classes
  • Division of labor
  • Methods functions
  • Classes/objects entities
  • A class represents a single concept from the
    problem domain
  • Name for a class should be a noun that describes
    a concept

4
Good Classes
  • Concepts from mathematics
  • PointRectangleEllipse
  • Path
  • Concepts from real life
  • BankAccountCashRegister
  • Bus
  • Taxi

5
Good Classes
  • Actors (end in -er, -or) objects that do some
    kind of work for you
  • ScannerRandom // better name
    RandomNumberGenerator
  • Utility classes no objects, only static methods
    and constants
  • Math
  • Degenerate case of a class only main() method

6
Bad Classes
  • Don't turn actions into classesPaycheck is
    better name than ComputePaycheck
  • The name can tip you off when you are on wrong
    track
  • Representing a pay check is more useful than only
    designing a class that seems to compute a pay
    check

7
OOP Class Diagram
The name of the class appears at the top
ClassName
fields
-private data public data
  • -private constructors
  • public constructors
  • -private methods
  • public methods

constructors and methods
8
Practice Questionclass Die
  • What are the operations needed to use the die?
  • What data are needed to represent the concept of
    a die?

9
class Die
Die
-sidesint -topint
roll()void getTop()int
10
Design Questions
  • How might I design a soccer game?

11
Cohesion
  • A class should represent a single concept
  • The public interface of a class is cohesive if
    all of its features are related to the concept
    that the class represents
  • methods and public constants/variables should all
    relate to the central idea of the class

12
Cohesion
  • This class lacks cohesion
  • public class CashRegister
  • public void enterPayment(int dollars, int
    quarters, int dimes, int nickels, int pennies)
  • . . .
  • public static final double NICKEL_VALUE
    0.05public static final double DIME_VALUE
    0.1public static final double QUARTER_VALUE
    0.25. . .

13
Cohesion
  • What is the problem with this class?
  • More confusion
  • Ties the role of a cash register to the value of
    the coins
  • What if we wanted to sell this cash register to
    other countries?

14
Solution
  • CashRegister, as described above, involves two
    concepts cash register and coin
  • Solution Make two classes
  • public class Coinpublic Coin(double aValue,
    String aName) . . . public double getValue()
    . . . . . .
  • public class CashRegisterpublic void
    enterPayment(int coinCount, Coin coinType) . .
    . . . .

15
Coupling
  • A class depends on another if it uses objects of
    that class
  • CashRegister depends on Coin to determine the
    value of the payment
  • Coin does not depend on CashRegister
  • High coupling many class dependencies

16
Coupling
  • Minimize coupling to minimize the impact of
    interface changes
  • To visualize relationships draw class diagrams
  • UML Unified Modeling Language. Notation for
    object-oriented analysis and design

17
(No Transcript)
18
(No Transcript)
19
Questions
  • How can we modify the BankAccount class to
    maintain information about the customer?
  • How might we minimize coupling with the Soccer
    game example?

20
Immutable Classes
  • Recall that accessor methods only retrieve
    information
  • They do not change the state of the object
  • Calling them multiple times in a row will yield
    same results
  • Immutable classes are classes that only have
    accessor methods

21
Immutable Classes
  • Example String Class
  • Advantages
  • Do not have to worry about the dangers of handing
    off references in methods
  • Disadvantages
  • Have to create a new object whenever you want the
    data to look different

22
Mutable Classes
  • Most classes are mutable they do have mutator
    methods
  • Bus, Taxi, Path, BankAccount are all mutable
  • StringBuffer can be used if you want to store
    strings that you can mutate

23
Side Effects
  • Mutators are designed to change the internal data
    of the implicit parameter
  • Side effect of a method modifying externally
    observable data (explicit parameters)
  • public void transfer(double amount, BankAccount
    other)
  • balance balance - amountother.balance
    other.balance amount
  • // Modifies explicit parameter

24
Side Effects
  • Updating an explicit parameter can be surprising
    to programmers, and it is best to avoid it if
    possible

25
Other Side Effects
  • Why dont we add a printBalance() method to
    BankAccount?
  • public void printBalance() // Not recommended
    System.out.println("The balance is now "
    balance)

26
Other Side Effects
  • Makes potentially dangerous assumptions
  • The BankAccount class will only be used in
    English speaking country
  • The machine using the program has a System.out
    association
  • An instance of too much coupling

27
Minimizing Side Effects
  • Never modify explicit parameters to a method
  • Treat them as constants
  • public void deposit(double amount)
  • balance amount balance

28
Passing Parameters
  • Two ways to pass parameters
  • Pass by reference the memory location is sent,
    meaning that the data can be changed
  • Pass by value a copy of the memory location is
    created
  • Java only uses pass by value
  • Even objects are passed by value, since the
    reference is copied

29
Example
  • public class BankAccount
  • public void trasnfer(double amount, BankAccount
    other)
  • balance balance amount
  • double newBalance other.balance amount
  • other new BankAccount(newBalance)
  • Does this work? Why or why not?

30
(No Transcript)
31
Preconditions
  • Precondition Requirement that the caller of a
    method must meet
  • Publish preconditions so the caller won't call
    methods with bad parameters
  • / Deposits money into this account. _at_param
    amount the amount of money to deposit (Preconditi
    on amount gt 0)/

32
Preconditions
  • Typical use
  • To restrict the parameters of a method
  • To require that a method is only called when the
    object is in an appropriate state
  • If precondition is violated, the method is not
    responsible for computing the correct result. It
    is free to do anything.

33
Example
  • What preconditions are needed for
    dropOffPassenger() in the Taxi class?
  • What a is precondition to next() in Scanner?

34
Handling Violations
  • Method does not have to behave correctly when
    preconditions are violated
  • But what should be done?
  • Assume data is correct. Only a
    corruption/crashes to happen at fault of user
  • Throw an exception if a condition is violated.
    The control is shifted to an exception handler
  • Runtime error messages are all exceptions

35
Handling Violations
  • Both have their disadvantages
  • Exceptions slow down the program
  • Doing nothing can be very dangerous
  • Best of both worlds assertions that can be
    turned on or off

36
Assertions
  • Method can do an assertion check
  • assert amount gt 0balance balance amount
  • To enable assertion checking
  • java -enableassertions MyProg
  • You can turn assertions off after you have tested
    your program, so that it runs at maximum speed

37
Common Error
  • Returning silently on an error is hard to detect
    and debug
  • if (amount lt 0) return
  • // Not recommended hard to debug
  • balance balance amount

38
Postconditions
  • Postcondition a condition that is true after a
    method has completed. This is the designer's
    duties (recall preconditions are the user's duty)
  • If method call is in accordance with
    preconditions, it must ensure that postconditions
    are valid

39
Postconditions
  • There are two kinds of postconditions
  • The return value is computed correctly
  • The object is in a certain state after the method
    call is completed (mutators)
  • /Deposits money into this account.(Postconditi
    on getBalance() gt 0)_at_param amount the amount
    of money to deposit(Precondition amount gt 0)
  • /

40
Postconditions
  • Don't document trivial postconditions that repeat
    the _at_return clause
  • Formulate pre- and postconditions only in terms
    of the interface of the class
  • amount lt getBalance()
  • // this is the way to state a
    postconditionamount lt balance
  • // wrong postcondition formulation

41
Contract
  • Think of pre- and postconditions as a contract
  • If caller fulfills the precondition, the method
    must fulfill the postcondition.

42
Static (Class) Fields
  • Stores a field outside of any particular instance
    (object)
  • Only one copy of a field that is shared by all
    instances of a class
  • Usually minimize the use of static for
    object-oriented programs

43
Static (Class) Methods
  • Can be invoked without creating an instance.
  • Can only access explicit parameters, class
    fields, class methods and constructors.
  • main method must be static

44
Field Modifier Choices
  • For each field
  • Choose identifier and data type.
  • Is it a constant?
  • If so, add final modifier
  • Does it require a different value for each
    instance?
  • If not, add static modifier
  • Is it safe to be directly accessible to methods
    in other classes?
  • If so, public
  • Else private
  • Is the static value known?
  • If so, initialize to that value

45
Method Modifier Choices
  • For each method
  • Choose identifier, return type, and parameters
  • Does it access any instance (non-static) fields
    or instance methods?
  • If not, add static modifier
  • Must it be called from methods in other classes?
  • If so, must be public
  • Else make it private
  • Is it safe to be overridden? (inheritance)
  • If not, add final modifier

46
Static (Class) Fields
  • Why make fields static?
  • so that only one copy of the valueis stored (all
    instances share the same copy)

47
Static Methods
  • Why make methods static?
  • So that the method is accessible without creating
    an instance
  • Note We can only do this if the method only
    requires explicit parameters or static fields.

48
Static Methods
  • Why write a method that does not operate on an
    object?
  • Common reason we're encapsulating some
    computation that involves only numbers. Numbers
    aren't objects, you can't invoke methods on them.
    E.g., x.sqrt() is not legal in Java

49
Static Methods Example
  • public class Financial public static double
    percentOf(double p, double a) return (p /
    100) a // More financial methods can be
    added here.

50
Static Methods Example
  • Call with class name instead of objectdouble
    tax Financial.percentOf(taxRate, total)
  • main is static there aren't any objects yet

51
Static Fields
  • Three ways to initialize
  • Do nothing. Field is 0 (for numbers), false (for
    boolean values), or null (for objects)
  • Use an explicit initializer, such as
  • public class BankAccount . . . private static
    int lastAssignedNumber 1000 // Executed once,
    when class is loaded
  • Use a static initialization block

52
(No Transcript)
53
Scope
  • Scope of a variable Region of program in which
    the variable can be accessed
  • Scope of a local variable extends from its
    declaration to end of the block that encloses it
  • Sometimes the same variable name is used in two
    methods.

54
Scope Example
  • public class RectangleTester public static
    double area(Rectangle rect) double r
    rect.getWidth() rect.getHeight() return
    r public static void main(String
    args) Rectangle r new Rectangle(5, 10, 20,
    30) double a area(r) System.out.println
    (r)

55
Scope
  • The scope of a local variable cannot contain the
    definition of another variable with the same name
  • Rectangle r new Rectangle(5, 10, 20, 30)if
    (x gt 0) double r Math.sqrt(x) //
    Errorcan't declare another variable //called r
    here . . .

56
Scope
  • However, we can have local variables with
    identical names if scopes do not overlap
  • if (x gt 0) double r Math.sqrt(x) . . .
    // Scope of r ends hereelse Rectangle r new
    Rectangle(5, 10, 20, 30) // OKit is legal to
    declare another r here . . .

57
Scope
  • private members have class scope You can access
    all members in any method of the class
  • Must qualify public members outside scope
  • Math.PIharrysChecking.BANK_FEE

58
Scope
  • Inside a method, there is no need to qualify
    fields or methods that belong to the same class
  • An unqualified instance field or method name
    automatically refers to the current object (this)
  • public class BankAccount
  • public void transfer(double amount,
    BankAccount other) withdraw(amount) //
    this.withdraw(amount) other.deposit(amount).
    . .

59
Scope
  • A local variable can shadow a field with the same
    name
  • Local scope wins over class scopepublic class
    Coin . . . public double getExchangeValue(doub
    le exchangeRate) double value // Local
    variable . . . return value private
    String name private double value // Field with
    the same // name

60
Scope
  • Access shadowed fields by qualifying them with
    the this reference
  • value this.value exchangeRate

61
Packages
  • A package is a set of related classes
  • To put classes in a package, you must place a
    line
  • package ltpackageNamegt
  • as the first instruction in the source file
    containing the classes
  • The package name consists of one or more
    identifiers separated by periods

62
Packages
  • For example, to put the Financial class
    introduced into a package named
    com.horstmann.bigjava, the Financial.java file
    must start as follows
  • package com.horstmann.bigjavapublic class
    Financial. . .

63
Packages Default
  • The default package has no name and thus no
    package statement
Write a Comment
User Comments (0)
About PowerShow.com