java概述課件05方法_第1頁
java概述課件05方法_第2頁
java概述課件05方法_第3頁
java概述課件05方法_第4頁
java概述課件05方法_第5頁
已閱讀5頁,還剩70頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1,第5章 方法,2,引言,從1到10的整數(shù)之和,從20到30,從35到45,分別為?,3,問題,int sum = 0; for (int i = 1; i = 10; i+) sum += i; System.out.println(Sum from 1 to 10 is + sum); sum = 0; for (int i = 20; i = 30; i+) sum += i; System.out.println(Sum from 20 to 30 is + sum); sum = 0; for (int i = 35; i = 45; i+) sum += i; System.ou

2、t.println(Sum from 35 to 45 is + sum);,4,問題,int sum = 0; for (int i = 1; i = 10; i+) sum += i; System.out.println(Sum from 1 to 10 is + sum); sum = 0; for (int i = 20; i = 30; i+) sum += i; System.out.println(Sum from 20 to 30 is + sum); sum = 0; for (int i = 35; i = 45; i+) sum += i; System.out.pri

3、ntln(Sum from 35 to 45 is + sum);,5,解決,public static int sum(int i1, int i2) int sum = 0; for (int i = i1; i = i2; i+) sum += i; return sum; public static void main(String args) System.out.println(Sum from 1 to 10 is + sum(1, 10); System.out.println(Sum from 20 to 30 is + sum(20, 30); System.out.pri

4、ntln(Sum from 35 to 45 is + sum(35, 45); ,6,學(xué)習(xí)目標(biāo),定義方法、調(diào)用待返回值的方法、調(diào)用無返回值的方法 、按值傳參(5.2-5.5). 開發(fā)模塊化的、易讀、易調(diào)試和易維護(hù)的可重用代碼(5.6). 編寫方法實(shí)現(xiàn)十進(jìn)制轉(zhuǎn)化十六進(jìn)制(5.7). 使用方法的重載、理解歧義重載(5.8). 確定變量的作用域 (5.9). 使用Math類中的方法解決數(shù)學(xué)問題 (5.10-5.11). 在軟件開發(fā)中應(yīng)用方法抽象的概念 (5.12). 使用逐步求精的辦法設(shè)計(jì)和實(shí)現(xiàn)方法 (5.12).,7,定義方法,方法許多語句的組合,共同組成這種操作。,8,方法的特征,方法定義(s

5、pecification):,9,形式參數(shù),被定義在方法頭上的參數(shù)被稱為形式參數(shù)。,10,實(shí)際參數(shù),當(dāng)一個(gè)方法被調(diào)用的時(shí)候所傳遞的參數(shù)被稱為實(shí)際參數(shù)。,11,返回類型,方法可以返回一個(gè)值。返回類型是一種數(shù)據(jù)類型,是由方法頭確定的。如果方法不返回一個(gè)值,則該返回類型是關(guān)鍵字void。例如,這個(gè)返回類型在這個(gè)main方法里是void。,12,調(diào)用方法,測(cè)試方法: 這個(gè)程序演示調(diào)用一個(gè)方法去獲取最大值。,TestMax,13,調(diào)用方法,animation,14,調(diào)用方法,i is now 5,animation,15,調(diào)用方法,j is now 2,animation,16,調(diào)用方法,invoke

6、max(i, j),animation,17,調(diào)用方法,invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2,animation,18,調(diào)用方法,declare variable result,animation,19,調(diào)用方法,(num1 num2) is true since num1 is 5 and num2 is 2,animation,20,調(diào)用方法,result is now 5,animation,21,調(diào)用方法,return result, which is 5,animation,2

7、2,調(diào)用方法,return max(i, j) and assign the return value to k,animation,23,調(diào)用方法,Execute the print statement,animation,24,注意,一個(gè)有返回類型的方法必須有對(duì)應(yīng)的return方法。如下左邊看上去是正確的,但是在編譯時(shí)候,編譯器會(huì)誤認(rèn)為方法可能沒有返回值。,為了解決這個(gè)問題,刪除黃色部分,使編譯器便可以理解為不論如何這個(gè)方法一定有返回值。,25,從其他類的復(fù)用方法,注:一種方法的好處是可重用的。該方法可以被另外的任類調(diào)用。如果我們創(chuàng)建一個(gè)新的類的測(cè)試,我們可以調(diào)用方法使用classname

8、.methodname(例如,TestMax.max()。,26,調(diào)用堆棧,27,調(diào)用堆棧,i is declared and initialized,animation,28,調(diào)用堆棧,j is declared and initialized,animation,29,調(diào)用堆棧,Declare k,animation,30,調(diào)用堆棧,Invoke max(i, j),animation,31,調(diào)用堆棧,pass the values of i and j to num1 and num2,animation,32,調(diào)用堆棧,pass the values of i and j to num

9、1 and num2,animation,33,調(diào)用堆棧,(num1 num2) is true,animation,34,調(diào)用堆棧,Return result and assign it to k,animation,35,調(diào)用堆棧,Assign num1 to result,animation,36,調(diào)用堆棧,Execute print statement,animation,37,void 方法舉例,這個(gè)方法沒有返回類型。,TestVoidMethod,38,參數(shù)的值傳遞,public static void nPrintln(String message, int n) for (in

10、t i = 0; i n; i+) System.out.println(message); ,如果我們這樣調(diào)用方法 nPrintln(“Welcome to Java”, 5); 會(huì)有怎樣的輸出呢?? 如果我們這樣調(diào)用方法 nPrintln(“Computer Science”, 15); 會(huì)有怎樣的輸出呢?,39,參數(shù)的值傳遞,這個(gè)程序演示了傳值的方法。,Increment,40,參數(shù)的值傳遞,由值測(cè)試通過 這個(gè)程序演示了傳值的方法。,TestPassByValue,41,按值傳遞(續(xù)),42,模塊化代碼,使用方法可以減少冗余的代碼,提高代碼的復(fù)用性。方法也可以用來模塊化代碼,以提高程序的

11、質(zhì)量。,GreatestCommonDivisorMethod,PrimeNumberMethod,43,重載方法,重載 max 方法 public static double max(double num1, double num2) if (num1 num2) return num1; else return num2; ,TestMethodOverloading,44,模糊的調(diào)用,有時(shí)可能有兩個(gè)或更多的方法被一個(gè)方法調(diào)用,但編譯器無法確定最匹配。這是被稱為模糊調(diào)用。模糊調(diào)用是一個(gè)編譯錯(cuò)誤。,45,模糊的調(diào)用,public class AmbiguousOverloading publ

12、ic static void main(String args) System.out.println(max(1, 2); 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; ,46,問題:將十進(jìn)制轉(zhuǎn)化為十六進(jìn)制,寫一個(gè)方法實(shí)現(xiàn)十進(jìn)制轉(zhuǎn)化十六進(jìn)制,Decimal2HexCo

13、nversion,47,局部變量的作用域,局部變量:變量定義在一個(gè)方法。 范圍:程序的一部分,其中的變量可以引用。 這種局部變量的作用域是從它被定義起到這塊代碼的結(jié)束。 一個(gè)局部變量在使用前必須先聲明。 我們可以在不同的模塊中定義名字相同的變量,但我們不能在一個(gè)嵌套模塊中定義相同變量。,48,局部變量的作用域,在for循環(huán)頭中初始聲明的一個(gè)變量,其作用域是整個(gè)for循環(huán)。但是在for循環(huán)體內(nèi)聲明的變量,其作用域只限于循環(huán)體內(nèi)部,是從他的聲明處開始,到包含該變量的塊結(jié)束為止。,49,局部變量的作用域,50,局部變量的作用域,/ Fine with no errors public static

14、void correctMethod() 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; ,51,局部變量的作用域,/ With no errors public static void incorrectMethod() int x = 1; int y = 1; for (int i = 1; i 10; i+) int x = 0; x += i; ,52,方法的抽象,方法的實(shí)現(xiàn)對(duì)用

15、戶隱藏在“黑匣子”中。,53,方法的好處,一次編寫多處調(diào)用 信息的隱蔽性: 把關(guān)鍵核心信息不暴露給調(diào)用方法的對(duì)象 減少復(fù)雜性,54,Math 類,Class 常數(shù): PI E Class 方法: 三角函數(shù) 指數(shù)函數(shù) 舍入方法 最小, 最大, 絕對(duì)值, 和隨機(jī)值方法,55,三角函數(shù),sin(double a) cos(double a) tan(double a) acos(double a) asin(double a) atan(double a),Radians toRadians(90),Examples: Math.sin(0) returns 0.0 Math.sin(Math.PI

16、 / 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,56,指數(shù)函數(shù),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 o

17、f 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.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.

18、5) returns 3.24,57,取整方法,double ceil(double x) X向上舍入到最近的整數(shù)。返回一個(gè)double值。 double floor(double x) X是向下舍入到最接近的整數(shù)。返回一個(gè)double值。 double rint(double x) X舍入到最近的整數(shù)。如果X是同樣接近兩個(gè)整數(shù),返回一個(gè)。 int round(float x) Return (int)Math.floor(x+0.5). long round(double x) Return (long)Math.floor(x+0.5).,58,取整方法 舉例,Math.ceil(2.1)

19、 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.rint(2.1) returns 2.0 Math.rint(2.0) returns 2.0 Math.rint(-2.0) returns 2.0 Math.r

20、int(-2.1) returns -2.0 Math.rint(2.5) returns 2.0 Math.rint(-2.5) returns -2.0 Math.round(2.6f) returns 3 Math.round(2.0) returns 2 Math.round(-2.0f) returns -2 Math.round(-2.6) returns -3,59,最小、最大絕、對(duì)值,max(a, b)and min(a, b) Returns the maximum or minimum of two parameters. abs(a) Returns the absolu

21、te value of the parameter. random() Returns a random double valuein the range 0.0, 1.0).,Examples: 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,60,隨機(jī)數(shù)方法,生成一個(gè)隨機(jī)的雙值大于或等于0和小于1(,Examples:,In general,61,案例研究:產(chǎn)生隨機(jī)的字符

22、,計(jì)算機(jī)程序處理的是數(shù)值數(shù)據(jù)和字符。前面已經(jīng)看到了許多涉及數(shù)值數(shù)據(jù)的例子,了解字符和如何處理字符很重要。 正如前邊所介紹的,每個(gè)字符都有一個(gè)唯一的在十六進(jìn)制數(shù)0到FFFF即(65535)之間的統(tǒng)一碼。生成一個(gè)隨機(jī)字符就是使用下面的表達(dá)式,生成從0-65535之間的一個(gè)隨機(jī)整數(shù)。(注意:因?yàn)?=Math.random()1.0,必須給65535加上1) (int)(Math.random() * (65535 + 1),62,案例研究:產(chǎn)生隨機(jī)的字符,現(xiàn)在讓我們來考慮如何生成一個(gè)隨機(jī)小寫字符。小寫字母的統(tǒng)一碼是一串連續(xù)的整數(shù),從小寫字母a的統(tǒng)一碼開始,然后是b、c、.和z的統(tǒng)一碼。a的統(tǒng)一碼是:

23、 (int)a 因此,(int)a和(int)z之間的隨機(jī)整數(shù)是 (int)(int)a + Math.random() * (int)z - (int)a + 1),63,案例研究:產(chǎn)生隨機(jī)的字符,正如2.13.3節(jié)中所討論的,所有的數(shù)字操作符都可以應(yīng)用到char操作數(shù)上。如果另一個(gè)操作數(shù)是數(shù)字或者字符,那么char型操作數(shù)就會(huì)被轉(zhuǎn)換成數(shù)字。這樣前面表達(dá)式就可以簡(jiǎn)化為如下” a + Math.random() * (z - a + 1) 這樣隨機(jī)小寫字母是: (char)(a + Math.random() * (z - a + 1),64,RandomCharacter 類,/ Rando

24、mCharacter.java: Generate random characters public class RandomCharacter /* Generate a random character between ch1 and ch2 */ public static char getRandomCharacter(char ch1, char ch2) return (char)(ch1 + Math.random() * (ch2 - ch1 + 1); /* Generate a random lowercase letter */ public static char getRandomLowerCaseLetter() return getRandomCharacter(a, z); /* Generate a random uppercase letter */ public static char getRandomUpperCaseLetter() return getRandomCharacter(A, Z); /* Generate a random digit character */ public static char getRandomDigit

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論