Chapter 6 More About Problem Domain Classes Dr. James Jiang PowerPoint PPT Presentation

presentation player overlay
1 / 43
About This Presentation
Transcript and Presenter's Notes

Title: Chapter 6 More About Problem Domain Classes Dr. James Jiang


1
Chapter 6More About Problem Domain ClassesDr.
James Jiang
2
Chapter 6 Topics
  • More about writing problem domain classes
  • Writing and invoking custom methods
  • Formatting numerical data for display
  • Using static variables and methods
  • Writing overloaded methods
  • Working with exceptions

3
Writing a Definition for the Slip Class
  • Process
  • Write class header
  • Write attribute definition statements
  • Write a parameterized constructor
  • Argument data types must be compatible with
    parameter data types

4
(No Transcript)
5
Writing a Definition for the Slip Class
  • Process (cont.)
  • Write standard accessors to populate the
    attributes
  • Write a tellAboutSelf method
  • Polymorphic method
  • Two methods with the same name residing in
    different classes that behave differently
  • Write a tester class
  • Sequence diagram

6
(No Transcript)
7
(No Transcript)
8
Writing Custom Methods
  • Standard Methods
  • Written to store and retrieve values
  • Accessor methods
  • Custom Methods
  • Written to do some processing

9
Writing Custom Methods
  • Custom Methods
  • Process
  • Write method header
  • Public accessibility (if required)
  • Appropriate return type to match data type of
    value returned (if any)
  • Write code to implement required process
  • Write tester class to demonstrate proper
    operation

10
Slip class definition with leaseSlip method (I)
11
Slip class definition with leaseSlip method (II)
12
TesterTwo.java
13
Formatting Output
  • NumberFormat DecimalFormat Classes
  • NumberFormat Class
  • Member of java.text package
  • Provides methods to format numerical data as
    currency with commas, dollar signs, and decimal
    points
  • Also provides for formatting currency for various
    countries

14
Formatting Output
  • NumberFormat DecimalFormat Classes
  • NumberFormat Class
  • Two steps to format data
  • Invoke getCurrencyInstance method to obtain a
    NumberFormat instance
  • NumberFormat currencyFormat
  • NumberFormat.getCurrencyInstance()
  • Invoke the format method for the instance
    obtained
  • System.out.println(Currency
    currencyFormat.format(fee)

15
Formatting Output
  • NumberFormat DecimalFormat Classes
  • DecimalFormat Class
  • Member of java.text package
  • Provides methods to format numerical data with
    commas and a decimal point

16
Formatting Output
  • NumberFormat DecimalFormat Classes
  • DecimalFormat Class
  • Two steps to format data
  • Create an instance of DecimalFormat using the new
    operator and pass the format mask
  • DecimalFormat decimalFormat new
  • DecimalFormat(,0.00)
  • Invoke the format method for the instance
    obtained
  • System.out.println(Decimal
    decimalFormat.format(fee)

17
Formatting Output
  • Using Escape Sequences
  • Escape sequence
  • The backslash character (\) followed by the
    escape character
  • Used to display
  • Characters that do not appear on the keyboard
  • Characters that have special meanings within
    certain contexts
  • Example
  • Tab ? \t
  • Double quote in an output String ? the \real\
    deal

18
(No Transcript)
19
TesterThree.java
20
TesterThree.java Output
21
Using Static Variables and Methods
  • Instance Variables and Methods
  • A new instance receives its own copy of all
    instance variables and methods
  • Methods not actually copied - to avoid redundancy
  • Class Variables and Methods
  • A new instance shares a copy of all class
    variables and methods
  • Keyword
  • Static ? used to declare class variables and
    methods as static

22
Slip class with Static method and variable (I)
23
Slip class with Static method and variable (II)
24
Slip class with Static method and variable (III)
25
TesterFour.java
26
Overloading Methods
  • Method Signature
  • Consists of
  • Method name
  • Its parameter list
  • Java identifies a method by its name and
    signature
  • Overloaded method
  • Methods within the same class having the same
    name but a different signature

27
Overloading Methods
  • Overridden Methods (polymorphism)
  • A method with the same signature as an inherited
    method
  • Replaces inherited method
  • Polymorphic Method
  • A method in one class has the same signature as a
    method in another class

28
Overloading Methods
  • Overloading a Constructor
  • Multiple constructors with the same name and
    different signatures
  • Should have a constructor for each way it makes
    sense to instantiate objects of the class

29
Overloading Methods
  • Overloading a Custom Method
  • Any method can be overloaded
  • Should have a method for each way it makes sense
    to input data to perform the required process

30
Slip class with Overloaded methods (I)
31
Slip class with Overloaded methods (II)
32
TesterFive.java
33
Working with Exceptions
  • Exception
  • An object instance that notifies of errors,
    problems, and other unusual conditions that may
    occur when the system is running
  • Keywords
  • try
  • catch
  • finally
  • throw
  • throws

34
Client-Server exception handling
35
Working with Exceptions
  • Exception Process
  • When a client invokes a method that may create
    and throw an exception, the invoking code must be
    placed in a try block
  • Server method indicates it may throw an exception
    by including throws keyword in header
  • If exception is detected, server sends exception
    instance to invoking client using throw keyword
  • Client catches exception in a catch block
  • finally block executes regardless of whether an
    exception is caught

36
Working with Exceptions
  • Data Validation for slipId
  • If a method is to create and throw an exception,
    its header must contain the throws keyword
    followed by the exception class it throws
  • public void setSlipId(int anId) throws Exception

37
Working with Exceptions
  • Data Validation for Width
  • If a method is to create and throw an exception,
    its header must contain the throws keyword
    followed by the exception class it throws
  • public void setWidth(int aWidth) throws Exception

38
Working with Exceptions
  • Catching Exceptions
  • the invoking code must be prepared to catch the
    exception
  • Otherwise JVM will terminate processing
  • try
  • method that throws exception
  • catch (Exception e)
  • code that handles it

39
Slip class with Data Validation (I)
40
Slip class with Data Validation (II)
41
Slip class with Data Validation (III)
42
TesterSix.java
43
TesterSix.java Output
Write a Comment
User Comments (0)
About PowerShow.com