通識計算機程式設計期末考試題, 6/25/2010 共 12 頁, 滿分 100 分外加最多 4 分 1. 撰寫 C# 敘述達成下列要求 : ( 假設 using System; 敘述已經包含於程式中 ) (a) 撰寫類別程式 Rhombus 代表菱形, 其中宣告私有 double 變數 d1 d2, 分別代表兩互相垂直的對角線長, 另有建構式設定 d1 d2 之值, 屬性 D1 D2 分別傳回與設定 d1 d2 之值, 此處可先不處理例外情況 (6%) (b) 撰寫類別程式 Square 代表正方形, 繼承 Rhombus, 在傳入邊長 a 之建構式中, 先呼叫父類別建構式設定 d1 d2 之值為 2 a, 由於設定初值之工作已於父類別建構式完成, 此一 Square 建構式主體部份可以是空的 (6%) (c) 撰寫介面 Shape, 其中定義函式 Area(), 用以計算面積, 傳回 double 數值 (3%) (d) 改寫 (a) 的類別程式 Rhombus, 實作 Shape, 並完成函式 Ares() (3%) [ 注意菱形面積為 d1*d2/2] (e) 在類別程式 Rhombus 的建構式中, 檢查輸入參數是否均為正, 若有不滿足之狀況發生,throw 一個 ArgumentException 例外物件 (3%) (f) 寫一段測試程式, 自鍵盤讀入 d1 d2 之值, 再以之建立一個菱形物件 rh 用 try-catch 攔截 FormatException ArgumentException Exception 例外物件, 並於例外發生時, 要求使用者重新輸入, 直到正確為止 (6%) 2. 找出以下程式片段之錯誤, 並予更正. (a) (3%) 一個錯誤 class B public static int Triple(int i) return 3*i; class Program static void Main(string[] args) B b = new B(); 1/12
Console.WriteLine(b.Triple(2)); (b) (3%) 一個錯誤 struct A public int i; public A() i = 0; public A(int i) this.i = i; class Program static void Main(string[] args) A a = new A(2); Console.WriteLine(a.i); (c) (6%). 一項錯誤, 請依註解以複製建構式更正 class C private int i; public C() i = 0; public int I set i = value; get return i; // 此處須加入複製建構式的宣告 class Program 2/12
static void Main(string[] args) C c1 = new C(); C c2 = new C(); // 這一行需改為利用複製建構式的寫法 c2 = c1; // 要讓 c1 和 c2 的 i 分別等於 10 和 20 c1.i = 10; c2.i = 20; (d) (3%) 一個錯誤 class B private int i; public B(int i) this.i = i; public int GetData() return i; class D : B private int j; public D(int i, int j) : base(i) this.j = j; public int GetData() return j; 3/12
(e) (3%) 一組錯誤 class S private int i; public S(int i) this.i = i; abstract public int F() return i; // 必需是抽象函式 3. 試寫出下列程式的輸出 (12%) using System; namespace Final2010Problem3 class Employee string id; string name; public Employee(string id, string name) this.id = id; this.name = name; Console.WriteLine("Employee(0,1)", id, name); class Faculty : Employee string department; public Faculty(string id, string name, string department) : base(id, name) 4/12
this.department = department; Console.WriteLine("Faculty(0,1, 2)", id, name, department); class Teacher : Faculty string course; public Teacher(string id, string name, string department, string course) : base(id, name, department) this.course = course; Console.WriteLine("Teacher(0,1,2,3)", id, name, department, course); class Program static void Main(string[] args) Teacher skjeng = new Teacher("7777", "Jeng", "EE", "Computer Programming"); 4. 試寫出下列程式的輸出 (12%) using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace Final2010Problem4 [Serializable] 5/12
class B private int i; public B(int i) this.i = i; public int I get return i; [Serializable] class D : B private int j; public D(int i, int j) : base(i) this.j = j; public int J get return j; class Program static void Main(string[] args) B[] ba = new B[2]; ba[0] = new D(3, 5); ba[1] = new B(1); BinaryFormatter formatter = new BinaryFormatter(); try 6/12
FileStream output = new FileStream("Test.dat", FileMode.Create, FileAccess.Write); formatter.serialize(output, ba[0]); formatter.serialize(output, ba[1]); output.close(); catch (SerializationException) Console.WriteLine( "Error writting to file in output"); catch (IOException) Console.WriteLine( "Can not create or close file for output"); try FileStream input = new FileStream("Test.dat", FileMode.Open, FileAccess.Read); try int i; for(i=0; i<ba.length; ++i) Object obj = formatter.deserialize(input); if (obj.gettype() == ba[0].gettype()) D di = (D)obj; Console.WriteLine(di.I); Console.WriteLine(di.J); else if (obj.gettype() == ba[1].gettype()) 7/12
B bi = (B)obj; Console.WriteLine(bi.I); else throw new SerializationException(); catch (IOException) Console.WriteLine( "Can not close file for input"); catch (FileNotFoundException) Console.WriteLine( "Can not open file in building intput"); 5. 依據以下描述及程式框架, 完成指定程式 你在答案卷只需寫下程式註解標示為 (a) 到 (c) 的部份 各部份配分分別註明, 總共 6% 程式描述 : 建立如下視窗介面, 由使用者按鈕後, 即在主選單顯示鬼牌 鬼牌之影像檔 J1.jpg 存放於專案 Final2010Problem5 之 bin 檔案夾內 鬼牌於主表單顯示之左上角座標為 5, 5; 橫向及縱向大小分為 85 及 150 像素 (pixels) 8/12
// 檔案 MainForm.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Final2010Problem5 9/12
public partial class MainForm : Form //******************************************* (a) 宣告必要物件變數 (1%) //******************************************* public MainForm() InitializeComponent(); private void button1_click(object sender, EventArgs e) //******************************************* DisplayImage(); //******************************************* //****************************************** private void DisplayImage() // (b) 完成所需敘述 (4%) protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) // (c) 完成所需敘述 (1%) //****************************************** 6. 撰寫一個 C# 程式以繪製歌曲之五線譜 為簡化問題, 只要寫主控台程式, 並在螢幕依照旋律顯示各個音符之種類及音高即可 以台大校歌 10/12
之第一小節為例, 其輸出如下圖 : 11/12
表示其前五個音符分別為四分音符 (quarter note) 附點八分音符(dotted eighth note) 十六分音符(sixteenth note) 四分音符 四分音符; 而對應之音高以固定唱名音高之 MIDI 編號表示分為 65 62 63 65 71 在中央 C (Do) 開始的一個八度內, 音高與 MIDI 編號之關係如下表 : C C# D D# E F F# G G# A A# B (Do) (Re) (Mi) (Fa) (Sol) (La) (Si) 60 61 62 63 64 65 66 67 68 69 70 71 以上述之台大校歌第一小節為測試場景撰寫程式, 不需撰寫額外內容 不使用類別者, 最高得 13 分 ; 使用類別, 不使用多型者, 最高得 20 分 ; 正確使用使用類別及多型者, 最高得 25 分 (25%) 7. ( 額外加分題, 至多 4%) 本課程中, 你覺得 : (a) 那一部份最有趣? 為什麼? (b) 那一部份最困難? 為什麼? (c) 講義有何可改進之處? (d) 怎麼做可以改進教學效果? 12/12