1 / 29

程序與函數的類別方法

第四單元. 程序與函數的類別方法. 模組化程式設計. 模組 (Modules) 擁有特定功能的相關資料和函式集合。 使用者只需知道模組對外的使用界面,就可以使用模 組提供的功能,而不用實際去瞭解模組內部程式碼的 實作和資料結構。. 模組化的優點: - 便於分析 - 加速開發 - 維護簡單 - 容易偵錯. 模組化程式設計. Top-down Design. 先將解決整個問題的方法分解成幾個大模組, 然後再將大模組分解成數個小模組,如此反覆 細分下去。等整個問題的樹狀架構圖完成後, 便從最底層的小模組做起,等完成後便將他們

humphrey
Download Presentation

程序與函數的類別方法

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第四單元 程序與函數的類別方法

  2. 模組化程式設計 模組(Modules)擁有特定功能的相關資料和函式集合。 使用者只需知道模組對外的使用界面,就可以使用模 組提供的功能,而不用實際去瞭解模組內部程式碼的 實作和資料結構。 模組化的優點: - 便於分析 - 加速開發 - 維護簡單 - 容易偵錯

  3. 模組化程式設計 Top-down Design 先將解決整個問題的方法分解成幾個大模組, 然後再將大模組分解成數個小模組,如此反覆 細分下去。等整個問題的樹狀架構圖完成後, 便從最底層的小模組做起,等完成後便將他們 組合成大模組,如此層層向上至整個軟體完成。

  4. 模組化程式設計 Top-down Design 的設計方法需注意下列問題: • 模組的獨立性 • 模組之間的結合 • 模組之間的溝通

  5. 程序是一個黑盒子 傳入參數 黑盒子 程序碼呼叫 程序 使用界面 語法 語意

  6. JAVA的類別方法 建立JAVA的類別方法 存取敘述static 傳回值型態 方法名稱(參數) { ………….. 程式敘述; ………….. } public :方法可以在任何程式任何地方呼叫 private :只能在同一類別內class呼叫

  7. JAVA的類別方法 private static void writeTriangle() { // 變數宣告 int i, j; // 巢狀迴圈 for ( i = 1; i <= 5; i++) { for ( j = 1; j <= i; j++) System.out.print("*"); System.out.print("\n"); } } // 類別方法: 顯示1加到5 ch5_3_1.java

  8. JAVA的類別方法 public static void add2Five() { // 變數宣告 int i; int total = 0; // 迴圈敘述 for ( i = 1; i <= 5; i++ ) { System.out.print("|" + i); total += i; } System.out.println("\n從小到大的總和: " + total); } // 主程式 ch5_3_1.java

  9. JAVA的類別方法 類別方法的參數傳遞 Ch5_3_2.java static void printTable(int lower, int upper) { // 變數宣告 int step = 10; // 增量 int c = lower; double f; // do/while迴圈敘述 System.out.println("攝氏 華氏"); do { f = (9.0 * c) / 5.0 + 32.0; System.out.println(c + "\t" + f); c += step; } while ( c <= upper); }

  10. JAVA的類別方法 類別方法的傳回值 ch5_3_3 // 類別方法: 計算n到N的數字 static int nAdd2N(int begin, int end) { // 變數宣告 int i; int total = 0; // 迴圈敘述 for ( i = begin; i <= end; i++ ) total += i; return total; // 傳回值 } // 類別方法: 攝氏轉成華氏 static double c2F(int c) { double f; // 轉換溫度 f = (9.0 * c) / 5.0 + 32.0; return f; // 傳回值 } // 主程式

  11. 10 x JAVA的類別呼叫方法 傳值呼叫 call by value 10 x func(int x) { x=x*x; System.out.println(x); } 100 main() { int x=10; func(x); }

  12. JAVA的類別呼叫方法 1 11 傳位址呼叫 x[0] call by reference 2 12 x[1] 3 13 x[2] main() { int x[3]={1,2,3}; func(x); } func(int x[]) { int i; for(i=0 ; i<3 ; i++) x[i]+=10; }

  13. JAVA的類別呼叫方法 ch5_3_4.java

  14. JAVA的類別變數和變數範圍 JAVA成員變數在宣告後,如果沒有指定初值,將擁 預定初值,數字為0,boolean型態為false,char型態 為unicode的0,如果是物件其預設值為null。 ch5_4_1

  15. JAVA的類別變數和變數範圍 ch5_4_2.java class class_name { 成員變數宣告; ……………………… public void method_name() { } } 區域變數宣告;

  16. Case study : Square private static int square(int n) { ……………… ……………… ……………… } 主程式:求1 到10的平方

  17. Case study : MAX private static int max(int a, int b, int c) { ……. } • 主程式: • 輸入三個整數 • 呼叫max求此三整數之最大值

  18. Case study : isPrimer private static int primer(int n) { ……………… ……………… ……………… } If n is a primer, return 1;otherwise return 0 求1到3000的所有質數

  19. Case Study : Play dice 搖一粒骰子6000次,統計1,2,3,4,5,6點各出現幾次 (int) Math.ceil(Math.random()*6);

  20. Homework : Play Dice game 玩家投擲兩顆骰子。每一顆骰子有六面,分別有1,2,3,4,5,和6個點。 當骰子靜止下來後,將兩個骰子朝天的那一面的點數相加起來。如果 第一次投擲便擲出7點或11點,那麼判定玩家贏。若第一次擲出2點、 3點或12點,那麼玩家輸。如果第一次擲出4點、5點、6點、8點、9點 、10點,則這個點數成為玩家的目標點數。玩家必須繼續投擲這兩顆 骰子,直到擲出目標點數才算贏。但若玩家在達成目標點數之前擲出 7點,則判定玩家輸。

  21. Homework : Play Dice game private static int rolldice() { ……………… } player rolled point rolldice player rolled point 4,5,6,8,9,10 2,3,12 case(point) target=point L print player loses rolldice F 7,11 T F 7 Point=target Point==7 w L print player win print player loses target T w print player win

  22. Final 120 5! 5! return 5*24 5*4! 5*4! return 4*6 4*3! 4*3! return 3*2 3*2! 3*2! return 2*1 2*1! 2*1! return 1 1 1 遞迴 ch5_5_1.java 遞迴函式解決問題方式都具有一些共同特點。他們會 重複不斷地呼叫自己。 5!=5*4*3*2*1 5!=5*(4*3*2*1) 5!=5*4! n!=n*(n-1)!

  23. Fibonacci 數列 Fibonacci數列: 0,1,1,2,3,5,8,13,21 Fibonacci數列可遞迴定義,如下: Fibonacci(0)=0 Fibonacci(1)=1 Fibonacci(n)=Fibonacci(n-1)+Fibonacci(n-2)

  24. f(3) return f(2) + f(1) return f(1) + f(0) return 1 return 1 return 0 Fibonacci 數列

  25. Case study : 以遞迴來求GCD GCD(32,12) =GCD(12,8) =GCD(8,4)

  26. Hanoi塔 solution:

  27. Hanoi塔 solution: 1 to 3 1 to 2 3 to 2 1 to 3 2 to 1 2 to 3 1 to 3

  28. Hanoi塔 solution: 1 to 3 1 to 2 3 to 2 1 to 3 2 to 1 2 to 3 1 to 3 source temp destination move n-1 rings from S to T move n’th ring from S to D move n-1 rings from T to D

  29. Hanoi塔 ch5_5_1.java • 演算法: • 將n-1個碟子由柱子1移到柱子2,柱子3作為暫存區。 • 將最後一個碟子由柱子1移到柱子3。 • 將n-1個碟子由柱子2移到柱子3,柱子1作為暫存區。 • 請撰寫一個程式來解決Hanoi塔問題。請使用一個具有下列四個參數 • 的遞迴函式。 • 1.要移動的碟子數 • 2.這些碟子的開始所在柱子位置。 • 3.這些碟子最後要移到的柱子位置。 • 4.被當作暫存區的柱子位置。

More Related