Building Java Programs Chapter 3 Introduction to Parameters
Building Java Programs Chapter 3: Introduction to Parameters and Objects 1
• parameters Chapter outline – passing parameters to static methods – writing methods that accept parameters • methods that return values – calling methods that return values (e. g. the Math class) – writing methods that return values • using objects – String objects – Point objects – console input with Scanner objects 2
Parameters reading: 3. 1 3
Section 3. 1 Parameters • Consider the task of drawing the following figures: ********************* ***** * * ***** – The lines and figures are similar, but not exactly the same. 4
public class Stars 1 { public static void main(String[]args) { draw. Line. Of 13 Stars(); draw. Line. Of 7 Stars(); draw. Line. Of 35 Stars(); draw 10 x 3 Box(); draw 5 x 4 Box(); } The methods at left are redundant. Would constants help us solve this public static void draw. Line. Of 13 Stars() { problem? - No for (int i = 1; i <= 13; i++) { System. out. print("*"); What would be a better solution? } System. out. println(); } public static void draw. Line. Of 7 Stars() { for (int i = 1; i <= 7; i++) { System. out. print("*"); } System. out. println(); } public static void draw. Line. Of 35 Stars() { for (int i = 1; i <= 35; i++) { System. out. print("*"); } System. out. println(); }. . . draw. Line - A method to draw a line of any number of stars. draw. Box - A method to draw a box of any size. 5
• parameterized method: One that is given extra information (e. g. number of stars to draw) when it is called. • parameter: A value passed to a method by its caller. • Writing parameterized methods requires 2 steps: – write the method to accept the parameter – call the method and pass the parameter value(s) desired main 7 13 draw. Line ************* 6
• Parameterized method declaration syntax: public static void <name> (<type> <name>){ <statement(s)> ; } Example: public static void print. Spaces(int count) { for (int i = 1; i <= count; i++) { System. out. print(" "); } } • Whenever print. Spaces is called, the caller must specify how many spaces to print. Example: print. Spaces(13); // The value 13 is copied into count 7
public static void main(String[] args) { for (int row = 1; row <= 6; row++) { print. Stars(row); System. out. println(); } } Actual Parameter or Argument Each time print. Stars(row) is called, the value of row is copied into num. Formal Parameter // print num stars horizontally w/o spaces public static void print. Stars(int num) { for (int i = 1; i <= num; i++) { System. out. print("*"); } } First, row = 1, print. Stars(1) is called. 1 is copied into num. row = 2, print. Stars(2) is called. 2 is copied into num. row = 3, print. Stars(3) is called. 3 is copied into num. row = 4, print. Stars(4) is called. 4 is copied into num. row = 5, print. Stars(5) is called. 5 is copied into num. row = 6, print. Stars(6) is called. 6 is copied into num. row = 7, 7 <= 6? , false, exit loop Output: * ** ****** Loops 1 time, returns to main Loops 2 times, returns to main Loops 3 times, returns to main Loops 4 times, returns to main Loops 5 times, returns to main Loops 6 times, returns to main 8
When passing parameters to a method, the value of the actual parameter is copied into the formal parameter. memory The formal and actual parameters occupy their own places in memory. Actually, the formal parameter is allocated no space until the method is invoked. public static void main(String[] args) { for (int row = 1; row <= 6; row++) { print. Stars(row); System. out. println(); } } // print num stars public static void print. Stars(int num) { for (int i = 1; i <= num; i++) { System. out. print("*"); } } 1 row 1 num 9
• When the parameterized method call executes: – the argument value is copied into the parameter variable – the method's code executes using that value public static void main(String[] args){ print. Spaces(7); As soon as count 13 print. Spaces(13); print. Spaces(7) is called, the formal } parameter count is allocated space in memory public static void print. Spaces(int count) { for (int i = 1; i <= count; i++) { System. out. print(" "); } } 10
• When the parameterized method call executes: – the argument value is copied into the parameter variable – the method's code executes using that value public static void main(String[] args){ print. Spaces(7); count 13 print. Spaces(13); 77 } public static void print. Spaces(int count) { for (int i = 1; i <= count; i++) { System. out. print(" "); } } 11
• When the parameterized method call executes: – the argument value is copied into the parameter variable – the method's code executes using that value public static void main(String[] args){ print. Spaces(7); count 13 print. Spaces(13); } public static void print. Spaces(int count) { for (int i = 1; i <= count; i++) { System. out. print(" "); } } 12
• Call by value semantics: When primitive variables (int, double) are passed as parameters, their values are copied. – Modifying the parameter inside the method will not affect the variable passed in. public static void main(String[] args) { int x = 23; strange(x); System. out. println("2. x = " + x); . . . } // unchanged Output: public static void strange(int x) { x = x + 1; 1. x = 24 System. out. println("1. x = " + x); 2. x = 23 } Why? Modularization. I can give any name to a variable inside a method and it is unknown outside that method. This includes the names of parameters. The parameter x in strange is declared within strange and unknown in main. 13
Common errors • If a method accepts a parameter, it is illegal to call it without passing any value for that parameter. strange(); // ERROR: parameter value required • The value passed to a method must be of the correct type, matching the type of its parameter variable. strange(3. 7); // ERROR: must be of type int • You will do this in Lab 3. 14
// Prints several lines of stars. // Uses a parameterized method to remove redundancy. public class Stars 2 { public static void main(String[] args) { draw. Line(13); draw. Line(7); draw. Line(35); } // Prints the given number of stars plus a line break. public static void draw. Line(int count) { for (int i = 1; i <= count; i++) { System. out. print("*"); } System. out. println(); } } Output: ******************** 15
• Methods can accept multiple parameters. – The parameters are separated by commas. – When the method is called, it must be passed values for each of its parameters. • Multiple parameters declaration syntax: public static void <name> ( <type> <name> , . . . , <type> <name> ) { <statement(s)> ; } • Multiple parameters call syntax: <name> ( <expression>, . . . , <expression> ); 16
Often have multiple parameters // print the specified character num times public static void print. Chars(int num, char c) { for (int i = 1; i <= num; i++) { System. out. print(c); } } Examples: print. Chars(4, '$'); $$$$ print. Chars(6, '>'); >>>>>> print. Chars(12, '@'); @@@@@@ 17
public static void main(String[] args) { print. Number(4, 9); print. Number(17, 6); print. Number(8, 0); print. Number(0, 8); } public static void print. Number(int number, int count) { for (int i = 1; i <= count; i++) { System. out. print(number); } System. out. println(); } Output: 44444 171717 0000 // Why the blank line? A: count is 0. 18
Stars solution // Prints several lines and boxes made of stars. // Uses multiple parameterized methods. public class Stars 3 { public static void main(String[] args) { draw. Line(13); draw. Line(7); draw. Line(35); System. out. println(); draw. Box(10, 3); draw. Box(5, 4); draw. Box(20, 7); } // Prints the given number of stars plus a line break. public static void draw. Line(int count) { for (int i = 1; i <= count; i++) { System. out. print("*"); } System. out. println(); }. . . 19
. . . Stars solution, cont'd. // Prints a box of stars of the given size. public static void draw. Box(int width, int height) { draw. Line(width); for (int i = 1; i <= height - 2; i++) { System. out. print("*"); print. Spaces(width - 2); System. out. println("*"); } } } draw. Line(width); // Prints the given number of spaces. public static void print. Spaces(int count) { for (int i = 1; i <= count; i++) { System. out. print(" "); } } 20
public class Print. Chars. Example { public static void main(String[] args) { print. Chars(20, '='); // top row System. out. println(); for(int i = 1; i <= 10; i++) { // 1 row per loop print. Chars(i, '>'); print. Chars(20 - 2 * i, ' '); print. Chars(i, '<'); System. out. println(); } } } print. Chars(20, '='); System. out. println(); // bottom row public static void print. Chars(int num, char c) { for (int i = 1; i <= num; i++) { ========== > < System. out. print(c); >> << } >>> <<< } >>>> <<<< >>>>> <<<<< >>>>>> <<<<<< >>>>>>> <<<<<<< >>>>> <<<<< >>>>><<<<< ========== 21
22
23
24
- Slides: 24