1 / 18

An Introduction to Java Programming and Object-Oriented Application Development

This chapter provides a brief review of the char data type, including its methods in the Character class. It also covers the String class and its methods, as well as the StringBuilder and StringTokenizer classes. Additionally, it introduces the printf and format methods for formatting output.

lbatts
Download Presentation

An Introduction to Java Programming and Object-Oriented Application Development

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. An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting

  2. Brief Review of the char Data Type • Recall a char is one of eight _________ types • A character is any symbol represented by Unicode • A char is always expressed inside single quotes • Every individual character has an equivalent ________ value • The methods of the Character class manipulate characters An Introduction to Java Programming and Object-Oriented Application Development

  3. Brief Review of the char Data Type (continued) An Introduction to Java Programming and Object-Oriented Application Development

  4. Methods in the Character Class • The Character class is a __________ class • The Integer and Double classes are also wrapper classes • Each primitive type has a corresponding wrapper class • A wrapper class ‘wraps’ the value of a primitive type within an object so methods can be applied An Introduction to Java Programming and Object-Oriented Application Development

  5. Methods in the Character Class (continued) • charValue retrieves the character stored in the Character object • compareTo returns the difference in _________ values for two characters • equals is true if two characters are identical • isDigit is true if a character is in [0-9] • isLetter is true if a character is in [a-z or A-Z] An Introduction to Java Programming and Object-Oriented Application Development

  6. Methods in the Character Class (continued) • isWhiteSpace is true if the character is a space • isWhiteSpace returns false if the character is newline, tab, etc. • toLowerCase changes uppercase to lowercase • toUpperCase changes lowercase to uppercase An Introduction to Java Programming and Object-Oriented Application Development

  7. Brief Review of the String Class • Recall a string is an object • String myStr = “Johnson” • String myStr = new String( “Johnson” ) • Recall the methods equals, length and substring • A string is a __________ variable that contains the memory address of an array of characters • Two classes StringBuilder and StringTokenizer manipulate strings An Introduction to Java Programming and Object-Oriented Application Development

  8. Brief Review of the String Class (continued) • StringBuilder provides methods to manipulate and build strings • StringTokenizer breaks strings into _______ • A token is a group of characters within a string • Tokens are separated by ___________ • The tokens in this sentence are delimited by white space An Introduction to Java Programming and Object-Oriented Application Development

  9. The StringBuilder Class • A String object is ____________ • i.e., once created it can never be changed • StringBuilder allows strings to be modified • StringBuilder is in the java.lang package • All methods in StringBuilder are _________ (i.e., can be called only by objects, not the class) An Introduction to Java Programming and Object-Oriented Application Development

  10. The StringBuilder Class (continued) • append adds text to an existing StringBuilder object • insert inserts a second argument just after the position given by the first argument • deleteCharAt deletes a single character from a StringBuilder at a given position • replace replaces all occurrences of one character with another • toString creates a _______ that contains the data from the StringBuilder object An Introduction to Java Programming and Object-Oriented Application Development

  11. The StringTokenizer Class • StringTokenizer is in the _________ package • StringTokenizer breaks a string into tokens • Create the string to be tokenized • Create the StringTokenizer object linked to the string • Apply the StringTokenizer object to the string to get a token • nextToken and countTokens are nonstatic An Introduction to Java Programming and Object-Oriented Application Development

  12. The printf and format Methods • We have been using System.out.println and string concatenation ( + ) • Instead of print and println, you can use printf and format An Introduction to Java Programming and Object-Oriented Application Development

  13. The printf and format Methods (continued) An Introduction to Java Programming and Object-Oriented Application Development

  14. The printf and format Methods (continued) An Introduction to Java Programming and Object-Oriented Application Development

  15. The printf and format Methods (continued) • printf takes a format string and a string variable: printf(“Cost:$%.2f\n”,cost); • The format specifier begins with %, ends with a conversion character: %.2f • Conversion suffix characters may follow the conversion characters • Integers use conversion characters d and x • Floating points use conversion characters e, E, f, and g An Introduction to Java Programming and Object-Oriented Application Development

  16. The printf and format Methods (continued) • Characters and strings use the conversion characters C and S • Dates and times use the conversion character t, possibly with suffix characters C, A, B, d, Y • The argument index specifies the argument to which the format specifier applies • $1 is the first, $2 is the second,…, $n is the nth • The field width is the number of spaces an argument occupies • The precision is the number of decimal places An Introduction to Java Programming and Object-Oriented Application Development

  17. The format Method • The printf method displays to the command window • printf cannot be used to output to a GUI or file • The format method in the Formatter class can output to a GUI or file • format is identical to printf An Introduction to Java Programming and Object-Oriented Application Development

  18. The format Method (continued) An Introduction to Java Programming and Object-Oriented Application Development

More Related