java語言程序設(shè)計(jì)作業(yè)答案_第1頁
java語言程序設(shè)計(jì)作業(yè)答案_第2頁
java語言程序設(shè)計(jì)作業(yè)答案_第3頁
java語言程序設(shè)計(jì)作業(yè)答案_第4頁
java語言程序設(shè)計(jì)作業(yè)答案_第5頁
已閱讀5頁,還剩46頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

Java語言程序設(shè)計(jì)(鄭莉)

第二章習(xí)題答案

L什么是對象、類,它們之間的聯(lián)系?

答:1)對象是包含現(xiàn)實(shí)世界物體特征的抽象實(shí)體,它反映系統(tǒng)為之保存信息和與它交互的

能力。對象是一些屬性及服務(wù)的封裝體,在程序設(shè)計(jì)領(lǐng)域,可以用“對象=數(shù)據(jù)+作用于這些

數(shù)據(jù)上的操作”來表示。現(xiàn)實(shí)生活中對象是指客觀世界的實(shí)體;在程序中對象是指一組變量

和相關(guān)方法的集合。

2)類是既有相同操作功能和相同的數(shù)據(jù)格式的對象的集合與抽象!

3)兩者的關(guān)系:對象是類的具體實(shí)例

4.請解釋類屬性、實(shí)例屬性及其區(qū)別。

答:實(shí)例屬性,由一個(gè)個(gè)的實(shí)例用來存儲(chǔ)所有實(shí)例都需要的屬性信息,不同實(shí)例的屬性

值可能會(huì)不同。

5.請解釋類方法、實(shí)例屬性及其區(qū)別。

答:實(shí)例方法表示特定對象的行為,在聲明時(shí)前面不加static修飾符,在使用時(shí)需要發(fā)送給

一個(gè)類實(shí)例。

類方法也稱為靜態(tài)方法,在方法聲明時(shí)前面需加static修飾符,類方法表示具體實(shí)例中

類對象的共有行為。

區(qū)別:實(shí)例方法可以直接訪問實(shí)例變量,調(diào)用實(shí)例方法,實(shí)例方法可以直接訪問類變量,

調(diào)用類方法;類方法可以直接調(diào)用類變量和類方法,類方法不能直接調(diào)用實(shí)例變量和實(shí)例方

法;

6.類的訪問控制符有哪幾種?具體含義及其區(qū)別。

答:類的訪問控制符只有public(公共類)及無修飾符(默認(rèn)類)兩種。

區(qū)別:當(dāng)使用public修飾符時(shí)表示所有其他的類都可以使用此類;當(dāng)沒有修飾符時(shí),則只有

與此類處于同一包中的其他類可以使用類。

7類成員的訪問控制符有哪幾種?他們對類成員分別有哪些訪問限制的作用?

答:

類成員的訪問控制符有public,private,protecte及無修飾符.

public(公有的):用public修飾的成分表示公有的,也就是它可以被其他任何對象訪問(前提

是對累成員所在的類訪問有訪問權(quán)限).

Private(保護(hù)的):類中限定為private的成員只能被這個(gè)類本身訪問,在類外不可見。

proteced(保護(hù)的)用該關(guān)鍵字修飾的成分是受保護(hù)的,只可以被同一類及其子類的實(shí)例

對象訪問。

無修飾符(默認(rèn)的):public,private,protected這個(gè)三個(gè)限定符不是必須寫的。如果不

寫,則表明是“friendly",相應(yīng)的成分可以被所在保重的各類訪問。

8簡述構(gòu)造方法的特點(diǎn)?

答:

構(gòu)造方法主要有以下特點(diǎn):

(1)構(gòu)造方法的方法名與類名相同;

(2)構(gòu)造方法沒有返回類型(修飾符void也不能有);

(3)構(gòu)造方法通常被聲明為公有的(public);

(4)構(gòu)造方法可以有任意多個(gè)參數(shù);

(5)構(gòu)造方法的主要作用是完成對象的初始化工作;

(6)構(gòu)造方法不能在程序中顯式的調(diào)用;

(7)在生成一個(gè)對象時(shí),系統(tǒng)會(huì)自動(dòng)調(diào)用該類的構(gòu)造方法為新生成的對象初始化。

9如果在類聲明中聲明了構(gòu)造方法,系統(tǒng)是否還提供默認(rèn)的構(gòu)造方法?

答:

用戶在進(jìn)行類聲明時(shí),如果沒有聲明任何構(gòu)造方法,系統(tǒng)會(huì)賦給此類一個(gè)默認(rèn)(無參)的

構(gòu)造方法。但是,只要用戶聲明了構(gòu)造方法,即使沒有聲明無參的構(gòu)造方法,系統(tǒng)也不會(huì)再

賦默認(rèn)的構(gòu)造方法。

11:聲明并測試一個(gè)復(fù)數(shù)類,其方法包括toString()及復(fù)數(shù)的加、減、乘運(yùn)算。

答:publicclassComplex

(

privatefloata;

privatefloatb;

publicStringtoString()

{if(a!=0)

return(a+"i"+"+"+b);

elsereturn(""+b);

}

publicComplex(floata,floatb)

(

this.a=a;

this.b=b;

)

publicvoidAdd(Complexp)

this.a+=p.a;

this.b+=p.b;

)

