Getting Started with Java
Getting Started with Java
Chapter 2 - 1
Objectives
Chapter 2 - 2
The First Java Program
Chapter 2 - 3
Program Ch2Sample1 Output
Chapter 2 - 4
Program Ch2Sample1
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow;
Declare a name
myWindow = new JFrame( );
Create an object
myWindow.setSize(300, 200);
Chapter 2 - 5
Program Diagram for Ch2Sample1
Chapter 2 - 6
Dependency Relationship
Ch2Sample1
myWindow : JFrame
Chapter 2 - 7
Object Declaration
Class Name
This class must be Object Name
defined before this One object is declared
declaration can be here.
stated.
JFrame myWindow;
Account customer;
More Student jan, jim, jon;
Examples Vehicle car1, car2;
Chapter 2 - 8
Object Creation
Chapter 2 - 9
Declaration vs. Creation
1 Customer
Customer customer;
customer;
customer = new Customer( );
2 customer = new Customer( );
2. A Customer object is
: Customer
2 created and the identifier
customer is set to refer to it.
Chapter 2 - 10
State-of-Memory vs. Program
customer
customer : Customer
: Customer
Chapter 2 - 11
Name vs. Objects
Customer
Customer customer;
customer;
customer = new Customer( );
customer = new Customer( );
customer = new Customer( );
customer = new Customer( );
customer
Chapter 2 - 12
Sending a Message
account.deposit( 200.0 );
student.setName(“john”);
More
car1.startEngine( );
Examples
Chapter 2 - 13
Execution Flow
Jframe myWindow;
: JFrame
myWindow = new JFrame( );
myWindow.setSize(300, 200); width 300
visible true
The diagram shows only four of the many data
members of a JFrame object.
Chapter 2 - 14
Program Components
– comments,
– class declarations.
Chapter 2 - 15
Three Types of Comments
/*
This is a comment with
three lines of
text. Multiline Comment
*/
// This is a comment
// This is another comment
// This is a third comment Single line Comments
/**
* This class provides basic clock functions. In addition
* to reading the current time and today’s date, you can
* use this class for stopwatch functions. javadoc Comments
*/
Chapter 2 - 16
Comment and Import Statement
/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java
*/
myWindow.setSize(300, 200);
Chapter 2 - 17
Class Declaration
/*
Chapter 2 Sample Program: Displaying a Window Class
Declaration
File: Ch2Sample2.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
Chapter 2 - 18
Method Declaration
/*
Chapter 2 Sample Program: Displaying a Window
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
Chapter 2 - 19
Method Declaration Elements
JFrame myWindow;
Method Body
myWindow = new JFrame( );
myWindow.setSize(300, 200);
Chapter 2 - 20
Access specifier
Same class x x x x
Subclass in the same - x x x
package
Non-subclass in the same - - x x
package
Subclass in another package - - x x
Non-subclass in another - - - x
package
Why Use Standard Classes
• Don’t reinvent the wheel. When there are existing objects that
satisfy our needs, use them.
• Learning how to use standard Java classes is the first step toward
mastering OOP. Before we can learn how to define our own classes,
we need to learn how to use existing classes
• We will introduce four standard classes here:
– Scanner
– String
– Date
– SimpleDateFormat.
Chapter 2 - 22
Standard Output
Chapter 2 - 23
Using the print Method
Chapter 2 - 24
Using the println Method
Chapter 2 - 25
String
• The textual values passed to the showMessageDialog method
are instances of the String class.
• A sequence of characters separated by double quotes is a
String constant.
• There are close to 50 methods defined in the String class. We
will introduce three of them here: substring, length, and
indexOf.
• We will also introduce a string operation called
concatenation.
Chapter 2 - 26
String is an Object
1 String
String name;
name;
name = new String(“Jon Java”);
2 name = new String(“Jon Java”);
: String
2
2. A String object is created
Jon Java and the identifier name is
set to refer to it.
Chapter 2 - 27
String Indexing
Chapter 2 - 28
Definition: substring
• Assume str is a String object and properly initialized to a
string.
• str.substring( i, j ) will return a new string by extracting
characters of str from position i to j-1 where 0 ≤ i < length of
str, 0 < j ≤ length of str, and i ≤ j.
Chapter 2 - 29
Examples: substring
text.substring(6,8)
“so”
text.substring(0,8)
“Espresso”
text.substring(1,5)
“spre”
text.substring(3,3)
“”
text.substring(4,2)
error
Chapter 2 - 30
Definition: length
• Assume str is a String object and properly initialized to a
string.
• str.length( ) will return the number of characters in str.
• If str is “programming” , then str.length( ) will return 11
because there are 11 characters in it.
• The original string str remains unchanged.
Chapter 2 - 31
Examples: length
String str1, str2, str3, str4;
str1 = “Hello” ;
str2 = “Java” ;
str3 = “” ; //empty string
str4 = “ “ ; //one space
str1.length( )
5
str2.length( )
4
str3.length( )
0
str4.length( )
1
Chapter 2 - 32
Definition: indexOf
• Assume str and substr are String objects and properly
initialized.
• str.indexOf( substr ) will return the first position substr occurs
in str.
• If str is “programming” and substr is “gram” , then
str.indexOf(substr ) will return 3 because the position of the
first character of substr in str is 3.
• If substr does not occur in str, then –1 is returned.
• The search is case-sensitive.
Chapter 2 - 33
Examples: indexOf
String str;
str = “I Love Java and Java loves me.” ;
3 7 21
str.indexOf( “J” )
7
str2.indexOf( “love” )
21
str3. indexOf( “ove” )
3
str4. indexOf( “Me” )
-1
Chapter 2 - 34
Definition: concatenation
• Assume str1 and str2 are String objects and properly
initialized.
• str1 + str2 will return a new string that is a concatenation of
two strings.
• If str1 is “pro” and str2 is “gram” , then str1 + str2 will return
“program”.
• Notice that this is an operator and not a method of the String
class.
• The strings str1 and str2 remains the same.
Chapter 2 - 35
Examples: concatenation
str1 + str2
“JonJava”
str1 + “ “ + str2
“Jon Java”
str2 + “, “ + str1
“Java, Jon”
“Are you “ + str1 + “?”
“Are you Jon?”
Chapter 2 - 36
Date
• The Date class from the java.util package is used to represent a
date.
• When a Date object is created, it is set to today (the current date set
in the computer)
• The class has toString method that converts the internal format to a
string.
Date today;
today = new Date( );
today.toString( );
sdf1.format(today); “12/18/08”
sdf2.format(today); “December 19, 2008”
Chapter 2 - 38
Standard Input
import java.util.Scanner;
Scanner scanner;
Chapter 2 - 39
Reading from Standard Input
3. Result is printed
Chapter 2 - 40
References
Chapter 2 - 41
Exercises: No. 20, 26, 28
Chapter 2 - 42