Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Objectives
► Become
familiar with the class String
► Learn how to use input and output dialog boxes
in a program
► Learn how to tokenize the input stream
► Explore how to format the output of decimal
numbers with the class DecimalFormat
Object and Reference Variables
► Declare
a reference variable of a class type
► Use the operator new to:
Allocate memory space for data
Instantiate an object of that class type
► Store
the address of the object in a reference
variable
The Operator new
► Statement:
Integer num;
num = new Integer(78);
► Result:
Garbage Collection
► Change
value of num:
num = new Integer(50);
► Old memory space reclaimed
Using Predefined Classes and
Methods in a Program
► Many
predefined packages, classes, methods in
Java
► Library: Collection of packages
► Package: Contains several classes
► Class: Contains several methods
► Method: Set of instructions
Using Predefined Classes and
Methods in a Program
► To
use a method you must know:
Name of class containing method (Math)
Name of package containing class (java.lang)
Name of method (pow), its parameters (int a, int b), and
the function or purpose of the method (a^b)
int i=2 j=4, k;
k= Math.pow( i , j );
The value of k will be 2^4=16
Using Predefined Classes and Methods
in a Program
► Example
method call:
import java.io.*;
//imports package
OR
import java.io.BufferReader: //imports class
Math.pow(2,3); //calls power method in
//class Math or package java.lang
Package java.lang is automatically imported
► (Dot)
. Operator: used to access the method in the class
The class String
► String
variables are reference variables
► Given String name;
Equivalent Statements:
name = new String("Lisa Johnson");
name = “Lisa Johnson”;
Commonly Used String Methods
► String(String str)
► char
charAt(int index)
► int indexOf(char ch)
► int indexOf(String str, int pos)
► int compareTo(String str)
Commonly Used String Methods
► String
concat(String str)
► boolean equals(String str)
► int length()
► String replace(char ToBeReplaced, char
ReplacedWith)
► String toLowerCase()
► String toUpperCase()
What is tokenizing
► Consider
the following string
“This is a test of string tokenizing”
► A token
is a substring of this string that is selected as
follows:
A particular delimeter or separation character is chosen, in
this case choose whitespace (blank or tab)
Starting at the beginning of the string characters are added to
the substring until a delimeter is encountered
The token is the substring (the delimeter is not added to the
substring)
What is tokenizing
► The
next token is selected as follows:
The next character in the string that is not a delimeter is
located. This character is the first character of the next token.
Starting at this character, additional characters are added to
the substring until a delimeter is encountered
The token is the substring (the delimeter is not added to the
substring)
► For
this example the second token would be “is”
► The pattern continues until all characters in the string
have been processed.
Tokenizing a String
► class StringTokenizer
Contained in package java.util
Tokens usually delimited by whitespace characters
(space, tab, newline, etc)
Contains methods:
►public StringTokenizer(String str, String delimits)
►public int countTokens()
►public boolean hasMoreTokens()
►public String nextToken(String delimits)
StringTokenizer tokenizer;
String inputLine;
String name;
String number;
inputLine = “Judy 3250”;
tokenizer = new StringTokenizer(inputLine);
name = tokenizer.nextToken();
num = Integer.parseInt( tokenizer.nextToken() );
Using Dialog Boxes for Input/Output
► Use
a graphical user interface (GUI)
► class JOptionPane
Contained in package javax.swing
Contains methods: showInputDialog and showMessageDialog
► Syntax:
str = JOptionPane.showInputDialog(strExpression)
► Program must end with System.exit(0);
Parameters for the Method
showMessageDialog
JOptionPane Options for the
Parameter messageType
JOptionPane Example
Formatting the Output of Decimal
Numbers
► Type
float: defaults to 6
decimal places
► Type double: defaults to
15 decimal places
class Decimal Format
► Import package java.text
► Create
DecimalFormat object and initialize
► Use method format
► Example:
DecimalFormat twoDecimal =
new DecimalFormat("0.00");
twoDecimal.format(56.379);
► Result:
56.38