publicvoidDecrease(Complexp)

this,a-=p.a;

this.b-=p.b;

)

publicvoidMultiply(Complexp)

this.a=this,a*p.a;

this.b=this.b*p.b;

)

)

publicclassComplexTexter

(

publicstaticvoidmain(Stringargs[])

(

Complexa=newComplex(2,4);

Complexb=newComplex(5,8);

a.Add(b);System.out.println(a.toString()+"\n");

a.Decrease(b);System.out.println(a.toString()+"\n");

a.Multiply(b);System.out.println(a.toString()+"\n");

)

)

p:\java>jauacComplex.Jaua

D:\jaua>jauacComplexTexter-jaua

ID:\jaua>jauaComplexTexter

7.0i+12.0

5.0i+8.0

10.0i+32.0

12:什么是UML?它由哪幾部分組成?

答:

UML是圖形化()即可視化的建模語言,成為面向?qū)ο蠼5臉?biāo)準(zhǔn)語言。

它由四部分組成:(1)視圖(2)圖(3)模型元素(4)通用機(jī)制

13.常用的類與類之間的關(guān)系有哪幾種。

答:有關(guān)聯(lián)、依賴、流、泛化、實(shí)現(xiàn)、使用。

Java語言程序設(shè)計(jì)(鄭莉)

第三章課后習(xí)題答案

1.設(shè)N為自然數(shù):

n!=l*2*3*....*n

稱為n的階乘,并且規(guī)定0!=1.試編程計(jì)算2!,4!,6!he10!.并將結(jié)果輸出到屏幕上。

答:

publicclassMui

publicstaticvoidmain(Stringargs[])

inti,n;

floats;

for(n=0;n<=10;n=n+2)

(

if(n==0)

System,out.println("0!=l\n");

else

{s=l;

for(i=l;i<=n;i++)

s=s*i;

System.out.println(n+"!="+s+"\n");

}

)

)

)

2.編寫程序,接收用戶從鍵鍵盤上輸入的三個(gè)整數(shù)xyz..從中選出最大和最小者,并編程實(shí)

現(xiàn)。

答:publicclassMath{

publicstaticvoidmain(Stringargs[]){

int[]IntArg=newint[args.length];

for(inti=O;i<args.length;i++){

lntArg[i]=lnteger.parselnt(args[i]);

)

intmax,min;

max=lntArg[O]>lntArg[l]?lntArg[O]:lntArg[l];

max=max>lntArg[2]?max:lntArg[2];

min=lntArg[O]<lntArg[l]?lntArg[O]:lntArg[l];

min=min<lntArg[2]?min:lntArg[2];

System.out.println("max="+max);

System.out.println("min="+min);

)

)

D:\jaua>jauacMath.jaua

D:\jaua>jauaMath451678

max=78

min=16

D1

3.求出100—枚的宿舍,并將這些數(shù)在屏幕上5個(gè)乙杭地顯示出來。

答:

