59
Chapter 2 Getting Started with Java CS 180 Sunil Prabhakar Department of Computer Science Purdue University 1

Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

Chapter 2 Getting Started with Java

CS 180Sunil PrabhakarDepartment of Computer Science Purdue University

1

Page 2: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

2

Objectives

This week we will study simple Java programs the difference between object declaration and

creation some useful classes the incremental development approach

2

Page 3: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

3

Our 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.

3

Page 4: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

4

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

4

Page 5: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

5

Program Diagram for Ch2Sample1

myWindow : JFrame

Ch2Sample1 setSize(300, 200)

setTitle(“My First Java Program”)

setVisible(true)

5

Page 6: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

6

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.

6

Page 7: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

7

Java basics

Java is Case-Sensitive myWindow Mywindow … are different

A program is made up of statements end with ;

Statements are composed of “words”

import new myFrame } ; { Some words have a special meaning in Java: reserved or

keywords. Shown in bold orange in the slides Spaces -- also called white spaces

Space, tabs, returns (show up as blanks) Multiple, contiguous whitespaces are ignored

7

Page 8: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

8

MoreExamples

Object Declaration

JFrame myWindow;

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

Object NameOne object is declared here.

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

8

Page 9: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

9

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

9

Page 10: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

10

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.

Class NameAn instance of this class is created.

ArgumentNo arguments are used here.

10

Page 11: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

11

Declaration vs. Creation

Customer customer;

customer = new Customer( );

Customer customer;

customer = new Customer( );

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.

1

2

customer1

: Customer2

11

Page 12: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

12

State-of-Memory vs. Program

customer

: Customer

State-of-MemoryNotation

customer : Customer

Program DiagramNotation

12

Page 13: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

13

Name vs. Objects

Customer customer;

customer = new Customer( );

customer = new Customer( );

Customer customer;

customer

customer = new Customer( );

customer = new Customer( );

: Customer : CustomerCreated with the first new.

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

13

Page 14: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

14

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.

Method NameThe name of the message we are sending.

ArgumentThe argument we are passing with the message.

14

Page 15: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

JFrame myWindow;

myWindow = new JFrame( );

myWindow.setSize(300, 200);

myWindow.setTitle (“My First Java Program”);myWindow.setVisible(true);

myWindow.setSize(300, 200);

JFrame myWindow;

myWindow.setVisible(true);

myWindow = new JFrame( );

myWindow.setTitle (“My First Java Program”);

15

Execution Flow

myWindow

State-of-Memory Diagram

: JFrame

width

height

title

visible

200

My First Java …

300

trueThe diagram shows only four of the many data members of a JFrame object.

Program Code

15

Page 16: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

16

Program Components

A Java program is composed of

comments,

import statements, and

class declarations.

16

Page 17: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

17

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

}}

Comments

Comment

17

Page 18: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

18

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.

These are part of the comment.

18

Page 19: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

19

Three Types of Comments/*

This is a comment with

three lines of

text.

*/

Multiline Comment

Single 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 Comments

19

Page 20: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

20

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

20

Page 21: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

21

Import Statement Syntax and Semantics

import <package name> . <class name> ;

e.g. import dorm . Resident;

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

MoreExamples

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.

21

Page 22: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

22

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

22

Page 23: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

23

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

}}

Class Declaration

23

Page 24: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

public static void main( String[ ] args ) {

JFrame myWindow;

myWindow = new JFrame( );

myWindow.setSize(300, 200);

myWindow.setTitle( “My First Java Program” );

myWindow.setVisible(true);

}

24

Method Declaration ElementsModifier Modifier Return Type Method Name Parameter

Method Body

24

Page 25: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

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

}}

25

Template for Simple Java Programs

Import Statements

Class Name

Comment

Method Body

25

Page 26: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

26

Why Use Standard Classes Don’t reinvent the wheel. When there are existing

classes 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 some standard classes here: System JOptionPane String Scanner Date SimpleDateFormat

26

Page 27: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

27

Standard Output

Using the print method of the System.out class is a simple way to write to the console window from which the program was run.

System.out.print(“How are you?”);

