課程名稱︰計算機程式設計
課程性質︰通識
課程教師︰鄭士康
開課學院:
開課系所︰
考試日期(年月日)︰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