1 / 13

第 15 週 課堂補充 遞迴呼叫 ( 使用 Java 動態劇場 ) 類別

第 15 週 課堂補充 遞迴呼叫 ( 使用 Java 動態劇場 ) 類別. 陳富國. ( 歷屆試題 ) ( 類別的觀念請參考 12~13 章 ) class rectangle { private: // 私有區域 float width; float length; public:// 公開區域 void set(float w, float len) { width = w; length = len; }

dimaia
Download Presentation

第 15 週 課堂補充 遞迴呼叫 ( 使用 Java 動態劇場 ) 類別

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. 第15週 課堂補充遞迴呼叫(使用Java動態劇場)類別 陳富國

  2. (歷屆試題) (類別的觀念請參考12~13章) class rectangle { private: //私有區域 float width; float length; public://公開區域 void set(float w, float len) { width = w; length = len; } void area() {cout << width * length << endl ;} } int main(){ rectangle desk ;//宣告並建立一個rectangle物件,並命名為desk return 0; } 依類別(class)之資料定義, 下列敘述何者 錯誤? (A)設定desk物件寬度為3.8、長度為6.4之指令敘述爲desk.width=3.8。desk.length=6.4。 (B)設定desk物件寬度為3.8、長度為6.4之指令敘述爲desk.set(3.8, 6.4) (C)輸出desk物件面積之指令敘述為desk.area()。 (D)封裝(Encapsulation)的概念是將物件中的資料結構及函數作細節隱藏, 其他物件或函數只能藉由其所提供的成員副程式,才能存取物件內部的資料。

  3. (歷屆試題) 依下列程式片段,程式執行後輸出結果為何?並繪出呼叫圖。 (每題六分) class K { public: char c1( ) { return ‘H’; } char c2( ) { return ‘K’; } char show_c1( ) { return c1( ); } char show_c2( ) { return c2( ); } }; class pc : public K /*pc繼承K */ { char c1( ) { return ‘O’; } char c2( ) { return ‘O’; } }; int main( ){ pc Ch; //宣告一個pc物件,並命名為Ch cout << Ch.show_c1( ) << Ch.show_c2( ); }

  4. (歷屆試題) 依下列程式片段,程式執行後輸出結果為何?並繪出呼叫圖。 (每題六分) class K { public: virtual char c1( ) { return ‘H’; } //注意virtual這個關鍵字 char c2( ) { return ‘K’; } char show_c1( ) { return c1( ); } char show_c2( ) { return c2( ); } }; class pc : public K /*pc繼承K */ { char c1( ) { return ‘O’; } char c2( ) { return ‘O’; } }; int main( ){ pc Ch; //宣告一個pc物件,並命名為Ch cout << Ch.show_c1( ) << Ch.show_c2( ); }

  5. (歷屆試題) 依下列類別(class)之資料定義, 下列敘述何者 錯誤? (A)base_fun()可以存取o、p、q變數。 (B)base_fun()不可以存取x、y、z變數。 (C)derived_fun()可以存取o、p、q變數。 (D)base_fun()、derived_fun()函數可供其他類別(class)或函數使用。 class base { private: int o; protected: int p; public: int q; void base_fun() { …略 } }; class derived : public base { private: int x; protected: int y; public: int z; void derived_fun() { …略 } };

  6. 追蹤程式的視覺化好幫手Java程式動態執行劇場

  7. 追蹤程式的視覺化好幫手 • Java程式動態執行劇場,Java的學習工 • 因為Java是C的弟弟,程式碼非常類似… • http://192.192.246.204/DS/Lists/Announcements/Attachments/4/Java%20Porgram%20Theater.rar • 解開後,執行目錄中的jeliot.bat檔 • 要執行上面的程式,需要先安裝Java的執行環境(電腦教室已有安裝),下載點http://192.192.246.204/DS/Shared%20Documents/jre-6u14-windows-i586.rar

  8. (歷屆試題) 執行下列程式後,最後印出的結果為何? int main( ) { int data1[6]={3,8,6,9,5,4}; int k,times,i,temp; k = 6 - 1; while(k != 0) {times=0; for(i=0; i <= k-1; i++) {if(data1[i] > data1[i+1]) {temp = data1[i]; data1[i] = data1[i+1]; data1[i+1] = temp; times=i; } } k=times; } for(i=0;i<=5;i++) cout << data1 [i]<<"\t"; system("PAUSE"); } (改成Java動態劇場中的Java程式) import jeliot.io.*; public class MyClass { public static void main() { int data[]={3,8,6,9,5,4}; int k,times,i,temp; k = 6 - 1; while(k != 0){ times=0; for(i=0; i <= k-1; i++){ if(data[i] > data[i+1]){ temp = data[i]; data[i] = data[i+1]; data[i+1] = temp; times=i; } } k=times; } for(i=0;i<=5;i++) System.out.print(data[i] +"\t"); } }

  9. (改成Java動態劇場中的Java程式) import jeliot.io.*; public class MyClass { static int p2(int n1,int n2){ if(n2 > 0) return n1 * p2(n1, n2-1); else return 1; } public static void main(String[] args) { System.out.print(p2(3, 5)); } } (歷屆試題) 執行下列程式後,最後印出的結果為何?並繪出呼叫圖。 (每題六分) #include <iostream> using namespace std; int p2(int n1,int n2){ if(n2) return n1 * p2(n1, n2-1); else return 1; } int main( ){ cout <<p2(3, 5); return 0; }

  10. (改成Java動態劇場中的Java程式) import jeliot.io.*; public class MyClass { static int f1(int n) { if(n > 1) return n * f1(n - 1); else return 1; } public static void main(String[] args) { System.out.print(f1(3)); } } (歷屆試題) 執行下列程式後,最後印出的結果為何? 並會出呼叫圖。 (每題六分) #include <iostream> using namespace std; int f1(int n) { if(n > 1) return n * f1(n - 1); else return 1; } int main( ){ cout <<f1(3); return 0; }

  11. (歷屆試題) 執行下列程式後,最後印出的結果為何?並繪出呼叫圖。 (每題六分) #include <iostream> using namespace std; int outx(int n1, int n2) { if(n1 >= n2) { cout<<n1 - 1; outx(n1-1, n2); } } int outy(int n1, int n2) { if(n1 >= n2) { outx(n1, n2); cout <<endl; outy(n1 - 1, n2); } } int main( ){ outy(5, 1); return 0; } (改成Java動態劇場中的Java程式) import jeliot.io.*; public class MyClass { static int outx(int n1, int n2) { if(n1 >= n2) { System.out.print(n1 - 1); outx(n1-1, n2); } } static int outy(int n1, int n2) { if(n1 >= n2) { outx(n1, n2); System.out.println(); outy(n1 - 1, n2); } } public static void main(String[] args) { outy(5, 1); } } 劇場只能跑出第一行的結果(無法進行間接遞迴) 43210 之後的答案自己跑

  12. 上週進位轉換程式說明 //轉16進位 int num=0; int temp[100];//先將長除法過程中的餘數放在這個整數陣列 int i = 0; cout << "輸入一個要轉換的數字:"; cin >> num; while (num >= 16 ) { temp[i] = num % 16; num = num / 16; i++; } temp[i] = num; //最後一個商數放入陣列 for (;i>=0;i--) {//倒著輸出陣列中的整數 if (temp[i] >= 10) cout << (char)(temp[i]+55); //將'A'的內碼ascii為, 'B'是, //所以~15要換成'A'~'F'就是 //將原本的數值+55後以char的方式輸出 else cout << temp[i]; } //轉8進位 int main( ){ int num=0; int temp[100];//先將長除法過程中的餘數放在這個整數陣列 int i = 0; cout << "輸入一個要轉換的數字:"; cin >> num; while (num >= 8 ) { temp[i] = num % 8; num = num / 8; i++; } temp[i] = num; //最後一個商數放入陣列 for (;i>=0;i--) cout << temp[i]; //倒著輸出陣列中的整數 }

  13. 16進位轉換程式的Java動態劇場 import jeliot.io.*; public class MyClass { public static void main(String[] args) { int num=0; int temp[] = new int[100];//先將長除法過程中的餘數放在這個整數陣列 int i = 0; /*cin >> num; 輸入這邊不支援,改以程式指定方式:*/ num = 187; System.out.print( num + "'s Hex number is "); while (num >= 16 ) { temp[i] = num % 16; num = num / 16; i++; } temp[i] = num; //最後一個商數放入陣列 for (;i>=0;i--) {//倒著輸出陣列中的整數 if (temp[i] >= 10) System.out.print((char)(temp[i]+55)); else System.out.print(temp[i]); } } }

More Related