Introduction
To Java - MFC 158 G
Week
6 Lecture notes - Fall 2000
Chapter 6 - Methods - MFC 158 (1 of 3)
I.
Programming in
Modules
A.
Modules are made up of classes and methods, combining
1.
New classes and methods the programmer writes
2.
"Prepackaged" methods and classes from the Java
API
a)
math, string, character manipulation, IO routines, and many
others.
B.
Methods
1.
Modularizes a program
2.
Should be created for pieces of code that needs to be called
several times.
3.
When using a method call - information is passed using arguments.
4.
Allows the 'divide-and-conquer' approach to problem solving.
5.
If you can't choose a concise name for a method, it may be
performing too many diverse tasks (break up into smaller methods).
Double a =
Math.sqrt(64); // return square root
of 64 and assign to a System.out.println(
Math.sqrt( 900) ); // nested method calls System.out.println(
Math.sqrt( c1 + d + f ) ); // using an expression as a method argument |
Import
java.awt.Container; import
javax.swing.*; public class
SquareInt extends JApplet { public void init() { String output = ""; JTextArea outputArea = new JTextArea(
10, 20 ); // get the applet's GUI component
display area Container c = getContentPane(); // attach outputArea to Container c c.add( outputArea ); int result; for ( int x = 1; x <= 10; x++ ) { result = square( x ); output += "The square of
" + x + " is " + result
+ "\n"; } outputArea.setText( output ); } // square method definition public int square( int y ) { return y * y; } } // (C)
Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. * |
-
the
on-screen display area for a Japplet has a content
pane to which the GUI components must be attached, in order to be displayed
(Container c = getContentPane( ); )
-
attach
the JTextArea outputArea to the container (c)
-
concatenate
several lines of text delimited by \n into String output
-
set
the outputArea with method setText and send the argument output to it. (displays the outputArea
to the applet's window.
3 ways to call
a method
-
a
method name by itself (contained in the same class) - square ( x )
-
a
reference to an object followed by a dot - g.drawLine(x1,y1,x2,y2)
-
a
class name followed by a method name -
Integer.parseInt(stringToConvert)
3 ways to
return control from a method
-
using
the statement return;
-
using
the statement return expression;
-
when
encountering the end of method right brace
}
Example of
nested methods
public
double maximum( double x, double y, double z ) { return Math.max( x, Math.max( y, z )
); } |
Coercion of
arguments and promotion of data types
-
we
can move data between variables of different data types (may or may not lose
data)
-
float
a; int b; {a = b} or {b = a} which assignment can cause a loss
of data?
-
We
can promote to larger types, promoting to smaller types can result in different
values.
-
The
compiler forces us to type cast values when promoting to smaller types
-
Int
a = (int) 2.71; // will lose the decimal
portion - truncate
A
table of allowable promotions is on pg 214.
Java API
Packages
-
A
strength in Java is the availability of a large number of classes in the
packages of Java API
-
Again,
avoid reinventing the wheel - get familiarized with what's out there
-
Table
on pg 215-218 shows a variety of reusable components.
-
Refer
to java.sun.com for a detailed listing of components.
Random Number
Generation
-
useful
for test data
-
useful
for games (the "other" player's personality)
double
randomValue = Math.random( ); //generates a double from 0.0 up to
1.0 -but not including
int a = (int)
(Math.random( ) * 6) // Produces value
range of 0-5 (* 6) causes scaling.
Truncates floating portion.
Refer
to Pg 220 for an example of obtaining statistics of how random the 'randomness'
is.
Duration and
Scope of Identifiers
The
attributes of variables and references
include name, type, size and value.
Two other attributes are duration and scope.
-
duration
is the period during which the identifier exists in memory.
-
automatic
duration conserves memory as they are created when control reaches their
declaration and destroyed when the block they reside in terminates. (local
variable in method)
-
Scope
is where an identifier can be referenced in a program (some can be referenced
throughout a program (instance variables) and others, such as local variables
are restricted)
-
Class
scope - a variable is accessible throughout the class (instance variables)
-
Block
scope - begins at a declaration and ends at the terminating right brace
for
(int I = 0; I <=10; I++)
{
take_some_action() }
-
It's
not legal to define the same variable into a deeper nested { } of the same name
-
It
IS legal to define a local variable of the same name as an instance variable,
but not a good idea. The local variable
will hide the instance variable until the local block of code ends.
An
example of scope is on page 233
Method
overloading
-
Java
enables several methods of the same name to be defined as long as the argument
lists are different (based on number of parameters, types of parameters and
order of them).
-
When
an overloaded method is called, the Java compiler selects the proper method
based upon what's being used
-
Commonly
used for methods that do similar tasks but use different data types
outputArea.setText( "The square of integer 7 is
" + square( 7 ) + "\nThe square of double 7.5 is
" + square( 7.5 ) ); } public int square( int x ) { return x * x; } public double square( double y ) { return y * y; } |