Java程序设计精编教程第版习题解答.docx

上传人:b****5 文档编号:12148772 上传时间:2023-04-17 格式:DOCX 页数:30 大小:22.54KB
下载 相关 举报
Java程序设计精编教程第版习题解答.docx_第1页
第1页 / 共30页
Java程序设计精编教程第版习题解答.docx_第2页
第2页 / 共30页
Java程序设计精编教程第版习题解答.docx_第3页
第3页 / 共30页
Java程序设计精编教程第版习题解答.docx_第4页
第4页 / 共30页
Java程序设计精编教程第版习题解答.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

Java程序设计精编教程第版习题解答.docx

《Java程序设计精编教程第版习题解答.docx》由会员分享,可在线阅读,更多相关《Java程序设计精编教程第版习题解答.docx(30页珍藏版)》请在冰豆网上搜索。

Java程序设计精编教程第版习题解答.docx

Java程序设计精编教程第版习题解答

习题解答

习题一(第1章)

1.JamesGosling

2.需3个步骤:

1)用文本编辑器编写源文件。

2)使用javac编译源文件,得到字节码文件。

3)使用解释器运行程序。

3.setclasspath=D:

\jdk\jre\lib\;.;

4.B

5.Java源文件的扩展名是.java,Java字节码的扩展名是.class。

6.D。

习题二(第2章)

1.

Oxab187

lader

height

bottom

Oxab187

ractangle

width

height

2.

publicclassTeacher{

doubleadd(doublea,doubleb){

returna+b;

}

doublesub(doublea,doubleb){

returna-b;

}

}

publicclassStudent{

publicvoidspeak(){

}

}

publicclassMainClass{

publicstaticvoidmain(Stringargs[]){

Teacherzhang=newTeacher();

Studentjiang=newStudent();

();

}

}

3.如果源文件中有多个类,但没有public类,那么源文件的名字只要和某个类的名字相同,并且扩展名是.java就可以了,如果有一个类是public类,那么源文件的名字必须与这个类的名字完全相同,扩展名是.java。

4.行尾风格。

习题三(第3章)

1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。

标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。

true不是标识符。

2.关键字就是Java语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。

不是关键字。

classimplementsinterfaceenumextendsabstract。

3.boolean,char,byte,short,int,long,float,double。

4.属于操作题,解答略。

5.属于操作题,解答略。

6.publicclassE{

publicstaticvoidmain(Stringargs[]){

charcStart='A',cEnd='Z';

for(charc=cStart;c<=cEnd;c++)

}

}

7.不可以。

习题四(第4章)

1.110。

不规范。

2.新亲亲斤!

!

3.

publicclassJudgeAward{

voidgiveMess(intnumber){

if(number==9||number==131||number==12){

}

elseif(number==209||number==596||number==27){

}

elseif(number==875||number==316||number==59){

}

else{

}

}

}

4.

publicclassComputer{

publicstaticvoidmain(Stringargs[]){

Scannerreader=newScanner;

doubleamount=0;2c2c27。

10.100和。

习题六(第6章)

1.如果子类和父类在同一个包中,那么,子类自然地继承了其父类中不是private的成员变量作为自己的成员变量,并且也自然地继承了父类中不是private的方法作为自己的方法,继承的成员或方法的访问权限保持不变。

当子类和父类不在同一个包中时,父类中的private和友好访问权限的成员变量不会被子类继承,也就是说,子类只继承父类中的protected和public访问权限的成员变量作为子类的成员变量;同样,子类只继承父类中的protected和public访问权限的方法作为子类的方法。

如果所声明的成员的变量的名字和从父类继承来的成员变量的名字相同(声明的类型可以不同),在这种情况下,子类就会隐藏掉所继承的成员变量。

2.不可以。

3.abstract类。

4.A类是B类的父类,当用子类创建一个对象b,并把这个对象b的引用放到父类的对象a中时,称a是b的上转型对象。

5.AD。

6.

7.

12。

习题七(第7章)

1.不能。

2.不能。

3.可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中。

那么该接口变量就可以调用被类实现的接口中的方法。

4.

8。

5.18

15。

习题八(第8章)

1.有效。

2.可以。

3.不可以。

4.大家好,祝工作顺利!

习题九(第9章)

1.ABD。

2.Love:

Game。

3.15

abc我们。

4.13579。

5.9javaHello。

6.publicclassE{

publicstaticvoidmain(Stringargs[]){

Strings1,s2,t1="ABCDabcd";

s1=();

s2=();

Strings3=(s2);

}

}

7.publicclassE{

publicstaticvoidmain(Stringargs[]){

Strings="ABCDabcd";

charcStart=(0);

charcEnd=()-1);

}

}

8.

import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Scannerread=newScanner;

CalendarBeancb=newCalendarBean();

intyear=2000,month=1;

year=();

month=();

(year);

(month);

String[]a=();oCharArray();

for(charc:

str){

3c",c);

}

for(inti=0;i<;i++){import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Scannerread=newScanner;

intyear1,month1,day1,year2,month2,day2;

year1=();

month1=();

day1=();

year2=();

month2=();

day2=();

Calendarcalendar=();

(year1,month1-1,day1);

longtimeYear1=();

(year2,month2-1,day2);

longtimeYear2=();

long相隔天数=((timeYear1-timeYear2)/(1000*60*60*24));

year2+"年"+month2+"月"+day2+"日相隔"+相隔天数+"天");

}

}

