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

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 10.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Fundamental Programming Structures in Java: Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data types and variables
JavaScript, Third Edition
ASP.NET Programming with C# and SQL Server First Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Objectives You should be able to describe: Data Types
Chapter 12: Adding Functionality to Your Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Chapter 7: Characters, Strings, and the StringBuilder.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
28 Formatted Output.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Objectives You should be able to describe: Interactive Keyboard Input
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Topics discussed in this section:
Lecture 07 String Jaeki Song.
CS2011 Introduction to Programming I Strings
Chapter 10 Thinking in Objects Part 2
Unit-2 Objects and Classes
Presentation transcript:

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

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

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

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

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;

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

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

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

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

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

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

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

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

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()

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

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

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

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)

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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