Title: Chapter 6 More About Problem Domain Classes Dr. James Jiang
1Chapter 6More About Problem Domain ClassesDr.
James Jiang
2Chapter 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
3Writing 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)
5Writing 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)
8Writing Custom Methods
- Standard Methods
- Written to store and retrieve values
- Accessor methods
- Custom Methods
- Written to do some processing
9Writing 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
10Slip class definition with leaseSlip method (I)
11Slip class definition with leaseSlip method (II)
12TesterTwo.java
13Formatting 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
14Formatting 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)
15Formatting Output
- NumberFormat DecimalFormat Classes
- DecimalFormat Class
- Member of java.text package
- Provides methods to format numerical data with
commas and a decimal point
16Formatting 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)
17Formatting 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)
19TesterThree.java
20TesterThree.java Output
21Using 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
22Slip class with Static method and variable (I)
23Slip class with Static method and variable (II)
24Slip class with Static method and variable (III)
25TesterFour.java
26Overloading 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
27Overloading 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
28Overloading 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
29Overloading 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
30Slip class with Overloaded methods (I)
31Slip class with Overloaded methods (II)
32TesterFive.java
33Working 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
34Client-Server exception handling
35Working 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
36Working 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
37Working 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
38Working 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
-
39Slip class with Data Validation (I)
40Slip class with Data Validation (II)
41Slip class with Data Validation (III)
42TesterSix.java
43TesterSix.java Output