10.import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

doublea=0,b=0,c=0;

a=12;

b=24;

c=;

c=;

c=

(1);

c=(8);

}

}

11.publicclassE{

publicstaticvoidmain(Stringargs[]){

Stringstr="ab123you你是谁";

Stringregex="\\D+";

str=(regex,"");

}

}

12.import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Stringcost="数学87分,物理76分,英语96分";

Scannerscanner=newScanner(cost);

doublesum=0;

intcount=0;

while()){

try{doublescore=();

count++;

sum=sum+score;

}

catch(InputMismatchExceptionexp){

Stringt=();

}

}

}

}

习题十(第10章)

1.使用FileInputStream。

2.FileInputStream按字节读取文件,FileReader按字符读取文件。

3.不可以。

4.使用对象流写入或读入对象时,要保证对象是序列化的。

5.使用对象流很容易得获取一个序列化对象的克隆,只需将该对象写入到对象输出流,那么用对象输入流读回的对象一定是原对象的一个克隆。

6.

import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Filef=newFile("");;

try{RandomAccessFilerandom=newRandomAccessFile(f,"rw");

(0);

longm=();

while(m>=0){

m=m-1;

(m);

intc=();

if(c<=255&&c>=0)

else{

m=m-1;

(m);

bytecc[]=newbyte[2];

(cc);

}

}

}

catch(Exceptionexp){}

}

}

7.

import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Filefile=newFile("");

FiletempFile=newFile("");

try{FileReaderinOne=newFileReader(file);

BufferedReaderinTwo=newBufferedReader(inOne);

FileWritertofile=newFileWriter(tempFile);

BufferedWriterout=newBufferedWriter(tofile);

Strings=null;

inti=0;

s=();

while(s!

=null){

i++;

(i+""+s);

();

s=();

}

();

();

();

();

();

}

catch(IOExceptione){}

}

}

8.属于上机操作题,解答略。

9.import.*;

import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Filefile=newFile("");

Scannersc=null;

doublesum=0;

intcount=0;

try{sc=newScanner(file);

while()){

try{doubleprice=();

count++;

sum=sum+price;

}

catch(InputMismatchExceptionexp){

Stringt=();

}

}

}

catch(Exceptionexp){

}

}

}

习题十一(第11章)

1.Frame容器的默认布局是BorderLayout布局。

2.不可以。

3.

import.*;

import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Computerfr=newComputer();

}

}

classComputerextendsJFrameimplementsDocumentListener{

JTextAreatext1,text2;

intcount=1;

doublesum=0,aver=0;

Computer(){

setLayout(newFlowLayout());

text1=newJTextArea(6,20);

text2=newJTextArea(6,20);

add(newJScrollPane(text1));

add(newJScrollPane(text2));

(false);

()).addDocumentListener(this);

setSize(300,320);

setVisible(true);

validate();

setDefaultCloseOperation;

}

publicvoidchangedUpdate(DocumentEvente){

Strings=();

sum=0;

aver=0;

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

try{sum=sum+(a[i]);

}

catch(Exceptionee){}

}

aver=sum/count;

(null);

("\n和:

"+sum);

("\n平均值:

"+aver);

}

publicvoidremoveUpdate(DocumentEvente){

changedUpdate(e);

}

publicvoidinsertUpdate(DocumentEvente){

changedUpdate(e);

}

}

4.

import.*;

import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

ComputerFramefr=newComputerFrame();

}

}

