Java实验报告四继承和多态.docx
《Java实验报告四继承和多态.docx》由会员分享,可在线阅读,更多相关《Java实验报告四继承和多态.docx(13页珍藏版)》请在冰豆网上搜索。
Java实验报告四继承和多态
福建农林大学计算机与信息学院实验报告
系
软件工程
专业
软件工程
年级
08级
成绩
姓名
学号
实验室
T313
机号
48
实验时间
2010年11月15日下午3、4节
教师签字
实验(四)继承和多态
一、实验目的和要求
1.掌握面向对象的继承关系
2.理解面向对象的多态
3.掌握方法的重写
4.掌握接口的实现
二、实验内容和原理
设计一个抽象类Shape,包括2个抽象方法,分别是计算形状的周长和面积。
设计具体类Rectangle和Circle,分别表示矩形和圆,它们都是Shapes的子类。
使Rectangle和Circle都实现Comparable接口(根据面积比较大小),编写一个通用的排序程序和一个通用的二分查找程序(自己写代码实现),能对这二种类型的数组进行排序并查找指定的对象。
三、实验环境
1.硬件环境:
2.软件环境:
JDK1.5
四、算法描述及实验步骤
1.算法描述(可以用类图、流程图、伪代码或源程序描述)
…
对实现了Comparable接口的对象数组data进行排序
publicstaticvoidsort(Comparable[]data)
在实现了Comparable接口的对象数组data中查找元素element
publicintbinarySearch(Comparable[]data,Comparableelement)
2.实验步骤
●创建一个UML项目,并设计类Shape、Rectangle和Circle,并建立描述Shape、Rectangle、Circle和Comparable间关系的类图
●创建一个Java应用项目
●把UML项目中的类自动生成代码到Java应用项目中
●实现类中的方法及通用的排序和检索程序
●进行编译
●进行测试,使用的测试用例:
输入:
预期输出:
…
五、调试过程
1.编译过程
改为:
2.调试过程
记录算法实现中发现的逻辑错误及改正,对每个测试用例,记录实际输出,并与预期输出进行比较,如果不同,分析产生错误的原因并改正。
输入:
预期输出:
【排序面积从小到大】:
【圆形数组】【长度】5【位置】0【圆形】【半径】:
2.0【面积】12.57【周长】:
12.57
【位置】1【圆形】【半径】:
3.0【面积】28.27【周长】:
18.85
【位置】2【圆形】【半径】:
4.0【面积】50.27【周长】:
25.13
【位置】3【圆形】【半径】:
5.0【面积】78.54【周长】:
31.42
【位置】4【圆形】【半径】:
6.0【面积】113.10【周长】:
37.70
【矩形数组】【长度】5
【位置】0【矩形】【长】:
1.0【宽】:
2.0【面积】:
2.00【周长】:
4.00
【位置】1【矩形】【长】:
2.0【宽】:
3.0【面积】:
6.00【周长】:
12.00
【位置】2【矩形】【长】:
3.0【宽】:
4.0【面积】:
12.00【周长】:
24.00
【位置】3【矩形】【长】:
4.0【宽】:
5.0【面积】:
20.00【周长】:
40.00
【位置】4【矩形】【长】:
5.0【宽】:
6.0【面积】:
30.00【周长】:
60.00
【二分法查找】:
【对象】【圆形】【半径】:
6.0【面积】113.10【周长】:
37.70位于【圆形】数组的第【4】位。
【对象】【矩形】【长】:
10.0【宽】:
10.0【面积】:
100.00【周长】:
200.00不存在【矩形】数组中。
实际输出:
分析:
实验的输出结果和预期输出的结果相同,程序无错误。
六、实验结果
用与测试用例不同的输入数据运行算法,写出得到的结果,并分析结果是否正确。
输入:
输出结果:
结果分析:
程序换个用例测试所得的结果与题目要求的完全一致,程序无错误。
七、总结
通过这次上机使我对JAVA的继承类和接口有了更进一步的认识,使我对运用JAVA有了更深的体会。
附录:
测试Main类:
packagejavaapplication1;
/**
*
*@authorAdministrator
*/
importjava.text.*;
publicclassMain{
publicstaticvoidmain(String[]args){
/*Shape[]shape1={newCircle(2.0),newCircle(3.0),newCircle(4.0),
newCircle(5.0),newCircle(6.0)};
Shape[]shape2={newRectangle(1.0,2.0),newRectangle(2.0,3.0),newRectangle(3.0,4.0),
newRectangle(4.0,5.0),newRectangle(5.0,6.0)};
Shapeshape3=newCircle(6.0);
Shapeshape4=newRectangle(10.0,10.0);*/
Shape[]shape1={newCircle(1.1),newCircle(1.2),newCircle(1.3),
newCircle(1.4),newCircle(1.5)};
Shape[]shape2={newRectangle(1.0,4.0),newRectangle(7.0,3.0),newRectangle(3.0,5.0),
newRectangle(9.0,5.0),newRectangle(5.0,4.0)};
Shapeshape3=newCircle(2.0);
Shapeshape4=newRectangle(9.5,5.0);
System.out.print("【排序面积从小到大】:
\n");
sort(shape1);
sort(shape2);
System.out.print("【圆形数组】【长度】"+shape1.length);
for(inti=0;iSystem.out.println("【位置】"+i+shape1[i].toString());
}
System.out.println("【矩形数组】【长度】"+shape2.length);
for(inti=0;iSystem.out.println("【位置】"+i+shape2[i].toString());
}
intindex=BinarySearch(shape1,shape3);
System.out.println("\n【二分法查找】:
");
if(index==0){
System.out.println("【对象】"+shape3.toString()+"不存在【圆形】数组中。
");
}
else{
System.out.println("【对象】"+shape3.toString()+
"位于【圆形】数组的第【"+index+"】位。
");
}
index=BinarySearch(shape2,shape4);
if(index==0){
System.out.println("【对象】"+shape4.toString()+
"不存在【矩形】数组中。
");
}
else{
System.out.println("【对象】"+shape4.toString()+
"位于【矩形】数组的第【"+index+"】位。
");
}
}
publicstaticvoidsort(Comparable[]list){
Comparablecom;
intn=list.length;
for(inti=0;iintm=i;
for(intj=i+1;jif(list[j].compareTo(list[m])==-1){
m=j;
}
}
if(m!
=i){
com=list[i];
list[i]=list[m];
list[m]=com;
}
}
}
publicstaticintBinarySearch(Comparable[]list,Comparablecom){
intlow=0;
inthigh=list.length-1;
intmid=0;
while(low<=high){
mid=(low+high)/2;
if(list[mid].compareTo(com)==0){
returnmid;
}
elseif(list[mid].compareTo(com)==1){
high=mid-1;
}
else{
low=mid+1;
}
}
return0;
}
}
Shape类:
packagejavaapplication1;
/**
*
*@authorAdministrator
*/
importjava.text.*;
publicabstractclassShapeimplementsComparable{
publicShape(){
}
publicabstractdoublegetPerimeter();
publicabstractdoublegetArea();
publicabstractStringtoString();
}
Cricle类:
packagejavaapplication1;
/**
*
*@authorAdministrator
*/
importjava.text.*;
publicclassCircleextendsShape{
privatedoubleradius;
publicCircle(){
radius=0.0;
}
publicCircle(doubleradius){
this.radius=radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleval){
this.radius=val;
}
publicdoublegetArea(){
returnMath.PI*radius*radius;
}
publicdoublegetPerimeter(){
return2.0*Math.PI*radius;
}
publicStringtoString(){
Stringstr;
DecimalFormatd1=newDecimalFormat("0.00");
str="【圆形】【半径】:
"+radius+"【面积】"+
d1.format(getArea())+"【周长】:
"+d1.format(getPerimeter());
returnstr;
}
publicintcomparaTo(Objecto){
if(this.getArea()>((Circle)o).getArea()){
return1;
}
elseif(this.getArea()==((Circle)o).getArea()){
return0;
}
else{
return-1;
}
}
publicintcompareTo(Objecto){
if(this.getArea()>((Circle)o).getArea())
return1;
elseif(this.getArea()==((Circle)o).getArea())
return0;
else
return-1;
}
}
Rectangle类:
packagejavaapplication1;
/**
*
*@authorAdministrator
*/
importjava.text.*;
publicclassRectangleextendsShape{
privatedoublelength;
privatedoublewidth;
publicRectangle(){
width=0.0;
length=0.0;
}
Rectangle(doublelength,doublewidth){
this.length=length;
this.width=width;
//thrownewUnsupportedOperationException("Notyetimplemented");
}
publicdoublegetLength(){
returnlength;
}
publicvoidsetLength(doubleval){
this.length=val;
}
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doubleval){
this.width=val;
}
publicdoublegetArea(){
returnlength*width;
}
publicdoublegetPerimeter(){
return2.0*length*width;
}
publicStringtoString(){
Stringstr;
DecimalFormatd1=newDecimalFormat("0.00");
str="【矩形】【长度】:
"+length+"【宽】:
"+width+"【面积】:
"+
d1.format(getArea())+"【周长】:
"+d1.format(getPerimeter());
returnstr;
}
publicintcomparaTo(Objectobj){
if(this.getArea()>((Circle)obj).getArea()){
return1;
}
elseif(this.getArea()==((Circle)obj).getArea()){
return0;
}
else{
return-1;
}
}
publicintcompareTo(Objecto){
if(this.getArea()>((Rectangle)o).getArea())
return1;
elseif(this.getArea()==((Rectangle)o).getArea())
return0;
else
return-1;
}
}