Objectives ► Become familiar with the class String ► ► Learn how to use input and output...

20
Objectives Objectives Become familiar with the class Become familiar with the class String 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 Explore how to format the output of decimal numbers with the class decimal numbers with the class DecimalFormat DecimalFormat

Transcript of Objectives ► Become familiar with the class String ► ► Learn how to use input and output...

Page 1: 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.

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

Page 2: 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.

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

Page 3: 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.

The Operator newThe Operator new

► Statement: Statement:

Integer num;Integer num;

num = new Integer(78);num = new Integer(78);► Result:Result:

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

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”;

Page 9: 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.

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)

Page 10: 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.

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

Page 11: 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.

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)

Page 12: 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.

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.

Page 13: 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.

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)

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

Parameters for the Method Parameters for the Method showMessageDialogshowMessageDialog

Page 17: 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.

JOptionPane Options for the JOptionPane Options for the Parameter messageTypeParameter messageType

Page 18: 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.

JOptionPane ExampleJOptionPane Example

Page 19: 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.

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

Page 20: 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.

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