Professional Documents
Culture Documents
04
04
©The 1
Objectives
©The 2
The First Java Program
©The 3
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);
©The 4
Program Diagram for Ch2Sample1
©The 5
Dependency Relationship
Ch2Sample1
myWindow : JFrame
©The 6
Object Declaration
Class Name
Object Name
This class must be
One object is declared
defined before this
here.
declaration can be stated.
JFrame myWindow;
Account customer;
More
Student jan, jim, jon;
Examples
Vehicle car1, car2;
©The 7
Object Creation
Class Name Argument
Object Name
An instance of this class No arguments are used
Name of the object we
is created. here.
are creating here.
©The 8
Declaration vs. Creation
1 Customer customer;
customer = new Customer(
2
);
customer customer
1 2
: Customer
©The 9
State-of-Memory vs. Program
customer
customer : Customer
: Customer
©The 10
Name vs. Objects
Customer customer;
customer = new Customer(
);
customer = new Customer(
);
customer
©The 11
Sending a Message
Object Name Method Name Argument
Name of the object to The name of the The argument we are
which we are sending a message we are sending. passing with the
message. message.
account.deposit( 200.0 );
More
student.setName(“john”);
Examples car1.startEngine( );
©The 12
Program Components
– comments,
– class declarations.
©The 13
Program Component: Comment
/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow; Comment
myWindow = new JFrame( );
myWindow.setSize(300, 200);
©The 14
Matching Comment Markers
/*
Comment number 1
*/
/*
Comment number 2
*/
Error: No matching
*/ beginning marker.
©The 15
Three Types of Comments
/*
This is a comment with
three lines of Multiline Comment
text.
*/
// This is a comment
// This is another comment Single line Comments
// This is a third comment
/**
* This class provides basic clock functions. In addition
* to reading the current time and today’s date, you can javadoc Comments
* use this class for stopwatch functions.
*/
©The 16
Import Statement
/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java
*/
Import
import javax.swing.*;
Statement
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
©The 17
Import Statement Syntax and Semantics
import javax.swing.JFrame;
More import java.util.*;
Examples import
com.drcaffeine.simplegui.*;
©The 18
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);
©The 19
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);
©The 20
Method Declaration Elements
JFrame myWindow;
Method Body
myWindow = new JFrame( );
myWindow.setSize(300, 200);
}
©The 21
Template for Simple Java Programs
/*
Chapter 2 Sample Program: Displaying a Window
Comment
File: Ch2Sample2.java
*/
myWindow.setSize(300, 200);
Method Body
myWindow.setTitle(“My First Java
Program”);
myWindow.setVisible(true);
}
}
©The 22
Why Use Standard Classes
©The 23
JOptionPane
©The 24
Displaying Multiple Lines of Text
JOptionPane.showMessageDialog(null,
“one\ntwo\nthree”);
©The 25
String
©The 26
String is an Object
1 String name;
name = new String(“Jon
2
Java”);
name name
1 2
: String
Jon Java
©The 27
String Indexing
©The 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.
• If str is “programming” , then str.substring(3, 7)
will create a new string whose value is “gram”
because g is at position 3 and m is at position
6.
• The original string str remains unchanged.
©The 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
©The 30
Definition: length
©The 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
©The 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.
©The 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
©The 34
Definition: concatenation
©The 35
Examples: concatenation
str1 + str2
“JonJava”
str1 + “ “ + str2
“Jon Java”
str2 + “, “ + str1
“Java, Jon”
“Are you “ + str1 + “?”
“Are you Jon?”
©The 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( );
“Fri Oct 31 10:05:18 PST 2003”
©The 37
SimpleDateFormat
sdf1.format(today); “10/31/03”
©The 38
JOptionPane for Input
name = JOptionPane.showInputDialog
(null, “What is your name?”);
©The 39
Problem Statement
• Problem statement:
Write a program that asks for the user’s first,
middle, and last names and replies with their
initials.
Example:
©The 40
Overall Plan
©The 41
Development Steps
©The 42
Step 1 Design
©The 43
Step 1 Code
/*
Chapter 2 Sample Program: Displays the Monogram
File: Step1/Ch2Monogram.java
*/
import javax.swing.*;
class Ch2Monogram {
public static void main (String[ ] args) {
String name;
name = JOptionPane.showInputDialog(null,
"Enter your full name (first, middle, last):“);
JOptionPane.showMessageDialog(null, name);
}
}
©The 44
Step 1 Test
©The 45
Step 2 Design
©The 46
Step 2 Design (cont’d)
©The 47
Step 2 Code
/*
Chapter 2 Sample Program: Displays the Monogram
class Ch2Monogram {
space = " “;
©The 48
Step 2 Code (cont’d)
//Extract first, middle, and last names
first = name.substring(0, name.indexOf(space));
name = name.substring(name.indexOf(space)+1,
name.length());
©The 49
Step 2 Test
©The 50
Program Review
©The 51