教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 教育文库 >

面向对象程序设计实验报告(2)

来源:网络收集 时间:2026-08-30
导读: } } (10)分析程序,写出程序的运行结果,并上机进行验证。 using System; namespace _1 { class Program { static void Main(string[] args) { int odd = 0, even = 0; 面向对象程序设计实验报告 int[] array = n

}

}

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

using System;

namespace _1

{

class Program

{

static void Main(string[] args)

{

int odd = 0, even = 0;

面向对象程序设计实验报告

int[] array = new int[] { 0, 4, 7, 8, 9, 10, 14, 17, 19, 24, 56 };

foreach (int i in array)

{

if (i % 2 == 0)

even++;

else

odd++;

}

Console.WriteLine("{0}个奇数,{1}个偶数", odd, even);

}

}

}

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

using System;

public class TestDoWhile

{

public static void Main ()

{

int x;

int y = 0;

do

{

x = y++;

Console.WriteLine(x);

}

while(y < 5);

}

}

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

using System;

namespace _1

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("---break---");

for (int i = 0; i < 10; i++)

{

if (i == 5) break;

Console.WriteLine("i is " + i);

}

Console.WriteLine("---continue---");

for (int i = 0; i < 10; i++)

面向对象程序设计实验报告

{

if (i == 5) continue;

Console.WriteLine("i is " + i);

}

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("delimeter={0},square={1}", delimeter, square);

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("delimeter={0},square={1}",delimeter,square);

面向对象程序设计实验报告

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;

= name;

this.age = age;

}

public void Display()

{

Console.WriteLine("id={0},name={1},age={2}",id,name,age);

}

public static void Main()

{

//string id, name;

//int age;

students stu = new students("0001","zhangsan",16);

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("{0}年{1}月{2}日",Year,Month,Day);

}

}

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类,要求:属性包含姓名、年龄、身份证号、工作、工资等,并

显示各属性的值。

面向对象程序设计实验报告

实验四 C#面向对象程序设计(二)

一、实验目的

1. 掌握构造函数和析构函数的含义与作用、定义方式和实现,能够根据要求正确定义和重载构造函数。能够根据给定的要求定义类并实现类的成员函数。

2. 理解类的成员的访问控制的含义,公有、私有和保护成员的区别。

3. 掌握参数传递的用法。

4. 掌握属性的作用和使用。

二、实验要求

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

2. 写出程序,并调试程序,要给出测试数据和实验结果。

3. 整理上机步骤,总结经验和体会。

4. 完成实验日志和上交程序。

三、实验内容

题目一:程序分析

(1) 分析程序,写出程序的运行结果,并上机进行验证,然后回答后面问题。 public class BankAccount

{

static int totalAccountNumber=0;

string BankAccountId;

double initialDepositAmount = 0.00;

public BankAccount(string myId)

{

this. …… 此处隐藏:3095字,全部文档内容请下载后查看。喜欢就下载吧 ……

面向对象程序设计实验报告(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/279525.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)