《Java基礎入門》-課后習題答案_第1頁
《Java基礎入門》-課后習題答案_第2頁
《Java基礎入門》-課后習題答案_第3頁
《Java基礎入門》-課后習題答案_第4頁
《Java基礎入門》-課后習題答案_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

《Java基礎入門》課后習題答案

第1章Java開發(fā)入門

一、填空題

1>JavaEE^JavaSE>JavaME

2、JRE

3、javac

4、bin

5、path>classpath

二、選擇題

1、ABCD2、C3、D4、B5、B

三、簡答題

1、面向對象、跨平臺性、健壯性、安全性、可移植性、多線程性、動態(tài)性等。

2、JRE(JavaRuntimeEnvironment,Java運行時環(huán)境),它相當于操作系統(tǒng)部分,提供了Java程序運

行時所需要的基本條件和許多Java基礎類,例如,IO類、GUI控件類、網(wǎng)絡類等。JRE是提供給

普通用戶使用的,如果你只想運行別人開發(fā)好的Java程序,那么,你的計算機上必須且只需安裝

JREo

JDK(JavaDevelopmentKit,Java開發(fā)工具包),它包含編譯工具、解釋工具、文檔制作工具、打

包工具多種與開發(fā)相關的工具,是提供給Java開發(fā)人員使用的。初學者學習和使用Java語言時,

首先必須下載和安裝JDKOJDK中已經(jīng)包含了JRE部分,初學者安裝JDK后不必再去下載和安裝

JRE了。

四、編程題