This dialog will appear at the center of the screen.

> How are you?

27

Page 28: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

28

Multiple Lines

We can display multiple lines of text by separating lines with a new line marker \n, or by using the println method.System.out.print(“How are you?”);System.out.println(“Counting:”);System.out.print(“One \n Two \n”);System.out.print(“Three”);

> How are you?Counting:One Two Three

28

Page 29: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

29

JOptionPane

Using showMessageDialog of the JOptionPane class is a simple way to bring up a window with a message.

JOptionPane.showMessageDialog(null, “How are you?”);

This dialog will appear at the center of the screen.

29

Page 30: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

30

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

30

Page 31: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

31

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.

31

Page 32: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

32

String is an Object

1

2

name1

String name;

name = new String(“Jane Java”);

String name;

name = new String(“Jane Java”);

2: String

Jane Java

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.

32

Page 33: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

33

String Indexing

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

String text;text = “Purdue!!”;

P

0

u

1

r

2

d

3

u

4

e

5

!

6

!

7

33

Page 34: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

34

Definition: substring

Assume str is a String object and properly initialized to a string.

str.substring( i, j ) will return a new string by extracting characters of str from position i to j-1 where 0 ≤ i < length of str, 0 < j ≤ length of str, and i ≤ j.

If str is “programming” , then str.substring(3, 7) will create a new string whose value is “gram” because g is at position 3 and m is at position 6.

The original string str remains unchanged.

34

Page 35: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

35

Examples: substring

text.substring(6,8)

text.substring(0,8)

text.substring(1,5)

text.substring(3,3)

text.substring(4,2)

“!!”

“Purdue!!”

“urdu”

error

“”

String text = “Purdue!!”;

35

Page 36: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

36

Definition: length

Assume str is a String object and properly initialized to a string.

str.length( ) will return the number of characters in str.

If str is “programming” , then str.length( ) will return 11 because there are 11 characters in it.

The original string str remains unchanged.

36

Page 37: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

37

Examples: length

String str1, str2, str3, str4;str1 = “Hello” ;str2 = “Java” ;str3 = “” ; //empty stringstr4 = “ ” ; //one space

str1.length( )

str2.length( )

str3.length( )

str4.length( )

5

4

1

0

37

Page 38: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

38

Definition: indexOf

Assume str and substr are String objects and properly initialized.

str.indexOf(substr) will return the first position substr occurs in str.

If str is “programming” and substr is “gram” , then str.indexOf(substr) will return 3 because the position of the first character of substr in str is 3.

If substr does not occur in str, then –1 is returned. The search is case-sensitive.

38

Page 39: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

39

Examples: indexOf

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

str.indexOf( “J” )

str.indexOf( “love” )

str.indexOf( “ove” )

str.indexOf( “Me” )

7

21

-1

3

3 7 21

39

Page 40: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

40

Definition: concatenation

Assume str1 and str2 are String objects and properly initialized.

str1 + str2 will return a new string that is a concatenation of two strings.

If str1 is “pro” and str2 is “gram” , then str1 + str2 will return “program”.

Notice that this is an operator and not a method of the String class.

The strings str1 and str2 remains the same.

40

Page 41: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

41

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?”

41

Page 42: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

42

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 a toString method that converts the

internal format to a string.

Date today;today = new Date( );

today.toString( );

“Wed Aug 30 4:05:18 EST 2006”

42

Page 43: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

43

SimpleDateFormat

The SimpleDateFormat class allows the Date information to be displayed with various formats.

Table 2.1 page 68 shows the formatting options.

Date today = new Date( );SimpleDateFormat sdf1, sdf2;sdf1 = new SimpleDateFormat( “MM/dd/yy” );sdf2 = new SimpleDateFormat( “MMMM dd, yyyy” );

sdf1.format(today);

sdf2.format(today);

“10/31/03”

“October 31, 2003”

43

Page 44: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

44

Standard Input and Scanner

The System class has a special object that accepts input from the keyboard: System.in

It reads only one byte at a time. We often need to read multiple bytes at a time.

The Scanner class provides the necessary methods.

