14秋12级net程序设计实验指导手册统招.docx
- 文档编号:8431516
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:26
- 大小:81.80KB
14秋12级net程序设计实验指导手册统招.docx
《14秋12级net程序设计实验指导手册统招.docx》由会员分享,可在线阅读,更多相关《14秋12级net程序设计实验指导手册统招.docx(26页珍藏版)》请在冰豆网上搜索。
14秋12级net程序设计实验指导手册统招
实验要求
一、可读性
编程高手都知道:
一个好的程序要具备可读性,可方便自己也可方便别人。
所以,要培养一个良好的编程习惯,可注意以下几方面:
1.代码的缩进;
2.有效使用空格;
3.简明的注释;
4.意义明确的命名;
5.着重表示的常量。
二、亲手编写源程序
在编写C#程序过程中,还可以利用一些可视化的开发工具,它们可以综合使用C#的编译器和调试器等,例如Symantec公司的VisualCafé、Kawa、Sun公司的C#Worshop和C#Studio、Inprise公司的JBuilder、微软的VisualJ++等,使用编程开发工具可以加快编程的速度。
但在初始学习时还是最好亲手编写源程序,以便理解类和编程思想。
三、立即运行程序
编写的源程序要立即上机编译运行来检验程序中存在的问题。
通过运行的结果验证程序的功能是否实现。
即时有一些系统类的方法、变量也需要上机实验去了解它们的含义。
实验报告
姓名:
学号:
日期:
实验名称:
成绩:
一、实验目的及要求
二、实验环境
WindowsXP操作系统,Visual2005
三、实验源程序及关键代码解释
四、实验调试过程及运行结果
五、实验总结:
对上机实践结果进行分析,问题回答,上机的心得体会及改进意见。
第一部分面向对象编程
实验一继承与多态编程练习
【实验目的】
1.理解继承的含义,掌握派生类的定义方法和实现;
2.理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。
3.编写体现类的继承性(成员变量,成员方法,成员变量隐藏)的程序;
4.编写体现类多态性(成员方法重载,构造方法重载)的程序。
【实验要求】
1.写出程序,并调试程序,要给出测试数据和实验结果。
2.整理上机步骤,总结经验和体会。
3.完成实验日志和上交程序。
【实验内容】
一、类的继承和构造函数的灵活应用
1、创建一个描述图书信息的类并测试。
类中应保存有图书的书号、标题、作者、出版社、价格等信息。
1)定义图书类Book,Book类中包含isbn(书号)、title(标题)、author(作者)、press(出版社)、price(价格)等私有字段。
由于对一本书来说,书号是唯一的,因此,isbn字段应声明为只读的。
2)为Book类中的每个字段定义相应的属性,由于isbn字段只读的,其相应属性也应该是只读的。
3)为Book类定义两个构造函数,其中,一个构造函数将所有字段都初始化为用户指定的值,另一个构造函数只要求用户指定有关书号的信息,它将调用上一个构造函数初始化对象,初始化时,价格取0,除书号的其他信息取“未知”。
4)为Book类定义方法Show,Show方法用于显示图书的所有信息。
5)编写Main方法测试Book类,Main方法中分别使用上述两个构造函数创建Book对象。
实验1程序:
classBook{
privatereadonlystringisbn;//书号
privatestringtitle;//标题
privatestringauthor;//作者
privatestringpress;//出版社
privateintprice;//价格
publicBook(stringisbn):
this(isbn,"未知","未知","未知",0){
}
publicBook(stringisbn,stringtitle,stringauthor,stringpress,intprice){
this.isbn=isbn;
this.title=title;
this.author=author;
this.press=press;
this.price=price;
}
publicstringISBN{
get{
returnisbn;
}
}
publicstringTitle{
get{
returntitle;
}
set{
title=value;
}
}
publicstringAuthor{
get{
returnauthor;
}
set{
author=value;
}
}
publicstringPress{
get{
returnpress;
}
set{
press=value;
}
}
publicintPrice{
get{
returnprice;
}
set{
price=value;
}
}
publicvoidShow(){
Console.WriteLine("书号:
{0}",isbn);
Console.WriteLine("标题:
{0}",title);
Console.WriteLine("作者:
{0}",author);
Console.WriteLine("出版社:
{0}",press);
Console.WriteLine("价格:
{0}",price);
}
}
classTest5_1{
staticvoidMain(){
Bookbook1=newBook("978-7-111-23423-4");
book1.Show();
Console.WriteLine();
book1.Title="C#程序设计(C#2.0版)";
book1.Author="刘慧宁";
book1.Press="机械工业出版社";
book1.Price=32;
book1.Show();
Console.WriteLine();
book1=newBook("978-7-302-15800-4","Java程序设计","温秀梅","清华大学出版社",29);
book1.Show();
}
}
2、编写一个程序计算出球、圆柱和圆锥的表面积和体积。
1)定义一个基类圆,至少含有一个数据成员:
半径;
2)定义基类的派生类:
球、圆柱、圆锥,都含有求体积函数,可以都在构造函数中实现,也可以将求体积和输出写在一个函数中,或者写在两个函数中,请比较使用。
3)定义主函数,求球、圆柱、圆锥的和体积。
classMyCircle
{
doubler;
publicMyCircle(doubler)
{
this.r=r;
}
}
classMyBall:
MyCircle
{
doublevolumn;
publicMyBall(doubler)
:
base(r)
{
volumn=Math.PI*r*r*r;
Console.WriteLine("球的体积是:
{0}",volumn);
}
}
classMyCylinder:
MyCircle
{
doublevolumn;
publicMyCylinder(doubler,doubleh)
:
base(r)
{
}
publicvoidcalvolomn(doubler,doubleh)
{
volumn=Math.PI*r*r*h;
}
publicvoidprint()
{
Console.WriteLine("圆柱体的体积是:
{0}",volumn);
}
}
classMyCone:
MyCircle
{
doubleh,volumn;
publicMyCone(doubler,doubleh)
:
base(r)
{
this.h=h;
}
publicvoidcalvolumn(doubler,doubleh)
{
volumn=Math.PI*r*r*h/3;
Console.WriteLine("圆锥体的体积是:
{0}",volumn);
}
}
publicclassTester
{
publicstaticvoidMain()
{
Console.Write("请输入球半径:
");
doubler=Convert.ToDouble(Console.ReadLine());
MyBallmb=newMyBall(r);
Console.Write("请输入圆柱体高度:
");
doubleh=Convert.ToDouble(Console.ReadLine());
MyCylindermc=newMyCylinder(r,h);
mc.calvolomn(r,h);
mc.print();
Console.Write("请输入圆锥体高度:
");
doubleh2=Convert.ToDouble(Console.ReadLine());
MyConemo=newMyCone(r,h2);
Console.WriteLine("h2={0},r={1}",h2,r);
mo.calvolumn(r,h2);
Console.ReadLine();
}
}
3、设计一个图书卡片类Card,用来保存图书馆卡片分类记录。
1)这个类的成员包括书名、作者、馆藏数量。
2)至少提供两个方法,store书的入库处理,show显示图书信息。
3)程序运行时,可以从控制台上输入需要入库图书的总量,根据这个总数创建Card对象数组,然后输入数据。
4)可以选择按书名、作者、入库量进行排序。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceLibraryMIS
{
classCard
{
privatestringtitle,author;
privateinttotal;
publicCard()
{
title="";
author="";
total=0;
}
publicCard(stringtitle,stringauthor,inttotal)
{
this.title=title;
this.author=author;
this.total=total;
}
publicvoidstore(refCardcard)//使用ref关键字进行引用传递
{
title=card.title;
author=card.author;
total=card.total;
}
publicvoidshow()
{
Console.WriteLine("Title:
{0},Author:
{1},Total:
{2}",title,author,total);
}
publicstringTitle//Title的属性可读可写
{
get
{
returntitle;
}
set
{
title=value;
}
}
publicstringAuthor
{
get
{
returnauthor;
}
set
{
author=value;
}
}
publicstringTotal
{
get
{
returnTotal;
}
set
{
total=int.Parse(value);
}
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceLibraryMIS
{
classTable
{
staticvoidMain(string[]args)
{
Console.WriteLine("**********图书管理系统*******");
Console.WriteLine("2012版1.0,作者:
王晓宁");
TableT=newTable();
Card[]books;
int[]index;
inti,k;
Cardcard=newCard();
Console.Write("请输入需要入库图书的总数:
");
stringNo=Console.ReadLine();
intnum=int.Parse(No);
books=newCard[num];
for(i=0;i books[i]=newCard(); index=newint[num]; for(i=0;i { Console.Write("请输入书名: "); card.Title=Console.ReadLine(); Console.Write("请输入作者: "); card.Author=Console.ReadLine(); Console.Write("请输入入库量: "); No=Console.ReadLine(); card.Total=No; books[i].store(refcard);//使用ref关键字进行引用传递 index[i]=i;} Console.Write("请选择按什么关键字排序(1.按书名,2.按作者,3.按入库量)"); No=Console.ReadLine(); intchoice=int.Parse(No); switch(choice) { case1: T.sortTitle(books,index); break; case2: T.sortAuthor(books,index); break; case3: T.sortTotal(books,index); break; } for(i=0;i { k=index[i]; books[k].show(); } Console.Read(); } //按存入书的书名的首字母进行排序 voidsortTitle(Card[]book,int[]index) { inti,j,m,n,temp; for(m=0;m { for(n=0;n { i=index[n]; j=index[n+1]; if(string.Compare(book[i].Title,book[j].Title)>0) { temp=index[n]; index[n]=index[n+1]; index[n+1]=temp; } } } } //按存入书的作者的名字的首字母进行排序 voidsortAuthor(Card[]book,int[]index) { inti,j,m,n,temp; for(m=0;m for(n=0;n { i=index[n]; j=index[n+1]; if(string.Compare(book[i].Author,book[j].Author)>0) { temp=index[n]; index[n]=index[n+1]; index[n+1]=temp; } } } //按存入书的数量进行排序 voidsortTotal(Card[]book,int[]index) { inti,j,m,n,temp; for(m=0;m for(n=0;n { i=index[n]; j=index[n+1]; if(int.Parse(book[i].Total)>int.Parse(book[j].Total)) { temp=index[n]; index[n]=index[n+1]; index[n+1]=temp; } } } } } 实验运行结果 1——1按书名的首字母进行排序 1——2按作者名字的首字母进行排序 二、类的多态性练习 1、设计雇员系统,定义雇员基类,共同的属性,姓名、地址和出生日期,派生类: 程序员,秘书,高层管理,清洁工,他们有不同的工资算法,其中高级主管和程序员采用底薪加提成的方式,高级主管和程序员的底薪分别是5000元和2000元,秘书和清洁工采用工资的方式,工资分别是3000和1000,以多态的方式处理程序。 usingSystem; namespaceVariables { publicclassemployee { internalstringname,address,birthday; publicemployee(stringname,stringaddress,stringbirthday) { this.name=name; this.address=address; this.birthday=birthday; } internaldoublesalary; publicvirtualvoidSalary() { Console.WriteLine("foroverriding"); } } publicclasssenior: employee { publicsenior(stringname,stringaddress,stringbirthday) : base(name,address,birthday) { Console.WriteLine("senior"); } publicoverridevoidSalary() { salary=10000; Console.WriteLine("thesalaryforsenoir{0}is{1}",name,salary); } } publicclassprogrammer: employee { publicprogrammer(stringname,stringaddress,stringbirthday) : base(name,address,birthday) { Console.WriteLine("programmer"); } publicoverridevoidSalary() { salary=5000; Console.WriteLine("thesalaryforprogrammeris{0}",salary); } } publicclasssecretary: employee { publicsecretary(stringname,stringaddress,stringbirthday) : base(name,address,birthday) { Console.WriteLine("secretary"); } publicoverridevoidSalary() { salary=2000; Console.WriteLine("thesalaryforsecretaryis{0}",salary); } } publicclassdustman: employee { publicdustman(stringname,stringaddress,stringbirthday) : base(name,address,birthday) { Console.WriteLine("dustman"); } publicoverridevoidSalary() { salary=1000; Console.WriteLine("{0}'sbirthdayis{1}.Asadustman,thesalaryis{2}",name,birthday,salary); } } publicclassTester { publicstaticvoidMain() { employeeemp; emp=newsenior("Jansy","zibo","1970-09-11"); emp.Salary(); emp=newdustman("Tom","zhangdian","1960-01-22"); emp.Salary(); Console.ReadLine(); } } } 实验二接口编程练习 【实验目的】 1.了解什么是接口,接口和抽象类的异同,掌握接口的方法实现,接口方法的重定义。 【实验要求】 1.写出程序,并调试程序,要给出测试数据和实验结果。 2.整理上机步骤,总结经验和体会。 3.完成实验日志和上交程序。 【实验内容】 一、分析实现接口的程序文件 分析以下实现接口的程序文件并回答问题: ●本程序中的接口包含方法的构成是哪些; ●实现接口的类包含哪些元素? ●类实现接口方法的参数如何变换实现的? ●给出程序的输出结果。 代码如下:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 14 12 net 程序设计 实验 指导 手册 统招