publicclassHelloWorld{

publicstaticvoidmain(String[]args){

System.out.printin("這是第一個Java程序!");

第2章Java編程基礎

一、填空題

1>class

2、true和false

3、單行注釋、多行注釋、文檔注釋

4、基本數(shù)據(jù)類型、引用數(shù)據(jù)類型

5、1、2、4、8

6、&&&III

7、0

8、5

9、34

10、56

二、判斷題

1、錯2、對3、錯4、對5、錯

三、選擇題

1、AD2、AD3、C4、ABCD5、C6、A7、AC8、A9、B10、A

四、程序分析題

1、編譯不通過。int值4和b相加時,由于變量b的類型為byte,取值范圍沒有int類型大,存不下int

類型的值,因此編譯不通過。

2、編譯不通過。這是因為y是在最里層的代碼塊中定義的一個變量,只有在那個代碼塊中才可使用,

在使用y=x;語句時已經(jīng)超過了y變量的作用域,所以編譯無法通過。

3、打印結果為:3o

4、打印結果為:

9

8

7

五、簡答題

1、Java語言的八種基本數(shù)據(jù)類型有:byte字節(jié)型,占一個字節(jié)。short短整型,占兩個字節(jié)。int整型,

占4個字節(jié)。long長整型,占8個字節(jié)。float單精度浮點型,占4個字節(jié)。double雙精度浮點型,

占8個字節(jié)。char字符型,占兩個字節(jié)?boolean型,表示邏輯值,有true和false兩個值,分別占

一個字節(jié)。

2、如果使用“&”在表達式之間進行連接,那么無論任何情況,“&”兩邊的表達式都會參與計算。

如果使用“&&”進行連接,當“&&”左邊的表達式為false,則不會執(zhí)行其右邊的表達式。例如

定義intx=2,y=0;booleanb=x<y&x/2>0表達是會發(fā)生被0除異常,因為x/y的表達式執(zhí)行

了。而booleanb=x<y&x/2>0是不會出現(xiàn)這種異常的,因為x<y為false,表達式x/y不會

執(zhí)行。

3、方法重載指的是在一個類中可以聲明多個同名的方法,而方法中參數(shù)的個數(shù)或者數(shù)據(jù)類型不一致。

調用這些同名的方法時,JVM會根據(jù)實際參數(shù)的不同綁定到不同的方法。

六、編程題

1、參考答案

publicclassTestOl{

publicstaticvoidmain(String[]args){

intsum=0;

for(inti=1;i<100;i++){

if(i%2!=0)

sum+=i;

)

System.out.printin(sum);

)

)

2、參考答案

publicclassTest02{

publicstaticvoidmain(Stringargs[]){

inty=function(0);

System.out.printin(y);

}

publicstaticintfunction(intx){

inty;

if(x>0){

y=x+3;

}elseif(x==0){

y=0;

}else{

y=x*x-1;

)

returny;

)

)

3、參考答案

publicclassTest03{

publicstaticvoidmain(String[]args){

int[]arr={25,24,12,76,101,96,28);

for(inti=0;i<arr.length-1;i++){

//定義內層循環(huán)

for(intj=0;j<arr.length-i-1;j++){

if(arr[j]>arr[j+1]){//比較相鄰元素

//下面的三行代碼用于交換兩個元素

inttemp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

)

)

)

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

System.out.print(arr[i]+"");//打印元素和空格

)

)

)

第3章面向對象(上)

一、填空題

1、封裝、繼承、多態(tài)

2、new

3、成員變量、局部變量

4、類、類

5、this

6、finalizeQ

7、靜態(tài)變量

8、內部類

9、javadoc

10>private

二、判斷題

1、對2、對3、錯4、對5、錯

三、選擇題

1、B2、D3、B4、ABC5、ABCD6、ACD7、ABCD8、ABCD9、D10、D

四、程序分析題

1、程序不能編譯通過,因為在類A中的成員變量secret用private修飾,所以在類Testi中無法訪問。

2、程序不能編譯通過,因為在靜態(tài)方法method。中不能訪問非靜態(tài)成員變量X。

3、程序能夠編譯通過,運行的結果為“inner”。

五、簡答題

1、構造方法是類的一個特殊成員,它會在類實例化對象時被自動調用。而普通方法只有在使用的時

候才會被調用。在定義構造方法時要求方法名與類名相同、在方法名的前面沒有返回值類型的聲

明、在方法中不能使用return語句返回一個值

2、單例模式可以保證在整個程序運行期間針對該類只存在一個實例對象。

六、編程題

1、參考答案

classStudent{

privateStringname;

privatedoublegrade;

publicStudent(){

)

publicStudent(Stringname,doublegrade){

=name;

this.grade=grade;

}

publicStringgetName(){

returnname;

)

publicvoidsetName(Stringname){

=name;

}

publicdoublegetGrade(){

returngrade;

)

publicvoidsetGrade(doublegrade){

this.grade=grade;

}

)

publicclassTestOl{

publicstaticvoidmain(String[]args){

Studentstul=newStudent();

stul.setName("zhangsan");

stul.setGrade(99);

Studentstu2=newStudent("lisi",100);

}

}

2、參考答案

classFather{

privateStringname="zhangjun'1;

classChild{

publicvoidintroFather(){

System.out.printin("MyFather'snameis"+name);

)

)

}

publicclassTest02{

publicstaticvoidmain(String[]args){

Father.Childchild=newFather().newChild();

roFather();

)

)

第4章面向對象(下)

一、填空題

1、繼承

2、方法,抽象類

3、import

4、子類、父類、基類

5、Exception

6>final

7、super

8>Object

9、try>catch

10、jar-cvf,java^jar

二、判斷題

1、錯2、對3、錯4、對5、對

三、選擇題

1、B2、C3、ABC4、ABCD5、C6、AC7、C8、D9、A10、B

四、程序分析題

1、程序編譯能通過,這是因為intx=2/0;System.out.println(x);這兩條語句使用了try塊,捕獲了程序

因為除以0而產(chǎn)生的異常情況,之后程序會繼續(xù)向下執(zhí)行,輸出''進入catch代碼塊”,“進入finally

代碼塊”。

2、程序編譯不通過,這是因為在程序中使用了final關鍵字修飾Animal類,使得Animal類不能被繼

承。shout。方法中同樣使用了final關鍵字,使得該方法不能被重寫。

3、程序編譯能通過,輸出結果為“動物叫!”和“汪汪……”,因為在程序中調用shout。方法時,首先

會通過super.shout。調用父類的方法說出“動物叫!”之后再輸出“汪汪……”

4、程序編譯不通過,因為接口中定義的方法不能有方法體,所以定義的eat()方法是錯誤的。接口中

的方法必須在子類中全部實現(xiàn),由于run()方法在子類中并沒有重新實現(xiàn),所以這也是錯誤的。

五、簡答題

1、在繼承關系中,子類的方法與父類的某一方法具有相同的方法名、返回類型和參數(shù)列表,則稱子

類的該方法重寫(覆蓋)父類的方法。

2、多態(tài)意味著一個對象有著多種形態(tài),可以在特定的情況下,表現(xiàn)不同的狀態(tài),從而對應著不同的

屬性和方法。簡單的說,多態(tài)就是使用父類類型的變量引用子類對象,根據(jù)被引用子類對象的特

性,程序會得到不同的運行效果。

3、在Java中,使用abstract關鍵字修飾的類稱之為抽象類。抽象類是不能被實例化的,通常需要寫一

個子類來繼承抽象類,同時實例化子類來獲得該類的對象。抽象類通常用于表示一種抽象的概念。

接口可以說是一種特殊的抽象類,接口中只能定義常量和抽象方法。由于接口的特殊性,在定義

時需要使用interface關鍵字。

六、編程題

1、參考答案

classStudent{

publicStringname;

publicintage;

publicStudent(Stringname,intage){

=name;

this.age=age;

)

publicvoidshow(){

System.out.printin("name:H+name+"age:H+age);

)

)

classUnderGraduateextendsStudent{

publicStringdegree;

publicUnderGraduate(Stringname,intage,Stringdegree){

super(name,age);

this.degree=degree;

)

publicvoidshow(){

System.out.printIn("name:"+name+"age:"+age+"degree:"+degree);

)

}

publicclassTestOl(

publicstaticvoidmain(String[]args){

Studentstudent=newStudent("zhangsan",16);

student.show();

UnderGraduateunderGraduate=newUnderGraduate("lisi",20,"bechalor");

underGraduate.show();

)

2、參考答案

interfaceShape{

doublearea(doublegivenValue);

}

classSquareimplementsShape{

publicdoublearea(doublesideLength){

returnsideLength*sideLength;

)

)

classCircleimplementsShape{

publicdoublearea(doubler){

returnMath.PI*r*r;

)

)

publicclassTest02{

publicstaticvoidmain(String[]args){

Shapesquare=newSquare();

Shapecircle=newCircle();

System.out.printin(square.area(2));

System.out.printin(circle.area(3));

)

)

3、參考答案

classNoThisSongExceptionextendsException{

publicNoThisSongException(){

super();

}

publicNoThisSongException(Stringmessage){

super(message);

}

)

classPlayer{

publicvoidplay(intindex)throwsNoThisSongException{

if(index>10){

thrownewNoThisSongException(”您播放的歌曲不存在“);

)

System.out.printin("正在播放歌曲");

)

}

publicclassTest03{

publicstaticvoidmain(String[]args){

Playerplayer=newPlayer();

try(

player.play(13);

}catch(NoThisSongExceptione){

System,out.printin("異常信息為:n+e.getMessage());

)

)

)

第5章多線程

一、填空題

1、線程、通信

2>Thread>Runnable

3、就緒

4,synchronized>對象、this

5、進程

6、新建狀態(tài)(New)、就緒狀態(tài)(Runnable)、運行狀態(tài)(Running)、阻塞狀態(tài)(Blocked)、死亡狀態(tài)(Terminated)

7、10、1

8、開啟一個新線程、run()方法

9、wait。、notify()-notifyAll()

10>selDaemon(true)、start()

二、判斷題

1、錯2、對3、對4、錯5、錯

三、選擇題

1、B2、AC3、ABC4、BC5、ABD6、ABC7、C8、D9、AB10、ABCD

四、程序分析題

1、程序不能編譯通過,因為RunHandler類沒有實現(xiàn)Runnable接口,因此RunHandler的實例對象不

能作為參數(shù)傳遞給Thread的構造方法。

2、程序不能編譯通過,因為Thread的子類A重寫的run()方法的訪問級別不能低于父類run()方法的。

訪問級別

3、程序不能編譯通過,因為同步方法中調用wait。方法的對象必須為同步鎖對象。

4、t.start();

五、簡答題

1、一?種是繼承java.lang包下的Thread類,覆寫Thread類的run()方法,在run()方法中實現(xiàn)運行在線

程上的代碼。

newThread(){

publicvoidrun(){}

}.start();

另一種就是實現(xiàn)java.lang.Runnable接口,同樣是在run()方法中實現(xiàn)運行在線程上的代碼。

newThread(newRunnable(){

publicvoidrun(){}

}).start()

2、調用sleep。方法,正在執(zhí)行的線程主動讓出CPU去執(zhí)行其他線程,在sleep。方法指定的時間過后,

CPU才會回到這個線程上繼續(xù)往下執(zhí)行,如果當前線程進入了同步鎖,sleep。方法并不會釋放鎖,

即使當前線程使用sleep。方法讓出了CPU,但其它被同步鎖擋住了的線程也無法得到執(zhí)行。wait()

在一個已經(jīng)進入了同步鎖的線程內進行調用,讓當前線程暫時讓出同步鎖,以便其它正在等待此

鎖的線程可以得到同步鎖并運行。當其它線程調用了notify。方法后,調用wait()方法的線程就會

解除wait狀態(tài),當再次獲得同步鎖后,程序可以繼續(xù)向下執(zhí)行。

六、編程題

1、參考答案

publicclassMyThreadextendsThread{

publicMyThread(Stringname){

super(name);

)

publicvoidrun(){

System.out.printIn(this.getName());

}

publicstaticvoidmain(String[]args){

newMyThread("Threadl").start();

newMyThread("Thread2").start();

)

)

2、參考答案

publicclassMyRunnableimplementsRunnable{

publicvoidrun(){

for(inti=0;i<50;i++){

System.out.printin("new");

)

}

publicstaticvoidmain(String[]args){

newThread(newMyRunnable()).start();

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

System.out.printin("main");

)

)

)

3、參考答案

publicclassTestOl{

publicstaticvoidmain(String[]args){

Teachert=newTeacher();

newThread(t,“陳老師“).start();

newThread(t,"高老師").start();

newThread(t,”李老師").start();

)

)

classTeacherimplementsRunnable{

privateintnotes=80;

publicvoidrun(){

while(true){

dispatchNotes();//調用售票方法

if(notes<=0){

break;

)

)

)

privatesynchronizedvoiddispatchNotes(){

if(notes>0){

try(

Thread.sleep(10);//經(jīng)過的線程休眠10亳秒

}catch(InterruptedExceptione){

e.printStackTrace();

)

System.out.printin(Thread.currentThread().getName()+"--發(fā)出的筆記”

+notes--);

)

)

)

4、參考答案

publicclassAccumulatorextendsThread{

privateintstratNum;

publicstaticintsum;

publicAccumulator(intstartNum){

this.stratNum=startNum;

)

publicstaticsynchronizedvoidadd(intnum){

sum+=num;

}

publicvoidrun(){

intsum=0;

for(inti=0;i<10;i++){

sum+=stratNum+i;

)

add(sum);

}

publicstaticvoidmain(String[]args)throwsException{

Thread[]threadList=newThread[10];

for(inti=0;i<10;i++){

threadList[i]=newAccumulator(10*i+1);

threadList[i].start();

)

for(inti=0;i<10;i++){

threadList[i].join();

)

System.out.printin("Sumis:"+sum);

}

)

第6章JavaAPI

一、填空題

1、String>StringBuffer

2、Date、Calendar>DateFormat

3、getRuntime()

4、sqrt()

5、DateFormat

6、五、e

7、Random>java.util

8、length()

9、靜態(tài)

10^edcba

二、判斷題

1、錯2、錯3、對4、錯5、對

三、選擇題

1、C2、C3、D4、C5、C6、B7、C8、A9>A10、B

四、程序分析題

1、程序編譯能通過,輸出結果如下

5

7.0

-8.0

-5

8.1

-6.1

2、程序編譯能通過,輸出結果如下

str.length():15

str.charAt(0):d

lastlndexOf(m):10

substring(2,4):fe

indexOf(g):5

五、簡答題

1、String類是不可變類,即字符串值一旦初始化后就不可能改變。StringBuffer是可變字符串類,類

似String的緩沖區(qū),可以修改字符串的值。

2、Date類用來表示某個特定的瞬間,能夠精確到毫秒。而在實際應用中,往往需要把?個日期中的

年、月、日等信息單獨返回進行顯示或處理,這個類中的大部分方法都已被標記過時。Calender

類基本取代了Date類,該類中定義了?系列用于完成日期和時間字段操作的方法。

Calendar的getTime()方法,getTime()返回一個表示Calendar時間值的Date對象,同時Calendar有

一個setTime(Datedate)方法,setTime()方法接收?個Date對象,將Date對象表示的時間值設置給

Calendar對象,通過這兩個方法就可以完成Date和Calendar對象之間的轉換。

六、編程題

1、參考答案

publicclassTestOl{

publicstaticvoidmain(String[]args){

Stringstr="HelloWorld";

//字符串轉成char數(shù)組

char[]ch=str.toCharArray();

StringBufferbuffer=newStringBuffer();

for(inti=str.length()-1;i>=0;i--){

if(ch[i]>='A,&&ch[i]<=?Z'){

buffer.append(String.valueOf(ch[i]).toLowerCase());

}elseif(ch[i]>=1a,&&ch[i]<=1z'){

buffer.append(String.valueOf(ch[i]).toUpperCase());

)

)

System.out.printin(buffer.toString());

)

)

2、參考答案

importjava.text.DateFormat;

importjava.util.Calendar;

importjava.util.Date;

publicclassTest02{

publicstaticvoidmain(String[]args){

Calendarcalendar=Calendar.getInstance();

calendar.add(Calendar.DATE,100);

Datedate=calendar.getTime();

DateFormatformat=DateFormat.getDatelnstance(DateFormat.FULL);

Stringstring=format.format(date);

System.out.printIn(string);

)

)

3、參考答案

importjava.util.Random;

publicclassTest03{

publicstaticvoidmain(String[]args){

Randomrand=newRandom();

int[]num=newint[5];

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

num[i]=20+rand.nextlnt(31);

System.out.printin(num[i]);

)

)

}

第7章集合類

一、填空題

1、集合

2、Comparator

3、有序、可重復,無序、不可重復

4^hashNext()Nnext()

5、Collection>Map

6、鍵、值

7^Listiterator

8、ArrayList^LinkedList,HashSet>TreeSet,HashMap^TreeMap

9、put。、get()

10、CollectionsxArrays

二、判斷題

1、錯2、對3、對4、錯5、對

三、選擇題

1、BC2、A3、D4、ABD5、C6、AB7、D8、AB9、ABC10、B

四、程序分析題

1、程序可以編譯通過,輸出結果是“a、b、c",因為TreeSet集合不允許存放重復元素,第2次增加

的元素c會覆蓋之前存入的元素c,所以輸出結果是“a、b、c”,而不是“a、b、c、c”。

2、程序不可以編譯通過,這是由于向ArrayList集合中存入元素時,集合并不能記住元素的類型,因

此在取出元素時,只能使用Object類型,而不能使用String類型。

3、程序可以編譯通過,但是什么也沒有打印。使用Listiterator進行從后向前的遍歷集合,可以使用

以下兩種方法,一是使用listlterator(intindex)方法將索引index的值設置為集合元素的數(shù)目,也就

是Listiteratorit=list.listlterator(3);,二是將程序先從前向后遍歷,然后再從后向前遍歷。

4、程序編譯不通過,由于Map集合在遍歷的過程中不能使用集合對象本身刪除元素,這會導致并發(fā)

修改異常,若想刪除集合中的元素,可以使用Iterator的remove。方法。

五、簡答題

1、為了使程序能方便的存儲和操作數(shù)目不固定的?組數(shù)據(jù),JDK提供了?套類庫,這些類都位

于java.util包中,統(tǒng)稱為集合。集合框架中包含3個接口,分別是List、Set>Map。

2、List的特點是元素有序、元素可重復。List接口的主要實現(xiàn)類有ArrayList和LinkedList。Set的特

點是元素無序、元素不可重復。Set接口的主要實現(xiàn)類有HashSet和TreeSet。Map的特點是存儲的

元素是鍵(Key)、值(Value)映射關系,元素都是成對出現(xiàn)的。Map接口的主要實現(xiàn)類有HashMap和

TreeMap?

3、Collection是個單例集合接口。它提供了對集合對象進行基本操作的通用方法。Collections是一

個工具類。它包含各種有關集合操作的方法。

六、編程題

1、參考答案

importjava.util.*;

publicclassTestOl(

publicstaticvo

溫馨提示

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

評論

0/150

提交評論