期中考範圍(該周至下周三)

 ---

// 題目內容為:介面的繼承及抽象函數的改寫(期中考【第一題】)
//第一個介面 Shape 抽象函數 area()
//第二個介面 Face 抽象函數 length()
//介面的抽象函數預設為 public
interface Shape { double area(); }
interface Face { String color(); }
// interface 介面名稱 { 傳回型態 方法(參數列); }
// 繼承介面使用 implements
public class Banana implements Shape , Face {
//改寫介面的抽象函數時,一定要宣告 public
 public double area() { return 20.5; }
 public String color() { return "blue"; }
 
 public static void main(String[] args) {
 }
}

 

 ---第二題

 

 

// 題目內容為:(期中考【第二題】)
import java.io.*;

public class Lamon {

 public static void main(String[] args) {
  BufferedReader buf = new BufferedReader(
    new InputStreamReader(System.in));
  String str;
  
  try
  {  str = buf.readLine();
   System.out.println("輸入的字串為:" + str);
   int n = Integer.parseInt(str);
   n = 21474800 ;
      n = n*1234567890 ;
      Integer s2 = new Integer(5);
      s2=null;
      System.out.println(s2.intValue());
   int A []=new int [5];
   A[5]=2;
  }
  
  catch (Exception e) { System.out.println(e); }
  finally { System.out.println("try內程式碼不管有無錯誤,finally都會執行。"); }
  System.out.println("有catch到程式不會中斷,還是會繼續執行。");
 // 申論題:try 程式碼執行有錯,會跳到 catch 執行
 // 程式碼沒有錯,不會執行 catch ,錯誤的類型要講出來。
  
 // ArrayIndexOutOfBoundsException 陣列索引超過範圍
 // NumberFormatException 字串轉型數字格式錯誤
 // ArithmeticException: / by zero 數學算式錯誤,分母為零
  
 // try { 疑似會錯的程式碼 }
 // catch ( Exception e ) { 出錯時,執行這邊 }
 // finally { 程式碼不管有無錯誤都會執行 }
 // 註:Exception 為錯誤訊息最大宗,e為錯誤訊息
 }
}

---------------------pack用法

package pack1;
import pack2.*;  //★
public class cla1 {
 
 public static void main(String args[]){
  
  cla02 oo=new cla02();//需與建立的檔案名稱一致

  
 }

}

 

 

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()