Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Similar presentations


Presentation on theme: "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."— Presentation transcript:

1 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 2 Objectives You should be able to describe: The String Class String Processing The StringBuffer Class Program Design and Development: Program Performance Measures and Object-Oriented Technology (OOT) Common Programming Errors

3 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition3 The String Class String literal –Sequence of characters enclosed in double quotation marks String value created as object of either class: –String or –StringBuffer

4 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition4 The String Class (continued) Main difference between two classes: –Strings created as String objects cannot be modified while StringBuffer objects can be modified

5 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition5 Creating a String Syntax: String identifier = new String(string-value); String identifier; identifier = new String(string-value); String identifier = string-value;

6 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition6 Creating a String (continued) Storage for string –Created in memory when string is created –Location of where string is stored is placed in variable Called a reference variable

7 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition7 Creating a String (continued) When data value, such as a string, is created from a class: 1.Variable declared 2.Object of correct data type created 3.Location of object created in step 2 stored in reference variable declared in step 1

8 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition8 Constructors Instantiating an object –Using new operator to create object Name of constructor method must be same name as class String class provides nine different constructors for creating String objects

9 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition9 Constructors (continued) Table 7.1: String Constructors

10 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition10 String Input and Output Output: –print() –println() Input: –read()

11 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition11 String Input and Output (continued) Table 7.2: Basic String Input and Output Methods

12 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition12 Formatting Strings Using printf Method of PrintStream class Width and precision modifiers used affect what is printed The precision specifier –Determines maximum number of characters displayed Minus sign must be used with width specifier

13 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition13 String Processing Manipulated using either: –Standard String class methods –Character-at-a-time methods provided by Character class

14 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition14 Caution Never use equality operator, == –Compares values stored in reference variables used to access strings Use: –compareTo() –equals()

15 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition15 Other String Methods charAt() method –Permits you to retrieve individual characters in string –Last position is always one less than number of characters in string

16 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition16 Character Methods Table 7.4: Character Class General-Purpose Methods

17 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition17 Conversion Methods Convert strings to and from primitive numerical data types General purpose Typically invoked by preceding method name with String class name and period

18 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition18 Conversion Methods (continued) Table 7.5: Primitive Types to String Conversion Methods (All Methods Are Members of the String Class)

19 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition19 Conversion Methods (continued) Table 7.6: String to Primitive Type Conversion Methods (All Methods Are Members of the String Class)

20 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition20 The StringBuffer Class Implemented as mutable sequence of characters –Can be modified Does not provide set of methods for: –Comparing strings –Locating characters and substrings within a string Created and stored in buffer having set character capacity

21 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition21 The StringBuffer Class (continued) String class –Preferred class for displaying or comparing strings that tend to remain constant StringBuffer class –Should be used for applications that require: Individual character additions Modifications Multiple text edits

22 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition22 The StringBuffer Class (continued) Table 7.7: StringBuffer Constructor Methods

23 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition23 The StringBuffer Class (continued) Figure 7.7: The initial storage of a StringBuffer object

24 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition24 The StringBuffer Class (continued) Figure 7.8: The string after the insertion

25 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition25 Program Design and Development: Program Performance Measures and Object-Oriented Technology (OOT) Implement: –“To put into effect according to a definite plan” –Plan was designed during design phase

26 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition26 Program Design and Development: Program Performance Measures and Object-Oriented Technology (OOT) (continued) Figure 7.11: The programming process

27 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition27 Program Design and Development: Program Performance Measures and Object-Oriented Technology (OOT) (continued) Figure 7.12: The programming learning sequence

28 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition28 Program Design and Development: Program Performance Measures and Object-Oriented Technology (OOT) (continued) Understand what constitutes good implementation Good program should provide: – Clarity – Efficiency – Robustness – Extensibility – Reusability – Programming-in-the-large

29 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition29 Program Design and Development: Program Performance Measures and Object-Oriented Technology (OOT) (continued) Procedure-oriented programming: –Has capability of producing clear, efficient, robust programs that can be programmed in large –Does not produce reusable and extensible programs –Very cumbersome and extremely time-consuming and costly to extend or reuse in new applications Object-oriented technology (OOT): –Produces both extendable and reusable code

30 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition30 Moving to OOT True OOT: –One works with objects from problem definition stage through programming stage –Encompasses: OOR: object-oriented requirements OOA: object-oriented analysis OOD: object-oriented design OOP: object-oriented programming

31 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition31 Moving to OOT (continued) Figure 7.13: The object-oriented technology (OOT) programming process

32 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition32 Moving to OOT (continued) Figure 7.14: The object-oriented technology (OOT) learning sequence

33 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition33 Moving to OOT (continued) Figure 7.14: The introduction of OOT by one development group at AT&T

34 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition34 Common Programming Errors Breaking a string across two or more lines Not remembering that first character in string located at position 0, not 1 Not remembering that number returned by length() method is one more than position number of string’s last character Using == operator to compare two strings –Rather than compareTo() or equals() methods

35 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition35 Common Programming Errors (continued) Not specifying one position beyond desired character to be extracted using substring() method

36 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition36 Summary Strings constructed from String class are immutable String class –More commonly used for constructing strings for input and output purposes StringBuffer class –Used when characters within string need to be replaced, inserted, or deleted on regular basis

37 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition37 Summary (continued) Strings can be manipulated using either: –Methods of class they are objects of –Using general-purpose string and character methods


Download ppt "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."

Similar presentations


Ads by Google