面向对象程序设计实验报告 下载本文

{

if (i == 5) continue;

Console.WriteLine(\ }

Console.Read(); } } }

题目二:程序编写

(1) 编写程序,定义一个包括学生基本资料的结构类型数据(要求包括学号、姓名、

性别、年龄、家庭住址等),并对其进行测试。

(2) 编写程序,将一年中的12个月建立一个枚举类型,并对其进行测试。 (3) 编写程序,使用int类型数据进行装箱与拆箱转换。

(4) 编写程序,分别用for、while、do…while语句实现求前n个自然数之和。 (5) 编写程序,输出九九乘法表。

实验三 C#面向对象程序设计

一、实验目的

1.理解C#语言是如何体现面向对象编程基本思想。 2.掌握类对象的定义。

3.了解类的封装方法,以及如何创建类和对象。 4.了解成员变量和成员方法的特性。 5.掌握静态成员的用法。

二、实验要求

1. 分析程序,上机验证结果。

2. 写出程序,并调试程序,要给出测试数据和实验结果。 3. 整理上机步骤,总结经验和体会。 4. 完成实验日志和上交程序。

三、实验内容

题目一:程序分析

(1)分析下面两个程序,确定那个程序好,说明理由。

程序要求:定义一个圆类,计算圆的面积和周长。 程序1:

public class circle {

public static void Main() {

double radium, delimeter, square; const double pai = 3.1415926;

radium = Convert.ToInt32(Console.ReadLine()); delimeter = 2 * pai * radium; square = pai * pai * radium;

Console.WriteLine(\Console.ReadLine(); } } 程序2:

public class circle {

double delimeter, square;

const double pai = 3.1415926; public void calculate(double rad) {

delimeter = 2 * pai * rad; square = pai * pai * rad;

Console.WriteLine(\

}

public static void Main() {

double radium;

circle cir = new circle();

radium = Convert.ToInt32(Console.ReadLine()); cir.calculate(radium); Console.ReadLine(); }

}

(2)分析程序,写出程序的运行结果,并上机进行验证。 Using System;

public class students {

string id,name; int age;

public students(string id,string name,int age ) {

this.id = id;

this.name = name; this.age = age; }

public void Display() {

Console.WriteLine(\}

public static void Main() {

//string id, name; //int age;

students stu = new students(\stu.Display();

Console.ReadLine(); } }

(3)分析程序,写出程序的运行结果,并上机进行验证。 public class Date {

private int Year, Month, Day;

public Date(int Year, int Month,int Day) {

this.Year=Year; this.Month=Month; this.Day=Day;

}

public Date(System.DateTime dt) {

Year = dt.Year; Month = dt.Month; Day = dt.Day; }

public void DisplayDate() {

Console.WriteLine(\年{1}月{2}日\}

}

public class Tester {

public static void Main() {

System.DateTime currentTime=System.DateTime.Now; Date dt=new Date(2008,7,18); dt.DisplayDate();

Date dt2 = new Date(currentTime); dt2.DisplayDate(); Console.ReadLine(); } }

题目二:程序编写

(1) 实现一个包含类属性方法的简单加法程序,并能显示结果。

(2) 实现一个Person类,要求:属性包含姓名、年龄、身份证号、工作、工资等,并

显示各属性的值。