Objectives ► Become familiar with the class String ► ► Learn how to use input and output...
-
Upload
darleen-dulcie-caldwell -
Category
Documents
-
view
219 -
download
0
Transcript of Objectives ► Become familiar with the class String ► ► Learn how to use input and output...
ObjectivesObjectives
►Become familiar with the class StringBecome 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 Explore how to format the output of decimal
numbers with the class DecimalFormatnumbers with the class DecimalFormat
Object and Reference VariablesObject and Reference Variables
►Declare a reference variable of a class typeDeclare a reference variable of a class type►Use the operator new to:Use the operator new to:
Allocate memory space for dataAllocate memory space for data Instantiate an object of that class typeInstantiate an object of that class type
►Store the address of the object in a reference Store the address of the object in a reference variablevariable
The Operator newThe Operator new
► Statement: Statement:
Integer num;Integer num;
num = new Integer(78);num = new Integer(78);► Result:Result:
Garbage CollectionGarbage Collection
►Change value of num:Change value of num:
num = new Integer(50);num = new Integer(50);►Old memory space reclaimedOld memory space reclaimed
Using Predefined Classes and Using Predefined Classes and Methods in a ProgramMethods in a Program
►Many predefined packages, classes, methods in Many predefined packages, classes, methods in Java Java
►Library: Collection of packagesLibrary: Collection of packages►Package: Contains several classesPackage: Contains several classes►Class: Contains several methodsClass: Contains several methods►Method: Set of instructionsMethod: Set of instructions
Using Predefined Classes and Using Predefined Classes and Methods in a ProgramMethods in a Program
►To use a method you must know:To use a method you must know: Name of class containing method (Math)Name of class containing method (Math) Name of package containing class (java.lang)Name of package containing class (java.lang) Name of method (pow), its parameters (int a, int b), and Name of method (pow), its parameters (int a, int b), and
the function or purpose of the method (a^b)the function or purpose of the method (a^b)
int i=2 j=4, k;int i=2 j=4, k;k= Math.pow( i , j );k= Math.pow( i , j );
The value of k will be 2^4=16The value of k will be 2^4=16
Using Predefined Classes and Methods Using Predefined Classes and Methods in a Programin a Program
► Example method call:Example method call:import java.io.*; import java.io.*; //imports package//imports packageORORimport java.io.BufferReader: //imports class import java.io.BufferReader: //imports class
Math.pow(2,3); //calls power method in Math.pow(2,3); //calls power method in //class Math or package //class Math or package
java.langjava.langPackage java.lang is automatically importedPackage java.lang is automatically imported
► (Dot) . Operator: used to access the method in the class(Dot) . Operator: used to access the method in the class
The class StringThe class String
►String variables are reference variablesString variables are reference variables►Given String name; Given String name;
Equivalent Statements:Equivalent Statements:
name = new String("Lisa Johnson");name = new String("Lisa Johnson");
name = “Lisa Johnson”;name = “Lisa Johnson”;
Commonly Used String MethodsCommonly Used String Methods
►String(String str)String(String str)►char charAt(int index)char charAt(int index)► int indexOf(char ch)int indexOf(char ch)► int indexOf(String str, int pos)int indexOf(String str, int pos)► int compareTo(String str)int compareTo(String str)
Commonly Used String MethodsCommonly Used String Methods
►String concat(String str)String concat(String str)►boolean equals(String str)boolean equals(String str)► int length()int length()►String replace(char ToBeReplaced, char String replace(char ToBeReplaced, char
ReplacedWith)ReplacedWith)►String toLowerCase()String toLowerCase()►String toUpperCase()String toUpperCase()
What is tokenizingWhat is tokenizing
► Consider the following stringConsider the following string ““This is a test of string tokenizing”This is a test of string tokenizing”
► A token is a substring of this string that is selected as A token is a substring of this string that is selected as follows:follows: A particular delimeter or separation character is chosen, in A particular delimeter or separation character is chosen, in
this case choose whitespace (blank or tab)this case choose whitespace (blank or tab) Starting at the beginning of the string characters are added to Starting at the beginning of the string characters are added to
the substring until a delimeter is encounteredthe substring until a delimeter is encountered The token is the substring (the delimeter is not added to the The token is the substring (the delimeter is not added to the
substring)substring)
What is tokenizingWhat is tokenizing
► The next token is selected as follows:The next token is selected as follows: The next character in the string that is not a delimeter is The next character in the string that is not a delimeter is
located. This character is the first character of the next token. located. This character is the first character of the next token. Starting at this character, additional characters are added to Starting at this character, additional characters are added to
the substring until a delimeter is encounteredthe substring until a delimeter is encountered The token is the substring (the delimeter is not added to the The token is the substring (the delimeter is not added to the
substring)substring)
► For this example the second token would be “is”For this example the second token would be “is”► The pattern continues until all characters in the string The pattern continues until all characters in the string
have been processed.have been processed.
Tokenizing a StringTokenizing a String
►class StringTokenizerclass StringTokenizer Contained in package java.utilContained in package java.util Tokens usually delimited by whitespace characters Tokens usually delimited by whitespace characters
(space, tab, newline, etc)(space, tab, newline, etc) Contains methods: Contains methods:
►public StringTokenizer(String str, String delimits)public StringTokenizer(String str, String delimits)►public int countTokens()public int countTokens()►public boolean hasMoreTokens()public boolean hasMoreTokens()►public String nextToken(String delimits)public String nextToken(String delimits)
StringTokenizer tokenizer;StringTokenizer tokenizer;
String inputLine;String inputLine;
String name;String name;
String number;String number;
inputLine = “Judy 3250”;inputLine = “Judy 3250”;
tokenizer = new StringTokenizer(inputLine);tokenizer = new StringTokenizer(inputLine);
name = tokenizer.nextToken();name = tokenizer.nextToken();
num = Integer.parseInt( tokenizer.nextToken() );num = Integer.parseInt( tokenizer.nextToken() );
Using Dialog Boxes for Using Dialog Boxes for Input/OutputInput/Output
► Use a graphical user interface (GUI)Use a graphical user interface (GUI)► class JOptionPaneclass JOptionPane
Contained in package javax.swingContained in package javax.swing Contains methods: showInputDialog and showMessageDialogContains methods: showInputDialog and showMessageDialog
► Syntax:Syntax:
str = JOptionPane.showInputDialog(strExpression)str = JOptionPane.showInputDialog(strExpression)► Program must end with System.exit(0);Program must end with System.exit(0);
Parameters for the Method Parameters for the Method showMessageDialogshowMessageDialog
JOptionPane Options for the JOptionPane Options for the Parameter messageTypeParameter messageType
JOptionPane ExampleJOptionPane Example
Formatting the Output of Decimal Formatting the Output of Decimal NumbersNumbers
► Type float: defaults to 6 Type float: defaults to 6 decimal placesdecimal places
► Type double: defaults to Type double: defaults to 15 decimal places15 decimal places
class Decimal Formatclass Decimal Format
►Import package java.textImport package java.text►Create DecimalFormat object and initializeCreate DecimalFormat object and initialize►Use method formatUse method format►Example:Example:
DecimalFormat twoDecimal = DecimalFormat twoDecimal =
new DecimalFormat("0.00");new DecimalFormat("0.00");twoDecimal.format(56.379);twoDecimal.format(56.379);
►Result: 56.38Result: 56.38