版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第第頁java程序設(shè)計課程--實驗報告-實驗13《Java開發(fā)技術(shù)》實驗報告
實驗序號:實驗13實驗項目名稱:繼承
學(xué)號
姓名
專業(yè)、班
takemybreathaway實驗地點
實1-316
指導(dǎo)教師
實驗時間
2023-12-5
一、實驗?zāi)康募耙?/p>
●理解繼承的基本概念
●理解繼承與可見性
●掌握繼承的設(shè)計
二、實驗設(shè)備(環(huán)境)及要求
PC機,windowsxp,軟件環(huán)境(jdk1.6,tomcatweb服務(wù)器,Eclip)
●硬件要求:CPUPII以上,64M內(nèi)存,100M硬盤空間。
●軟件要求:WindowsXP,IE5以上。
●開發(fā)環(huán)境:JDK1.6.0_10
三、實驗內(nèi)容與步驟
修改類繼承中的錯誤
文件Dog.java聲明了一個Dog類,文件Labrador.java和Yorkshire.java是兩個繼承自Dog的類,文件DogTest.java是一個簡單的驅(qū)動程序。將文件保存至本地磁盤并仔細(xì)閱讀。按照以下步驟對上述程序進(jìn)行修改:
1.在DogTest.java文件中添加語句,要求在創(chuàng)建和打印Dog對象之后,還要創(chuàng)建和打印Yorkshire和Labrador對象。注意Labrador構(gòu)造器有兩個參數(shù):name和color,都是字符串類型。不要修改DogTest之外的任何文件,重新編譯DogTest.java,觀察碰到的錯誤。然后修改相應(yīng)文件來修正該錯誤。
2.在DogTest.java中添加代碼,打印Labrador和Yorkshire兩個類的平均種群重量。提示:使用avgBreedWeight()方法。在編譯中如果遇到錯誤,請解決該錯誤,并給出正確結(jié)果。
3.添加一個抽象方法intavgBreedWeight()至Dog.java。注意這就意味著需要使用關(guān)鍵字abstract來描述avgBreedWeight()方法,并且該方法沒有方法體。重新編譯所有程序,記錄編譯中出現(xiàn)的錯誤,以及解決的方法。
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();
}
設(shè)計類繼承
1.編寫一個抽象類(Shape),長方形、三角形和圓形均為其子類,并各有各的屬性。其父類中有計算周長和面積的方法。在測試類中,分別建立如干個對象,計算并輸出各對象的周長和面積。
Shape.java源代碼如下:
publicclassShape{
privatedoublearea;
privatedoublecircumference;
//圖形類的構(gòu)造函數(shù)
publicShape(){
area=0;
circumference=0;
}
//返回圖形的面積
publicdoublegetarea(){
returnarea;
}
//返回圖形的周長
publicdoublegetcircumference(){maewest
returncircumference;
}
}
idRectangle.java源代碼如下:
publicclassRectangleextendsShape{
privatedoublelength;
privatedoublewidth;
privatedoublearea;
privatedoublecircumference;
//長方形類的構(gòu)造函數(shù)1,對長和寬賦值
publicRectangle(doublel,doublew){
length=l;
width=w;
area=l*w;
circumference=2*(l+w);
}
//長方形類的構(gòu)造函數(shù)2,對長和寬賦值
publicRectangle(intl,intw){
length=(double)l;
width=(double)w;
area=length*width;
prent的意思circumference=2*(length+width);
}
//長方形類的構(gòu)造函數(shù)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;
//三角形類的構(gòu)造函數(shù),對三角形各邊賦值
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;
//圓形類的構(gòu)造函數(shù)
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類的數(shù)組實現(xiàn)*/
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)設(shè)計一個表示二維平面上點的類Point,包含有表示坐標(biāo)位置的protected類型的成員變量x和y,獲取和設(shè)置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的坐標(biāo)為:"+p1.getX());
System.out.println("y的坐標(biāo)為:"+p1.getY());
}
}
(2).設(shè)計一個表示二維平面上圓的類Circle,它繼承自類Point,還包含有表示圓半徑的protected類型的成員變量r、獲取和設(shè)置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).設(shè)計一個表示圓柱體的類Cylinder,它繼承自類Circle,還包含有表示圓柱體高的protected類型的成員變量h、獲取和設(shè)置h值的public方法、計算圓柱體體積的public方法。
(4).建立若干個Cylinder對象,輸出其軸心位置坐標(biāo)、半徑、高及其體積的值。
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026湖北東風(fēng)汽車研發(fā)總院整車與平臺開發(fā)招聘筆試模擬試題及答案解析
- 2026中國地質(zhì)調(diào)查局局屬單位招聘714人(第一批)考試備考試題及答案解析
- 2026云南中醫(yī)藥中等專業(yè)學(xué)校招聘2人筆試參考題庫及答案解析
- 2026一季度浙商銀行深圳分行社會招聘考試備考題庫及答案解析
- 2026四川中煙投資有限責(zé)任公司多元化企業(yè)(第一次)員工招聘36人筆試備考題庫及答案解析
- 2026年鄉(xiāng)村振興項目運營培訓(xùn)
- 2026年水文地質(zhì)模型及其應(yīng)用
- 2026上半年云南事業(yè)單位聯(lián)考保山市事業(yè)單位公開招聘工作人員考試備考題庫及答案解析
- 2026年聚焦住宅地產(chǎn)的投資機會
- 2025年美團saas定向班筆試及答案
- 供貨流程管控方案
- 章節(jié)復(fù)習(xí):平行四邊形(5個知識點+12大??碱}型)解析版-2024-2025學(xué)年八年級數(shù)學(xué)下冊(北師大版)
- 中試基地運營管理制度
- 老年病康復(fù)訓(xùn)練治療講課件
- 2024中考會考模擬地理(福建)(含答案或解析)
- CJ/T 164-2014節(jié)水型生活用水器具
- 購銷合同范本(塘渣)8篇
- 貨車充電協(xié)議書范本
- 屋面光伏設(shè)計合同協(xié)議
- 生鮮業(yè)務(wù)采購合同協(xié)議
- 夫妻門衛(wèi)合同協(xié)議
評論
0/150
提交評論