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.

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Chapter 2 - Introduction to Java Applications
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
Chapter 3: Introduction to Objects and Input/Output.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer.
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Lecture 2 Objectives Learn about objects and reference variables.
Using Java Class Library
String Handling StringBuffer class character class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Dialog Boxes.
1 CHAPTER 3 StringTokenizer. 2 StringTokenizer CLASS There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
(Dreaded) Quiz 2 Next Monday.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Lecture 2
Chapter 7 User-Defined Methods.
Outline Creating Objects The String Class Packages Formatting Output
Objectives You should be able to describe: Interactive Keyboard Input
Using Classes and Objects
Classes, Libraries & Packages
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 3: Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
Chapter 2: Basic Elements of Java
Chapter 3: Introduction to Objects and Input/Output
Introduction to Java Programming
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
Presentation transcript:

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