Professional Documents
Culture Documents
By
Javed Siddique
Objectives
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow; Declare a name
myWindow.setSize(300, 200);
myWindow.setTitle( “My First Java Program” );
myWindow.setVisible(true);
}
}
Use an object
Program Diagram for Ch2Sample1
Ch2Sample1
myWindow : JFrame
Java is Case-Sensitive
myWindow Mywindow … are different
A program is made up of statements
end with ; (Semicolon)
Statements are composed of
“words”
import new myFrame } ; {
Some words have a special meaning in Java: reserved or
keywords. Shown in bold gold in the slides
Spaces -- also called white spaces
Space, tabs, returns (show up as blanks)
Multiple, contiguous whitespaces are ignored
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;
Identifiers
In order to manipulate an object, we have to give it
a name and also create the object.
Names are also called identifiers
An identifier
Cannot be a reserved word
Can consist only of letters(A..Z,a..z), digits(0..9), and _
Cannot begin with a digit
Examples is recitation
These are required rules. We also have naming
conventions that make programs easier to read
Identifiers begin with a lowercase letter
Class names begin with an uppercase letter
Object Creation
2. A Customer object is
: Customer
2 created and the identifier
customer is set to refer to it.
State-of-Memory vs. Program
customer
customer : Customer
: Customer
customer
account.deposit( 200.0 );
More student.setName( “john” );
Examples car1.startEngine( );
Execution Flow
State-of-Memory Diagram
Program Code myWindow
Jframe
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.
Program Components
comments,
class declarations.
Comments
/*
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.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
Matching Comment Markers
/*
Comment number 1
*/
/*
Comment number 2
*/
/*
/*
This is a comment
*/ Error: No matching
beginning marker.
*/
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
javadoc Comments
* to reading the current time and today’s date, you can
* use this class for stopwatch functions.
*/
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.setSize(300, 200);
myWindow.setTitle( “My First Java Program” );
myWindow.setVisible(true);
}
}
Import Statement Syntax and Semantics
Package Name Class Name
Name of the package The name of the class we want
that contains the classes to import. Use asterisks to
we want to use. import all classes.
More import
import
javax.swing.JFrame;
java.util.*;
Examples import com.drcaffeine.simplegui.*;
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.setSize(300, 200);
myWindow.setTitle( “My First Java Program” );
myWindow.setVisible(true);
}
}
Method Declaration
/*
Chapter 2 Sample Program: Displaying a Window
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow;
myWindow.setSize(300, 200);
myWindow.setTitle( “My First Java Program” );
myWindow.setVisible(true);
}
}
Method Declaration Elements
Modifier Modifier Return Type Method Name Parameter
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
Template for Simple Java Programs
/*
Chapter 2 Sample Program: Displaying a Window
Comment
File: Ch2Sample2.java
*/
}
}
Why Use Standard Classes
JOptionPane.showMessageDialog(null,“one\ntwo\nthree”);
String
: String
2
2. A String object is created
Jane Java and the identifier name is
set to refer to it.
String Indexing
text.substring(6,8) “so”
text.substring(0,8) “Espresso”
text.substring(1,5)
“spre”
text.substring(3,3)
“”
text.substring(4,2)
error
Definition: length
str1.length( ) 5
str2.length( )
4
str3.length( )
0
str4.length( )
1
Definition: indexOf
3 7 21
str.indexOf( “J” ) 7
str2.indexOf( “love” )
21
str3. indexOf( “ove” )
3
str4. indexOf( “Me” )
-1
Definition: concatenation
today.toString( );
sdf1.format(today); “10/31/03”
name = JOptionPane.showInputDialog
(null, “Your full name:”);
Problem statement:
Write a program that asks for the
user’s first, middle, and last names
and replies with their initials.
Example:
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);
}
}
Step 1 Test
“Ben” “Cosner”
“ABC”
Step 2 Code
/*
Chapter 2 Sample Program: Displays the Monogram
File: Step 2/Ch2MonogramStep2.java
*/
import javax.swing.*;
class Ch2Monogram {
space = “ ” ;