NTU-Exam 板


LINE

課程名稱︰計算機程式設計 課程性質︰通識 課程教師︰鄭士康 開課學院: 開課系所︰ 考試日期(年月日)︰2008/6/17 考試時限(分鐘):220 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題 : 1.撰寫C#敘述達成下列要求:(假設using System;敘述已包含於程式中) (a)撰寫類別程式Triangle代表三角形,其中宣告私有double變數a,b,c分別代表三邊長, 另有建構式設定a,b,c之值,此處可先不處理例外情況 (b)撰寫類別程式RTriangle代表直角三角形,繼承Triangle,在傳入兩股長aR,bR之建構式 中,先呼叫父類別建構式設定a,b,c之值為aR,bR,(aR2+bR2)1/2,由於設定初值之工作 已於父類別建構式完成,此一RTriangle建構式主體部分可以是空的 (c)撰寫介面Shape,其中定義函式Perimeter(),用以計算周長,傳回double數值 (d)改寫(a)的類別程式Triangle,繼承Shape,並實作函式Perimeter() (e)在類別程式Triangle的建構式中,檢查輸入參數是否均為正,且滿足三角形邊長要件 a+b>c,b+c>a,c+a>b,若有不滿足之狀況發生,throw一個ArguementException例外物件 (f)寫一段測試程式,自鍵盤讀入a,b,c之值,再以之建立一個三角形物件t.用 try-catch攔截FormatException,ArguementException,Exception例外物件,並於 例外發生時,要求使用者重新輸入,直到正確為止 2.找出下列程式片段之錯誤,並予更正 (a)兩個錯誤 ... sealed public class B{ private int i; i=0; public B(){ this.i=i; } public int I{ get{return i;} } } public class D:B { int j; public D():base(0) { j=1; } } (b)兩個錯誤 public class Test { private int nConstructed=0;//用已累計已建立的物件數 public Test(){ ++nConstructed; } //傳回已建立的Test物件數 public int GetNConstructed() { return nConstructed; } } (c)一個錯誤 //一個抽象類別 public abstract class Bird{ abstract public void Fly()//抽象函式 { System.Console.WriteLine("飛行"); } } (d)一個錯誤 struct Point2D { public int x; public int y; public Point2D() { x=0; y=0; } public Point2D(int x, int y) { this.x=x; this.y=y; } } (e)一組錯誤 public class Test{ int i; public Test(int i){ this.i=i; } public int I{ get{return i;} set{i=value;} } } ...... //主程式 Test t1= new Test(1); Test t2= new Test(2); Test t3= new Test(3); t2=t1; t3=t1; t2.I=5;//須不影響t1,t3 3.試寫出下列程式的輸出 using System; namespace Final2008 { public enum Status { OK, BlACK_JACK, EXPLODED } class Program { static void Main(string[] args) { Deck deck= new Deck(); Player[] player= new Player[2]; Player[0]= new HumanPlayer("Tom"); Player[1]= new ComputerPlayer(); Card c; int i; for(int n=0; n<2; ++n) { for(i=0; i<2; ++i) { c= deck.DealACard(); player[i].SaveACard(c); Console.WriteLine("{0} get a {1}{2}", player[i].Name, c.suite, c.faceValue); } } bool gameOver= false; for(i=0; i<2; ++i) { string name= player[i].Name; while(!gameOver && player[i].WantACard()) { c= deck.DealACard(); Console.WriteLine("{0} get a {1}{2}", name, c.suite, c.faceValue); player[i].SaveACard(c); Status s= player[i].GetStatus(); switch(s) { case Status.BLACK_JACK: Console.WriteLine("{0} wins", name); gameOver= true; break; case Status.EXPLODED: Console.WriteLine("{0} explodes, name"); gameOver= true; break; default: break; } } if(gameOver) break; } if(!gameOver) { int humanPoints= player[0].GetPoints(); int computerPoints= player[1].GetPoints(); endGame(player[0].Name, humanPoints, computerPoints); } } static void endGame(string name, int humanPoints, int computerPoints) { Console.WriteLine("Computer gets {0} points", computerPoints); Console.WriteLine("{0} gets {1} points", name, humanPoints); if(computerPoints>= humanPoints) { Console.WriteLine("Computer wins"); } else { Console.WriteLine("{0} wins", name); } } } public struct Card { public char suite; public int faceValue; public Card(char s, int v) { suite= s; faceValue= v; } } public class Deck { private Card[] card= new Card[10]; private stastic int nCard= 0; public Deck() { card[0]= new Card('c',2); card[1]= new Card('d',5); card[2]= new Card('h',8); card[3]= new Card('s',3); card[4]= new Card('c',9); card[5]= new Card('d',3); card[6]= new Card('h',4); card[7]= new Card('s',4); card[8]= new Card('c'.6); card[9]= new Card('d',10); } public Card DealACard(){ nCard++; return card[nCard-1]; } } public abstract class Player { private string name; private Card[] card= new Card[5]; private int n=0; private int total=0; public Player(string name) { this.name= name; } public string Name { get{return name;} } public void SaveACard(Card c) { Card[n]= c; total+= c.faceValue; n++; } public Status GetStatus() { if(total>21) return Status.EXPLODED; if(total== 21) return Status.BLACK_JACK; return Status.OK; } public int GetPoints() { return total; } public int NCard { get{return n;} } abstract public bool WantACard(); } public class HumanPlayer: Player { public HumanPlayer(string name): base(name) { } public override bool WantACard() { bool result= (base.NCard<3); return result; } } public class ComputerPlayer: Player { public ComputerPlayer(): base("Computer") { } public override bool WantACard() { return (base.GetPoints()< 17); } } } 4.依據以下描述及程式框架,完成指定程式。只須寫下程式註解標示為(a)~(d)的部分 程式描述: 建立視窗介面,同時建立一長100、寬50之矩形。使用者每按一次按鈕即將矩形邊長 之長寬各加一,再以訊息盒顯示矩形面積 //檔案MainForm.cs using System; using System.Collections,Generic; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Final2008 { public partial class MainForm: Form { //****************************************** /* (a)加入敘述,宣告並建立長100、寬50之Rectangle物件rec*/ //****************************************** public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //********************************************** /*(b)加入敘述,利用Rectangle之++運算子遞增rec之長與寬 /*(c)加入敘述,以訊息盒顯示rec之面積 //********************************************** } } } //檔案Rectangle.cs using System; using System.Cllections.Generic; using System.Text; namespace Final2008 { public class Rectangle { private int width; private int length; private int area; public Rectangle(int width, int length) { this.width= width; this.length= length; area= width*length; } public int Area() { return area; } /*(d)撰寫運算元++函式,將長與寬加1後,傳回結果物件*/ } } 5.撰寫測試主程式及類別Complex(複數),其中需測試及定義複數之+,-,*,/四種運算子及 傳回絕對值大小之函數Abs()。 注意複數為a+bi形式,其中a,b為實數(double)。除以0時應throw一個 DivideByZeroException例外物件。 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 116.59.120.4







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BabyMother站內搜尋

TOP