Download presentation
Presentation is loading. Please wait.
Published byBryce Alexander Boyd Modified over 9 years ago
1
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes
2
Chapter 6 - More About Problem Domain Classes2 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
Chapter 6 - More About Problem Domain Classes3 Writing a Definition for the Slip Class Process –Write class header –Write attribute definition statements –Write a parameterized constructor Argument data types must be assignment compatible with parameter data types
4
Chapter 6 - More About Problem Domain Classes4
5
5 Writing a Definition for the Slip Class Process (cont.) –Write accessors to populate the attributes –Write a tellAboutSelf method Polymorphic method –Two methods with the same name residing in different classes that behave differently –See Figure 6-2, pp 172 –Write a tester class
6
Chapter 6 - More About Problem Domain Classes6 See Figure 6-4 for the Sequence diagram
7
Chapter 6 - More About Problem Domain Classes7
8
8 Writing Custom Methods Standard Methods –Written to store and retrieve values Accessor methods –getXXX(), setXXX() Custom Methods –Written to do some processing
9
Chapter 6 - More About Problem Domain Classes9 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
Chapter 6 - More About Problem Domain Classes10
11
Chapter 6 - More About Problem Domain Classes11 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
12
Chapter 6 - More About Problem Domain Classes12 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);
13
Chapter 6 - More About Problem Domain Classes13 Formatting Output NumberFormat & DecimalFormat Classes –DecimalFormat Class Member of java.text package Provides methods to format numerical data with commas and a decimal point
14
Chapter 6 - More About Problem Domain Classes14 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);
15
Chapter 6 - More About Problem Domain Classes15 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”
16
Chapter 6 - More About Problem Domain Classes16
17
Chapter 6 - More About Problem Domain Classes17
18
Chapter 6 - More About Problem Domain Classes18
19
Chapter 6 - More About Problem Domain Classes19 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 See Figure 6-11, pp. 185
20
Chapter 6 - More About Problem Domain Classes20
21
Chapter 6 - More About Problem Domain Classes21
22
Chapter 6 - More About Problem Domain Classes22 Overloading Methods Method Signature –Consists of: Method name Its parameter list –Java identifies a method by its signature Overloaded method –Methods within the same class having the same name but a different signature
23
Chapter 6 - More About Problem Domain Classes23 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
24
Chapter 6 - More About Problem Domain Classes24 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 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 See Figure 6-14, pp. 191
25
Chapter 6 - More About Problem Domain Classes25 overload constructor overload custom method Figure 6-14
26
Chapter 6 - More About Problem Domain Classes26
27
Chapter 6 - More About Problem Domain Classes27
28
Chapter 6 - More About Problem Domain Classes28 Working with Exceptions Exception –An object instance that notifies you of errors, problems, and other unusual conditions that may occur when your system is running –Keywords: try catch finally throw throws
29
Chapter 6 - More About Problem Domain Classes29
30
Chapter 6 - More About Problem Domain Classes30 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
31
Chapter 6 - More About Problem Domain Classes31 Working with Exceptions Data Validation –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 public void setWidth(int aWidth) throws Exception
32
Chapter 6 - More About Problem Domain Classes32 Working with Exceptions Catching Exceptions –If a method contains throws in its header, 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 } –See Figure 6-18, pp. 197 –See Figure 6-19, pp. 201
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.