地特四等申論題
111年
[資訊處理] 程式設計概要
第 四 題
針對以下 C#程式,其輸出為 80, 85,完成程式碼(I)、(II)、(III)、(IV)、
(V),使之執行正確。(25 分)
public (I) class Score {
protected int [] data;
protected int value;
public Score(int [] d) {
data = new int[d.Length];
value = 0;
for (int i = 0; i < d.Length; i++)
data[i] = d[i];
}
private void print() {Console.WriteLine(""+value); }
protected abstract void compute(int[] weight);
public void getValue(int[] weight) {
compute(weight);
(II) ();
}
}
public class WeightScore:Score {
public WeightScore(int [] data): (III) (data) {}
protected (IV) void compute(int [] weight) {
int totalWeight = 0;
for (int i = 0; i < data.Length; i++) {
value = value + data[i] * weight[i];
totalWeight = totalWeight + weight[i];
}
value = value / (V) ;
}
}
public class Test {
static void Main(string[] args) {
int[] data1 = {90, 80, 70, 85};
int[] data2 = {90, 80, 80, 85};
int[] weight1 = {1, 2, 3, 4};
int[] weight2 = {4, 2, 1, 3};
Score x = new WeightScore(data1);
Score y = new WeightScore(data2);
x.getValue(weight1);
y.getValue(weight2);
}
}
📝 此題為申論題
思路引導 VIP
本題測驗 C# 物件導向程式設計的基礎語法與加權平均邏輯。看到題目應先辨識出「抽象類別 (abstract)」、「父類別建構子呼叫 (base)」以及「方法覆寫 (override)」等關鍵字,最後透過追蹤加權平均數的計算邏輯來推導分母的變數。
🤖
AI 詳解
AI 專屬家教
【解題關鍵】掌握 C# 物件導向程式設計中的抽象類別、建構子繼承與方法覆寫語法,並理解加權平均數的計算邏輯。 【解答】 (I) abstract
▼ 還有更多解析內容