Documente Academic
Documente Profesional
Documente Cultură
Ralph Morelli
Trinity College
Hartford, CT
published by Prentice
Hall
Java, Java, Java
Object Oriented Problem Solving
Chapter 1
Java Program Design
and Development
Objectives
• Know the basic steps involved in the program
development process.
• Understand the difference between a Java
application and a Java applet.
• Understand how a Java program is translated into
machine language.
• Know how to edit, compile, and run Java
programs.
• Understand some basic Java language elements.
• Know how to use simple output operations.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Outline
• Designing Good Programs Java
• Java Language Summary
• Compiling and Executing a Java Program
• Applications and Applets
• From the Java Library: The System and
PrintStream classes.
• In the Laboratory: Editing, Compiling, and
Running an Applet
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Designing Good Programs
• Always precede coding with careful design.
• Remember: The sooner you begin to type
code, the longer the program will take to
finish.
• Design includes designing classes, data,
methods, and algorithms.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
The Program Development Process
• Problem Specification
• Problem Decomposition
• Design Specification
• Data, Methods, and Algorithms
• Coding into Java
• Testing, Debugging, and Revising
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Object-Oriented Program Development
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Problem Specification
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Problem Decomposition
• Divide-and-Conquer: What objects will be
used and how will they interact?
• Nouns: In OOD, choosing objects means
looking for nouns in the problem
specification.
Problem Specification: Design a class that will
represent a simple geometric rectangle with a
given length and width. The definition of this
class should make it possible to create rectangles
and calculate their areas.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Object Design Questions
• What role will the object perform?
• What data or information will it need?
– Look for nouns.
• Which actions will it take?
– Look for verbs.
• What interface will it present to other objects?
– These are public methods.
• What information will it hide from other objects?
– These are private.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Design Specification for a Rectangle
• Class Name: Rectangle
• Role: To represent a geometric rectangle
• Attributes (Information or instance variables)
- Length: A variable to store rectangle’s length (private)
- Width: A variable to store rectangle's width (private)
• Behaviors (public methods)
- Rectangle(): A method to set a rectangle’s length and
width
- calculateArea(): A method to calculate a rectangle’s
area
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
UML Design Specification
Class Name
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Method Design
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Coding into Java
} // Rectangle class
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Java Language Summary: Syntax
• The syntax of a programming language is
the set of rules that determines whether its
statements are correctly formulated.
• Example Rule: All Java statements must
end with a semincolon.
• Syntax error: sum = 0
• Syntax errors can be detected by the
compiler, which will report an error
message.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Java Language Summary: Semantics
• The semantics of a programming language
is the set of rules that determine the
meaning of its statements.
• Example Rule: In a + b, the + operator will
add a and b.
• Semantic error: User intended to add a and
b but coded a - b.
• Semantic errors cannot be detected by the
compiler, because it can’t read the
programmer’s mind.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Java Language Summary: Program Elements
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Java Language Summary: Class Definition
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Java Language
Java Language Summary: Access Rules
Summary: Access Rules
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Java Language Summary: Identifiers
• An identifier must begin with a letter (A to Z, a to
z) and may be followed by any number of letters
or digits (0 to 9) or underscores (_). An identifier
may not be identical to a Java keyword.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Primitive Data Types
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Applications vs. Applets
Java Applications Java Applets
• Stand-alone program • Embedded program.
• Runs independently • Runs in a Web browser
• Has a main() method • No main() method.
• No HTML file • Requires an HTML file
• Run using JDK’s java • Run using JDK’s
interpreter appletviewer
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
The HelloWorld Application
/* Multi-line
* The HelloWorld application program comment block
*/
Single-line
public class HelloWorld // Class header
{ // Start of class body comments
} // End of HelloWorld
Execution starts on
the first line of main()
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
The HelloWorld Applet
/*
* HelloWorld applet program
These statements import
*/ Java class names.
import java.applet.Applet; // Import the Applet class
import java.awt.Graphics; // and the Graphics class
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Editing, Compiling, and Running
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
The Java Development Process
• Step 1: Editing the Program
– Software: Any text editor will do.
• Step 2: Compiling the Program
– Software: Java Development Kit (JDK)
– JDK: javac HelloWorld.java
• Step 3: Running the Program
– JDK: java HelloWorld (Application)
– JDK: appletviewer file.html (Applet)
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Editing a Java Program
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Compiling a Java Program
• Compilation translates the source program
into Java bytecode.
– Bytecode is platform-independent
• JDK Command: javac HelloWorld.java
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Running a Java Application
• The class file (bytecode) is loaded into
memory and interpreted by the Java
Virtual Machine (JVM)
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Running a Java Applet
• Running an applet requires an HTML file containing
an <applet> tag:
<HTML>
...
<APPLET CODE=“HelloWorld.class”
WIDTH=200 HEIGHT=200>
</APPLET>
...
</HTML>
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Coding into Java
• Stepwise Refinement is the right way to code.
- Code small stages at a time, testing in between.
- Errors are caught earlier.
• Syntax rules must be followed.
- Syntax is the set of rules that determine whether
a particular statement is correctly formulated
• Semantics must be understood.
- Semantics refers to the meaning (effect on the
program) of each Java statement.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Testing, Debugging, and Revising
• The java.lang.PrintStream
class contains the print() and
println() methods that
perform output.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Example: OldMacDonald Program
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Qualified Names
• A qualified name takes the form
reference.elementName
where reference refers to some object (or class or
package) and elementName is the name of one of
the object’s (or class’s or package’s) elements.
• Use: To refer to elements in Java’s package, class,
element hierarchy.
• Context dependent.
System.out.println(); //println() method in System.out class
pet1.eat(); // eat() method in pet1 object
java.awt.Button // Button class in java.awt package
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
In the Laboratory: FirstApplet
• Objectives
– To familiarize you with the process of
editing, compiling, and running a Java
applet.
– To introduce the stepwise refinement
coding style.
– To provide some examples of both syntax
and semantic errors.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
FirstApplet Demo
• Click here to run the FirstApplet
and read its source code.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Program Walkthrough: Documentation
• The program begins with a comment block:
/*
* File: FirstApplet.java
* Author: Java Java Java
* Description: This applet plays the click-me-not game
* with the user.
*/
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Program Walkthrough: Documentation
• Documentation comments /** … */ are used to
document the class and its methods.
/**
* The FirstApplet class plays click-me-not with the user.
* @author Java Java Java
*/
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Program Walkthrough: Import Statement
• An import statement is a convenience that lets you
refer to a library class by its short name (Applet)
instead by its fully qualified name.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
The actionPerformed()Method
• The actionPerformed() method handles user
actions such as button clicks.
public void actionPerformed (ActionEvent e)
{
if (clickMe.getLabel().equals(“Click Me!”))
clickMe.setLabel(“Click Me Not!”);
else
clickMe.setLabel(“Click Me!”);
} //actionPerformed()
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Stepwise Refinement
• Stepwise refinement is a coding and testing
strategy that employs the divide-and-
conquer principle.
• It helps to break a large task into smaller,
more manageable subtasks.
• It helps to localize and identify errors in
your code.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Stepwise Refinement of FirstApplet
• Stage 1: Input the comment block, the
import statements, and class definition.
• Compile and test.
• Stage 2: Input the variable declarations.
• Compile and test.
• Stage 3: Input the init() method.
• Compile and test.
• Stage 4: Complete actionPerformed()
method.
• Compile and test.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Summary: Technical Terms
• algorithm • method invocation
• applet • object instantiation
• application • operator
• assignment expression • package
• block (compound statement • primitive data type
• declaration statement • pseudocode
• executable statement • semantics
• flow of control • software life cycle
• HyperText Markup Language • stepwise refinement
(HTML) • stub class
• identifier • stub method
• instance variable (field) • syntax
• literal value • variable
• method • waterfall model
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Summary: Key Points
• A Java applet is an embedded program that runs
within the context of a WWW browser. Java
applets are identified in HTML documents by
using the <applet> tag.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 1: Java Program Development
Summary: Key Points