ICS11 Introduction to Computer Programming II - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

ICS11 Introduction to Computer Programming II

Description:

ICS11 Introduction to Computer Programming II. Joselito dela Cruz Ty. Java sa Eskwela (JsE) Program. Xavier University ... String name='Snuffleupagus' ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 18
Provided by: jayrobertc
Category:

less

Transcript and Presenter's Notes

Title: ICS11 Introduction to Computer Programming II


1
Xavier University Computer Center Sun
Microsystems, Philippines
Java sa Eskwela (JsE) Program
  • ICS11 Introduction to Computer Programming II
  • Joselito dela Cruz Ty

2
Chapter 10
Strings and Characters
3
Outline
4
chars and Characters
  • In Java there are two types of variables
  • Objects
  • e.g. Point, JApplet, String
  • Native data types
  • e.g. int, float, double, long, byte, char
  • char is a native data type it is a number
    between 0-255

5
chars and Characters
  • Examples of char
  • a
  • b
  • c
  • Etc
  • Every char can be represented as an integer from
    1 to 255
  • See Appendix D ASCII Character Set

6
chars and Characters
  • Exercise1 Create a program that outputs the
    letters a-z in the Standard output
    (System.out.println)
  • Exercise2 Create a program that creates 26
    buttons labeled with the letters A-Z
  • Exercise3 Add a textfield in your previous
    program whenever you click a letter, the letter
    gets added into the textfield

7
chars and Characters
  • Because char is a native data type, it is not an
    Object
  • It does not define methods to perform operations
    on it
  • To perform operations on a char, use the methods
    define in class Character

8
Characters
  • Methods of class Character (p489)
  • isDefined()
  • isDigit()
  • isLetter()
  • isLetterOrDigit()
  • isLowerCase()
  • isUpperCase()
  • toUpperCase()
  • toLowerCase()

9
Characters
  • Exercise Add a KeyboardListener to your previous
    program so that the user can use the keyboard to
    press the letters in the frame

10
Strings
  • A String in Java is not a native data type it is
    an Object
  • Other programming languages represent a String as
    an array of chars
  • Samples of Strings
  • hello
  • Happy New Year!
  • Etc
  • Therefore, Strings have instance variables,
    methods, and constructors

11
Strings
  • A String is usually created this way
  • String nameSnuffleupagus
  • A String can also be created using its
    constructors (see pp.459-461)

12
String Methods
  • length()
  • charAt()
  • getChars()
  • equals()
  • equalsIgnoreCase()
  • regionMatches()
  • indexOf()
  • lastIndexOf()
  • substring()
  • replace()
  • toLowerCase()
  • toUpperCase()
  • trim()
  • valueOf()

13
String Methods
  • Exercise from the code below, output separately
    the first name and the last names
  • String nameSantos, Carlo, dela Cruz,
    Jose, Concepcion, Miguel Fernando
  • String nameSantos, Carlo dela Cruz, Jose
    Concepcion, Miguel Fernando

14
Type Conversion
  • Any native data type and any object can be
    converted to String by using the method
    .toString()
  • What is the code to convert from each of the
    following data types
  • int
  • char
  • String

15
StringBuffer
  • A limitation of a String object is that it can
    not be changed once it is created
  • A StringBuffer allows dynamic changing of String
    information

16
StringBuffer
  • Methods of class StringBuffer
  • charAt()
  • setCharAt()
  • deleteCharAt()
  • delete()
  • getChars()
  • reverse()
  • append()
  • insert()

17
StringTokenizer
  • Breaks down String elements into tokens or
    words
  • Methods
  • countTokens()
  • hasMoreTokens()
  • nextToken()
Write a Comment
User Comments (0)
About PowerShow.com