Transcript ch03-1

Building Java Programs
Chapter 3:
Introduction to Parameters and Objects
These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2006. They may not be
rehosted, sold, or modified without expressed permission from the authors. All rights reserved.
1
Chapter outline

parameters




return values

calling methods that return values (e.g. the Math class)

writing methods that return values
using objects



passing parameters to static methods
writing methods that accept parameters
String
Point
console input with Scanner objects
2
Parameters

suggested reading: 3.1
3
Reminder: global constants

In the last chapter, we used global constants to fix
"magic number" redundancy problems:
public static final int FIGURE_WIDTH = 5;
public static void main(String[] args) {
drawFigure1();
}
public static void drawFigure1() {
drawPlusLine();
System.out.print("|");
for (int i = 1; i <= 2 * FIGURE_WIDTH; i++) {
System.out.print(" ");
}
System.out.println("|");
}
drawPlusLine();
4
Another repetitive figure

Now consider the task of drawing the following figures:
*************
*******
***********************************
**********
*
*
**********
*****
*
*
*
*
*****

We'd like to structure the output using static methods,
but each figure is different.
5
A redundant solution

Using what we already know, we could write a solution
with the following methods:







A
A
A
A
A
method
method
method
method
method
to
to
to
to
to
draw
draw
draw
draw
draw
a
a
a
a
a
line
line
line
box
box
of
of
of
of
of
13 stars.
7 stars.
35 stars.
stars, size 10 x 3.
stars, size 5 x 4.
These methods would be largely redundant.
Constants would not help us solve this problem,
because we do not want to share one value but instead
want to run similar code with many different values.
6
Parameterization

A better solution would look something like this:



It is possible to write parameterized methods; methods
that are passed information when they are called which
affects their behavior.


A method to draw a line of any number of stars.
A method to draw a box of stars of any size.
Example: A parameterized method to draw a line of stars might
ask us to specify how many stars to draw.
parameter: A value passed into a method by its caller,
which becomes a variable inside the method.

The variable's value affects the method's behavior.
7
Methods with parameters

Declaring a method that accepts a parameter:
public static void <name> ( <type> <name> ) {
<statement(s)> ;
}

Example:
// Prints the given number of spaces.
public static void printSpaces(int count) {
for (int i = 1; i <= count; i++) {
System.out.print(" ");
}
}
8
Passing parameters


Calling a method and specifying a value for its
parameter is called passing a parameter.
Syntax for method call with parameter:
<name> ( <value> );
Example:
System.out.print("*");
printSpaces(7);
System.out.print("**");
int x = 3 * 5;
printSpaces(x + 2);
System.out.println("***");
 Output:

*
**
***
9
How parameters are passed


formal parameter: The variable declared in the
method.
actual parameter: The value written between the
parentheses in the call.

When the program executes, the actual parameter's value is
stored into the formal parameter variable, then the method's
code executes.
printSpaces( 13 );
...
13
public static void printSpaces(int count) {
for (int i = 1; i <= count; i++) {
System.out.print(" ");
}
}
10
Parameterized figure

This code parameterizes two lines of stars:
public static void main(String[] args) {
printLineOfStars(13);
Output:
System.out.println();
*************
printLineOfStars(7);
}
*******
public static void printLineOfStars(int length) {
for (int i = 1; i <= length; i++) {
System.out.print("*");
}
System.out.println();
}
11
Some parameter details

If a method accepts a parameter, it is illegal to call it
without passing any value for that parameter.


// ILLEGAL
The actual parameter value passed to a method must
be of the correct type, matching the type of the formal
parameter variable.


printSpaces();
printSpaces(3.7);
// ILLEGAL -- must be type int
Two methods may have the same name as long as they
accept different parameters (this is called overloading).


public static void printLineOfStars() { ... }
public static void printLineOfStars(int length) { ... }
12
Value semantics

value semantics: When primitive variables (such as int or
double) are passed as parameters in Java, their values are copied
and stored into the method's formal parameter.

Modifying the formal parameter variable's value will not affect the
value of the variable which was passed as the actual parameter.
int x = 23;
strange(x);
System.out.println("2. x = " + x);
...
// this x is unaffected
public static void strange(int x) {
x = x + 1;
// modifies my x
System.out.println("1. x = " + x);
}
Output:
1. x = 24
2. x = 23
13
Multiple parameters

A method can accept more than one parameter,
separated by commas:
public static void <name> ( <type> <name> ,
<type> <name> , ..., <type> <name> ) {
<statement(s)> ;
}

Example:
public static void printPluses(int lines, int count) {
for (int line = 1; line <= lines; line++) {
for (int i = 1; i <= count; i++) {
System.out.print("+");
}
System.out.println();
}
}
14
Parameterized box figure

Write the parameterized method drawBoxOfStars to
match the following code:
public static void main(String[] args) {
drawBoxOfStars(10, 3);
System.out.println();
drawBoxOfStars(4, 5);
}

Answer:
public static void drawBoxOfStars(int width, int height) {
drawLineOfStars(width);
for (int line = 1; line <= height - 2; line++) {
System.out.print("*");
printSpaces(width - 2);
System.out.println("*");
}
drawLineOfStars(width);
15
}
Parameter questions

Rewrite the following program to use the printSpaces and
printLineOfStars methods:
// Draws triangular figures of stars.
public class Loops {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i - 1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 10 - 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
for (int i = 1; i <= 12; i++) {
for (int j = 1; j <= i - 1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 25 - 2 * i; j++) {
System.out.print("*");
}
System.out.println();
}
16
"Parameter mystery"

What is the output of the following program?
public class Mystery {
public static void main(String[] args) {
int x = 5, y = 9, z = 2;
mystery(z, y, x);
System.out.println(x + " " + y + " " + z);
mystery(y, x, z);
System.out.println(x + " " + y + " " + z);
}
public static void mystery(int x, int z, int y) {
x++;
y = x - z * 2;
x = z + 1;
System.out.println(x + " " + y + " " + z);
}
}
17
Parameter questions

Write a method named printDiamond that accepts a
height as a parameter and prints a diamond figure:
*
***
*****
***
*


Write a method named multiplicationTable that
accepts a maximum integer as a parameter and prints
a table of multiplication from 1 x 1 up to that integer
times itself.
Write a method named bottlesOfBeer that accepts an
integer as a parameter and prints the "XX Bottles of
Beer" song with that many verses.
18