classComputerFrameextendsJFrameimplementsActionListener{

JTextFieldtext1,text2,text3;

JButtonbuttonAdd,buttonSub,buttonMul,buttonDiv;

JLabellabel;

publicComputerFrame(){

setLayout(newFlowLayout());

text1=newJTextField(10);

text2=newJTextField(10);

text3=newJTextField(10);

label=newJLabel("",;

;

add(text1);

add(label);

add(text2);

add(text3);

buttonAdd=newJButton("加");

buttonSub=newJButton("减");

buttonMul=newJButton("乘");

buttonDiv=newJButton("除");

add(buttonAdd);

add(buttonSub);

add(buttonMul);

add(buttonDiv);

(this);

(this);

(this);

(this);

setSize(300,320);

setVisible(true);

validate();

setDefaultCloseOperation;

}

publicvoidactionPerformed(ActionEvente){

doublen;

if()==buttonAdd){

doublen1,n2;

try{n1=());

n2=());

n=n1+n2;

(n));

("+");

}

catch(NumberFormatExceptionee)

{("请输入数字字符");

}

}

elseif()==buttonSub){

doublen1,n2;

try{n1=());

n2=());

n=n1-n2;

(n));

("-");

}

catch(NumberFormatExceptionee)

{("请输入数字字符");

}

}

elseif()==buttonMul)

{doublen1,n2;

try{n1=());

n2=());

n=n1*n2;

(n));

("*");

}

catch(NumberFormatExceptionee)

{("请输入数字字符");

}

}

elseif()==buttonDiv)

{doublen1,n2;

try{n1=());

n2=());

n=n1/n2;

(n));

("/");

}

catch(NumberFormatExceptionee)

{("请输入数字字符");

}

}

validate();

}

}

5.import.*;

import.*;

publicclassE{

publicstaticvoidmain(Stringargs[]){

Windowwin=newWindow();

("使用MVC结构");

(100,100,420,260);

}

}

classWindowextendsJFrameimplementsActionListener{

Laderlader;rim());

doublebottom=().trim());

doubleheight=().trim());

(above);

(bottom);

(height);

doublearea=();

("面积:

"+area+"\n");

}

catch(Exceptionex){

("\n"+ex+"\n");

}

}

}

classLader{

doubleabove,bottom,height;

publicdoublegetArea(){

doublearea=(above+bottom)*height/;

returnarea;

}

publicvoidsetAbove(doublea){

above=a;

}

publicvoidsetBottom(doubleb){

bottom=b;

}

publicvoidsetHeight(doublec){

height=c;

}

}

习题十二(第12章)

1.4种状态:

新建、运行、中断和死亡。

2.有4种原因的中断:

(1)JVM将CPU资源从当前线程切换给其他线程,使本线程让出CPU的使用权处于中断状态。

(2)线程使用CPU资源期间,执行了sleep(intmillsecond)方法,使当前线程进入休眠状态。

(3)线程使用CPU资源期间,执行了wait()方法,使得当前线程进入等待状态。

(4)线程使用CPU资源期间,执行某个操作进入阻塞状态,比如执行读/写操作引起阻塞。

3.死亡状态,不能再调用start()方法。

4.新建和死亡状态。

5.两种方法:

用Thread类或其子类。

6.使用setPrority(intgrade)方法。

7.Java使我们可以创建多个线程,在处理多线程问题时,我们必须注意这样一个问题:

当两个或多个线程同时访问同一个变量,并且一个线程需要修改这个变量。

我们应对这样的问题作出处理,否则可能发生混乱。

8.当一个线程使用的同步方法中用到某个变量,而此变量又需要其它线程修改后才能符合本线程的需要,那么可以在同步方法中使用wait()方法。

使用wait方法可以中断方法的执行,使本线程等待,暂时让出CPU的使用权,并允许其它线程使用这个同步方法。

其它线程如果在使用这个同步方法时不需要等待,那么它使用完这个同步方法的同时,应当用notifyAll()方法通知所有的由于使用这个同步方法而处于等待的线程结束等待。

9.不合理。

10.“吵醒”休眠的线程。

一个占有CPU资源的线程可以让休眠的线程调用interrupt方法“吵醒”自己,即导致休眠的线程发生InterruptedException异常,从而结束休眠,重新排队等待CPU资源。

11.publicclassE{

publicstaticvoidmain(Stringargs[]){

Cinemaa=newCinema();

}

}

classTicketSellerpublicclassE{

publicstaticvoidmain(Stringargs[]){

ClassRoomroom6501=newClassRoom();

try{(1000);

}

catch(Exceptionexp){}

}

}

classClassRoomimplementsRunnable{

Threadstudent1,student2,teacher;

ClassRoom(){

teacher=newThread(this);

student1=newThread(this);

student2=newThread(this);

("王教授");

("张三");

("李四");

}

publicvoidrun(){

if()==student1){

(1000*60*10);

}

catch(InterruptedExceptione){

}

();

publicclassE{

publicstaticvoidmain(Stringargs[]){

JoinWorkwork=newJo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1