实验3类与对象Word下载.docx

上传人:b****1 文档编号:3703236 上传时间:2023-05-02 格式:DOCX 页数:15 大小:144.63KB
下载 相关 举报
实验3类与对象Word下载.docx_第1页
第1页 / 共15页
实验3类与对象Word下载.docx_第2页
第2页 / 共15页
实验3类与对象Word下载.docx_第3页
第3页 / 共15页
实验3类与对象Word下载.docx_第4页
第4页 / 共15页
实验3类与对象Word下载.docx_第5页
第5页 / 共15页
实验3类与对象Word下载.docx_第6页
第6页 / 共15页
实验3类与对象Word下载.docx_第7页
第7页 / 共15页
实验3类与对象Word下载.docx_第8页
第8页 / 共15页
实验3类与对象Word下载.docx_第9页
第9页 / 共15页
实验3类与对象Word下载.docx_第10页
第10页 / 共15页
实验3类与对象Word下载.docx_第11页
第11页 / 共15页
实验3类与对象Word下载.docx_第12页
第12页 / 共15页
实验3类与对象Word下载.docx_第13页
第13页 / 共15页
实验3类与对象Word下载.docx_第14页
第14页 / 共15页
实验3类与对象Word下载.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验3类与对象Word下载.docx

《实验3类与对象Word下载.docx》由会员分享,可在线阅读,更多相关《实验3类与对象Word下载.docx(15页珍藏版)》请在冰点文库上搜索。

实验3类与对象Word下载.docx

5、实验报告给出所有源代码。

 

四、实验环境

1、PC微机;

2、DOS操作系统或Windows操作系统;

3、Eclipse程序集成环境。

五、实验步骤

内容一:

1、创建“复数”类Complex,定义实部、虚部成员

2、定义构造函数接收二个double参数用来初始化数据成员

3、定义二个复数运算函数plus()以及minus()各接收一个复数类型以实现复数与复数的加减运算。

4、定义二个复数运算函数plus()以及minus()各接收一个double类型以实现复数与与实数的加减运算。

4、定义一个打印方法。

5、在main()方法中创建复数对象并调用相关方法来验证。

内容二:

1、建立角色类Role,给出相应的成员,并能以生命值、经验值初始化角色对象。

2、在角色类中建立fight方法,接收一个角色类型的参数并与之“战斗”,返回胜者信息。

3、在主函数中初始化二个角色,调用fight方法。

六、测试数据

七、实验报告(学生完成)

实验报告应包括以下几个部分:

1、程序流程图;

鉴于复数类Complex没有复杂的流程,所以没有画流程图。

角色类的流程图,我分了三个部分:

2、程序的数据结构设计;

复数类Complex包含:

成员:

double类型的real和imaginary,分别代表实部和虚部

方法:

publicComplexplus(Complexa);

publicComplexminus(Complexa);

publicComplexplus(doublea);

publicComplexminus(doublea);

分别进行复数与复数或实数的加减法运算

publicStringprint()//打印复数类的信息

角色类Role包含:

privateintlife;

//生命值

privateintexp;

//经验值

privateintlifeSub;

//每次战斗生命值减少的量

privateintexpAdd;

//每次战斗经验值增加的量

publicstaticintnum;

//交手的次数

privateintexpBase;

//经验值增加50的基准

方法:

私有成员exp和life的set和get方法。

publicbooleanfight(Roleother)//参数为Role的fight方法。

publicvoidfight()//不带参数的fight方法。

3、程序的源代码及相关注释

复数类源码:

packageshiyan3;

