12
Java class 2010.10.29

Java class 2010.10.29. Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java

  • View
    234

  • Download
    3

Embed Size (px)

Citation preview

Java class2010.10.29

Outline

• Defining a method

• Calling Method

• Passing parameters

[Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java

程式 :

A method that return a max value of two

values.

-> TestMethod.java

Defining a method

public static int max( int num1, int num2 )

{

int result;

if (num1 > num2)

result = num1;

else

result = num2;

return result; // return value

}

Modifier 回傳值型態Method name

參數 2Methodheader

Methodbody

Method signature ( 方法署名 )

Parameter list ( 參數列 )

參數 1

Calling Method

EX:

• int num3 = max( 10, 20 );

• System.out.println( max(10, 20) );

main(){.....num3 = max( num1,num2 );. ....}

main(){.....num3 = max( num1,num2 );. ....}

max( int a, int b ){........return result;..}

max( int a, int b ){........return result;..}

num1 值傳給 a

num2 值傳給 b

result 值傳給 max(num1,num2)

Passing parameters

• Method with a return value

• Method without a return value

程式 :

Two types about passing parameters.

-> TestMethod2.java

程式 :

Find the Greatest Common Divisor number.

-> GCD.java

程式 :

Find the Prime numbers between 1 to num.

-> prime.java

程式練習 :

1. 輸入一個十進位的整數2. 寫一個 ToDecimal method, 用來

將 十進位數字轉成二進制

Ex: 1010 = 10102

回家看 :

課本 p.180 ~ p.182