Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2006. They.

Similar presentations


Presentation on theme: "1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2006. They."— Presentation transcript:

1 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.

2 2 Chapter outline parameters passing parameters to static methods writing methods that accept parameters return values calling methods that return values (e.g. the Math class) writing methods that return values using objects String Point console input with Scanner objects

3 3 Parameters suggested reading:3.1

4 4 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(); }

5 5 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.

6 6 A redundant solution Using what we already know, we could write a solution with the following methods: A method to draw a line of 13 stars. A method to draw a line of 7 stars. A method to draw a line of 35 stars. A method to draw a box of stars, size 10 x 3. A method to draw a box of 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.

7 7 Parameterization A better solution would look something like this: A method to draw a line of any number of stars. A method to draw a box of stars of any size. It is possible to write parameterized methods; methods that are passed information when they are called which affects their behavior. 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.

8 8 Methods with parameters Declaring a method that accepts a parameter: public static void ( ) { ; } Example: // Prints the given number of spaces. public static void printSpaces(int count) { for (int i = 1; i <= count; i++) { System.out.print(" "); }

9 9 Passing parameters Calling a method and specifying a value for its parameter is called passing a parameter. Syntax for method call with parameter: ( ); Example: System.out.print("*"); printSpaces(7); System.out.print("**"); int x = 3 * 5; printSpaces(x + 2); System.out.println("***"); Output: * ** ***

10 10 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 );... public static void printSpaces(int count) { for (int i = 1; i <= count; i++) { System.out.print(" "); } 13

11 11 Parameterized figure This code parameterizes two lines of stars: public static void main(String[] args) { printLineOfStars(13); System.out.println(); printLineOfStars(7); } public static void printLineOfStars(int length) { for (int i = 1; i <= length; i++) { System.out.print("*"); } System.out.println(); } Output: ************* *******

12 12 Some parameter details If a method accepts a parameter, it is illegal to call it without passing any value for that parameter. printSpaces(); // ILLEGAL The actual parameter value passed to a method must be of the correct type, matching the type of the formal parameter variable. 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) {... }

13 13 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

14 14 Multiple parameters A method can accept more than one parameter, separated by commas: public static void (,,..., ) { ; } 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(); }

15 15 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); }

16 16 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(); }

17 17 "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); }

18 18 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.


Download ppt "1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2006. They."

Similar presentations


Ads by Google