作者AFrederica (Fred)
看板NTU-Exam
标题[试题] 96下 郑士康 计算机程式设计 期末考
时间Sun Jun 22 02:08:46 2008
课程名称︰计算机程式设计
课程性质︰通识
课程教师︰郑士康
开课学院:
开课系所︰
考试日期(年月日)︰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