A scanner object is created that “wraps” the System.in object.

Calls to the method next() return one “word” at a time from the standard input

Words are separated by whitespaces.

44

Page 45: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

45

Standard Input and Scanner

import java.util.*;...Scanner scanner;String firstName;scanner = new scanner(System.in);System.out.print(“Enter your first name: ”);firstName = scanner.next();System.out.println(“Hello ”+ firstName + “.”);

“Lisa” is typed by the user followed by the Enter (Return) key

> Enter your first name: Lisa ↩> Hello Lisa.

45

Page 46: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

46

Reading in multiple words

import java.util.*;...Scanner scanner;String firstName, lastName;scanner = new scanner(System.in);System.out.print(“Enter your first and last name: ”);firstName = scanner.next();lastName = scanner.next();System.out.println(“Hello ”+ firstName + “ “ + lastName + “.”);

> Enter your first name: Lisa Smith↩> Hello Lisa Smith.

46

Page 47: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

47

JOptionPane for Input

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

String name;

name = JOptionPane.showInputDialog (null, “Your full name:”);

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

47

Page 48: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

48

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

48

Page 49: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

49

Overall Plan

Identify the major tasks the program has to perform.

We need to know what to develop before we develop!

Tasks: Get the user’s first, middle, and last names Extract the initials and create the monogram Output the monogram

49

Page 50: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

50

Development Steps

We will develop this program in two steps:

1. Start with the program template and add code to get input

2. Add code to compute and display the monogram

50

Page 51: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

51

Step 1 Design

The program specification states “get the user’s name” but doesn’t say how.

We will consider “how” in the Step 1 design We will use JOptionPane for input Input Style Choice #1

Input first, middle, and last names separately Input Style Choice #2

Input the full name at once We choose Style #2 because it is easier and

quicker for the user to enter the information

51

Page 52: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

52

Step 1 Code

/* Chapter 2 Sample Program: Displays the Monogram File: Step1/Ch2Monogram.java*/import javax.swing.*;

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

52

Page 53: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

53

Step 1 Test

In the testing phase, we run the program and verify that we can enter the name the name we enter is displayed correctly

53

Page 54: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

54

Step 2 Design

Our programming skills are limited, so we will make the following assumptions: input string contains first, middle, and last names first, middle, and last names are separated by

single blank spaces Example

John Quincy Adams (okay) John Kennedy (not okay) Harrison, William Henry (not okay)

54

Page 55: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

55

Step 2 Design (cont’d)

Given the valid input, we can compute the monogram by breaking the input name into first, middle, and last extracting the first character from them concatenating three first characters

“Aaron Ben Cosner”

“Aaron” “Ben Cosner”

“Ben” “Cosner”

“ABC”

55

Page 56: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

56

Step 2 Code

/* Chapter 2 Sample Program: Displays the Monogram File: Step 2/Ch2MonogramStep2.java*/import javax.swing.*;

class Ch2Monogram {

public static void main (String[ ] args) { String name, first, middle, last, space, monogram; space = “ ” ;

//Input the full name name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):” );

56

Page 57: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

57

Step 2 Code (cont’d)

//Extract first, middle, and last names first = name.substring(0, name.indexOf(space)); name = name.substring(name.indexOf(space)+1,name.length());

middle = name.substring(0, name.indexOf(space)); last = name.substring(name.indexOf(space)+1, name.length());

//Compute the monogram monogram = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0,1); //Output the result JOptionPane.showMessageDialog(null, "Your monogram is " + monogram); }}

57

Page 58: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

58

Step 2 Test

In the testing phase, we run the program and verify that, for all valid input values, correct monograms are displayed.

We run the program numerous times. Seeing one correct answer is not enough. We have to try out many different types of (valid) input values.

58

Page 59: Getting Started with Javacs180/Spring2012Web/... · 9 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers

59

Program Review

The work of a programmer is not done yet. Once the working program is developed, we

perform a critical review and see if there are any missing features or possible improvements

One suggestion Improve the initial prompt so the user knows the

valid input format requires single spaces between the first, middle, and last names

59