49
Parameterized Recipe
Recipe for baking
20
cookies:
Mix the following ingredients in a bowl:
•
4
cups flour
•
1
cup butter
•
...
Recipe for baking
N
cookies:
Mix the following ingredients in a bowl:
•
N/5
cups flour
•
N/20
cups butter
•
N/20
cups sugar
•
N/10
eggs
•
2N
bags chocolate chips ...
Place on sheet and Bake for about 10 minutes.
50
Parameters
Up to now we have defined only
parameterless
methods,
though we have used built-in methods that have parameters.
Consider a Scratch 2.0 example:
formal
parameter
actual
argument
51
Parameters in Java Methods
A method can accept multiple parameters (separated by
,
)
When calling it, you must pass an actual value for each parameter.
Declaration
:
static void
methodName
(
type
1
param
1
,
...
,
type
n
param
n
)
{
Java statement[s]
}
Call
:
methodName
(expr
1
, expr
2
, ... ,
expr
n
);

52
Multiple Parameters Example
public static void main (String [] args)
{
printNumber (4, 9);
printNumber (17, 6);
printNumber (8, 0);
printNumber (0, 8);
}
// what output does this program produce?
static void printNumber (int number, int count)
{
for (int i = 1; i <= count; i++)
{
System.out.print(number);
}
System.out.println();
}
53
A "Parameter Mystery" Problem
class ParameterMystery {
public static void main(String[] args) {
int x = 9;
int y = 2;
int z = 5;
mystery (z, y, x);
mystery (y, x, z);
}
static void mystery (int x, int z, int y)
{
System.out.println (z + " and " + (y - x));
}
}
54
Song Generation
How to print the words for "The Ants Came
Marching” by observing a basic pattern:
The ants came marching 1 by 1, hurrah!
Hurrah!
The ants came marching 1 by 1, hurrah!
Hurrah!
The ants came marching 1 by 1,
The little one stopped to suck his thumb.
And they all go marching down.
To the ground.
To get out of the rain!
(Boom, boom, boom ... )
The ants came marching 2 by 2, hurrah!
Hurrah!
The ants came marching 2 by 2, hurrah!
Hurrah!
The ants came marching 2 by 2,
The little one stopped to tie his shoe.
And they all go marching down.
To the ground.
To get out of the rain!
(Boom, boom, boom ... )
…

55
Song Generation, continued
public static void main (String [] args)
{
verse (1, "suck his thumb.\n");
verse (2, "tie his shoe, \n");
verse (3, "climb a tree, \n");
…
…
}
// see MarchingAnts.java
56
The boolean Data Type
The
boolean
data type is named after the
mathematician George Boole (1815-64), a pioneer
in the study of formal logic.
Some simple examples of
boolean
expressions
(b*b - 4.0*a*c)
>=
0.0
// non-neg discrim.
(year % 100)
== 0
//
beginning of a century
One can also declare and use boolean variables and parameters.
57
Lines and Boxes Example
Consider the following main method:
public static void main( String [] args)
{
line (13, '*', true);
line (7, '/', false);
line (35, '%', true);
System.out.println();
box (10, 3, '*');
box (5, 4, '=');
box (20, 7, '$');
}
// see Boxes.java

58
Exchange Value of 2 Variables
Intended effect
final value of
a
becomes the
initial value of b
final value of
b
becomes the
initial value of a
Tempting, but wrong:
a
= b;
// now a is
???

#### You've reached the end of your free preview.

Want to read all 13 pages?

- Fall '17
- HenryH.Leitner