6.1 introduction to program modules in java
efficient design of large programs requires modular construction
divide and conquer
modules are designed to accomplish specific tasks
modules may be reused through repetitive calls
modular as a program
hiding implementation details
Hierarchical management analogy (boss gives worker task to complete)
modules in Java called methods and classes
In Java programs written by combining new methods and classes with “prepackaged” methods and classes
Java application programming interfaces (APIs)
math, strain, character manipulation, input/output, air or checking, etc.
Hint: familiarize yourself with existing classes and methods in the John API
avoid reinventing the wheel
Example method calls:
Math.sqrt( 900.0) method call, method argument, . operator
(automatic import of java.lang.* package including Math class)
g.drawString( “Hello”) );
sending the drawString message to object g with argument “Hello”
showStatus( “Hello”);
as part of applet, direct method call
Math.sqrt( c1 + d * f )
Math.PI Math.E public static final (constants)
Other Math class methods (fig 6.2 p 206)
6.5 method definitions
definitions:
definition
call
header
argument(s)
parameter(s)
method body
local variables (variable scope)
return vs return expression
coercion of arguments
hint: each method should be limited to performing a single well-defined task
hint: the method should be no longer than a page, mostly
hint: method names should reflect that task
hint: therefore if naming is hard may be that task does too much
Example (fig 6.3 p.207) public int square( int y) { return y * y; }
method square defined as above
parameter y (expected/required argument), local variable, call-by-value
returns integer results
generic method definition template
access-specifier return-value-type method-name ( parameter-list ) {
decorations and statements
}
indent, style guidelines, code blocks
Error: defining a method outside a code block is an error
error (sort of): defining a method inside another method should be avoided, for now (JDK 1.2)
error: to method header to method calls should agree in the number, type, and order
example (fig 6.4 p. 212)
coercion of arguments
promotion rules (fig 6.5 p. 214)
converting large to smaller types results in changed values
mixed type expressions, all promoted to highest type
casting
6.6 the Java API
fig 6.6 p. 215 -- 218
6.7 simulation example
random number generation
double randomValue = Math.random()
generates a double value from 0.0 up to but not including 1.0
n = shifting-value + (int) (Math.random() * scaling-value);
shifting factor == first number in desired range
scaling == width of desired range of values
randomness testexample fig 6.8
change to JApplet?
Example fig 6.9 p. 223 Craps
Comments in programs
init() == GUI setup
WON, LOST, CONTINUE
Formatting constants
addActionListener( this ) line 53
java.awt.event.ActionListener Interface line 7
ActionListener Object as argument
ActionPerformed( ActionEvent e ) line 58
Nested method calls line 60
Assignment: add a running tally
6.9 local/automatic variables
attributes of variables & references: name, type, size, value, duration, and scope
duration equals lifetime
automatic or local duration
static duration
scope equals visibility
class scope
block scope
package scope?
Method scope for labels used with break and continue
hint: the principal of least privilege
6.11 recursion
so far disciplined hierarchical manager and methods
definition: a recursive method is a method that calls itself either directly or indirectly
the recursive method is called to solve a problem.
Method actually knows how to solve only the simplest or base case
if called the base case return the result
if called more complex case divide and call itself with both parts
method launches a fresh copy of itself to work on smaller problem
recursive step execute original call is still open
example factoring N! = N * (N-1)! (fig 6.11,6.12)
example the Fibonacci series (fig 6.13 p. 239)
Side effects & the order of operator evaluation is left to right in Java (wow)
A word of caution about recursion
exponential increase in method invocation
20th value 21,891 method calls
30th value 2,692,537
recursion vs. iteration
both involve repetition
primary negative of recursive methods is call overhead
any problem that can be solved recursively also be solved interactively
avoid www than in perfmance situations
6.14 Method overloading
methods with same name different parameter set
method signature
different return types is not enough
6.15 methods of the JApplet class
public void init(), start(), paint(Graphics g), stop(), destroy()
interesting exercises: 6.14, 6.15, 6.17, 6.23, 6.25, 6.26, 6.37, 6.48