Presentation is loading. Please wait.

Presentation is loading. Please wait.

Method Examples CS 139 Algorithm Development 10/06/2008.

Similar presentations


Presentation on theme: "Method Examples CS 139 Algorithm Development 10/06/2008."— Presentation transcript:

1 Method Examples CS 139 Algorithm Development 10/06/2008

2 SimpleMethod.java System.out.println("Hello from the main method."); displayMessage(); System.out.println("Back in the main method."); System.out.println("Hello from the displayMessage method."); main (String args []) displayMessage() At the point of calling the method, execution suspends and the method carries out its task. println(String output)

3 Two part compilation – SimpleMethodDriver and SimpleContainer System.out.println("Hello from the main method."); SimpleContainer.displayMessage(); System.out.println("Back in the main method."); System.out.println("Hello from the displayMessage method."); main (String args []) displayMessage() SimpleMethodDriver SimpleContainer

4 DeeperAndDeeper.java System.out.println("I am starting in main."); deep(); System.out.println("Now I am back in main."); System.out.println("I am now in deep."); deeper(); System.out.println("Now I am back in deep."); System.out.println("I am now in deeper."); main() deep() deeper()

5 Stack trace. We can’t control Scanner, but we can find the problem in our code or where that problem occurred. ----jGRASP exec: java DeepAndDeeper I am starting in main. I am now in deep. I am now in deeper. Type something: a Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at DeepAndDeeper.deeper(DeepAndDeeper.java:40) at DeepAndDeeper.deep(DeepAndDeeper.java:23) at DeepAndDeeper.main(DeepAndDeeper.java:11) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.

6 LocalVars.java Illustration of block {} scope in Java. Birds are two different variables and are visible only within their respective methods. texas(); california(); int birds; birds = 5000; System.out.println("In Texas there are birds + " birds."); int birds; birds = 3500; System.out.println("In California there are birds + " birds."); main() texas() california() As we leave texas, birds is destroyed As we leave california, birds is destroyed LocalVars.java

7 PassArg.java demo of different kinds of values that may be passed to a method public class PassArg { public static void main(String[] args) { int x; x = 10; System.out.println("I am passing values to displayValue."); displayValue(5); // Pass 5 (literal) displayValue(x); // Pass 10 (variable) displayValue(x * 4); // Pass 40 (expression) displayValue(Integer.parseInt("700")); // Pass 700 (method call) System.out.println("Now I am back in main."); } /** The displayValue method displays the value of its integer parameter. */ public static void displayValue(int num) { System.out.println("The value is " + num); } } Any value can be passed as long as the type of the value matches the type of the parameter.

8 5-8 Defining a Value-Returning Method public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } Return type This expression must be of the same data type as the return type The return statement causes the method to end execution and it returns a value back to the statement that called the method.

9 ValueReturn.java public class ValueReturn { public static void main(String[] args) { int total; int value1; int value2; value1 = 20; value2 = 40; total = sum(value1, value2); System.out.println("The sum of " + value1 + " and " + value2 + " is " + total); } public static int sum(int num1, int num2) { int result; // result is a local variable result = num1 + num2; return result; } }

10 ValueReturn.java public class ValueReturn { public static void main(String[] args) { int total; int value1; int value2; value1 = 20; value2 = 40; total = sum(value1, value2); System.out.println("The sum of " + value1 + " and " + value2 + " is " + total); } public static int sum(int num1, int num2) { int result; // result is a local variable result = num1 + num2; return result; // The integer value is returned and “replaces” the method call. } }

11 Stubs – A “stub” is a working method which is holding the place while you work on other parts of the program. This program will compile. public class ValueReturn { public static void main(String[] args) { int total; int value1; int value2; value1 = 20; value2 = 40; total = sum(value1, value2); System.out.println("The sum of " + value1 + " and " + value2 + " is " + total); } // documentation goes here public static int sum(int num1, int num2) { return -9999999; } }


Download ppt "Method Examples CS 139 Algorithm Development 10/06/2008."

Similar presentations


Ads by Google