Getting Started With Java
Getting Started With Java
Lesson Objectives
After you have read and studied this lesson, you should be
able to:
• Identify the basic components of Java programs
• Write simple Java programs
• Describe the difference between object declaration and creation
• Describe the process of creating and running Java programs
• Use the Date, SimpleDateFormat, String, and JOptionPane standard
classes
• Develop Java programs, using the incremental development approach
The First Java Program
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
Use an object
Program Diagram for Ch2Sample1
Ch2Sample1
myWindow : JFrame
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
jan, jim, jon;
Examples
Student car1, car2;
Object Creation
1 Customer customer;
2 customer = new Customer( );
customer customer
1 2
: Customer
State-of-Memory vs. Program
customer
customer : Customer
: Customer
Customer customer;
= new Customer( );
customer = new Customer( );
customer
customer
account.deposit( 200.0 );
More
student.setName(“john”);
Examples car1.startEngine( );
Program Components
– comments,
– class declarations.
Program Component: Comment
/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) {
Comment
JFrame myWindow;
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
import
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);
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
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
Template for Simple Java Programs
Comment
Import
Statements
class {
Method Body
}
}
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:
JOptionPane
String
Date
SimpleDateFormat.
JOptionPane
JOptionPane.showMessageDialog(null,
“one\ntwo\nthree”);
String
name name
1 2
: String
Jon Java
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
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
Definition: concatenation
Date today;
today = new Date( );
today.toString( );
“Fri Oct 31 10:05:18 PST 2020”
SimpleDateFormat
String name;
name = JOptionPane.showInputDialog
(null, “What is your name?”);
• Problem statement:
Write a program that asks for the user’s first,
middle, and last names and replies with their
initials.
Example:
File: Step1/Ch2Monogram.java
*/
import javax.swing.*;
class Ch2Monogram {
public static
void main
(String[ ] args)
{
String name;
name =
JOptionPane.s
howInputDialo
g(null,
Step 1 Test
“ABC”
Step 2 Code
/*
Chapter 2 Sample Program: Displays the Monogram
class Ch2Monogram {
space = " “;
monogram is " +
Step 2 Test
• In the testing phase, we run the program and verify that, for all
valid input values, correct monograms are displayed.