publicclassComplex{

privatedoublereal;

privatedoubleimaginary;

publicComplexplus(Complexa){

doublex=this.real+a.getReal();

doubley=this.imaginary+a.getImaginary();

Complextmp=newComplex(x,y);

returntmp;

}

publicComplexminus(Complexa){

doublex=this.real-a.getReal();

doubley=this.imaginary-a.getImaginary();

publicComplexplus(doublea){

doublex=this.real+a;

doubley=this.imaginary;

publicComplexminus(doublea){

doublex=this.real-a;

publicStringprint(){

if(this.imaginary>

0)

returnthis.real+"

+"

+this.imaginary+"

i"

;

elseif(this.imaginary<

"

else

publicdoublegetReal(){

returnreal;

publicvoidsetReal(doublereal){

this.real=real;

publicdoublegetImaginary(){

returnimaginary;

publicvoidsetImaginary(doubleimaginary){

this.imaginary=imaginary;

publicComplex(){

};

//无参数的构造方法。

publicComplex(doublereal,doubleimaginary){

publicstaticvoidmain(String[]args){

Complexa=newComplex(1,2);

Complexb=newComplex(4,5);

Complexc=newComplex(2,3);

System.out.println(a.print()+"

和"

+b.print()+"

相加等于"

+a.plus(b).print());

+c.print()+"

相减等于"

+a.minus(c).print());

+12+"

+a.plus(12).print());

+10+"

+a.minus(10).print());

}

角色类Role源码:

publicclassRole{

privateintlifeSub;

publicintgetLife(){

returnlife;

publicvoidsetLife(intlife){

this.life=life;

publicintgetExp(){

returnexp;

publicvoidsetExp(intexp){

this.exp=exp;

publicvoidfight(){

life-=lifeSub;

exp+=expAdd;

if((exp-expBase)>

=50){

life++;

expBase+=50;

}

publicbooleanfight(Roleother){

num++;

this.fight();

other.fight();

System.out.println("

第"

+num+"

次战斗开始"

);

if(life<

=0&

&

other.getLife()<

=0){

System.out.println("

战斗结束,两人打平"

returntrue;

}elseif(life<

战斗结束,角色2获胜"

}elseif(other.getLife()<

战斗结束,角色1获胜"

}else{

角色1的生命值为"

+life+"

,角色2的生命值为"

+other.getLife());

角色1的经验值为"

+exp+"

,角色2的经验值为"

+other.getExp());

returnfalse;

publicRole(intlife,intexp,intlifeSub,intexpAdd){

super();

this.expBase=exp;

this.lifeSub=lifeSub;

this.expAdd=expAdd;

Rolea=newRole(1000,0,1,2);

Roleb=newRole(1000,0,2,3);

//死循环,两人战斗,直到分出胜负为止

while(!

a.fight(b)){

4、程序运行结果的分析。

复数类的运行结果:

角色类程序的部分运行结果:

八、思考题(学生完成)

1.定义一个点类“Point”。

Point类的属性有x轴和y轴坐标,可以通过方法setX()设置x轴坐标,方法setY()设置y轴坐标,方法getX()获得x轴坐标,方法getY()获得y轴坐标。

编写一个测试类PointTest来创建Point类的对象,测试该类。

2.编程定义一个栈类,它封装了栈数组,判断栈空方法、判断栈满方法以及进栈和出栈。

Point类:

classPoint{

privatedoublex;

privatedoubley;

publicdoublegetX(){

returnx;

publicvoidsetX(doublex){

this.x=x;

publicdoublegetY(){

returny;

publicvoidsetY(doubley){

this.y=y;

publicclassPointTest{

//TODOAuto-generatedmethodstub

Pointa=newPoint();

a.setX(10);

a.setY(20);

该点的坐标为("

+a.getX()+"

"

+a.getY()+"

)"

a.setX(30.5);

a.setY(55.3);

Stack类:

importjava.util.Scanner;

publicclassStack{

intstacksize=10;

privateint[]elem;

publicStack(intstacksize){

elem=newint[stacksize];

this.stacksize=stacksize;

//当前栈中元素的个数

privateintcount;

publicbooleanisEmpty(){

returncount==0?

true:

false;

publicbooleanisFull(){

returncount==stacksize?

publicvoidpush(intvar){

if(this.isFull()){

栈已满,不能进栈"

return;

elem[count++]=var;

publicvoidpop(){

if(this.isEmpty()){

栈已空,不能进栈"

return;

--count;

return;

publicvoidprint(){

当前栈中的元素为:

for(inti=0;

i<

count;

i++)

System.out.print(elem[i]+"

"

System.out.println();

请输入栈的大小"

Scannersc=newScanner(System.in);

intsize=sc.nextInt();

Stacks=newStack(size);

intop;

while(true){

请选择要进行的操作:

1进栈;

2出栈"

op=sc.nextInt();

if(op==1){

System.out.print("

请输入要进栈的数:

intvar=sc.nextInt();

s.push(var);

s.print();

}

elseif(op==2){

s.pop();

else{

System.out.println("

输入有误,请重新输入。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2