Download presentation
Presentation is loading. Please wait.
Published byDarleen Dulcie Caldwell Modified over 9 years ago
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 stream ► Explore how to format the output of decimal numbers with the class DecimalFormat
2
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
3
The Operator new ► Statement: Integer num; num = new Integer(78); ► Result:
4
Garbage Collection ► Change value of num: num = new Integer(50); ► Old memory space reclaimed
5
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
6
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
7
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
8
The class String ► String variables are reference variables ► Given String name; Equivalent Statements: name = new String("Lisa Johnson"); name = “Lisa Johnson”;
9
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)
10
Commonly Used String Methods ► String concat(String str) ► boolean equals(String str) ► int length() ► String replace(char ToBeReplaced, char ReplacedWith) ► String toLowerCase() ► String toUpperCase()
11
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)
12
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.
13
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)
14
StringTokenizer tokenizer; String inputLine; String name; String number; inputLine = “Judy 3250”; tokenizer = new StringTokenizer(inputLine); name = tokenizer.nextToken(); num = Integer.parseInt( tokenizer.nextToken() );
15
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);
16
Parameters for the Method showMessageDialog
17
JOptionPane Options for the Parameter messageType
18
JOptionPane Example
19
Formatting the Output of Decimal Numbers ► Type float: defaults to 6 decimal places ► Type double: defaults to 15 decimal places
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.