chapter 2

29
Chapter 2 Getting Started with Java

Upload: stacey

Post on 12-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

Chapter 2. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 2

Chapter 2

Getting Started with Java

Page 2: Chapter 2

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

Page 3: Chapter 2

The First Java Program

• The fundamental OOP concept illustrated by the program:

An object-oriented program uses objects.

• This program displays a window on the screen.• The size of the window is set to 300 pixels wide

and 200 pixels high. Its title is set to My First Java Program.

Page 4: Chapter 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 nameDeclare a name

Create an objectCreate an object

Use an objectUse an object

Page 5: Chapter 2

Program Diagram for Ch2Sample1

myWindow : JFrame

Ch2Sample1 setSize(300, 200)

setTitle(“My First Java Program”)

setVisible(true)

Page 6: Chapter 2

Dependency Relationship

myWindow : JFrame

Ch2Sample1

Instead of drawing all messages, we summarize it by showingonly the dependency relationship. The diagram shows that Ch2Sample1 “depends” on the service provided by myWindow.

Page 7: Chapter 2

MoreExamples

Object Declaration

JFrame myWindow;

Account customer;Student jan, jim, jon;Vehicle car1, car2;

Object NameOne object is declared here.

Object NameOne object is declared here.

Class NameThis class must be defined before this declaration can be stated.

Class NameThis class must be defined before this declaration can be stated.

Page 8: Chapter 2

Object Creation

myWindow = new JFrame ( ) ;

MoreExamples

customer = new Customer( );jon = new Student(“John Java”);car1 = new Vehicle( );

Object NameName of the object we are creating here.

Object NameName of the object we are creating here.

Class NameAn instance of this class is created.

Class NameAn instance of this class is created.

ArgumentNo arguments are used here.

ArgumentNo arguments are used here.

Page 9: Chapter 2

Declaration vs. Creation

Customer customer;

customer = new Customer( );

1. The identifier customer is declared and space is allocated in memory.

1. The identifier customer is declared and space is allocated in memory.

2. A Customer object is created and the identifier customer is set to refer to it.

2. A Customer object is created and the identifier customer is set to refer to it.

1

2

customer

2

: Customer

customer

1

Page 10: Chapter 2

State-of-Memory vs. Program

customer

: Customer

State-of-MemoryNotation

customer : Customer

Program DiagramNotation

Page 11: Chapter 2

Name vs. Objects

Customer customer;

customer = new Customer( );

customer = new Customer( );

customer

: Customer : CustomerCreated with the first new.

Created with the first new.

Created with the second new. Reference to the first Customer object is lost.

Created with the second new. Reference to the first Customer object is lost.

Page 12: Chapter 2

Sending a Message

myWindow . setVisible ( true ) ;

MoreExamples

account.deposit( 200.0 );student.setName(“john”);car1.startEngine( );

Object NameName of the object to which we are sending a message.

Object NameName of the object to which we are sending a message.

Method NameThe name of the message we are sending.

Method NameThe name of the message we are sending.

ArgumentThe argument we are passing with the message.

ArgumentThe argument we are passing with the message.

Page 13: Chapter 2

Program Components

• A Java program is composed of

– comments,

– import statements, and

– class declarations.

Page 14: Chapter 2

/*Chapter 2 Sample Program: Displaying a Window

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

}

Program Component: Comment

CommentComment

Page 15: Chapter 2

Matching Comment Markers

/* This is a comment on one line */

/*

Comment number 1

*/

/*

Comment number 2

*/

/*

/*

/*

This is a comment

*/

*/

Error: No matching beginning marker.

Error: No matching beginning marker.

These are part of the comment.

These are part of the comment.

Page 16: Chapter 2

Three Types of Comments

/*

This is a comment with

three lines of

text.

*/

Multiline CommentMultiline Comment

Single line CommentsSingle line Comments

// This is a comment

// This is another comment

// This is a third comment

/**

* This class provides basic clock functions. In addition

* to reading the current time and today’s date, you can

* use this class for stopwatch functions.

*/

javadoc Commentsjavadoc Comments

Page 17: Chapter 2

Import Statement

/*Chapter 2 Sample Program: Displaying a Window

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

}

Import Statement

Import Statement

Page 18: Chapter 2

Import Statement Syntax and Semantics

<package name> . <class

name> ;

e.g. dorm . Resident;

MoreExamples

import javax.swing.JFrame;import java.util.*;import com.drcaffeine.simplegui.*;

Class NameThe name of the class we want to import. Use asterisks to import all classes.

Class NameThe name of the class we want to import. Use asterisks to import all classes.

Package NameName of the package that contains the classes we want to use.

Package NameName of the package that contains the classes we want to use.

Page 19: Chapter 2

Class Declaration

/*Chapter 2 Sample Program: Displaying a Window

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

}

Class Declaration

Class Declaration

Page 20: Chapter 2

Method Declaration

/*Chapter 2 Sample Program: Displaying a Window

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

Method Declaration

Page 21: Chapter 2

Method Declaration Elements

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 BodyMethod Body

ModifierModifier ModifierModifier Return TypeReturn Type Method NameMethod Name ParameterParameter

Page 22: Chapter 2

Template for Simple Java Programs

/* Chapter 2 Sample Program: Displaying a Window

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

}}

Import Statements

Import Statements

Class NameClass Name

CommentComment

Method BodyMethod Body

Page 23: Chapter 2

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.

Page 24: Chapter 2

JOptionPane

• Using showMessageDialog of the JOptionPane class is a simple way to display a result of a computation to the user.

JOptionPane.showMessageDialog(null, “I Love Java”);

This dialog will appear at the center of the screen.

This dialog will appear at the center of the screen.

Page 25: Chapter 2

Displaying Multiple Lines of Text

• We can display multiple lines of text by separating lines with a new line marker \n.

JOptionPane.showMessageDialog(null, “one\ntwo\nthree”);

Page 26: Chapter 2

String

• The textual values passed to the showMessageDialog method are instances of the String class.

• A sequence of characters separated by double quotes is a String constant.

• There are close to 50 methods defined in the String class. We will introduce three of them here: substring, length, and indexOf.

• We will also introduce a string operation called concatenation.

Page 27: Chapter 2

name

String is an Object

1. The identifier name is declared and space is allocated in memory.

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.

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

name2

Page 28: Chapter 2

String Indexing

The position, or index, of the first character is 0.

The position, or index, of the first character is 0.

Page 29: Chapter 2

JOptionPane for Input

• Using showInputDialog of the JOptionPane class is a simple way to input a string.

String name;

name = JOptionPane.showInputDialog(null, “What is your name?”);

This dialog will appear at the center of the screen ready to accept an input.

This dialog will appear at the center of the screen ready to accept an input.