Getting Started With Java: Animated Version
Getting Started With Java: Animated Version
Animated Version
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow; Declare
Declareaaname
name
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
Use
Usean
anobject
object
Ch2Sample1
myWindow : JFrame
JFrame myWindow;
Account customer;
More
Student jan, jim, jon;
Examples
Vehicle car1, car2;
1 Customer customer;
2 customer = new Customer( );
1.1.The
Theidentifier
identifiercustomer
customerisis
customer declared
declaredand
andspace
spaceisis
1 allocated
allocatedininmemory.
memory.
2.2.AACustomer
Customerobject
objectisis
: Customer
2 created
createdand
andthe
theidentifier
identifier
customer
customer is set torefer
is set to refertotoit.it.
customer
customer : Customer
: Customer
Customer customer;
customer = new Customer( );
customer = new Customer( );
customer
Created
Createdwith
withthe
thesecond
second
Created
Createdwith
with new.
the first new.
: Customer : Customer new. Reference tothe
Reference to thefirst
first
the first new. Customer object is lost.
Customer object is lost.
account.deposit( 200.0 );
More student.setName(“john”);
Examples car1.startEngine( );
Jframe
JFrame myWindow;
: JFrame
myWindow = new JFrame( );
myWindow.setSize(300, 200); width 300
visible true
The
Thediagram
diagramshows
showsonly
onlyfour
fourofofthe
themany
manydata
data
members of a JFrame object.
members of a JFrame object.
– comments,
– class declarations.
/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
Comment
Comment
JFrame myWindow;
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
/*
Comment number 1
*/
/*
Comment number 2
*/
These
Theseare
arepart
partofofthe
the
/* comment.
comment.
/*
/*
This is a comment
*/ Error:
Error:No
Nomatching
matching
beginning marker.
beginning marker.
*/
/*
This is a comment with
three lines of Multiline
MultilineComment
Comment
text.
*/
// This is a comment
// This is another comment Single
Singleline
lineComments
Comments
// This is a third comment
/**
* This class provides basic clock functions. In addition
javadoc
javadocComments
Comments
* to reading the current time and today’s date, you can
* use this class for stopwatch functions.
*/
File: Ch2Sample2.java
*/
Import
Import
import javax.swing.*; Statement
Statement
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow;
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
More import
import
javax.swing.JFrame;
java.util.*;
Examples import com.drcaffeine.simplegui.*;
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);
}
}
class Ch2Sample1 {
public static void main(String[ ] args) {
JFrame myWindow;
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
Modifier
Modifier Modifier
Modifier Return
ReturnType
Type Method
MethodName
Name Parameter
Parameter
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
©The McGraw-Hill Companies, Inc. Permission re
quired for reproduction or display. 4th Ed Chapter 2 - 22
Template for Simple Java Programs
/*
Chapter 2 Sample Program: Displaying a Window
Comment
Comment
File: Ch2Sample2.java
*/
Method
MethodBody
Body
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
This
Thisdialog
dialogwill
willappear
appear
atatthe center of the
the center of the
screen.
screen.
1 String name;
2 name
name =
= new
new String(“Jon Java”);
String(“Jon Java”);
1.1.The
Theidentifier
identifiername
nameisis
name
declared
declaredand
andspace
spaceisis
1 allocated
allocatedininmemory.
memory.
: String
2
2.2.AAString
Stringobject
objectisiscreated
created
Jon Java and
and the identifier nameisis
the identifier name
set
settotorefer
refertotoit.it.
The
Theposition,
position,ororindex,
index,ofof
the
thefirst
firstcharacter
characterisis0.0.
text.substring(6,8)
“so”
text.substring(0,8) “Espresso”
text.substring(1,5) “spre”
text.substring(3,3) “”
text.substring(4,2) error
str1.length( ) 5
str2.length( ) 4
str3.length( ) 0
str4.length( ) 1
String str;
str = “I Love Java and Java loves me.” ;
3 7 21
str.indexOf( “J” ) 7
str2.indexOf( “love” ) 21
today.toString( );
sdf1.format(today); “10/31/03”
name = JOptionPane.showInputDialog
(null, “What is your name?”);
This
Thisdialog
dialogwill
willappear
appear
atatthe center of the
the center of the
screen
screenready
readytotoaccept
accept
an input.
an input.
• 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);
}
}
“Ben” “Cosner”
“ABC”
©The McGraw-Hill Companies, Inc. Permission re
quired for reproduction or display. 4th Ed Chapter 2 - 48
Step 2 Code
/*
Chapter 2 Sample Program: Displays the Monogram
File: Step 2/Ch2MonogramStep2.java
*/
import javax.swing.*;
class Ch2Monogram {
space = " “;