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