2012.11.09. overloading methods the scope of variables the math class floating point format...

21
JAVA 2012.11.09

Upload: tatum-chapel

Post on 01-Apr-2015

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

JAVA2012.11.09

Page 2: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

OUTLINE Overloading Methods The Scope of Variables The Math Class Floating point Format

[Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

TestRandomCharacter 、 RandomCharacter.java

Page 3: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

OVERLOADING METHODS

定義 : Method name is the same but the operation will be different.

Page 4: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

解釋 :

Overloading 有人將他稱為「多載」,是指說在「相同類別」中,定義「名稱相同」,但是「引數個數不同」,或是「引數型態不同」的函式,這樣 Java 就可以根據引數的個數、或是引數的型態,呼叫到對應的函式。

Page 5: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

程式 : TestMethodOverloading.java

課本 p.220

Page 6: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

public class TestMethodOverloading{

public static void main(String[] args){

// Invole the max method with int parametersSystem.out.println("The maximum between 3 and 4 is " + max(3,4));System.out.println("The maximum between 3.0 and 5.4 is " + max(3.0,5.4));System.out.println("The maximum between 3.0 and 5.4, and 10.14 is " +

max(3.0,5.4,10.14));}// Return the max between two int valuespublic static int max(int num1, int num2){

if(num1 > num2)return num1;

else return num2;

}// Find the max between two double valuespublic static double max(double num1, double num2){

if(num1 > num2)return num1;

else return num2;

}// Return the max among three double valuespublic static double max(double num1, double num2, double num3){

return max( max(num1,num2), num3 );}

}

Page 7: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

max(3,4) max(3.0,5

.4)max(3.0,5.4,10.14)

public static int max(int num1, int num2)

public static double max(double num1, double num2)

public static double max(double num1, double num2, double num3)

Page 8: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

程式 :

AmbiguousOverloading.java

課本 p.221

Page 9: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

public class AmbiguousOverloading{

public static void main(String[] args){

// Invole the max method with int parametersSystem.out.println( max(3,4) );

}

public static double max(int num1, double num2){

if(num1 > num2)return num1;

else return num2;

}

public static double max(double num1, int num2){

if(num1 > num2)return num1;

else return num2;

}}

Page 10: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

THE SCOPE OF VARIABLES[ 變數的有效範圍 ]

public static void main()

{

int a = 20;

for( int b=0 ; b<10 ; b++ )

{

System.out.println(b);

}

System.out.println(a);

}

The scope of a The scope of b

課本 5.9 節

Page 11: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

THE SCOPE OF VARIABLES[ Java 變數重複宣告的錯誤 ]

public static void main()

{

int a = 20;

for( int a=0 ; a<10 ; a++ )

{

System.out.println(a);

}

System.out.println(a);

}

The scope of a The scope of a

課本 5.9 節

Page 12: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

C PROGRAM

C 在巢狀 blocks 中可以宣告同名的變數Java 不行

Page 13: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

GLOBAL( 全域 ) & LOCAL( 區域 ) VARIABLE C 有「全域變數」和「區域變數」的概念

Java 只有「區域變數」的概念 Java 的全域變數比較類似 Class variable ( 定義在 method 裡的變數 , 是一種 local

variable)

※ In Java, all variables that are not local variables are fields of a class.

課本 p.195

Page 14: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

THE MATH CLASS Trigonometric Exponent Rounding min, max, abs random

Java API :http://docs.oracle.com/javase/7/docs/api/

課本 5.10 節

Page 15: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

THE RANDOM METHOD

範圍 :0 <= Math.random() < 1.0

EX:(int) (Math.random() * 10) 0 to 9

50 + (int) (Math.random() * 50) 50 to 99

Page 16: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

程式 :

TestRandomCharacter.java RandomCharacter.java

課本 p.227

Page 17: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

GENERATING RANDOM CHARACTERS Random integer

(int)(Math.random()) *(65535+1) (int)((int)’a’ + Math.random() * ((int)’z’-(int)’a’+1))

Random char (char)(‘a’+Math.random()*(‘z’-’a’+1))

Page 18: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

public class TestRandomCharacter {

public static void main(String[] args) {

final int NUMBER_OF_CHARS =175;

final int CHARS_PER_LINE =25;

for(int i=0;i<NUMBER_OF_CHARS;i++){

char ch =RandomCharacter.getRandomLowerCaseLetter();

if((i+1)% CHARS_PER_LINE ==0){

System.out.println(ch);

}else{

System.out.print(ch);

}

}

}

Page 19: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

import java.lang.Math;public class RandomCharacter {

public static char getRandomCharacter(char ch1,char ch2){return (char)(ch1+Math.random()*(ch2-ch1+1));}public static char getRandomLowerCaseLetter(){return getRandomCharacter('a','z');}

public static char getRandomUpperCaseLetter(){return getRandomCharacter('A','Z');}public static char getRandomCharacter(){return getRandomCharacter('\u0000','\uFFFF');}

}//section 2.17 :character has a unique Unicode between 0 and FFFF in hexadecimal(65535 in decimal)

Page 20: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

PRACTICE1. 喜巴辣

電腦與玩家各擲兩顆骰子 方法 getRandomNum() 產生 1~6 的亂數 方法 compare(a,b), 比較點數大小

Page 21: 2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java

Ppt下載: http://oss.csie.fju.edu.tw/~jastine01/

ppt.html