Java Programming Language Chapter 5 Methods Liang Introduction































- Slides: 31

Java Programming Language Chapter 5 Methods Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 1

Introducing Methods A method statements that are grouped together to perform an operation. 2




Parameter List ������������ argument ของ method������ เมอมการ ���� method นน F ��������� argument �� จาก method ������ parameter list F ������ parameter list ������� 1 ��� ใน method ������ , (comma) ระหวาง parameter list F F Example static viod min(int a, int b) static int min(int a, int b) static void max() 6

animation Calling Methods 7

animation Trace Method Invocation i is now 5 8

animation Trace Method Invocation j is now 2 9

animation Trace Method Invocation invoke max(i, j) 10

animation Trace Method Invocation invoke max(i, j) Pass the value of i to num 1 Pass the value of j to num 2 11

animation Trace Method Invocation declare variable result 12

animation Trace Method Invocation (num 1 > num 2) is true since num 1 is 5 and num 2 is 2 13

animation Trace Method Invocation result is now 5 14

animation Trace Method Invocation return result, which is 5 15

animation Trace Method Invocation return max(i, j) and assign the return value to k 16

animation Trace Method Invocation Execute the print statement 17

Overloading Methods Overloading the max Method public static double max(double num 1, double num 2) { } if (num 1 > num 2) return num 1; else return num 2; public static int max(int num 1, int num 2 } ( if (num 1 > num 2 ( return num 1 ; else return num 2; { 18

Scope of Local Variables // Fine with no errors public static void correct. Method() { int x = 1; int y = 1; // i is declared for (int i = 1; i < 10; i++) { x += i; } // i is declared again for (int i = 1; i < 10; i++) { y += i; } } 19

Scope of Local Variables, cont. // With no errors public static void incorrect. Method() { int x = 1; int y = 1; for (int i = 1; i < 10; i++) { int x = 0; x += i; } } 20

method substring() ���������� (substring( F ������ msg. substring(n, m; ( F • ������������� n (������������� 0)������������ m-1 • msg ��� ชอตวแปร String Object ���������� • substring ����� method substring ซงอยใน class String F Example String message = “Technic Bangkok”; String msg = message. substring(8, 15)+”Campus”; 21


Method length() F method length() F �������� ตวแปร String • • ������ msg. length; () msg คอตวแปร String ทตองการหาคาความยาวของตวแปร length() เปน method ทอยใน class String Example String input = “Campus”; //กำหนดคาตวแปร input int n = input. length(); //return n = 6 System. out. println(n; ( String ชอ 23

class Number. Format import java. text. * ; double tax = 23. 436742 ; ตำแหนง //ตองทำการ import class text //กำหนดคาตวแปร tax เปนตวเลขทศนยม 6 //����� Format ������ Number. Format formatter =Number. Format. get. Number. Instance; () //��������� 2 �������������� formatter. set. Maximum. Fraction. Digits(2 ; ( //������ 0 ������������ formatter. set. Minimum. Fraction. Digits(2; ( //พมพขอมลออกทางจอภาพจาก object formatter System. out. println("Tax: $" + formatter. format(tax; (( 24

The Math Class F Class constants: § PI §E F Class § § methods: Trigonometric Methods Exponent Methods Rounding Methods min, max, abs, and random Methods 25

Trigonometric Methods F sin(double a) F cos(double a) F tan(double a) F acos(double a) F asin(double a) F atan(double a) Examples: Math. sin(0) returns 0. 0 Math. sin(Math. PI / 6) returns 0. 5 Math. sin(Math. PI / 2) returns 1. 0 Math. cos(0) returns 1. 0 Math. cos(Math. PI / 6) returns 0. 866 Math. cos(Math. PI / 2) returns 0 Radians to. Radians(90) 26

Exponent Methods F exp(double a) Returns e raised to the power of a. F log(double a) Returns the natural logarithm of a. F log 10(double a) Returns the 10 -based logarithm of a. F pow(double a, double b) Returns a raised to the power of b. F sqrt(double a) Returns the square root of a. Examples: 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(10. 5) returns 3. 24 27

Rounding Methods F double ceil(double x) x rounded up to its nearest integer. This integer is returned as a double value. F double floor(double x) x is rounded down to its nearest integer. This integer is returned as a double value. F int round(float x) Return (int)Math. floor(x+0. 5). F long round(double x) Return (long)Math. floor(x+0. 5). 28

Rounding Methods Examples Math. ceil(2. 1) returns 3. 0 Math. ceil(2. 0) returns 2. 0 Math. ceil(-2. 0) returns – 2. 0 Math. ceil(-2. 1) returns -2. 0 Math. floor(2. 1) returns 2. 0 Math. floor(2. 0) returns 2. 0 Math. floor(-2. 0) returns – 2. 0 Math. floor(-2. 1) returns -3. 0 Math. round(2. 0) returns 2 Math. round(-2. 0 f) returns -2 Math. round(-2. 6) returns -3 29

min, max, and abs F max(a, b)and min(a, b) Examples: Returns the maximum or minimum of two parameters. F abs(a) Returns the absolute value of the parameter. F random() Returns a random double value in the range [0. 0, 1. 0). Math. max(2, 3) returns 3 Math. max(2. 5, 3) returns 3. 0 Math. min(2. 5, 3. 6) returns 2. 5 Math. abs(-2) returns 2 Math. abs(-2. 1) returns 2. 1 30

The random Method Generates a random double value greater than or equal to 0. 0 and less than 1. 0 (0 <= Math. random() < 1. 0). Examples: In general, 31