Transcript ch03_notes

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
Requirements for code to be
consider reusable




Code is divided into small components
that have a clear purpose.
Each component must be well
documented.(what is does, not how)
The components must be thoroughly
tested and provide error handling.
Components should be customizable.
3-5
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-6
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-7
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-8
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-9
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-10
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-11
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-12
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-13
OOP Benefits

Facilitates team development

Easier to reuse software components
and write reusable software

Easier GUI (Graphical User Interface)
and multimedia programming
3-14
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-15
The First “Bug”
“(moth) in relay”
Mark II Aiken Relay Calculator (Harvard University, 1945)
3-16
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-17
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 - text of the
program)
3-18
Compiler vs.
Interpreter

Compiler:
checks syntax
Converts a
program int
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-19
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-20
Java’s Compiler + Interpreter
Editor

Compiler

Source
code


Hello.java

Bytecodes

Hello.class
Interpreter
Hello,
World!


Interpreter
Hello,
World!

3-21
JIT – used to speed up the
process of loading applets.



JIT (Just-In-Time) compilers is the name
of a new software technology that has
emerged.
A JIT compiler combined the features of
a compiler and an interpreter.
While interpreting bytecodes it also
compiles them into executable code.
3-22
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-23
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-24
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-25
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-26
Types of Programs:

Console
applications


GUI applications
Applets
3-27
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-28
Greetings.java
System is a class that is imported
into all java programs.
import javax.swing.*;
out is a object in this class.
public class Greetings
{
public static void main(String[ ] args)
{
String fname = “”;
String lname = “”;
fname = JOptionPane.showInputDialog(Enter first name);
lname = JOptionPane.showInputDialog(Enter last name);
System.out.println("Hello, " + fName + " " + lName);
}
}
Note: Names of classes must begin with
a capital letter.
3-29
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-30
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-31
GUI Applications
Menus
Clickable
panel
Buttons
Slider
3-32
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-33
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-34