C#程序设计期末A卷以及答案

get { return speed; } set { speed = value; } }

public Vehicle(float speed) {

this.speed = speed; }

public virtual float Run(float distance) {

return distance / speed; }

public abstract void Speak();

}

public class Truck : Vehicle {

private float weight; public float Weight {

get { return weight; } }

private float load; public float Load {

get { return load; } }

public Truck(int weight, int load) : base(50) {

this.weight = weight; this.load = load; }

public 6 float Run(float distance) {

return (1 + load / Weight / 2) * base.Run(distance); }

public 7 void Speak() {

Console.WriteLine(\叭...叭...\ } }

3. 下面这段程序的功能是:把数列a1=1,a2=1,an?an?1?an?2 (n>2)的每一项求出来写到文件C:\\num.txt文件中去,然后我们根据要求的数列项从文件中把该项读出来。请把程序补充完整。 static void Main()

{

FileStream fs1 = new FileStream( 8 , FileMode.Create); for (ushort a = 1, b = 1; a <= 10000; 9 ) {

ushort t = b; b = a; a = t;

fs1.WriteByte((byte)(a / 256)); //写入前8位 fs1.WriteByte((byte)(a % 256)); //写入后8位 }

fs1.Position = 0;

Console.Write(\请输入要读取的数列项: \ int i = int.Parse(Console.ReadLine()); fs1.Position = 2 * i;

int x = 10 ; 第 6 页 共 10 页

Console.WriteLine(\数列项为: \ fs1.Close();

Console.ReadLine(); }

三、 编程题(每题10分,2题共计20分)

1. 编写一个矩形类,私有数据成员为矩形的长(len)和宽(wid),无参构造函数将len和wid设置为0,有参

构造函数设置长(len)和宽(wid)的值。另外,类还包括求矩形的周长、求矩形的面积、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。 2. 编写出一个通用的人员类(Person),该类具有姓名(Name)、性别(Sex)、年龄(Age)等域。然后对

Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类的构造函数进行重载,至少给出三个形式。最后编程对Student类的功能进行验证。

洛阳理工学院 2010/2011 学年 第一学期 面向对象程序设计 期末考试评分标准(A)

适用班级: B100513/14/15/16 考试时间: 120分钟

注意:看清题目要求,书写整洁!

四、 选择题(每小题2分,30题共60分)

1-5: CBAAA 6-10: CACBB

11-15:DDABA 16-20: BCDCB 21-25:DCCAA 26-30:CCBBD 五、 程序填空题 (每空2分,10空共计20分)

1. ref a 2. ref b 3. ref int x 4. ref int y5. abstract 6. override 7. override

8. \或@\ 9. a += b或a=a+b 10. 256 * fs1.ReadByte() + fs1.ReadByte() 六、 编程题(每题10分,2题共计20分)

1. 编写一个矩形类,私有数据成员为矩形的长(len)和宽(wid),无参构造函数将len和wid设置为0,有参

构造函数设置长(len)和宽(wid)的值。另外,类还包括求矩形的周长、求矩形的面积、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。

参考答案:

class Rectangle 。。。。。。。。。。。。。。1分 {

private float len; //长

private float wid; //宽 。。。。。。。。。。。。。。1分

//无参构造函数

public Rectangle() 。。。。。。。。。。。。。。1分 {

this.len = 0; this.wid = 0; }

//有参构造函数

public Rectangle(float length, float width) 。。。。。。。。。。。。。。1分 {

this.len = length; this.wid = width; }

//取矩形的长度

public float GetLength() 。。。。。。。。。。。。。。1分 {

return this.len; }

//取矩形的宽度

public float GetWidth() 。。。。。。。。。。。。。。1分 {

return this.wid;

第 7 页 共 10 页

}

//修改矩形的长度

public void SetLegth(float length) 。。。。。。。。。。。。。。1分 {

this.len = length; }

//修改矩形的宽度

public void SetWidth(float width) 。。。。。。。。。。。。。。1分 {

this.wid = width; }

//求矩形的周长

public float GetGirth() 。。。。。。。。。。。。。。1分 {

return (this.len + this.wid) * 2; }

//求矩形的面积

public float GetArea() 。。。。。。。。。。。。。。1分 {

return this.len * this.wid; }

}

2. 编写出一个通用的人员类(Person),该类具有姓名(Name)、性别(Sex)、年龄(Age)等域。然后对

Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类的构造函数进行重载,至少给出三个形式。最后编程对Student类的功能进行验证。

参考答案:

//通用的人员类:Person

class Person 。。。。。。。。。。。。。。2分 {

protected string Name; protected string Sex; protected int Age;

public Person() {

}

public Person(string name, string sex, int age) {

this.Name = name; this.Sex = sex; this.Age = age; } }

//继承自人员类的学生类:Student

class Student : Person 。。。。。。。。。。。。。。1分 {

//用于存储5门课成绩的整型数组 private int[] Score;

//构造函数1 public Student()

第 8 页 共 10 页

{

this.Name = \张三\; this.Sex = \男\; this.Age = 20;

this.Score = new int[] { 60, 70, 80, 90, 100 }; }

//构造函数2

public Student(string name, string sex, int age, int[] score) : base(name, sex, age) {

this.Score = score; }

//构造函数3

public Student(int[] score) 。。。。。。。。。。。。。。2分 {

this.Name = \王五\; this.Sex = \男\; this.Age = 19; this.Score = score; }

//求平均成绩

public float Avg() 。。。。。。。。。。。。。。1分 {

int total = 0;

for (int i = 0; i < this.Score.Length; i++) {

total = total + Score[i]; }

return total / Score.Length; }

public void Display() 。。。。。。。。。。。。。。1分 {

Console.WriteLine(\姓名:{0}\, this.Name); Console.WriteLine(\性别:{0}\, this.Sex); Console.WriteLine(\年龄:{0}\, this.Age); Console.Write(\成绩:\); foreach (int K in this.Score) {

Console.Write(K + \); }

Console.WriteLine(\平均成绩是:{0}\, Avg()); Console.WriteLine(); } }

class Program {

static void Main(string[] args) 。。。。。。。。。。。。。。3分 {

Student stu1 = new Student(); stu1.Display();

Student stu2 = new Student(\李四\, \女\, 20, new int[] { 65, 75, 85, 95, 100 }); stu2.Display();

第 9 页 共 10 页

Student stu3 = new Student(new int[] { 70, 75, 80, 85, 90 }); stu3.Display(); } }

第 10 页 共 10 页

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4