Ch2: Getting Started with Java - Objectives

slimwhimperSoftware and s/w Development

Nov 3, 2013 (4 years and 1 month ago)

147 views


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

1

Ch2: Getting Started with Java
-

Objectives


After you have read and studied this chapter, 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 McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

2

Program Ch2Sample1

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
);


}

}

Declare a name

Create an object

Use an object


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

3

Program Diagram for Ch2Sample1

myWindow : JFrame

Ch2Sample1

setTitle(“My First Java Program”)

myWindow : JFrame

Ch2Sample1

Dependency Relationship


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

4

Object Declaration and Creation

JFrame


myWindow;

myWindow = new JFrame ( ) ;

Declaration vs. Creation?

Sending a message


Program Components


comments,


import

statements, and


class declarations.


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

5

Why Use Standard Classes


Don’t reinvent the wheel. When there are existing
objects that satisfy our needs, use them.


We will introduce four standard classes here:


JOptionPane : showMessageDialog(…),


String : substring(), length(), indexOf()


Date


SimpleDateFormat.


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

6

name

String is an Object

1. The identifier
name

is
declared and space is
allocated in memory.

2. A
String

object is created
and the identifier
name

is
set to refer to it.

1

2

1

String name;

name = new String(“Jon Java”);

: String

Jon Java

name

2


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

7

Examples: substring

String text =
“Espresso”
;

text.substring(6,8)

text.substring(0,8)

text.substring(1,5)

text.substring(3,3)

text.substring(4,2)

“so”

“Espresso”

“spre”

error


“”


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

8

Examples: length

String str1, str2, str3, str4;

str1 =
“Hello”

;

str2 =
“Java”

;

str3 =
“”

;
//empty string

str4 =
“ “

;
//one space

str1.length( )

str2.length( )

str3.length( )

str4.length( )

5


4


1


0



©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

9

Examples: indexOf

String str;

str =
“I Love Java and Java loves me.”

;

str.indexOf(
“J”

)

str2.indexOf(
“love”

)

str3. indexOf(
“ove”

)

str4. indexOf(
“Me”

)

7


21


-
1


3


3

7

21


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

10

Examples: concatenation

String str1, str2;

str1 =
“Jon”

;

str2 =
“Java”

;

str1 + str2

str1 + “ “ + str2

str2 + “, “ + str1

“Are you “ + str1 + “?”

“JonJava”

“Jon Java”

“Java, Jon”

“Are you Jon?”


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

11

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.



SimpleDateFormat: for formatting date options



JOptionPane for Input:
String name;


name = JOptionPane.showInputDialog





(
null
,
“What is your name?”
)
;


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

12

Problem Statement


Problem statement:


Write a program that asks for the user’s first,
middle, and last names and replies with their
initials.

Example:



input:


Andrew Lloyd Weber


output:

ALW


©The McGraw
-
Hill Companies, Inc. Permission
required for reproduction or display.


4
th

Ed Chapter 2

-

13

Overall Plan


Identify the major tasks the program has to
perform.


Tasks:


Get the user’s first, middle, and last names


Extract the initials and create the monogram


Output the monogram


This is given in the textbook.


Let us do an in
-
class demo of a program titled
“EmailParser.java”.