---第一題
//題目內容為:介面的繼承及抽象函數的改寫(期中考【第一題】)
//第一個介面 Shape 抽象函數 area()
//第二個介面 Face 抽象函數 length()
//介面的抽象函數預設為 public
interface Shape { double area(); }
interface Face { String color(); }
interface abc { int def(); } //★
// interface 介面名稱 { 傳回型態 方法(參數列); }
// 繼承介面使用 implements
public class t01 implements Shape , Face {
//改寫介面的抽象函數時,一定要宣告 public
public double area() { return 20.5; }
public String color() { return "blue"; }
public int def() { return 5; }//★
public static void main(String[] args) {
}
}
--------(與第一題無相關) 改寫+呼叫
//題目內容為:介面的繼承及抽象函數的改寫(期中考【第一題】)
//第一個介面 Shape 抽象函數 area()
//第二個介面 Face 抽象函數 length()
//介面的抽象函數預設為 public
interface Shape { double area(); }
interface Face { String color(); }
interface abc { int def(); } //★
// interface 介面名稱 { 傳回型態 方法(參數列); }
// 繼承介面使用 implements
public class t01 implements Shape , Face {
//改寫介面的抽象函數時,一定要宣告 public
public double area() { return 20.5; }
public String color() { return "blue"; }
public static int def() { return 5; }//★
public static void main(String[] args) {
t01 oo = new t01();
System.out.println(def());//呼叫
}
}
-----------------------------第二題
// 題目內容為:(期中考【第二題】)
import java.io.*;
public class t02 {
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);//輸入英文字abc 轉型整數錯誤 java.lang.NumberFormatException:
n = 0 ;
n = 1234567890/n ; // java.lang.NumberFormatException:
Integer s2 = new Integer(5);
// s2=null; //物件空值要使用null。 java.lang.NullPointerException
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內程式碼有錯 則此行下面的try程式碼都不會執行
// 申論題:try 程式碼執行有錯,會跳到 catch 執行
// 程式碼沒有錯,不會執行 catch ,錯誤的類型要講出來。
// ArrayIndexOutOfBoundsException 陣列索引超過範圍
// NumberFormatException 字串轉型數字格式錯誤
// ArithmeticException: / by zero 數學算式錯誤,分母為零
//java.lang.NullPointerException 18行 s2=null;
// try { 疑似會錯的程式碼 }
// catch ( Exception e ) { 出錯時,執行這邊 }
// finally { 程式碼不管有無錯誤都會執行 }
// 註:Exception 為錯誤訊息最大宗,e為錯誤訊息
}
}
--------------------第三題
-------------3-1
//期中考第三題 執行子類別或父類別建構元
//(3-1) 1.建立有1個字串的子類別建構元物件 2.接著執行同類別[this()]無參數建構元
//(3-2) 1.建立無參數的子類別建構元物件 2.接著執行父類別[super()]1字串1小數
public class appsub extends app {
String color="WHITE";
public appsub() { super("正方體",2.0);//執行父類別[super()]1字串1小數
System.out.println(color);
}
public appsub(String ss) { //String =宣告 BLUE
this();//執行第6行同類別[this()]無參數建構元
color=ss;
System.out.println(color);
}
public static void main(String[] args) {
appsub oapp=new appsub("BLUE");//執行有1個字串的建構元(第6行)
//appsub oapp2=new appsub();//執行無參數的建構元(第4行)
}
}
------------ 3-2
//期中考第三題 執行子類別或父類別建構元
//(3-1) 1.建立有1個字串的子類別建構元物件 2.接著執行同類別[this()]無參數建構元
//(3-2) 1.建立無參數的子類別建構元物件 2.接著執行父類別[super()]1字串1小數
public class appsub extends app {
String color="WHITE";
public appsub() { super("正方體",2.0);//執行父類別[super()]1字串1小數
System.out.println(color);
}
public appsub(String ss) { //String =宣告 BLUE
this();//執行第6行同類別[this()]無參數建構元
color=ss;
System.out.println(color);
}
public static void main(String[] args) {
//appsub oapp=new appsub("BLUE");//執行有1個字串的建構元(第6行)
appsub oapp2=new appsub();//執行無參數的建構元(第4行)
}
}
---app
public class app {
String shape="圓形";
double side = 1.0;
public app(){ System.out.println(shape + "A:" + side*3.1416); }
public app(String ss, Double dd)
{ shape=ss;
side=dd;
System.out.println(shape + "A:" + side*side);
}
}
----------------------------第四題
//import java.util.*;
import java.math.*;
public class ka {
//印出圓面積
public ka(double rr) {System.out.println("圓形面積:" + rr*rr*3.1416);}
public ka(double w,double h) {System.out.println("矩形面積:" + w*h);}
public ka(double a, double b, double c)
{
double s=(a+b+c)/2;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("三角形面積:" + area);}
public static void main(String[] args) {
ka oo1=new ka(2.0);
ka oo2=new ka(3.0,4.0);
ka oo3=new ka(3.0,4.0,5.0);
}
}
------------------------------第五題
//【第五題】改錯 使其編譯正確
class Caaa {
private int num;
public Caaa(int n){num=n;}
//public Caaa(){}
public int get(){return num;}
}
class Cbbb extends Caaa{
public Cbbb(){ super(5);}
public void show(){ System.out.println("num="+get()); }
}
public class hw10_5{
public static void main(String[] args) {
Cbbb bb=new Cbbb();
bb.show();
}
}
留言列表