Transcript Ch03

Java
Methods
TM
Maria Litvin
Gary Litvin
An Introduction
to Object-Oriented Programming
ch  3
An Introduction to Software
Development
Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.
Objectives:

Understand the software development
process, tools, and priorities

Learn about algorithms and see examples

Learn basic facts about OOP

Understand compilers and interpreters

Learn about Java Virtual Machine, bytecodes

Learn to set up simple console applications,
GUI applications, and applets in Java
3-2
Software Today:
84,700,000
3-3
Software Applications






Large business
systems
Databases
Military
Embedded systems
Scientific research
AI




Word processing and
other small business
and personal
productivity tools
Internet, e-mail, etc.
Graphics / arts / digital
photography
Games
3-4
Software Development
Early era (1950s):

Emphasis on
efficiency
Now:

– programmer’s
productivity
– team development
– reusability of code
– easier maintenance
– portability
– fast algorithms
– small program size
– limited memory use


Often cryptic code
Not user-friendly
Emphasis on


Better documented
User-friendly
3-5
Programming Languages
Assembly
languages
1940
1950
C
LISP
Scheme
Logo
1960
1970
Fortran
Machine
code
C++
1980
C#
Java
1990
2000
Pascal
Basic
Smalltalk Smalltalk-80
3-6
Algorithms
“A more or less abstract, formal, and
general step-by-step recipe that tells how to
perform a certain task or solve a certain
problem.”

Examples:
– Binary Search (guess-the-number
game)
– long division
– Euclid’s algorithm for finding the
greatest common factor (c. 300 BC)
3-7
Properties of Algorithms



Abstract: do not depend on a particular
language or machine
General: apply to any “size” of task or
any input values
Short: Use iterations or recursion to
repeat the same steps multiple times
3-8
Pseudocode
and Flowcharts
Input:
pos0, dir0
pos  pos0
1. Start at pos0, facing dir0
dir  dir0
2. If wall in front,
turn 90º clockwise
No
else
Wall in front?
go forward
3. If back to initial position pos 
pos + forward
and direction, stop
4. Proceed to Step 2
pos = pos0
and dir = dir0?
Yes
dir 
dir + 90º
No
Yes
Stop
3-9
OOP —
Object-Oriented Programming



An OOP program models a world of
active objects.
An object may have its own “memory,”
which may contain other objects.
An object has a set of methods that can
process messages of certain types.
3-10
OOP (cont’d)

A method can change the object’s state,
send messages to other objects, and
create new objects.

An object belongs to a particular class,
and the functionality of each object is
determined by its class.

A programmer creates an OOP
application by defining classes.
3-11
The Main OOP Concepts:


Inheritance: a subclass extends a
superclass; the objects of a subclass
inherit features of the superclass and
can redefine them or add new features.
Event-driven programs: the program
simulates asynchronous handling of
events; methods are called
automatically in response to events.
3-12
OOP Benefits

Facilitates team development

Easier to reuse software components
and write reusable software

Easier GUI (Graphical User Interface)
and multimedia programming
3-13
Software Development Tools

Editor

– programmer writes
source code

– converts one or
several object
modules into an
executable program
Compiler
– translates the source
into object code
(instructions specific
to a particular CPU)
Linker

Debugger
– stepping through the
program “in slow
motion,” helps find
logical mistakes
(“bugs”)
3-14
The First “Bug”
“(moth) in relay”
Mark II Aiken Relay Calculator (Harvard University, 1945)
3-15
Compiled Languages:
Edit-Compile-Link-Run
Editor
Editor
Editor
Source
code
Compiler
Source
code
Compiler
Source
code
Compiler
Object
code
Object
code
Object
code
Linker Executable
program

3-16
IDE —
Integrated Development Environment

Combines editor, compiler, linker,
debugger, other tools

Has GUI (graphical user interface)

Compiles + links + runs at the click of a
button

Helps put together a project with several
modules (source files)
3-17
Compiler vs.
Interpreter

Compiler:
checks syntax
generates
machine-code
instructions
not needed to run
the executable
program
the executable
runs faster
Editor

Source
code
Interpreter

Interpreter:
checks syntax
executes appropriate
instructions while
interpreting the
program statements
must remain installed
while the program is
interpreted
the interpreted
program is slower
3-18
Java’s Hybrid Approach:
Compiler + Interpreter

A Java compiler converts Java source
code into instructions for the Java
Virtual Machine.

These instructions, called bytecodes,
are the same for any computer /
operating system.

