1 / 32

Chapter 6 More About Problem Domain Classes

Chapter 6 More About Problem Domain Classes. 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.

tania
Download Presentation

Chapter 6 More About Problem Domain Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6More About Problem Domain Classes Chapter 6 - More About Problem Domain Classes

  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 Chapter 6 - More About Problem Domain Classes

  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 assignment compatible with parameter data types Chapter 6 - More About Problem Domain Classes

  4. Chapter 6 - More About Problem Domain Classes

  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 Chapter 6 - More About Problem Domain Classes

  6. See Figure 6-4 for the Sequence diagram Chapter 6 - More About Problem Domain Classes

  7. Chapter 6 - More About Problem Domain Classes

  8. Writing Custom Methods • Standard Methods • Written to store and retrieve values • Accessor methods • getXXX(), setXXX() • Custom Methods • Written to do some processing Chapter 6 - More About Problem Domain Classes

  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 Chapter 6 - More About Problem Domain Classes

  10. Chapter 6 - More About Problem Domain Classes

  11. 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 Chapter 6 - More About Problem Domain Classes

  12. 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); Chapter 6 - More About Problem Domain Classes

  13. Formatting Output • NumberFormat & DecimalFormat Classes • DecimalFormat Class • Member of java.text package • Provides methods to format numerical data with commas and a decimal point Chapter 6 - More About Problem Domain Classes

  14. 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); Chapter 6 - More About Problem Domain Classes

  15. 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” Chapter 6 - More About Problem Domain Classes

  16. Chapter 6 - More About Problem Domain Classes

  17. Chapter 6 - More About Problem Domain Classes

  18. Chapter 6 - More About Problem Domain Classes

  19. 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 Chapter 6 - More About Problem Domain Classes

  20. Chapter 6 - More About Problem Domain Classes

  21. Chapter 6 - More About Problem Domain Classes

  22. 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 Chapter 6 - More About Problem Domain Classes

  23. 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 Chapter 6 - More About Problem Domain Classes

  24. 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 Chapter 6 - More About Problem Domain Classes

  25. Figure 6-14 overload constructor overload custom method Chapter 6 - More About Problem Domain Classes

  26. Chapter 6 - More About Problem Domain Classes

  27. Chapter 6 - More About Problem Domain Classes

  28. 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 Chapter 6 - More About Problem Domain Classes

  29. Chapter 6 - More About Problem Domain Classes

  30. 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 Chapter 6 - More About Problem Domain Classes

  31. 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 Chapter 6 - More About Problem Domain Classes

  32. 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 Chapter 6 - More About Problem Domain Classes

More Related