slide1.3_2009 - Module 1.3 Module Control Statements and...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Module 1.3 Module Control Statements and Methods Methods EE3206/EE5805 Java Programming & Applications EE3206/EE5805 1 Intended Learning Outcomes To use various control statements for common selection and repetition tasks and To declare methods, invoke methods, and pass arguments to a method. arguments To use method overloading and know ambiguous overloading. overloading. To learn the concept of method abstraction. To To design and implement methods using stepwise refinement. refinement. To determine the scope of local variables. To To use the Math class for mathematic calculations. Math EE3206/EE5805 Java Programming & Applications EE3206/EE5805 2 Control Statements There are a few control statements same as that of C language. They are used to control the flow of a program. control • • • • if, if-else switch for-loop, while-loop, do-while-loop break, continue EE3206/EE5805 Java Programming & Applications EE3206/EE5805 3 What is Method? • A method is: • a collection of statements that are grouped together to perform an operation. • an execution routine or a building block. • the counterpart of the function in C language Define a method modifier method header method body return value type method name formal parameters Invoke a method public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 int z = max(x, y); actual parameters (arguments) parameter list return value 4 Understanding Methods • • • Method signature is the combination of the method name and the parameter list. The variables defined in the method header are known as formal parameters. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. • public static void main(String args) {……} • • EE3206/EE5805 Java Programming & Applications EE3206/EE5805 5 Calling Static Methods Method is an execution routine for reuse. There are two types of method and they are invoked in different ways. types 1. Static method – invoked through its associated class name. Static 2. Non-static method – invoked through its associated object static invoked name (we will talk more about this in next module) name For example, a static method max() is defined in a class max() is TestMax. You can then invoke the method using TestMax You TestMax.max(). TestMax.max • ClassName.methodName TestMax Run EE3206/EE5805 Java Programming & Applications EE3206/EE5805 6 Call Stacks Call Call Stack is a memory space to hold data of a call. • • When the current scope is switched by invoking other function call, data of the current scope will be temporarily stored in call stack. When a function call returns, its data will be erased from the stack and the scope switches back to the previous one. See the next animation. Space required for the max method result: 5 num2: 2 num1: 5 Space required for the main method k: j: 2 i: 5 Space required for the main method k: j: 2 i: 5 Space required for the main method k: 5 j: 2 i: 5 Stack is empty The main method is invoked. The max method is invoked. The max method is finished and the return value is sent to k. The main method is finished. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 7 animation Trace Call Stack i is declared and initialized public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 8 i: 5 The main method is invoked. animation Trace Call Stack j is declared and initialized public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 9 j: 2 i: 5 The main method is invoked. animation Trace Call Stack Declare k public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 10 Space required for the main method k: j: 2 i: 5 The main method is invoked. animation Trace Call Stack Invoke max(i, j) public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 11 Space required for the main method k: j: 2 i: 5 The main method is invoked. animation Trace Call Stack pass the values of i and j to num1 and num2 public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 num2: 2 num1: 5 Space required for the main method k: j: 2 i: 5 The max method is invoked. 12 animation Trace Call Stack pass the values of i and j to num1 and num2 public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 result: num2: 2 num1: 5 Space required for the main method k: j: 2 i: 5 The max method is invoked. 13 animation Trace Call Stack (num1 > num2) is true public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 result: num2: 2 num1: 5 Space required for the main method k: j: 2 i: 5 The max method is invoked. 14 animation Trace Call Stack Assign num1 to result public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } Space required for the max method result: 5 num2: 2 num1: 5 Space required for the main method k: j: 2 i: 5 The max method is invoked. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 15 animation Trace Call Stack Return result and assign it to k public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } Space required for the max method result: 5 num2: 2 num1: 5 Space required for the main method k:5 j: 2 i: 5 The max method is invoked. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 16 animation Trace Call Stack Execute print statement public static void main(String args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 17 Space required for the main method k:5 j: 2 i: 5 The main method is invoked. Passing Arguments In Java, there are two ways to pass an argument • All primitive data are passed by VALUE. • Objects are passed by REFERENCE. Therefore object type is also known as reference type. Testing Pass by value This program demonstrates passing values to the methods. TestPassByValue Run EE3206/EE5805 Java Programming & Applications EE3206/EE5805 18 Pass by Value The values of num1 and num2 are passed to n1 and n2. Executing swap does not affect num1 and num2. Space required for the swap method temp: n2: 2 n1: 1 Space required for the main method num2: 2 num1: 1 Space required for the main method num2: 2 num1: 1 Space required for the main method num2: 2 num1: 1 Stack is empty The main method is invoked The swap method is invoked The swap method is finished The main method is finished EE3206/EE5805 Java Programming & Applications EE3206/EE5805 19 Overloading Methods Java compiler identifies the method being invoked by the method signature. invoked • Method signature = method name + parameter lists Two methods can be declared with different signatures, hence using same name but different parameter lists is allowed. parameter This is called method overloading. method TestMethodOverloading Run EE3206/EE5805 Java Programming & Applications EE3206/EE5805 20 Ambiguous Invocation Sometimes there may be two or more possible matches for an Sometimes invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. ambiguous Ambiguous invocation is a compilation error. Ambiguous public class AmbiguousOverloading { public AmbiguousOverloading public static void main(String args) { public args System.out.println(max(1, 2)); )); } public static double max(int num1, double num2) { public max(int num1, if (num1 > num2) if return num1; else return num2; } public static double max(double num1, int num2) { public int if (num1 > num2) return num1; else return num2; return } } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 21 Method Abstraction You can think of the method body as a black box that contains the You detailed implementation for the method. detailed Allow program designers to plan system functions without going into Allow nto the implementation details. Benefit of Methods Benefit • • • Write a method once and reuse it anywhere. Information hiding. Hide the implementation from the user. Reduce complexity. Build with building blocks. Optional arguments for Input Optional return value Method Signature Black Box Method body EE3206/EE5805 Java Programming & Applications EE3206/EE5805 22 Scope of Variables Data field (of class) Data • A variable defined inside a class but outside a method. Local variable (of method) Local • A variable defined inside a method. Scope Scope • the part of the program where the variable can be “seen” or the or referenced. referenced. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used, but cannot be re-declared in the same scope. cannot EE3206/EE5805 Java Programming & Applications EE3206/EE5805 23 Scope of Local Variables A variable declared in the initial action part of a for loop for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope for loop limited in the loop body from its declaration to the end of the block that contains the variable. the The scope of i The scope of j public static void method1() { . . for (int i = 1; i < 10; i++) { . . // here cannot see j . int j; . . . } } 24 EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Scope of Local Variables It is fine to declare i in two non-nesting blocks public static void method1() { int x = 1; int y = 1; for (int i = 1; i < 10; i++) { x += i; } for (int i = 1; i < 10; i++) { y += i; } } } It is wrong to declare i in two nesting blocks public static void method2() { int i = 1; int sum = 0; for (int i = 1; i < 10; i++) { sum += i; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 25 Introducing Math Class Introducing Math The Math class is a standard Java API class that provides functionalities of mathematical calculation. calculation. Class constants: Class • PI •E Class methods: (all are static methods) Class • • • • Trigonometric Methods Trigonometric Exponent Methods Rounding Methods min, max, abs, and random Methods EE3206/EE5805 Java Programming & Applications EE3206/EE5805 26 Trigonometric and Exponent Methods sin(double a) cos(double a) tan(double a) acos(double a) asin(double a) atan(double a) exp(double a) Returns e raised to the power of a. log(double a) Returns the natural logarithm of a. log10(double a) Returns the 10-based logarithm of a. pow(double a, double b) Returns a raised to the power of b. sqrt(double a) Returns the square root of a. Examples: Math.sin(0) returns 0.0 Math.sin(0) Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.sin(Math.PI Math.cos(0) returns 1.0 Math.cos(Math.PI / 6) returns 0.866 6) Math.cos(Math.PI / 2) returns 0 Math.cos(Math.PI 2) Math.exp(1) returns 2.71 Math.log(2.71) returns 1.0 Math.pow(2, 3) returns 8.0 Math.pow(3, 2) returns 9.0 Math.pow(3.5, 2.5) returns 22.91765 Math.sqrt(4) returns 2.0 Math.sqrt(4) Math.sqrt(10.5) returns 3.24 27 EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Rounding Methods double ceil(double x) x rounded up to its nearest integer. This integer is returned as a double value. double floor(double x) x is rounded down to its nearest integer. This integer is returned as a double value. double rint(double x) x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double. int round(float x) Return (int)Math.floor(x+0.5) long round(double x) Return (long)Math.floor(x+0.5) // -0.5 for negative x // -0.5 for negative x 28 EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Rounding Methods Examples Math.ceil(2.1) returns 3.0 Math.ceil(2.1) Math.ceil(2.0) returns 2.0 Math.ceil(2.0) Math.ceil(-2.0) returns –2.0 2.0) Math.ceil(-2.1) returns -2.0 2.1) Math.floor(2.1) returns 2.0 Math.floor(2.0) returns 2.0 Math.floor(-2.0) returns –2.0 2.0) Math.floor(-2.1) returns -3.0 2.1) Math.rint(2.1) returns 2.0 Math.rint(2.0) returns 2.0 Math.rint(-2.0) returns –2.0 2.0) Math.rint(-2.1) returns -2.0 2.1) Math.rint(2.5) returns 2.0 Math.rint(-2.5) returns -2.0 2.5) Math.round(2.6f) returns 3 Math.round(2.6f) Math.round(2.0) returns 2 Math.round(-2.0f) returns -2 Math.round( 2.0f) Math.round(-2.5) returns -3 Math.round( 2.5) EE3206/EE5805 Java Programming & Applications EE3206/EE5805 29 min, max, and abs max(a, b)and min(a, b) max(a, and min(a, Returns the maximum or Returns minimum of two parameters. minimum Examples: Math.max(2, 3) returns 3 Math.max(2, Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs( Math.abs(-2.1) returns 2.1 abs(a) abs(a) Returns the absolute value of Returns the parameter. the EE3206/EE5805 Java Programming & Applications EE3206/EE5805 30 The random Method The random random() returns a random double value in the range [0.0, 1.0). double value 0 <= Math.random() < 1.0 <= Examples: (int)(Math.random() * 10) Returns a random integer between 0 and 9. Returns a random integer between 50 and 99. 50 + (int)(Math.random() * 50) In general, In a + Math.random() * b Returns a random number between a and a + b, excluding a + b. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 31 ...
View Full Document

This note was uploaded on 04/20/2010 for the course ELECTRONIC EE3206 taught by Professor Tingchiwang during the Spring '10 term at École Normale Supérieure.

Ask a homework question - tutors are online