# Java第五週[JPA08、JPA201~205 覆載運用、基礎題練習] #### 複習用 ## 開始解題(JPA08、JPA201~205): ### 第一題(JPA08)-撰寫三個同樣名為add的方法[覆載(OverLoad)]: #### 覆載、多載(overload) 是指同名的函數,但是參數個數或是參數的資料型態不同。 在java中的特殊寫法,會自動判斷**變數{多寡跟型態}** 若要在 C++ 中則要將函數分別命名 變成 ```java=1 add1(int a,int b) //整數相加 add2(double a,double b) //浮點數相加 add3(String a,String b) //字串串接 ``` 因為 C++ 沒有overload的功能 ![](https://i.imgur.com/DaxZJwp.png) 解答: ```java public class JPA08 { public static int add(int a,int b) { System.out.printf("Adding two integers:%d,%d\n",a,b); //題目要求 return a+b; //回傳整數 } public static double add(double a,double b) { System.out.printf("Adding two doubles:%2.1f,%2.1f\n",a,b); //題目要求 return a+b; //回傳浮點數 } public static String add(String a,String b) { System.out.printf("Adding two strings:%s,%s\n",a,b); //題目要求 return a+b; //回傳一個新字串 +代表自串串接 } //老師自訂題_________________________________________________________ public static int add(int a,int b,int c) { System.out.printf("Adding three integers:%d,%d,%d\n",a,b,c); return a+b+c; } public static double add(double a,double b,double c) { System.out.printf("Adding three doubles:%2.1f,%2.1f,%2.1f\n",a,b,c); return a+b+c; } public static String add(String a,String b,String c) { System.out.printf("Adding three strings:%s,%s,%s\n",a,b,c); return a+b+c; } //___________________________________________________________________ public static void main (String[] args) //主程式 { int i = add(2,3); //題給 i=5 double d = add(5.2,4.3); //d=9.5 String s = add("I love ","Java!!"); //s="I love Java" int i2 = add(2,3,4); //老師自訂題 double d2 = add(4.5,7.9,6.2); //老師自訂題 String s2 = add("Happy ","Birthday ","To You "); //老師自訂題 System.out.printf("%d %.6f %s \n", i, d, s); System.out.printf("%d %.6f %s \n", i2, d2, s2);//老師自訂題輸出 } } ``` --- ### 第二題(JPA201)-分數篩選[基礎題(if判斷)]: * 注意: <60不做任何處理(所以不必用 `if then else`) 本類型的題目是考**判斷式**,所以要看清題目, 注意要不要用到`== 、 >= 、 <=` 邏輯運算要用`&& 、 ||` 不要用**單個 & 、 |** ![](https://i.imgur.com/r7DyhLA.png) 解答: ```java import java.util.Scanner; public class JPA201 { static Scanner keyboard = new Scanner(System.in); //static為公用類別(),在主副程式之外 //主程式與函數可共用keyboard public static void main(String[] args) { test(); test(); } public static void test() { System.out.println("Please enter score:"); int a=keyboard.nextInt(); if(a>=60) { System.out.println("You pass"); } System.out.println("End"); } } ``` --- ### 第三題(JPA202)-比較大小[基礎題(a>b、a<b)]: ![](https://i.imgur.com/ByscbrQ.png) 解答: ```java import java.util.*; class JPA202 { static Scanner keyboard = new Scanner(System.in); //static為公用類別(),在主副程式之外 public static void main(String[] args) { test(); test(); } public static void test() { System.out.println("Input:"); int a=keyboard.nextInt(); int b=keyboard.nextInt(); if(a>b) { System.out.printf("%d is larger than %d\n",a,b); } else { System.out.printf("%d is larger than %d\n",b,a); } } } ``` --- ### 第四題(JPA203)-判斷奇偶數[基礎題(a%2==0)]: #### static(共用空間、靜態區域){類似VB的ByRef} 有人稱它是靜態的意思,是因為有用static修飾過的屬性是**存放在靜態區域(獨力記憶空間)** 一開始就被載入記憶體,從程式碼開始就有這屬性,直到結束後才消失, 而這**空間是大家共用的**,因此**數值有改變就不會有初始化**的動作 EX.`int a=0; //有可能清為0` 參考資料:http://blog.kenyang.net/2011/03/09/java-staticfinal ![](https://i.imgur.com/YP8t7AB.png) 解答: ```java import java.util.*; public class JPA203 { static Scanner input = new Scanner(System.in); //static為公用類別(),在主副程式之外 //名稱可自訂,由keyboard改為input public static void main(String[] args) { test(); test(); } static void test() { System.out.println("Input an integer:"); int a=input.nextInt(); if(a%2==0) { System.out.println("The number is even."); } else { System.out.println("The number is odd."); } } } ``` --- ### 第五題(JPA204)-公倍數計算[基礎題a%5==0]: ![](https://i.imgur.com/blzokH1.png) 解答: ```java import java.util.*; class JPA204 { static Scanner input = new Scanner(System.in); //static為公用類別(),在主副程式之外 //名稱可自訂,由keyboard改為input public static void main(String[] args) { test(); test(); } public static void test() { System.out.println("Input:"); int a=input.nextInt(); if((a%5==0)&&(a%9==0)) { System.out.println("Yes"); } else { System.out.println("No"); } } } ``` --- ### 第六題(JPA205)-倍數判斷[使用數學函數、副程式]: * **6的倍數為2、3的倍數**,所以只要求2、3的倍數即可。 ![](https://i.imgur.com/8G4xq00.png) 解答: ```java import java.util.*; class JPA205 { static Scanner input = new Scanner(System.in); //static為公用類別(),在主副程式之外 //名稱可自訂,由keyboard改為input public static void main(String[] args) { test(); test(); test(); test(); } public static void test() { System.out.println("Enter an integer:"); int a=input.nextInt(); if(a%2==0) //2的倍數 { if(a%3==0) //3的倍數 { System.out.printf("%d是2、3、6的倍數\n",a); } else { System.out.printf("%d是2的倍數\n",a); } } else { if(a%3==0) //3的倍數 { System.out.printf("%d是3的倍數\n",a); } else { System.out.printf("%d不是2、3、6的倍數\n",a); } } } } ``` --- 最後編輯時間:2021/4/2 2:33am. ###### tags: `JAVA課堂學習` `複習用` `高科大`