A Java interpreter executes bytecodes
on a particular computer.
3-19
Java’s Compiler + Interpreter
Editor

Compiler

Source
code


Hello.java

Bytecodes

Hello.class
Interpreter
Hello,
World!


Interpreter
Hello,
World!

3-20
Why Bytecodes?

Platform-independent.

Load from the Internet faster than source
code.

Interpreter is faster and smaller than it
would be for Java source.

Source code is not revealed to end users.

Interpreter performs additional security
checks, screens out malicious code.
3-21
Java SDK (a.k.a. JDK) —
Software Development Kit

javac

– Java compiler

– generates HTML
documentation
(“docs”) from source
java
– Java interpreter

appletviewer
– tests applets without
a browser

jar
– packs classes into jar
files (packages)
javadoc

jdb
– command-line
debugger
All these are
command-line
tools, no GUI
3-22
Java SDK (cont’d)

Available free from Sun Microsystems.

All documentation is online:
http://java.sun.com/j2se/1.4/docs

Lots of additional Java resources on the
Internet:
http://www.skylit.com/javamethods/appxg.html
3-23
Java IDEs


GUI front end for SDK
Integrates editor, javac, java, appletviewer,
debugger, other tools:
– specialized Java editor with syntax highlighting,
autoindent, tab setting, etc.
– clicking on a compiler error message takes you
to the offending source code line

Some IDEs have their own copy of SDK;
others need SDK installed separately.
3-24
Types of Programs:

Console
applications


GUI applications
Applets
3-25
Console Applications

Simple text dialog:
prompt  input, prompt  input ...  result
C:\javamethods\Ch03> set classpath=.;C:\javamethods\EasyIO
C:\javamethods\Ch03> javac Greetings2.java
C:\javamethods\Ch03> java Greetings2
Enter your first name: Josephine
Enter your last name: Javadoc
Hello, Josephine Javadoc
Congratulations on your third program!
C:\javamethods\Ch03> _
3-26
Greetings2.java
The EasyReader class (written
by the Java Methods authors) is
used for getting keyboard input.
EasyReader.class
public class Greetings2
must be in the same
{
folder as your
public static void main(String[ ] args)
program (or set the
{
classpath to include
EasyReader console = new EasyReader(); its location).
System.out.print("Enter your first name: ");
Prompts String firstName = console.readLine();
System.out.print("Enter your last name: ");
String lastName = console.readLine();
System.out.println("Hello, " + firstName + " " + lastName);
System.out.println("Congratulations on your third program!");
}
}
3-27
Command-Line Arguments
C:\javamethods\Ch03> javac Greetings.java
C:\javamethods\Ch03> java Greetings Josephine Javadoc
Hello, Josephine Javadoc
public class Greetings
{
public static void main(String[ ] args)
{
String firstName = args[ 0 ];
String lastName = args[ 1 ];
System.out.println("Hello, " + firstName + "
}
}
Command-line
arguments are
passed to main
as an array of
Strings.
" + lastName);
3-28
Command-Line Args (cont’d)


Can be used in GUI applications, too
IDEs provide a way to set them
(Or prompt
for them)
Josephine Javadoc
3-29
GUI Applications
Menus
Clickable
panel
Buttons
Slider
3-30
HelloGui.java
import java.awt.*;
import javax.swing.*;
GUI libraries
The ExitButtonListener class
(written by the Java Methods
authors) is used for handling
the “window close” button.
ExitButtonListener.class must
be in the same folder as your
program (or set the classpath to
include its location).
public class HelloGui extends JFrame
{
... < other code >
public static void main(String[ ] args)
{
HelloGui window = new HelloGui();
window.addWindowListener(new ExitButtonListener());
window.setSize(300, 100);
window.show();
}
}
3-31
HelloApplet.java
import java.awt.*;
import javax.swing.*;
public class HelloApplet extends JApplet
{
public void init()
No main in applets: the init
{
method is called by the applet
...
viewer or the browser
}
... < other methods >
}
3-32
Review:






What are some of the current software
development concerns?
What is OOP?
Define inheritance.
What are editor, compiler, linker, debugger
used for?
Define IDE.
How is a compiler different from an
interpreter?
3-33
Review (cont’d):






Name some of the benefits of Java’s
compiler+interpreter approach.
What is a console application?
What are command-line arguments?
What is a GUI application?
What is the difference between a GUI
application and an applet?
Where does the EasyReader class come
from? What does it do?
3-34