实验四面向对象程序设计.doc_第1页
实验四面向对象程序设计.doc_第2页
实验四面向对象程序设计.doc_第3页
实验四面向对象程序设计.doc_第4页
实验四面向对象程序设计.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验四 面向对象程序设计实验类型: 验证性 实验课时: 8 指导教师: 陈志勇 时 间: 2015年 10月 21日课 次: 第 3、4节 教学周次: 第 7周 实验分室: 5-4 实验台号: 29 实 验 员: 马征 一、实验目的1.了解并掌握面向对象程序设计的基本思想和方法。2.掌握类的定义及对象的使用方法。3.掌握C#程序中的异常处理。4.理解程序中继承的使用。二、实验内容及要求1.定义长方形rectangle类:属性:长和宽两个属性;方法:无参数构造函数:长和宽的值为0两个参数构造函数:长和宽的值为对应参数值计算周长方法:返回长方形的周长计算面积方法:返回长方形的面积定义test类中,在Main方法实例化两个对象,并输出其周长和面积程序:namespace shiyan4._1 class rectangle public int c, k, s, z; public rectangle() c = 0; k = 0; public rectangle(int c, int k) this.c = c; this.k = k; public int zhouchang(int c, int k) z = (c + k) * 2; return (z); public int mianji(int c, int k) s = c * k; return (s); class test static void Main(string args) Console.WriteLine(请输入长方形的长:); int x = Int32.Parse(Console.ReadLine(); Console.WriteLine(请输入长方形的宽:); int y= Int32.Parse(Console.ReadLine(); rectangle a = new rectangle(x, y); Console.WriteLine(长方形的周长为:0, a.zhouchang(x, y); Console.WriteLine(长方形的面积为:0, a.mianji(x, y); Console.Read(); 运行结果: 2(1)设计一个Person1类,包含下列数据:字段:姓名(name)、血型(blood)、体重(weight)、身高(height)。属性:Name和Blood访问姓名(name)、血型(blood)字段方法:显示姓名PrintName()、显示血型PrintBlood()、显示重量PrintWeight()、显示身高PrintHeight()、增加身高AddHeight()、增加体重AddWeight()、减少体重SubWeight()、显示对象本身Tostring()。构造函数:Person1() Person1(string name,string blood,int weight,int height) 对于姓名的设置要进行验证,字符数不能大于4 ,不能小于2。(2)设计一个Person2类,除了Person1类字段及方法外,增加字段电话telephone,增加方法PrintTelephone(),两个构造函数person2()和Person2(string name,string blood,int weight,int height,int telephone)(3)在主函数中创建Person1类和Person2类的对象,进行测试。程序:namespace shiyan4._2 class person1 public string name, blood; public int weight, height; public person1() name = ; blood = ; weight = 0; height = 0; public person1(string name, string blood, int weight, int height) name = 张三; blood = AB; weight = 68; height = 180; public string Name get return name; public string Blood get return blood; public int Weight get return weight; set weight = value; public int Height get return height; set weight = value; public string printname() return name; public string printblood() return blood; public int printweight() return weight; public int printheight() return height; public int Addweight(int x) return weight + x; public int Subweight(int x) return weight - x; public int Addheight(int x) return height + x; public int Subheight(int x) return height - x; class person2 public string name, blood; public int weight, height, telephone; public person2() name = ; blood = ; weight = 0; height = 0; telephone = 0; public person2(string name, string blood, int weight, int height, int telephone) name = 张三; blood = AB; weight = 68; height = 180; telephone = 56782434; public string Name get return name; public string Blood get return blood; public int Weight get return weight; set weight = value; public int Height get return height; set weight = value; public string Printname() return name; public string Printblood() return blood; public int Printweight() return weight; public int Telephone() return telephone; public int Printheight() return height; public int Addweight(int x) return weight + x; public int Subweight(int x) return weight - x; public int Addheight(int x) return height + x; public int Subheight(int x) return height - x; class test static void Main(string args) string name = 张三; string blood = AB; int weight = 68; int height = 180; person1 m = new person1(); Console.WriteLine(姓名:0, name); Console.WriteLine(血型:0, blood); Console.WriteLine(体重:0, weight); Console.WriteLine(身高:0, height); Console.WriteLine(请输入要增加的身高); int p = Int32.Parse(Console.ReadLine(); int h=m.Addheight(p); Console.WriteLine(身高为:0, height + h); Console.WriteLine(请输入要增加的体重); int t = Int32.Parse(Console.ReadLine(); int z = m.Addheight(t); Console.WriteLine(体重为:0, weight + z); Console.WriteLine(请输入要减少的体重); int j = Int32.Parse(Console.ReadLine(); int n = m.Addheight(j); Console.WriteLine(体重为:0, weight + z - n); Console.Read(); 运行结果:3. 输入以下程序,体会错误处理机制:class Program static void Main(string args) Console.WriteLine(请输入年龄); int i = int.Parse(Console.ReadLine(); 输入年龄时分别输入”aa”、”1111111111111111111111”看看程序运行会发生什么,注意观察系统信息。输入aa,提示输入格式不正确输入111111111111111111,提示输入的值太大或太小然后运行以下程序,分别输入120、aa、1111111111111111111看看这回程序运行状态。class AgeException : Exception public string reason; public AgeException(int age) if (age 110) reason = 太大了; class Program static void Main(string args) try Console.WriteLine(请输入年龄); int i = int.Parse(Console.ReadLine(); if (i 110) throw new AgeException(i); catch (AgeException e) Console.WriteLine(e.reason); catch (FormatException e) Console.WriteLine(e.Message); catch (Exception e) Console.WriteLine(e.Message); (1)为什么AgeException的父类定义为Exception,不定义行吗?Exception 是异常类的基类,要继承父类的属性及方法,必须定义。(2)在这个程序中哪个是系统抛出的异常,哪个是代码自己抛出的?AgeException是代码自己抛出的,FormateException是系统抛出的。4. 类的层次关系如图所示:PointCircleRectangleCylinder(1) 点(Point)类具有以下属性:坐标X、坐标Y,且具有以下方法: ToString( ):点的字符串表示形式(2)圆(Circle)类具有以下属性:坐标X、坐标Y、半径Radius,且具有以下方法: Diameter():求直径 CircumFerence():求周长 Area():求圆的面积 ToString():圆的字符串表示形式(3)矩形(Rectangle)类具有以下属性:坐标X、坐标Y、长length、宽width,且具有以下方法: zhouchang():求周长 Area():求矩形的面积 ToString():矩形的字符串表示形式(4)除继承了Circle类的属性和方法外,还应具有:Height:圆柱体高度,Volumn(): 求体积方法:底面积*高,并重载:Area():求圆柱体表面积:2*底面积+底周长*高(5)编程实现上述类,并定义Test类进行测试。程序:namespace shiyan4 class Point protected double x, y; public Point() x = 0.0; y = 0.0; public Point(double x, double y) this.x = x; this.y = y; public override string ToString() return 点的横坐标为: + x + ,纵坐标为: + y ; public virtual double CircumFerence() return 0.0; public virtual double Area() return 0.0; class Cirle : Point protected double Radius; protected double PI = 3.1415926; protected double diameter, circumFerence, area; public Cirle() x = 0.0; y = 0.0; Radius = 0.0; public Cirle(double Radius) this.Radius = Radius; public double Diameter(double Radius) diameter = this.Radius * 2; return diameter; public override double CircumFerence() circumFerence = 2 * PI * this.Radius; return circumFerence; public virtual double Area(double r) area = PI * r * r; return area; public override string ToString() return 圆的半径为: + this.Radius + n + 圆的直径为: + this.diameter + n + 圆的周长为: + this.circumFerence + n + 圆的面积为: + this.area; class Rectangle : Point protected double length, width, s, c; public Rectangle() x = 0.0; y = 0.0; length = 0.0; width = 0.0; public Rectangle(double length, double width) length = this.length; width = this.width; public double zhouchang(double width, double length) c = 2 * (length + width); return c; public virtual double Area(double width, double length) s = width * length; return s; public override string ToString() return 矩形的周长为: + c + n + 矩形的面积为: + s; class Cylinder : Cirle public double height, volumn, mj; public Cylinder() ; public Cylinder(double Radius, double height) : base(Radius) this.height = height; public double Volumn(double Radius, double height) volumn = PI * Radius * Radius * height; return volumn; public override double Area() mj = 2 * PI * Radius * Radius + 2 * PI * Radius * height; return mj; public override string ToString() return 圆柱体的体积为: + volumn + n + 圆柱体的面积为: + mj; class Text static void Main(string args) double x, y, Radius, zj, zc, mj, jzc, jmj, length, width, height, volumn, zmj; Console.Write(请输入横坐标x:); x = double.Parse(Console.ReadLine(); Console.Write(请输入纵坐标y:); y = double.Parse(Console.ReadLine(); Point p = new Point(x, y); Console.WriteLine(p); Console.Write(请输入圆的半径:); Radius = double.Parse(Console.ReadLine(); Point a = new Point(); Point b = new Point(x, y); Cirle c = new Cirle(Radius); zj = c.Diameter(Radius); Console.WriteLine(该圆的直径为: + zj); zc = c.CircumFerence(); Console.WriteLine(该圆的周长为: + zc); mj = c.Area(Radius); Console.WriteLine(该圆的面积为: + mj); Console.WriteLine(c); Console.Write(请输入矩形的宽:); width = int.Parse(Console.ReadLine(); Console.Write(请输入矩形的长:); length = int.Parse(Console.ReadLine(); Rectangle z = new Rectangle(width, length); jzc = z.zhouchang(width, length); jmj = z.Area(width, length); Console.WriteLine(z); Console.Write(请输入圆柱的高:); height = int.Parse(Console.ReadLine(); Cylinder cirle = new Cylinder(Radius, height); volumn = cirle.Volumn(Radius, height); zmj = cirle.Area(); Console.WriteLine(cirle); Console.ReadLine(); 运行结果:5.定义一个学生类,包含学号、姓名、性别等信息,定义一个班级类,在班级类中包含一个可以指定个数的学生类数组,并提供两个索引器,一个是int型参数用于向数组指定下标元素赋值或读取操作,一个是String型参数,用于根据参数在数组中查找指定元素。然后编写一个测试程序进行验证。程序:namespace shiyan4 class student public string name; public string xingbie; public string xuehao; public student() name = 张三; xingbie = 男; xuehao = 0914130528; public student(string name, string xingbie, string xuehao) = name; this.xingbie = xingbie; this.xuehao = xuehao; class banji public student number = new studentsize; static int size = 26; public student thisint index get return numberindex; set numberindex = value; public student thisstring s get int i; for (i = 0; i 26; i+) if ( = s) return numberi; return null; class test static void Main(string args) student a = new student(); student b = new student(李四, 男, 0914130529); student c = new student(王丽, 女, 0914130530); student d = new student(周伟, 男, 0914130531); student f = new student(刘丽, 女, 0914130532); student e = new student(李玲, 女, 0914130533); student g = new student(李伟, 男, 0914130534); banji bj = new banji(); bj0 = b; bj1 = c; bj2 = d; bj3 = e; bj4 = f; bj5 = g; a = bj刘丽; Console.WriteLine(学号:0+n+姓名:1+n+性别:2, a.xuehao, , a.xingbie); Console.Read(); 运行结果:6.定义一个shape抽象类,利用它作为基类派生出Rectangle、Circle等具体形状类,已知具体形状类均具有两个方法GetArea和GetPerim,分别用来求形状的面积和周长。最后编写一个测试程序对产生的类的功能进行验证。可参考如下代码:public abstract class shape public abstract float GetArea(); public abstract float GetPerim(); class Point public float x; public float y; public Point(float x, float y) this.x = x;/当形参名与字段名相同时,需要在字段名前加this.代表当前对象中的成员 this.y = y; class Rectangle : shape public Point lefttop;/代表矩形左上点 public float width; public float height; public Rectangle(float x, float y, float w, float h) lefttop = new Point(x, y); width = w; height = h; public override float GetArea() return width * height; public override float GetPerim() return 2 * width + 2 * height; class Circle : shape private const float pi = 3.1415926f; public Point dot;/代表原点 public float r; public Circle(float x, float y, float r) dot = new Point(x, y); this.r = r; public override float GetArea() return pi * r * r; public override float GetPerim() return 2 * pi * r; class Program static void Main(string args) Rectangle r1 = new Rectangle(0, 0, 10, 20); Console.WriteLine(矩形的左上点的x坐标:0,y坐标:1,面积:2,周长:3, r1.lefttop.x, r1.lefttop.y, r1.GetArea(), r1.GetPerim(); Circle c1 = new Ci

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论