第6章 面向对象程序设计 习题(3)

2019-01-05 11:28

{ Weeks w = new Weeks();

w[0] = \星期天\ w[1] = \星期一\

Console.WriteLine(w[2]); Console.WriteLine(w[7]); }

}

Tues 星期天

4.阅读下列程序,写出程序的输出结果。考察委托使用

delegate void mydelegate(double x, double y); class MyDeClass

{ public void add(double x, double y)

{ Console.WriteLine(\ public void sub(double x, double y)

{ Console.WriteLine(\ }

class Program

{ static void Main(string[] args)

{ MyDeClass obj = new MyDeClass(); mydelegate p;

p = obj.add; p += obj.sub; p(10, 20); } } 10+20=30 10-20=-10

5.阅读下列程序,写出程序的输出结果。考察委托使用

public delegate int mydelegate(int x, int y); class Math

{ public int fun1(int x, int y) { return x * x + y * y; } public int fun2(int x, int y) { return x * x - y * y; } }

class Program

{ static void Main(string[] args) { int result;

mydelegate objCall;

Math objMath = new Math(); objCall = objMath.fun1; result = objCall(5, 3);

Console.WriteLine(\结果为{0}\

} } objCall = objMath.fun2; result = objCall(5, 3);

Console.WriteLine(\结果为{0}\ Console.Read(); } }

结果为34 结果为16

5. 阅读下列程序,写出程序的输出结果。考察事件

public delegate void mydelegate1(int i);

class MyEvent { public event mydelegate1 Event1; public void FireEvent1(int i)

{ if (Event1 != null) Event1(i); } }

public class Program { public static void show(int i)

{ Console.Write(\ } public static void Main()

{ MyEvent a = new MyEvent(); a.Event1 += new mydelegate1(show); for (int i = 0; i <= 30; i++)

if (i % 7 == 0) a.FireEvent1(i); } }

0 7 14 21 28

6. 阅读下列程序,写出程序的输出结果。考察运算符重载

class Complex { double a, b;

public Complex(double m, double n) { a=m; b=n; }

public static Complex operator +(Complex x,Complex y) { Complex t=new Complex(0,0); t.a=x.a+y.a; t.b=x.b+y.b; return(t); }

public void DispCom() { if (b>0 ) Console.WriteLine(\ else Console.WriteLine(\ } }

class Program

{ public static void Main(string[] args)

{ Complex x=new Complex(1,2); Complex y=new Complex(3,4); Complex z=new Complex(0,0);

z=x+y;

x.DispCom(); y.DispCom();

z.DispCom(); } } 1+2i 3+4i 4+6i

六.编程题

1.创建一个Student类,要求:

? 该类封装学生的姓名、性别和成绩等信息。 ? 通过构造函数给姓名和性别信息赋值。

? 姓名和性别信息只能读不能写,成绩信息通过属性进行读写,对成绩属性进行赋值

时,若成绩大于100分赋100分,若成绩低于0分赋0分。 ? 具有一个判断成绩等级的方法 class Student

{ private string name;

private string sex;

private int cj;

public Student(string name, string sex)

{ this.name = name; this.sex = sex; } public string Name

{ get { return name; } } public string Sex

{ get { return sex; } } public int Score

{ get { return cj; }

set { if (value > 100) cj = 100; else if (value < 0) cj = 0; else cj = value; }

}

public string grade(int a) { string dj;

if (a >= 90) dj = \优秀\ else if (a >= 80) dj = \良好\ else if (a >= 70) dj = \中等\ else if (a >= 60) dj = \及格\ else dj = \不及格\

return dj; } }

2.声明一个用于检查用户合法性及用户级别的类CheckUser,具体要求如下:

? 该类包含UserName(用户名)和UsePwd(密码)两个string类型的属性。 ? 包含一个带有两个string类型参数的构造函数。

? 包含一个返回值为int类型的UserLevel()方法,返回值为0表示高级用户,为1表

示普通用户,为2表示用户不存在。若用户名为“zhangsan”,密码为”123456”,则为高级用户。若用户名为“lisi”,密码为”654321”,则为普通用户。所有其他用户均为不合法用户。

class CheckUser

{ private string name; private string pwd;

public string UserName { get { return name; } set { name = value; } }

private string UserPwd { get { return pwd; } set { pwd = value; } }

public CheckUser(string name, string pwd)

{ this.name = name; this.pwd = pwd; } public int UserLevel() { int level = 2;

if (UserName == \ level = 0; if (UserName == \ level = 1; return level; } }

3.设计实现一个商品销售的简单管理程序。具体要求如下:

? 每一种商品对象要存储的是商品名称、库存量及商品的单价。 ? 每销售一件商品要计算销售总额和商品库存量。

classGoods {

privatestring name; publicstring Name {

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

privateint count; //商品库存量 privatedouble price; //商品价格

staticprivatedouble sum = 0; //所有商品的销售额

public Goods(string name, int count, double price)

{ this.name = name; this.count = count; this.price = price; } publicvoid makesale(int num) {

this.count = this.count - num; //修改商品库存量

sum = sum + this.price * num; //修改所有商品的销售总额 } publicint Count

{ get { return count; } } publicstaticdouble Sum

{ get { return sum; } } }

classProgram {

staticvoid Main(string[] args) {

Goods Candy = newGoods(\糖果\, 200, 2); Goods Chips = newGoods(\薯片\, 500, 5);

Candy.makesale(50); Chips.makesale(10);

Console.WriteLine(\的库存={1}\, Candy.Name, Candy.Count); Console.WriteLine(\的库存={1}\, Chips.Name,Chips.Count); Console.WriteLine(\, Goods.Sum); Console.Read(); } }

4.声明一个名为MyCar的类,具体要求如下:

? MyCar类可以被任何类访问

? 该类包含有两个属性:string类型的CarType和double类型的CarPrice

? 该类具有两个重载构造函数:一个无参数,一个包含两个参数(对应CarType和

CarPrice属性)。使用无参数构造函数初始化MyCar类对象时,CarType属性值默认为“SUV”,CarPrice属性值默认为24.5。

5.编写一个名为CTimer1的类,该类能够存放时间信息(设定三个域分别用来存放时分秒

信息),并且具有设置时间和显示时间的功能(设置和显示时间可定义成类的方法)。 6.创建一个名为Person的类,具体要求如下:

? 含有静态字段total(存放学生总数),实例字段ID(学号)、name(姓名)、sex(性

别)

? 创建构造函数,为新实例设置字段值,并记录实例个数。 ? 创建静态方法,显示当前学生总数。

? 创建实例方法,显示学生的学号、姓名和性别。

? 在主函数中调用实例化Person类,并调用静态方法和实例方法,要求第一次实例化

显示自己的学号和姓名等。

class Person

{ public static int total=0;

public int num;


第6章 面向对象程序设计 习题(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:生物化学解答题(附答案)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: