版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第第页java程序设计课程--实验报告-实验13《Java开发技术》实验报告
实验序号:实验13实验项目名称:继承
学号
姓名
专业、班
takemybreathaway实验地点
实1-316
指导教师
实验时间
2023-12-5
一、实验目的及要求
●理解继承的基本概念
●理解继承与可见性
●掌握继承的设计
二、实验设备(环境)及要求
PC机,windowsxp,软件环境(jdk1.6,tomcatweb服务器,Eclip)
●硬件要求:CPUPII以上,64M内存,100M硬盘空间。
●软件要求:WindowsXP,IE5以上。
●开发环境:JDK1.6.0_10
三、实验内容与步骤
修改类继承中的错误
文件Dog.java声明了一个Dog类,文件Labrador.java和Yorkshire.java是两个继承自Dog的类,文件DogTest.java是一个简单的驱动程序。将文件保存至本地磁盘并仔细阅读。按照以下步骤对上述程序进行修改:
1.在DogTest.java文件中添加语句,要求在创建和打印Dog对象之后,还要创建和打印Yorkshire和Labrador对象。注意Labrador构造器有两个参数:name和color,都是字符串类型。不要修改DogTest之外的任何文件,重新编译DogTest.java,观察碰到的错误。然后修改相应文件来修正该错误。
2.在DogTest.java中添加代码,打印Labrador和Yorkshire两个类的平均种群重量。提示:使用avgBreedWeight()方法。在编译中如果遇到错误,请解决该错误,并给出正确结果。
3.添加一个抽象方法intavgBreedWeight()至Dog.java。注意这就意味着需要使用关键字abstract来描述avgBreedWeight()方法,并且该方法没有方法体。重新编译所有程序,记录编译中出现的错误,以及解决的方法。
DogTest.java源代码如下:
publicclassDogTest{
publicstaticvoidmain(String[]args){
Yorkshireyorkshire=newYorkshire("xiaohei");
Labradorlabrador=newLabrador("xiaobai","white");
System.out.Name()+"says"+yorkshire.speak());
System.out.Name()+"says"+labrador.speak());
System.out.Name()+
"BreedWeight"+yorkshire.avgBreedWeight()+"says"+yorkshire.speak());
System.out.Name()+
"BreedWeight"+labrador.avgBreedWeight()+"says"+labrador.speak());
}
}
Yorkshire.java源代码如下:
publicclassYorkshireextendsDog{
privateintbreedWeight=50;
publicYorkshire(Stringname){
super(name);
}
publicStringspeak(){
return"woof";
}
publicintavgBreedWeight(){
returnbreedWeight;
}
}
Labrador.java源代码如下:
publicclassLabradorextendsDog{
privateStringcolor;//black,yellow,orchocolate?
privateintbreedWeight=75;
publicLabrador(Stringname,Stringcolor){
super(name);
lor=color;
}
publicStringspeak(){
return"WOOF";
}
publicintavgBreedWeight(){
returnbreedWeight;
}
}
Dog.java源代码如下:
publicabstractclassDog{
protectedStringname;
publicDog(Stringname){
=name;
}
publicStringgetName(){
returnname;
}
publicStringspeak(){
munreturn"Woof";
}
publicabstractintavgBreedWeight();
}
设计类继承
1.编写一个抽象类(Shape),长方形、三角形和圆形均为其子类,并各有各的属性。其父类中有计算周长和面积的方法。在测试类中,分别建立如干个对象,计算并输出各对象的周长和面积。
Shape.java源代码如下:
publicclassShape{
privatedoublearea;
privatedoublecircumference;
//图形类的构造函数
publicShape(){
area=0;
circumference=0;
}
//返回图形的面积
publicdoublegetarea(){
returnarea;
}
//返回图形的周长
publicdoublegetcircumference(){maewest
returncircumference;
}
}
idRectangle.java源代码如下:
publicclassRectangleextendsShape{
privatedoublelength;
privatedoublewidth;
privatedoublearea;
privatedoublecircumference;
//长方形类的构造函数1,对长和宽赋值
publicRectangle(doublel,doublew){
length=l;
width=w;
area=l*w;
circumference=2*(l+w);
}
//长方形类的构造函数2,对长和宽赋值
publicRectangle(intl,intw){
length=(double)l;
width=(double)w;
area=length*width;
prent的意思circumference=2*(length+width);
}
//长方形类的构造函数3,对长和宽赋值
publicRectangle(Stringl,Stringw){
length=Double.parDouble(l);
width=Double.parDouble(w);
area=length*width;
circumference=2*(length+width);
}
//返回长方形的长
publicdoublegetlength(){
returnlength;
}
//返回长方形的宽
publicdoublegetwidth(){
returnwidth;
}
//返回长方形的面积
publicdoublegetarea(){
returnarea;
}
//返回长方形的周长
googlecodejampublicdoublegetcircumference(){
returncircumference;
}
//重新定义equals方法
publicbooleanequals(ObjectotherObject){
if(this==otherObject)
returntrue;
if(otherObject==null)
returnfal;
if(getClass()!=Class())
returnfal;
Rectangleother=(Rectangle)otherObject;
returnlength==other.lengthwidth==other.width
area==other.areacircumference==other.circumference;
}
//重新定义hashCode方法
publicinthashCode(){
return(newDouble(this.width).hashCode()+newDouble(this.length).hashCode());
}
//重新定义toString方法
publicStringtoString(){
return"长:"+length+"宽:"+width+"面积:"+area+"周长"+circumference;
}
}
Triangle.java源代码如下:
publicclassTriangleextendsShape{
privatedoublecircumference;
ass是什么意思privatedoublearea;
privatedoubleedge1;
privatedoubleedge2;
privatedoubleedge3;
//三角形类的构造函数,对三角形各边赋值
publicTriangle(doubleE1,doubleE2,doubleE3){
doublep,q;
edge1=E1;
edge2=E2;
edge3=E3;
circumference=edge1+edge2+edge3;
p=0.5*(edge1+edge2+edge3);
q=p*(p-edge1)*(p-edge2)*(p-edge3);
area=Math.sqrt(q);
}
//返回边1的长?
publicdoublegetedge1(){
returnedge1;
}
//返回边2的长?
publicdoublegetedge2(){
returnedge2;
}
//返回边3的长?
publicdoublegetedge3(){
returnedge3;
}
//返回三角形的面积
publicdoublegetarea(){
returnarea;
}
//返回三角形的周长
publicdoublegetcircumference(){
returncircumference;
}
//重新定义equals方法
sufferedpublicbooleanequals(ObjectotherObject){
if(this==otherObject)
returntrue;
if(getClass()!=Class())
returnfal;
if(otherObject==null)
returnfal;
Triangleother=(Triangle)otherObject;
returnedge1==other.edge1
edge2==other.edge2
edge3==other.edge3
area==other.area
circumference==other.circumference;
}
//重新定义hashCode方法
publicinthashCode(){
return(newDouble(this.edge1).hashCode()+newDouble(this.edge2).hashCode()+newDouble(this.edge3).hashCode());
}//重新定义toString方法
publicStringtoString(){
return"边1:"+edge1+"边2:"+edge2+"边3:"+edge3+"面积:"+area+"周长"+circumference;
}
}
Circle.java源代码如下:
publicclassCircleextendsShape{
doubleradius;
doublepi=3.14;
privatedoublearea;
privatedoublecircumference;
//圆形类的构造函数
publicCircle(doubler){
radius=r;
area=pi*r*r;
circumference=2*pi*r;
}
//返回圆的半径
publicdoublegetradius(){
returnradius;
}
//返回圆的面积
publicdoublegetarea(){
returnarea;
}
//返回圆的周长
publicdoublegetcircumference(){
returncircumference;
}
publicbooleanequals(ObjectotherObject){
if(this==otherObject)
returntrue;lor什么意思中文
if(getClass()!=Class())
returnfal;
if(otherObject==null)
returnfal;
Circleother=(Circle)otherObject;
returnradius==other.radius
pi==other.pi
area==other.area
circumference==other.circumference;
}
publicinthashCode(){
return(newDouble(this.radius).hashCode());
}
publicStringtoString(){
return"半径:"+radius+"面积:"+area+"周长"+circumference;
}
}
Test.java源代码如下:
/**
*Test类的数组实现*/
publicclassTest{
publicstaticvoidmain(String[]args){
doublesumArea;
doublesumCircumference;
Shape[]s=newShape[3];
s[0]=newRectangle(3,4);
//定义一个长方形r1,长为3,宽为4
s[1]=newCircle(2);
s[2]=newTriangle(2,6,5);
for(Shapeelement:s)
System.out.println(element);
sumArea=s[0].getarea()+s[1].getarea()+s[2].getarea();
sumCircumference=s[0].getcircumference()+
s[1].getcircumference()+s[2].getcircumference();
System.out.println("总面积:"+sumArea+",总周长:"+sumCircumference);
}
}
2.
(1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x和y值的public方法。
Point.java源代码如下:
publicclassPoint{
floatx,y;
publicPoint(floata,floatb){
x=a;
y=b;
}
publicvoidtPoint(floatx,floaty){
this.x=x;
this.y=y;
}
publicfloatgetX(){
returnx;
}
publicfloatgetY(){
returny;
}
publicstaticvoidmain(String[]args){
Pointp1=newPoint(12,20);
System.out.println("x的坐标为:"+p1.getX());
System.out.println("y的坐标为:"+p1.getY());
}
}
(2).设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、获取和设置r值的public方法、计算圆面积的public方法。
NCircle.java源代码如下:
publicclassNCircleextendsPoint{
protectedfloatradius;
//finalfloatPI=3.14;
publicNCircle(floata,floatb,floatr){
super(a,b);
radius=r;
}
publicvoidtCircle(floatr){
radius=r;
}
floatgetRadius(){
returnradius;
}
doublearea(){
return3.14*radius*radius;
}
publicstaticvoidmain(String[]args){
NCirclec1=newNCircle(3,4,5);
System.out.println("半径为:"+c1.getRadius()+"面积为:"+c1.area());
}
}
(3).设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public方法。
(4).建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值。
Cylinder.java源代码如下:
publicclassCylinderextendsNCircle{
floatheigh;
publicCylinder(floata,floatb,floatr,floath){
super(a,b,r);
heigh=h;
}
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 现浇墩、台帽或盖梁现场质量检验报告单
- 小学英语课堂教学达标量化评分表
- 「境启城芯·悦见生活」房地产项目宣传片策划方案
- 广东省汕尾市2026届高三冲刺模拟语文试卷含解析
- 医学26年:增强现实应用要点解读 查房课件
- 【2025】哈尔滨市香坊区新成街道工作人员招聘考试真题
- 【福建省福州市中考语文复习重点解析】
- 【公共营养师(四级)技能培训水平测试题库】
- 【2026】高中英语届高考读后续写主题金句(共十一类)
- 【2025】成都医学院第一附属医院招聘考试真题
- 村级妇联换届选举课件
- 秋季朋克青年硬核养生节活动方案
- 呼吸功能障碍课件
- 2025年全国高考(新课标Ⅰ卷)数学真题卷含答案解析
- 安宁疗护舒适照护课件
- 城区地下管网维护与运营管理方案
- 桡骨远端骨折护理课件
- 2025年学校食品安全事故应急演练实施方案(含演练脚本)
- 重症医学科护理质控体系
- 太仓用人单位劳动合同(2025版)
- 译林版七年级下册英语Unit5 Animal Friends基础专项巩固训练(含答案)
评论
0/150
提交评论