Professional Documents
Culture Documents
學號 : 姓名 : 分數 :
1. 填充題 (20%)
public class ParamTest { class CDegree {
public static void main(String args[]) { double t;
CDegree(double pt) {
int x = 10; t = pt;
System.out.println("x="+x); }
modifyValue(x); public String toString() {
return "攝氏"+t+"度";
System.out.println("x="+x);
}
int [] a = {10, 5}; }
System.out.println("a[0]="+a[0]);
modifyArray(a); class FDegree extends CDegree {
FDegree(double pt) {
System.out.println("a[0]="+a[0]); super(pt);
System.out.println("a[1]="+a[1]); }
} public String toString() {
return "華氏 "+((9.0/5.0)*t+32)+"度";
}
static void modifyValue(int x) { }
x += 5;
} public class DegreeTest {
public static void main(String[] args) {
CDegree c = new CDegree(30.0);
static void modifyArray(int[] a) { System.out.println("c.t="+c.t);
a[0] += 5; System.out.println("c.toString()="+c.toString());
FDegree f = new FDegree(30.0);
}
System.out.println("f.t="+f.t);
} System.out.println("f.toString()="+f.toString());
c.t += 10;
System.out.println("c.t="+c.t);
}
}
ParamTest 的輸出 : DegreeTest 的輸出 :
x= 10 c.t= 30
x= 10 c.toString()= 攝氏 30 度
a[0]= 10 f.t= 30
a[0]= 15 f.toString()= 華氏 86 度
a[1]= 5 c.t= 40
2.函數 (20%)
(1) 請以廻圈的方式定義一個函數 sum(),可以將 a 陣列的 (2) 請以廻圈的方式定義一個函數 reverse() 可以將 a 陣列反
加總傳回。(例如:若 a = {1,2,3} , 呼叫 sum(a) 後,傳回 6 ) 轉。 (例如:若 a = {1,2,3} , 呼叫 reverse 後, a 變成 {3,2,1} )
static void reverse(int[] a) {
static int sum (int[] a) { for (int i=0; i<a.length/2; i++) {
int s = 0; int t = a[i];
for (int i=0; i<a.length; i++) a[i] = a[a.length-1-i];
s = s + a[i]; a[a.length-1-i] = t;
return s; }
} }
1
金門技術學院 資管系 夜二技一年級 程式設計 94 上學期 期末考 出題者 : 陳鍾誠
學號 : 姓名 : 分數 :
3. 程式題 (20%)
2
金門技術學院 資管系 夜二技一年級 程式設計 94 上學期 期末考 出題者 : 陳鍾誠
學號 : 姓名 : 分數 :
5. 時間物件 (20%)
(1) 請定義一個時間物件,包含 時(hour)、分(minute)、秒 class Time {
(second) 等三個欄位。 int hour, minute, second;
Time(int h, int m, int s) {
(2) 請建立一個建構函數 Time(int h, int m, int s) 、可以設定時 hour = h; minute = m; second = s;
為 h, 分為 m, 秒為 s。 }
輸出:
t1.toString() = 16 時 14 分 20 秒。
t1. format12()=下午 4 時 14 分 20 秒。
t3= 14960 秒