实验六深入面向对象编程2.docx

上传人:b****7 文档编号:10784306 上传时间:2023-02-22 格式:DOCX 页数:15 大小:55.66KB
下载 相关 举报
实验六深入面向对象编程2.docx_第1页
第1页 / 共15页
实验六深入面向对象编程2.docx_第2页
第2页 / 共15页
实验六深入面向对象编程2.docx_第3页
第3页 / 共15页
实验六深入面向对象编程2.docx_第4页
第4页 / 共15页
实验六深入面向对象编程2.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

实验六深入面向对象编程2.docx

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

实验六深入面向对象编程2.docx

实验六深入面向对象编程2

实验3深入面向对象编程

(2)

淮海工学院计算机科学系

实验报告书

课程名:

__Java___________

题目:

__深入面向对象编程

(2)

班级:

_____________

学号:

____________

姓名:

_____________

 

一、实验目的

学会运用属性的继承和隐藏、方法的继承、重载与覆盖、构造函数的继承与重载等来实现Java的继承与多态的思想;理解的Java的包、接口与异常处理机制,正确运用异常处理、包、接口、内部类、匿名类等语法实现对象的上转型、接口的定义、接口回调及多态编程。

正确使用字符串相关类(String、StringBuffer、StringTokenizer)、日期时间类(Date、Calendar)、Math类、System类、BigInteger类。

另外,对于ArrayList、Vecto、HashTable等具有泛型特征的集合类,在理解其工作原理的基础上,能进行初步的编程和应用。

二、实验环境

操作系统:

windows2000或WindowXP

集成开发环境:

JDK1.6及以上版本

三、实验学时

2学时,必做实验。

四、实验内容

(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!

=null)

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.awt.*;

importjava.applet.*;//添包

publicclassErr015extendsApplet

{

Readerr1;

VIPReaderr2;//":

"改为";"

publicvoidinit()

{

r1=newReader("zhang",1000);

r1.setBalance(-50);

r2=newVIPReader(2000,"wang");//构造方法参数位置不对

r2.setBalance(100);

}

publicvoidpaint(Graphicsg)

{

if(r1!

=null)

g.drawString(r1.toString(),20,100);//String改为toString

if(r2!

=null)

g.drawString(r2.toString(),20,130);//;改为英文状态下的输入;

}

}

classReader//去掉private

{

intreaderNo;

StringreaderName;

Stringpwd;

doublebalance;

Reader()

{

balance=0;

pwd="123456";//数据与类型不匹配加""

}

Reader(Stringname,intno)

{

this();//放在第一行

readerNo=no;//去掉this

readerName=name;

}

voidsetBalance(doubleinitSal)//缺少类型void

{

if(initSal<0)

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

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

else

balance=initSal;

}

publicStringtoString()//abstract改为public

{

Strings;//大写改为小写

s="编号:

"+readerNo+"|姓名:

"+readerName+

"|密码:

"+pwd+"|账户余额:

"+balance;

returns;

}

}

classVIPReaderextendsReader//保留字extends错误

{

VIPReader(intno,Stringname)

{

super(name,no);//this改为super

}

publicStringtoString()

{

Strings;

s=super.toString();

returns;//缺少返回语句

}

}

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

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

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

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

(2)适当定义2个构造函数;(3)定义不少于4个对有关属性进行适当操作的方法;(4)自定义1个异常,当成绩值为负数时抛出该异常;(5)编写一个测试程序验证该类的设计效果。

importjava.awt.*;

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<0)

throw(newaException());

grade=newgrade;

}

publicdoublegetgrade()

{

returngrade;

}

publicStringtoString()

{

return"运动员信息是:

"+number+"|"+name+"|"+sportitems+"|"+grade+unit;

}

}

classaExceptionextendsException

{

Stringmessage;

aException()

{

message="成绩值不能为负数!

";

}

publicStringtoString()

{

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());

else

{

test2t=newtest2();

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

output.setText("计算结果:

"+x);

}

}

}

abstractclasstest2

{

staticinti=0;

privateintcal(intx)

{

i=x*x;

}

}

改正:

importjava.awt.*;//缺少;

importjava.awt.event.*;//javax改为java

importjava.applet.*;//添加包

publicclassTestextendsAppletimplementsActionListener//添加接口

{

Labelprompt;

TextFieldinput,output;//TextField拼写错误

Buttonbtnok;

publicvoidinit()//添加void类型

{

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

");//""的输入状态错误

input=newTextField(20);

output=newTextField(20);//添加此行

btnok=newButton("求该数的平方");//Button拼写错误

add(prompt);

add(input);

add(output);

add(btnok);

input.addActionListener(this);//添加this

btnok.addActionListener(this);//添加此行

setSize(500,400);//大小写

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==input)

output.setText(input.getText());//note改为output

else

{

test2t=newtest2();

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

output.setText("计算结果:

"+x);//;的输入状态错误

}

}

}

classtest2//去掉abstract

{

staticinti=0;

intcal(intx)//去掉private

{

i=x*x;

returni;//缺少返回语句

}

}

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

interfaceAnimal

{voideat();

voidsleep();

}

classZoo

{privateclassTigerimplementsAnimal

{publicvoideat()

{

System.out.println("tigereat");

}

publicvoidsleep()

{

System.out.println("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-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1