publicclassSu{

publicstaticvoidmain(Stringargs[]){

intn,i,k=0,y;

for(n=2;n<=100;n++){

y=i;

for(i=2;i<n;i++)

if(n%i==0)

{y=0;break;}

if(y==i){

k++;

System.out.print(n+"");

if(k%5==0)

System.out.print("\n");

}

)

}

:\jaua>jauacSu.Jaua

[D:\jaua>jauaSu

23571113

1719232931

3741434753

5961677173

79838997

D:\jaua>■

4.使用java.Iang.Math類,生成100個(gè)0---99之間的隨機(jī)整數(shù),找出他們之中的最大值和最

小值,并統(tǒng)計(jì)大于50的整數(shù)個(gè)數(shù)。

publicclassRandom{

publicstaticvoidmain(String[]args)

(

intMinNum,MaxNum,n=O;

int[]array=newint[100];

array[0]=(int)(Math.random()*100);

MinNum=array[0];

MaxNum=array[0];

System,out.printin(〃數(shù)列為:”);

System.out.print(array[O]+"");

for(inti=0;i<100;i++)

(

array[i]=(int)(Math.random()*100);

if(array[i]>50)

n++;

if(array[i]>=MaxNum)

MaxNum=array[i];

if(array[i]<=MinNum)

MinNum=array[i];

System.out.print(array[i]+"");

}

System.out.println();

System.out.println("MinNum="+MinNum);

System.out.println("MaxNum="+l\/laxNum);

System.out.printin(〃大于50的整數(shù)個(gè)數(shù)有:"+n);

)

}

聲C:\VIIDOVS\systeB32\cBd.exe-3

D:\我的文檔、桌面3aua>J.vacRandom.Jaua

D:的文檔、臬面\J.uQJauaRandon

△A石ig

260515523

841.

232587S44812637346S111,?308S646glb05159b64636882S0225

547533381971611903537133121799865V3956862471594389

386854383?426986717678697977356SI

inNun=1

axNun=98

大于56的整數(shù)個(gè)數(shù)有:52

7.什么是異常?解釋拋出、捕獲的含義。

答:異常又稱為例外,是特殊的運(yùn)行錯(cuò)誤對象,在程序中可以強(qiáng)制編譯器來處理程序運(yùn)行中

的發(fā)生的并非由程序本身所造成的錯(cuò)誤;

拋出異常:把生成異常對象并提交的過程稱為拋出異常;

拋出異常是java中一個(gè)程序處理動(dòng)作,檢查異常時(shí)要么在方法中聲明一個(gè)異常拋出,用

try-catch語句捕獲異常,并進(jìn)行處理。

8.簡述Java的異常處理機(jī)制。

答:java中聲明了很多異常類,每個(gè)異常類都代表了一種運(yùn)行錯(cuò)誤,類中包含了該運(yùn)行錯(cuò)誤

的信息和處理錯(cuò)誤的方法等內(nèi)容。每當(dāng)java程序運(yùn)行過程中發(fā)生一個(gè)可識別的運(yùn)行錯(cuò)誤時(shí),

即該錯(cuò)誤有一個(gè)異常類與之相對應(yīng)時(shí),系統(tǒng)都會(huì)產(chǎn)生一個(gè)相應(yīng)的該異常類的對象,即產(chǎn)生一

個(gè)異常。一旦一個(gè)異常對象產(chǎn)生了,系統(tǒng)中就一定有相應(yīng)的機(jī)制來處理它,確保不會(huì)產(chǎn)生死

機(jī)、死循環(huán)或其他對操作系統(tǒng)的損害,從而保證了整個(gè)程序運(yùn)行的安全性。

9.系統(tǒng)定義的異常與用戶自定義的異常有何不同?如何使用這兩類異常?

答:系統(tǒng)定義的特定情況出現(xiàn)的問題,而此時(shí)用來對可能遇到的問題進(jìn)行處理。用戶定義的

是自己覺得可能會(huì)出現(xiàn)問題時(shí),需要處理的。這樣避免程序中斷或是出現(xiàn)未知錯(cuò)誤。

系統(tǒng)異常有兩種一種是運(yùn)行時(shí)異常,一種是普通異常,普通異常要求用戶捕獲或者拋出的,

不補(bǔ)貨或者拋出就會(huì)編譯不通過。運(yùn)行時(shí)異常編譯可以通過,但是運(yùn)行時(shí)才顯露出來。

10.用戶如何自定義異常?編程實(shí)現(xiàn)一個(gè)用戶自定義異常。

(1)定義mytest

importjava.io.*;

publicclassmytest{

privatestaticintquotient(intnumber,intdenominator)throws

DivideByZeroException{

if(denominator==0)

thrownewDivideByZeroException();

return(number/denominator);

}

publicstaticvoidmain(Stringargs[]){

intnumberl=0,number2=0,result=0;

try{

System.out.printin("輸入第一個(gè)數(shù)字:");

numberl=Integer.valueOf(Keyboard.getString()).intValue();

System.out.printin(〃輸入第二個(gè)數(shù)字:〃);

number2=Integer.valueOf(Keyboard.getString()).intValue();

result=quotient(numberl,number2);

)

catch(NumberFormatExceptione){

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

System,exit(-1);

)

System,out.printIn(numberl+,//,,+number2+,,=,z+result);

}

)

(2)定義DivideByZeroException

publicclassDivideByZeroExceptionextendsArithmeticException{

publicDivideByZeroException(){

super(〃除數(shù)不能為0!〃);

)

}

(3)定義Keyboard

importjava.io.*;

publicclassKeyboard{

staticBufferedReaderinputStream=newBufferedReader(new

InputStreamReader(System,in));

publicstaticintgetlnteger(){

try{

return(Integer.valueOf(inputstream.readLine().trim()).intValue());

}catch(Exceptione){

e.printStackTrace();

return0;

}

)

publicstaticStringgetString(){

try{

return(inputstream.readLine());

}catch(lOExceptione){return〃0〃;}

D:\jaua>jauanytest

輸入第一個(gè)數(shù)字:

76

輸入第二個(gè)數(shù)字:

Exceptioninthread"main"DiuideBi/ZeroException:除數(shù)不能為0?

atmytest.quotient〈mytest.Jaua:6>

atnytest.main<nytest.jaua:17>

D二7aua>

Java語言程序設(shè)計(jì)

第四章課后習(xí)題答案

1.子類將繼承父類所有的屬性和方法嗎?為什么?

答:不,子類不能直接訪問從父類中繼承的私有屬性及方法,但可以對公有及保護(hù)方法進(jìn)行

訪問。

2.方法的覆蓋與方法的重載有何不同?

答:覆蓋是運(yùn)用在子類中實(shí)現(xiàn)與父類相同的功能,但采用不同的算法或公式;在名字相同的

方法中,要做比父類更多的事情;在子類中需要取消從父類繼承的方法。

8.什么是抽象類?抽象類中是否一定要包括抽象方法?

答:抽象類是一個(gè)不完全的類,不能使用new方法進(jìn)行實(shí)例化。

抽象類可以包含抽象方法,也可以不包含抽象方法,但有抽象方法的必須是抽象類。

9.this和super分別有哪些特殊含義?都有哪些種用法?

答:this表示當(dāng)前類;super表示調(diào)用父類。

在定義類的時(shí)候用到,this是當(dāng)前對象的引用,super是當(dāng)前對象的父類對象的引用,一般

的都是把super用在構(gòu)造函數(shù)中。

10.完成下面父類及子類的聲明:

(1)聲明Student類屬性包括學(xué)號、姓名、英語成績、數(shù)學(xué)成績、計(jì)算機(jī)成績和總成績。方

法包括構(gòu)造方法、get方法、set方法、toString方法、equals方法、compare方法(比較兩

個(gè)學(xué)生的總成績,結(jié)果分為大于、小于、等于),sum方法(計(jì)算總成績)和testscore方

法(計(jì)算評測成績)。

注:評測成績可以取三門課成績的平均分,另外任何一門課的成績的改變都需要對總成績進(jìn)

行重新計(jì)算,因此,在每一個(gè)set方法中應(yīng)調(diào)用sum方法計(jì)算總成績。

publicclassStudent{

Stringid;

Stringname;

floatscoreOfenglish;

floatscoreOfmath;

floatscoreOfcomputer;

floatscoreOfsum;

〃構(gòu)造方法

publicStudent(){

)

publicStudent(Stringaid,Stringaname,floatascoreOfenglish,floatascoreOfmath,float

ascoreOfcomputer){

this.id=aid;

=aname;

this.scoreOfenglish=ascoreOfenglish;

this.scoreOfmath=ascoreOfmath;

this.scoreOfcomputer=ascoreOfcomputer;

//this.scoreOfsum=ascoreOfenglish+ascoreOfmath+ascoreOfcomputer;

this.scoreOfsum=sum();

)

//sum方法

publicfloatsum(){

return(this.scoreOfenglish+this.scoreOfmath+this.scoreOfcomputer);

)

//testScore測評成績/平均分

publicfloattestScore(){

return(this.scoreOfsum/3);

)

〃6個(gè)get方法

publicStringgetid(){

return(id);

)

publicStringgetname(){

return(name);

}

publicfloatgetscoreOfenglish(){

return(scoreOfenglish);

)

publicfloatgetscoreOfmath(){

return(scoreOfmath);

}

publicfloatgetscoreOfcomputer(){

return(scoreOfcomputer);

)

publicfloatgetscoreOfsum(){

return(scoreOfsum);

)

〃5個(gè)set方法

publicvoidsetid(Stringnewid){

this.id=newid;

)

publicvoidsetname(Stringnewname){

=newname;

)

publicvoidsetscoreOfenglish(floatnewscoreOfenglish){

this.scoreOfenglish=newscoreOfenglish;

this.scoreOfsum=sum();

)

publicvoidsetscoreOfmath(floatnewscoreOfmath){

this.scoreOfmath=newscoreOfmath;

this.scoreOfsum=sum();

)

publicvoidsetscoreOfcomputer(floatnewscoreOfcomputer){

this.scoreOfcomputer=newscoreOfcomputer;

this.scoreOfsum=sum();

)

//toString方法

publicStringtoString(){

return(〃學(xué)號:"+this.id+"\n姓名:"+name+"\n英語:"+this.scoreOfenglish+"\n數(shù)學(xué):

"+this.scoreOfmath+"\n計(jì)算機(jī):"+this.scoreOfcomputer+"\n總分:"+this.scoreOfsum);

}

//compare方法/比較2學(xué)生總分

publicvoidcompare(Studentx){

if(this.getscoreOfsum0>x.getscoreOfsum())System,out.println(this.getname()+〃總

分大于”+x.getname());

if(this.getscoreOfsum()<x.getscoreOfsum())System,out.println(this.getname()+〃總

分小于"+x.getname。);

elseSystem,out.printin(this,getname()+〃總分等于”+x.getname());

)

//equals方法/比較2學(xué)生學(xué)號是否相等(還沒完善)

/*

*publicbooleanequals(Objectx){

if(this.getClass()!=x.getClass())returnfalse;

Studentb=(Student)x;

if(this.id==b,getid())returntrue;

)

*/

}

(2)聲明StudentXW(學(xué)習(xí)委員)類為Student類的子類。

在StudentXW類中增加責(zé)任屬性,并重寫testScore方法(評測成績二三門課平均分+3)

publicclassStudentXWextendsStudent{

Stringresponsibility;

〃構(gòu)造方法

publicStudentXW(){

super();

//responsibility="

)

publicStudentXW(Stringaid,Stringaname,floatascoreOfenglish,floatascoreOfmath,float

ascoreOfcomputer,Stringaresponsibility){

super(aid,aname,ascoreOfenglish,ascoreOfmath,ascoreOfcomputer);

responsibility=aresponsibility;

}

//testScore測評成績/平均分

publicfloattestScore(){

return(this.scoreOfsum/3+3);

)

//toString方法

publicStringtoString(){

return(〃學(xué)號:"+this.id+"\n姓名:"+name+"\n英語:"+this.scoreOfenglish+"\n數(shù)學(xué):

,,+this.scoreOfmath+"\n計(jì)算機(jī):,l+this.scoreOfcomputer+"\n總分:"+this.scoreOfsum+"\n職

位:"+this.responsibility);

}

}

(3)聲明StudentBZ類為Student類的子類

在StudentBZ類中增加責(zé)任屬性,并重寫testScore方法(評測成績=三門課平均分+5)

publicclassStudentBZextendsStudent{

Stringresponsibility;

//構(gòu)造方法

publicStudentBZ(){

super();

//responsibility="";

}

publicStudentBZ(Stringaid,Stringaname,floatascoreOfenglish,floatascoreOfmath,float

ascoreOfcomputer,Stringaresponsibility){

super(aid,aname,ascoreOfenglish,ascoreOfmath,ascoreOfcomputer);

responsibility=aresponsibility;

)

//testScore測評成績/平均分

publicfloattestScore(){

return(this.scoreOfsum/3+5);

)

//toString方法

publicStringtoString(){

return(“學(xué)號:"+this,id+"\n姓名:"+name+"\n英語:"+this.scoreOfenglish+"\n數(shù)學(xué):

"+this.scoreOfmath+"\n計(jì)算機(jī):"+this.scoreOfcomputer+"\n總分:"+this.scoreOfsum+"\n職

位:"+this.responsibility);

)

}

4)聲明測試類,生成若干個(gè)Student類、StudentXW類及StudentBZ類對象,并分別計(jì)算它

們的評測成績(建議采用:寫一個(gè)測試函數(shù),該函數(shù)以父類student數(shù)組作為參數(shù))。

importjava.text.*;

publicclasstest4_10{

publicstaticvoidmain(Stringargs[]){

Studentsu二newStudent(〃001〃,〃蘇軾”,56.00f,87.00f,95.00f);

Studentdu=newStudent(〃002〃,〃杜甫'',86.00f,75.00f,80.00f);

Studentbai=newStudent(〃003〃,〃白居易”,42.OOf,77.OOf,65.OOf);

Studentliu二newStudent(〃004〃,〃柳宗元”,67.00f,67.OOf,67.OOf);

StudentXWou=newStudentXW(〃005〃,〃歐陽修”,89.00f,98.00f,90.00fJ數(shù)學(xué)代表”);

StudentXWwang=newStudentXW("006","王安石",98.00力87.001:,36.00力"英語代表");

StudentBZli=newStudentBZ("007","李白",89.00f,87.00f,87.00f,“班長)

System.out.print(li);

System.out.println(〃\n評測成績:"+newDecimalFormat("0.00").format(li,testScore()));

System.out.println();

System.out.print(wang);

System,out.println(z,\n評測成績:"+new

DecimalFormat("0.00").format(wang.testScore()));

System.out.println();

System.out.print(ou);

System,out.printin(,z\n評測成績:"+new

DecimalFormat("0.00").format(ou.testScore()));

System.out.println();

System.out.print(su);

System,out.println(z,\n評測成績:"+new

DecimalFormatCO.OO'^.formatJsu.testScoreO));

System.out.println();

System.out.print(du);

System,out.printin(〃\n評測成績:"+new

DecimalFormat("0.00").format(du.testScore()));

System.out.println();

System.out.print(bai);

System,out.printin(〃\n評測成績:"+new

DecimalFormatCO.OO'^.formatfbai.testScoreO));

System.out.println();

System.out.print(liu);

System,out.printin(〃\n評測成績:"+new

DecimalFormat("0.00").format(liu.testScore()));

)

}

運(yùn)行test4_10的結(jié)果如下:

圖.Problems@Javadoc/Dec

〈terminated〉test410[JavaAppli

。

。

)7白

英@:S

數(shù)"

緊8O

計(jì)7

其0

機(jī):.c

去2O

877.-

班5長3

fe3

評:

:92

測67

學(xué)號:006

姓名:王安石

英語:98.0

數(shù)學(xué):87.0

計(jì)算機(jī):36.0

總分:221.0

職位:英語代表

評測成績:76.67

學(xué)號:005

姓名:歐陽修

英語:89.0

數(shù)學(xué):98.0

計(jì)算機(jī):90.0

總分:277.0

職位:數(shù)學(xué)代表

評測成績:95.33

學(xué)號

.“1

.

姓名

.蘇

.

英語.

.56.o

數(shù)學(xué).

87

機(jī):

計(jì)算.0

:.(

總分230

成958

評別.9

:7S2

學(xué)號

:002

姓名

:杜甫

英語

:86.0

數(shù)學(xué)

:75.0

計(jì)算

機(jī):

總分80.0

評別.0

80.33

學(xué)3

.

00白

.髓

.

.。

.42?

學(xué)

數(shù).O

機(jī)7.7?

?

計(jì)6

總5.

ls績4

成.O

?61

?

學(xué)號:004

姓名:柳宗元

英語:67.0

數(shù)學(xué):67.0

計(jì)算機(jī):67.0

總分:201.0

評測成績:67.00

(很好玩吧,李白,我讓你掛科。)

11.包有什么作用?如何創(chuàng)建包和引用包中的類?

答:包是一種松散的類的組合,一般不要求處于同一包中的類型有明確的相互關(guān)系,但由于

同一包中的類在默認(rèn)情況下可以相互訪問,所以為了方便編程和管理,通常把需要在一起工

作的類放在一個(gè)包里。利用包來管理類,可實(shí)現(xiàn)類的共享與復(fù)用。

在操作系統(tǒng)中,目錄用來組織文件,設(shè)置權(quán)限。只要在要用到包中類的時(shí)候,在該引用類的

第一行加上:package(包的全路徑)即可。

Java語言程序設(shè)計(jì)

第五章課后習(xí)題答案

1.什么是接口?接口起什么作用?接口與抽象類有何區(qū)別?

答:Java中的接口是一系列方法的聲明,是一些方法特征的集合,一個(gè)接口只有方法的特征

沒有方法的實(shí)現(xiàn),因此這些方法可以在不同的地方被不同的類實(shí)現(xiàn),而這些實(shí)現(xiàn)可以具有不

同的行為(功能)。

作用:接口是面向?qū)ο蟮囊粋€(gè)重要機(jī)制,使用接口可以實(shí)現(xiàn)多態(tài)繼承;接口中的所有方法都

是抽象的,這些抽象方法由實(shí)現(xiàn)這一接口的不同類型來具體;接口還可以用來實(shí)現(xiàn)不同類之

間的常量共享。

與抽象類不同的是:接口允許在看起來不相干的類之間定義共同行為。

3.在什么情況下,可以對父類對象的引用進(jìn)行強(qiáng)制類型轉(zhuǎn)換,使其轉(zhuǎn)化成子類對象的引用?

答:一個(gè)對象被塑型為父類或接口后,可以再一次被塑型回到它原來所屬的類,即轉(zhuǎn)化成原

類對象的引用。

4.聲明一個(gè)接口,此接口至少具有一個(gè)方法;在一個(gè)方法中聲明內(nèi)部類實(shí)現(xiàn)此接口,并返回

此接口的引用。

//A類接口

publicinterfaceA{

voidfuck();

}

//B類

publicclassB{

publicAfuck(){

returnnewA(){

publicvoidfuck(){

System.out.printin("B類聲明實(shí)現(xiàn)A接口類并返回接口的應(yīng)用

----Fuckyou!");

)

};/*注意這里有分號“;”*/

}

}

//test5_4

publicclasstest5_4{

publicstaticvoidmain(String[]args){

newB().fuck().fuck();

)

運(yùn)行結(jié)果:

怯,:Problems|@Javadoc|臉Declaration1里Console區(qū)

〈terminated〉test5_4[JavaApplication]D:\ProgramFiles\l

3類聲明實(shí)現(xiàn)A接口類并返回接口的應(yīng)用一Fuckyou!

7.什么是多態(tài)?如何實(shí)現(xiàn)多態(tài)?

答:多態(tài)性是指不同類型的對象可以響應(yīng)相同的消息。利用向上塑性技術(shù),一個(gè)父類的應(yīng)引

用變量可以指向不同的子類對象;而利用動(dòng)態(tài)綁定技術(shù),可以再運(yùn)行時(shí)根據(jù)父類引用變量所

指對象的世紀(jì)類型執(zhí)行相應(yīng)的子類方法,從而實(shí)現(xiàn)多態(tài)性。

Java語言程序設(shè)計(jì)

第六章課后習(xí)題答案

3.創(chuàng)建一存儲(chǔ)若干隨機(jī)整數(shù)的文本文件,文件名、整數(shù)的個(gè)數(shù)及范圍均由鍵盤輸入。

//memory存儲(chǔ)類

importjava.io.

importjava.util.Random;

publicclassmemory{

privateStringname;

privateintcount;

privateintMax;

privateintMin;

publicmemory(Stringn,intcfintmin,intmax){

=n;

this.count=c;

this.Min=min;

this.Max=max;

}

publicvoidstartmemory(){

try{

FileWriterout=newFileWriter(name);

intlimit=Max-Min;

Randomrandom=newRandom();

for(inti=l;i<=count;i++){

intnumber=Min+random.nextlnt(limit);

System.out.print(number);

System.out.print(nn);

out.write(number+n");

)

out.close();

}catch(lOExceptioniox){

System,out.printIn("方法startmemory()有I句題”);

)一

)

)

//test6_3

importjava.io.

importjava.util.Scanner;

publicclasstest6_3{

publicstaticvoidmain(String[]args)throwslOException{

//BufferedReader

StringfileName;

intcount,min,max;

Scannerin=newSeann一r(System.W_n);

System.out.printin("輸入要存儲(chǔ)的文件名");

fileName=in.next();

System,out.printin("輸入隨機(jī)數(shù)個(gè)數(shù)”);

count=in.nextlnt();

System.out.printin("輸入隨機(jī)數(shù)最小值”);

min=in.nextInt();

System.out.printin("輸入隨機(jī)數(shù)最大值”);

max=in.nextInt();

memoryM=newmemory(fileName,count,minAmax);

M.startmemory();

運(yùn)行結(jié)果:

廣.Problems@Javadoc限,Declaration曰Console漢

〈terminated〉test63[JavaApplication]D:\ProgramFiles\MyEc

輸入要存儲(chǔ)的文件名

naruto.txt

輸入隨機(jī)數(shù)個(gè)數(shù)

10

輸入隨機(jī)數(shù)最小值

輸入隨機(jī)數(shù)最大值

112

|311105942894

naruto文件存儲(chǔ)二進(jìn)制數(shù):

T

,naruto.txt-記事本

文件(F)編輯(E)格式(O)查看(V)幫助(H)

11105942894

6.用記事本程序創(chuàng)建一篇包含幾十個(gè)英語單詞的小文章,要求從屏幕輸出每一個(gè)單詞。

//test6_6

importjava.io.

publicclasstest6_6{

publicstaticvoidmain(String[]args)throwslOException{

Fil一Readerfr=newFil一Read一r("naruto.txt”);

ints;

while((s=fr?read())!=-l){

if(s>='a'&&s<='z'||s>='A'&&s<='Z')

System.out.print((char)s);

else

System.out.print(H\n");

}

fr.close();

)

運(yùn)行結(jié)果:

|氏Problems|@Javadoc

〈terminated〉test66[Java

Water

Pollution

Water

is

very

important

to

us

Factories

and

plants

need

water

for

Iindustrial

8.創(chuàng)建一個(gè)學(xué)生類(包括姓名、年齡、班級、密碼),創(chuàng)建若干該類的對象并保存在文件中

(密碼不保存),從文件讀取對象后顯示在屏幕上。

//Student類

importjava.io.Serializable;

classStud一ntimplementsSerializable{

Stringname;

intage;

intgrade;

transientStringsecret;

publicStudent(Stringname,intage,intgrade,Stringsecret){

this.name=nam一;

this.age=age;

this.grade=grade;

this.secret=secret;

//test6_8

importjava.io.

publicclasstset6_8{

publicstaticvoidmain(String[]args)throws

lOException,ClassNotFoundException{

Studentstudent[]={

newStudent("蒼井空”,19,101,Hchangjingkong**),

newStud一nt(“吉沃明”,19,103,"jizeming"),

newStudent("武藤蘭",20,104,"wut一nglan"),

newStudent(“我愛女優(yōu)”,21,105f"woainvyou")};

//創(chuàng)建輸出

ObjectOutputStreamoos=newObjectOutputStream(

newFileOutputStream("naruto.dat"));

for(inti=0;i<student.length;i++)

oos.writeObject(student[i]);

oos.close();

//將對象數(shù)組student全部清空,后邊你會(huì)明白的。

for(inti=0;i<student.length;i++)

student[i]=null;

//創(chuàng)建輸入

ObjectInputStreamois=newObjectInputstream(

newFileinputStream("naruto.datn));

for(inti=0;i〈student.length;i++)

stud一nt[i]=(Stud一nt)ois.readObj一ct();//重新從文件中讀入,之前的清

空就因?yàn)榇俗x入。注意,讀入要強(qiáng)制轉(zhuǎn)化為學(xué)生類

ois.clos一();

//顯示信息

for(inti=0;i<student.length;i++){

if(i==student.length-1){

System.out.printIn(“我的名字:"+stud一nt[i].name);

System,out.printIn("我的年齡:n4-student[i].age);

System,out.printIn("我的班級:H+student[i].grade);

System,out.printIn("我的密碼:"+student[i].secret);

System.out.printIn();

}

else{

System,out.printIn("?n+(i+1)+”個(gè)女學(xué)生名字:

n+student[i].name);

System,out.printIn("第"+(i+1)+”個(gè)女學(xué)生年齡:

n+student[i].age);

System.out.printIn((i+1)+”個(gè)女學(xué)生班級:

n+student[i].grade);

System.out.println(“第”+(i+1)+”個(gè)女學(xué)生密碼:

n+student[i].secret);

System.out.printIn();

)

[t?Problems@Javadoc除Decla

<terminated>tset6_8[JavaApplicat

第1個(gè)女學(xué)生名字:元井空

第1個(gè)女學(xué)生年齡:19

第1個(gè)女學(xué)生班級:1。1

第1個(gè)女學(xué)生密碼:null

第2個(gè)女學(xué)生名字:吉沃明

第2個(gè)女學(xué)生年齡:19

第2個(gè)女學(xué)生班級:1。3

第2個(gè)女學(xué)生密碼:null

第3個(gè)女學(xué)生名字:武藤蘭

第3個(gè)女學(xué)生年齡:20

第3個(gè)女學(xué)生班級:104

第3個(gè)女學(xué)生密碼:null

我的名字:我愛女優(yōu)

我的年齡:21

我的班級:105

我的密碼:null

Java語言程序設(shè)計(jì)

第七章課后習(xí)題答案

1.數(shù)組的聲明與數(shù)組元素的創(chuàng)建有什么關(guān)系?

答:聲明數(shù)組僅僅是代表試圖創(chuàng)建數(shù)組,不分配任何存儲(chǔ)空間,聲明是為創(chuàng)建做“鋪墊”。

2.Vector類的對象與數(shù)組有什么關(guān)系?什么時(shí)候適合使用數(shù)組,什么時(shí)候適合使用Vector?

答:vector是一個(gè)能夠存放任意對象類型的動(dòng)態(tài)數(shù)組,容量能自動(dòng)擴(kuò)充,而數(shù)組存儲(chǔ)固定且

類型相同的對象;對于存儲(chǔ)固定類型相同的對象使用數(shù)組,對于存儲(chǔ)不同類型或者動(dòng)態(tài)調(diào)整

數(shù)組大小的情況使用Vector。

3.與順序查找相比,二分查找有什么優(yōu)勢?使用二分查找的條件?

答:對于大數(shù)據(jù)量中進(jìn)行查找時(shí)二分查找比順序查找效率高得多;條件是已排序的數(shù)組。

4.試舉出三種常見的排序算法,并簡單說明其排序思路。

答:①選擇排序:基本思想是站在未排序列中選一個(gè)最小元素,作為已排序子序列,然后再

重復(fù)地從未排序子序列中選取一個(gè)最小元素,把它加到已經(jīng)排序的序列中,作為已排序子序

列的最后一個(gè)元素,直到把未排序列中的元素處理完為止。

②插入排序:是將待排序的數(shù)據(jù)按一定的規(guī)則逐一插入到已排序序列中的合適位置處,直到

將全部數(shù)據(jù)都插入為止。

③二分查找:將表中間位置記錄的關(guān)鍵字與查找關(guān)鍵字比較,如果兩者相等,則查找成功;

否則利用中間位置記錄將表分成前、后兩個(gè)子表,如果中間位置記錄的關(guān)鍵字大于查找關(guān)鍵

字,則進(jìn)一步查找前一子表,否則進(jìn)一步查找后一子表。重復(fù)以上過程,直到找到滿足條件

的記錄,使查找成功,或直到子表不存在為止,此時(shí)查找不成功。

5.聲明一個(gè)類People,成員變量有姓名、出生日期、性別、身高、體重等;生成10個(gè)People

類對象,并放在一個(gè)以為數(shù)組中,編寫方法按身高進(jìn)行排序。

//People類

publicclassPeople{

privateStringname;

privateStringbirthdaydate;

privateStringsex;

privatedoubleheight;

privatedoubleweight;

publicP一opl一(){//默認(rèn)構(gòu)造函數(shù)

publicPeople(Peoplep){

=;

this.birthdaydate=p.birthdaydate;

this.sex=p.sex;

this,height=p.height;

this.weight=p.weight;

}

publicPeople(Stringname,Stringbirthdaydate,Stringsex,double

height,doubleweight){

this.name=name;

this.birthdaydate=birthdaydate;

this.sex=sex;

this.height=height;

this.weight=weight;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

=name;

}

publicStringgetBirthdaydate(){

returnbirthdaydate;

)

publicvoidsetBirthdaydate(Stringbirthdaydate){

this.birthdaydate=birthdaydate;

)

publicStringgetSex(){

returnsex;

publicvoidsetSex(Stringsex){

this.sex=sex;

}

publicdoublegetHeight(){

returnheight;

)

publicvoidsetHeight(doubleheight){

this.height=height;

}

publicdoublegetWeight(){

returnweight;

)

publicvoidsetweight(doubleweight){

this.weight=weight;

}

publicStringtoString(){

return"姓名:"+name+"\n出生年月:"+birthdaydat一+"\n性別|:n+sex+n\n

身高:"+h一ight+"\n體重:"+weight;

)

//test7_5類

publicclasstest7_5{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

People[]people={

newPeople("林楚金","1989年8月13日"男",182,63.5),

newPeople("諸葛亮","181年7月23日","男",184,76.6),

newPeople("邁克杰克遜","1958年8月29日","男",180,60),

newPeople("喬丹","1963年2月17日","男",198,98.1),

newPeople("拿破侖","1769年8月15日","男",159.5,63),

newPeople("蒼井空","1983年11月11日","女",155,45),};

Peopletemp=newPeople();

for(inti=0;i1<people.length-1;i++)

for(intj=i+l;j<people.length;j++){

if(people[i].g一tHeight()<people[j].getHeight()){

temp=people[j];

people[j]=peopl一[i];

people[i]=temp;

)

}

System.out.printin("按身高從小到大排序后的結(jié)果如下:");

for(inti=0;i<people.length;i++)

System.out.printin(peopl一[i]+"\n");

運(yùn)行結(jié)果:

息Problems@JavadocI凰Declaratic

〈terminated〉test75[JavaApplication]

按身高從小到大排序后的結(jié)果如下:

姓名:喬丹

出生年月:1963年2月170

性別:男

身高:198.0

體重:98.1

姓名:諸葛亮

出生年月:282年7月23日

性別:男

身高:184.0

體重:76.6

姓名:林楚金

出生年月:1989年S月13日

性別:男

身高:182.0

體重:63.5

姓名:邁克杰克遜

出生年月:1958年3月29日

性別:男

身高:180.0

體重:60.0

姓名:拿破侖

出生年月:1769年S月15日

性別:男

身高:159.5

體重:63.0

姓名:蒼井空

出生年月:1983年11月11B

性別:女

身高:155.0

體重:45.0

6.聲明一個(gè)類,此類使用私有的ArrayList來存儲(chǔ)對象。使用一個(gè)Class類的引用得到第一個(gè)

對象的

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論