Presentation is loading. Please wait.

Presentation is loading. Please wait.

物件導向實習 07 97.11.17. 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average.

Similar presentations


Presentation on theme: "物件導向實習 07 97.11.17. 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average."— Presentation transcript:

1 物件導向實習 07 97.11.17

2 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average are used in C2. 那就是要寫 兩個同名字的 method.

3 A1 – A2 1. Write an application that uses an enhanced for statement to sum the integer values passed by the command-line arguments. Finally, prints the sum and the number of integers entered. 2. Finish the following program by completing the main method and giving the class C2. In class C2, the method add adds a row of array a and a row of b and places the result in sum. The method display outputs the array sum. public class Problem2 { public static void main( String args[] ) { int a[][]= {{1,2,3}, {4,5,6}}; int b[][]= {{5,7,3}, {1,3,9}}; int sum[][]... ; C2 obj2 = new C2();.... obj2.add(a[], b[], sum[]);.... obj2.display(sum); }

4 解題 A2 123 456 a 573 139 b 696 5815 sum

5 A3 3. (1) Define Method Overloading (2) Complete the following program by giving the class C2, which returns the maximum of two integers or three integers. Two overloaded methods max are used in C2. public class Problem3 { public static void main( String args[] ) { System.out.println(C2.max(4, 5)); // print the maximum of two integers System.out.println(C2.max(1, 5, 7)); // print the maximum of three integers }

6 A4 4. Finish the following Java program by completing the class Problem4. The program creates an array of Student. That is, an array of NUMBER_OF_STUDENT objects. Each of them have a field score. The program inputs the score in every object and increments the scores by one. Finally, outputs the scores. Method increment increases the score by one and method display outputs the final score. You are not allowed to change class Student. public class Problem4 { public static void main( String args[] ) { Student list3a[]; final int NUMBER_OF_STUDENTS = 5; int num; Scanner input = new Scanner( System.in ); list3a = new Student[NUMBER_OF_STUDENTS]; for (int i = 0; i < NUMBER_OF_STUDENTS; i++) { // create object and input an integer } incandprint(....); } public static void incandprint(....) {....// call increment( ) and display( ) }

7 A4 public class Student { private int score; public Student(int num) { score = num; } public void increment() { score++; } public void display() { System.out.printf("%4d", score); }

8 B1 同 A4 1. Finish the following Java program by completing the class Problem1. The program creates an array of Student. That is, an array of NUMBER_OF_STUDENT objects. Each of them have a field score. The program inputs the score in every object and increments the scores by one. Finally, outputs the scores. Method increment increases the score by one and method display outputs the final score. You are not allowed to change class Student. public class Problem1 { public static void main( String args[] ) { Student list3a[]; final int NUMBER_OF_STUDENTS = 5; int num; Scanner input = new Scanner( System.in ); list3a = new Student[NUMBER_OF_STUDENTS]; for (int i = 0; i < NUMBER_OF_STUDENTS; i++) { // create object and input an integer } incandprint(....); } public static void incandprint(....) {....// call increment( ) and display( ) }

9 B1 public class Student { private int score; public Student(int num) { score = num; } public void increment() { score++; } public void display() { System.out.printf("%4d", score); }

10 B2-B3 (B3 同 A2) 2. Write an application that gets an integer passed by the command-line arguments. Suppose the integer is n. Create an array of n integers. The array is filled by the integers between 1 and 10 generated by the class Random. Finally, output the integers in the array sequentially and the frequency (the number of appearances) of each integer. 3. Finish the following program by completing the main method and giving the class C2. In class C2, the method add adds array a and b and places the result in sum.The method display outputs the array sum. public class Problem3 { public static void main( String args[] ) { int a[][]= {{1,2,3}, {4,5,6}}; int b[][]= {{5,7,3}, {1,3,9}}; int sum[][]... ; C2 obj2 = new C2(); obj2.add(a, b, sum);.... obj2.display(sum[]); }

11 B4 類似於 A3 4. (1) Define Method Overloading (2) Complete the following program by giving the class C2, which returns the average of two integers or three integers. Two overloaded methods average are used in C2. public class Problem4 { public static void main( String args[] ) { System.out.println(C2.average(4, 5)); //print the average of two integers System.out.println(C2.average(1, 6, 8)); //print the average of three integers }

12 Method Overloading 方法多載 Methods of the same name can be declared in the same class, as long as they have different sets of parameters (number, types and order of the parameters). Void method1(int a, float b) Void method1(float a, int b) 多載是指利用相同的名字, 不同的參數及不同 的資料型態, 定義屬性和方法. 多載化程式的目 的是希望以相同的介面處理不同的物件, 以簡 化系統發展的複雜性并增加其彈性.


Download ppt "物件導向實習 07 97.11.17. 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average."

Similar presentations


Ads by Google