实验六深入面向对象编程2Word文件下载.docx

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

实验六深入面向对象编程2Word文件下载.docx

《实验六深入面向对象编程2Word文件下载.docx》由会员分享,可在线阅读,更多相关《实验六深入面向对象编程2Word文件下载.docx(15页珍藏版)》请在冰点文库上搜索。

实验六深入面向对象编程2Word文件下载.docx

四、实验内容

(1)进一步熟悉Java面向对象相关语法、熟练运用自定义异常处理、包、内部类等相关面向对象深化语法编写程序

(2)按要求编写相关算法程序

五、实验步骤与结果

(1)改正以下程序中出现的错误,并进行注释说明

importjava.awt.*;

publicclassErr015extendsApplet

{

Readerr1;

VIPReaderr2:

publicvoidinit()

{

r1=newReader("

zhang"

1000);

r1.setBalance(-50);

r2=newVIPReader("

wang"

2000);

r2.setBalance(100);

}

publicvoidpaint(Graphicsg)

if(r1!

=null)

g.drawString(r1.String(),20,100);

if(r2!

g.drawString(r2.toString(),20,130);

privateclassReader

intreaderNo;

StringreaderName;

Stringpwd;

doublebalance;

Reader()

balance=0;

pwd=123456;

Reader(Stringname,intno)

readerNo=this.no;

readerName=name;

this();

setBalance(doubleinitSal)

if(initSal<

0)

System.out.println(“钱款不能为负数!

”);

else

balance=initSal;

abstractStringtoString()

StringS;

s="

编号:

"

+readerNo+"

|姓名:

+readerName+

"

|密码:

+pwd+"

|账户余额:

+balance;

returns;

classVIPReaderextendReader

VIPReader(intno,Stringname)

this(name,no);

publicStringtoString()

Strings;

s=super.toString();

改正:

importjava.applet.*;

//添包

VIPReaderr2;

//"

:

改为"

;

r2=newVIPReader(2000,"

);

//构造方法参数位置不对

g.drawString(r1.toString(),20,100);

//String改为toString

g.drawString(r2.toString(),20,130);

//;

改为英文状态下的输入;

classReader//去掉private

pwd="

123456"

//数据与类型不匹配加"

this();

//放在第一行

readerNo=no;

//去掉this

voidsetBalance(doubleinitSal)//缺少类型void

System.out.println("

钱款不能为负数!

//“”英文状态下的输入"

publicStringtoString()//abstract改为public

//大写改为小写

classVIPReaderextendsReader//保留字extends错误

super(name,no);

//this改为super

//缺少返回语句

(2)进一步Java自定义异常处理机制。

按以下要求完成相关程序1定义一个运动员成绩类:

(1)用适当的数据类型定义5个成员属性:

运动员编号、姓名、参加项目、成绩、所属单位;

(2)适当定义2个构造函数;

(3)定义不少于4个对有关属性进行适当操作的方法;

(4)自定义1个异常,当成绩值为负数时抛出该异常;

(5)编写一个测试程序验证该类的设计效果。

classAthelet

Stringnumber;

Stringname;

Stringsportitems;

doublegrade;

Stringunit;

Athelet()

number="

12"

name="

王明"

sportitems="

800米"

grade=97;

unit="

淮海工学院"

publicvoidsetsportitems(Stringnewsportitems)

{

sportitems=newsportitems;

}

publicStringgetsportitems()

returnsportitems;

publicvoidsetgrade(doublenewgrade)throwsaException

if(newgrade<

throw(newaException());

grade=newgrade;

publicdoublegetgrade()

returngrade;

return"

运动员信息是:

+number+"

|"

+name+"

+sportitems+"

+grade+unit;

}

classaExceptionextendsException

Stringmessage;

aException()

message="

成绩值不能为负数!

returnmessage;

publicclasstestExample6

publicstaticvoidmain(Stringargs[])

Atheleta1=newAthelet();

System.out.println(a1.toString());

Atheleta2=newAthelet();

try

a2.setgrade(-10);

catch(aExceptione)

System.out.println(e.toString());

System.out.println(a2.toString());

}

(3)改正以下程序中出现的错误,并进行注释说明

importjava.awt.*

importjavax.awt.event.*;

publicclassTestextendsApplet

Labelprompt;

TextFiledinput,output;

Buttonbtnok;

publicTest()

prompt=newLabel(“请输入一个整数:

input=newTextField(20);

btnok=newBotton("

求该数的平方"

add(prompt);

add(input);

add(output);

add(btnok);

input.addActionListener();

setsize(500,400);

publicvoidactionPerformed(ActionEvente)

if(e.getSource()=input)

note.setText(input.getText());

test2t=newtest2();

intx=t.cal(input.getText());

output.setText("

计算结果:

+x);

abstractclasstest2

staticinti=0;

privateintcal(intx)

i=x*x;

//缺少;

importjava.awt.event.*;

//javax改为java

//添加包

publicclassTestextendsAppletimplementsActionListener//添加接口

TextFieldinput,output;

//TextField拼写错误

publicvoidinit()//添加void类型

prompt=newLabel("

请输入一个整数:

的输入状态错误

output=newTextField(20);

//添加此行

btnok=newButton("

//Button拼写错误

input.addActionListener(this);

//添加this

btnok.addActionListener(this);

setSize(500,400);

//大小写

publicvoidactionPerformed(ActionEvente)

if(e.getSource()==input)

output.setText(input.getText());

//note改为output

intx=t.cal(Integer.parseInt(input.getText()));

//错误

+x);

//;

classtest2//去掉abstract

intcal(intx)//去掉private

returni;

(4)(选做)编写并调试运行如下程序,理解JAVA中内部类实现接口应用

interfaceAnimal

{voideat();

voidsleep();

classZoo

{privateclassTigerimplementsAnimal

{publicvoideat()

{

System.out.println("

tigereat"

publicvoidsleep()

tigersleep"

AnimalgetAnimal()

{returnnewTiger();

classInnerClassTest

publicstaticvoidmain(String[]args)

{

Zooz=newZoo();

Animalan=z.getAnimal();

an.eat();

an.sleep();

六、实验分析与体会

(1)理解JAVA接口语法及内部类实现接口

(2)JAVA异常处理中自定义异常处理、throw与throws区别

(3)总结常见Java错误及调试方法

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

当前位置:首页 > 法律文书 > 调解书

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

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