Lecture 8 Parameters and Return Values
Lecture 8 Parameters and Return Values
Computer Programming
*******
***********************************
**********
* *
**********
*****
* *
* *
*****
A redundant solution
public class Stars1 {
public static void main(String[] args) { • This code is redundant.
lineOf13();
lineOf7();
lineOf35();
box10x3();
box5x4(); • What is a better solution?
}
public static void lineOf13() { – line - A method to draw a
for (int i = 1; i <= 13; i++) {
System.out.print("*"); line of any number of stars.
}
System.out.println(); – box - A method to draw a box
}
public static void lineOf7() { of any size.
for (int i = 1; i <= 7; i++) {
System.out.print("*");
}
System.out.println();
}
public static void lineOf35() {
for (int i = 1; i <= 35; i++) {
System.out.print("*");
}
System.out.println();
}
...
Parameterization
• parameter: A value passed to a method by its
caller.
• Example:
public static void sayPassword(int code) {
System.out.println("The password is: " + code);
}
– When sayPassword is called, the caller must specify
the integer code to print.
Passing a parameter
Calling a method and specifying values for its parameters
methodName (expression);
• Example:
public static void main(String[] args) {
sayPassword(42);
sayPassword(12345);
}
Output:
The password is 42
The password is 12345
Parameters and loops
• A parameter can guide the number of repetitions of a loop.
public static void main(String[] args) {
chant(3);
chant(7);
}
// print factorial of 4
int n = 4;
int fact = 1;
for(int i = 2; i <= n ; i++) {
fact *= i;
}
System.out.println(fact);
// print factorial of 7
n = 7;
fact = 1;
for(int i = 2; i <= n ; i++) {
fact *= i;
}
System.out.println(fact);
// print factorial of 9
n = 9;
fact = 1;
for(int i = 2; i <= n ; i++) {
fact *= i;
}
System.out.println(fact);
}
}
Factorial – Solution 2
public static void fact4() {
int n = 4;
int fact = 1;
for(int i = 2; i <= n ; i++) {
fact *= i;
}
System.out.println(fact);
}
fact(4);
fact(7);
fact(9);
}
int fact = 1;
for(int i = 2; i <= n? ; i++) {
fact *= i;
}
System.out.println(fact);
}
}
Multiple parameters
• A method can accept multiple parameters. (separate by , )
– When calling it, you must pass values for each parameter.
• Declaration:
public static void methodName (type name, ..., type name) {
statement(s);
}
• Call:
methodName (value, value, ..., value);
Multiple params example
public static void main(String[] args) {
printNumber(4, 9);
printNumber(17, 6);
printNumber(8, 0);
printNumber(0, 8);
}
public static void printNumber(int number, int count) {
for (int i = 1; i <= count; i++) {
System.out.print(number);
}
System.out.println();
}
Output:
444444444
171717171717
00000000
"Parameter Mystery" problem
public 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); 9
} 2
5
mystery(z, y, x);
mystery(y, x, z); 5
} 9
2
- Math.abs(-
42 4 42)
mai 2
n 2.7
3 1
Math.round(2.71
Returning a value
public static type methodName(parameters) {
statements;
...
return expression;
}
• Example:
// Returns the slope of the line between the given points.
public static double slope(int x1, int y1, int x2, int y2) {
double dy = y2 - y1;
double dx = x2 - x1;
return dy / dx;
}
public static double slope(int x1, int x2, int y1, int y2) {
double dy = y2 - y1;
double dx = x2 - x1;
double result = dy / dx;
return result;
}
Fixing the common error
• Instead, returning sends the variable's value back.
– The returned value must be stored into a variable or used in
an expression to be useful to the caller.
public static void main(String[] args) {
double s = slope(0, 0, 6, 3);
System.out.println("The slope is " + s);
}
public static double slope(int x1, int x2, int y1, int y2) {
double dy = y2 - y1;
double dx = x2 - x1;
double result = dy / dx;
return result;
}
Fixing the common error
(use the returned value in an expression)
public static double slope(int x1, int x2, int y1, int y2) {
double dy = y2 - y1;
double dx = x2 - x1;
double result = dy / dx;
return result;
}
Example (CountFactors)
• Write a method countFactors that returns
the number of factors of an integer.
– countFactors(24) returns 8 because
1, 2, 3, 4, 6, 8, 12, and 24 are factors of 24.
• Solution:
// Returns how many factors the given number has.
public static int countFactors(int number) {
int count = 0;
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
count++; // i is a factor of number
}
}
return count;
}
Returning boolean Example (isPrime)