• 212.95 KB
  • 2022-04-22 11:23:05 发布

JAVA程序设计基础 吴小东 著 清华大学出版社 (吴小东 著) 清华大学出版社 课后答案

  • 30页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'课后答案网,用心为你服务!大学答案---中学答案---考研答案---考试答案最全最多的课后习题参考答案,尽在课后答案网(www.khdaw.com)!Khdaw团队一直秉承用心为大家服务的宗旨,以关注学生的学习生活为出发点,旨在为广大学生朋友的自主学习提供一个分享和交流的平台。爱校园(www.aixiaoyuan.com)课后答案网(www.khdaw.com)淘答案(www.taodaan.com) Java程序设计基础习题答案第1章1选择题(1)BCD(2)D(3)CD(4)CD第2章1、错误:-0x3221fa008.33Ee-10整数:18564l0210xa60xa2e335-789990L浮点数:0x3.45-3.811.34e-8-.67e325.053.249-1E3八进制数:053.249021十六进制数:0xa60x3.450xa2e32、正确的标识符:_book,book3,_9days,I,copy_file,_56,up_down,if_count,agentEventListener错误的标识符:int,println,static,a$,5files,-number,date:x,+digit,$abcd,3、(1)inti改为staticinti。(2)j=i改为j=(short)i。(3)i为数组,应该改为inti[]=newint[10]。同时后面也要做相应修改。(4)将k=i&j改为k=(Boolean)i&j。4、x=10,y=6,z=false(1)false(2)x=16(3)true(4)x=60(5)6(6)6(7)true(8)false5、x=5,y=7,z=0(1)35(2)-1(3)6(4)13(5)36、(1)x>=y?10:3y%=(2)x<10i>6j==5&&||y=(3)b+c+da-=(4)a"Z"){count++;}}}catch(Exceptione){if(fr!=null){try{fr.close(); }catch(Exceptioner){e.printStackTrace();}}}returncount;}}(3)importjava.lang.StringBuffer;importjava.io.FileReader;importjava.io.FileWriter;classC83{publicbooleanwordCount(StringfileName){intcount=0;FileReaderfr=null;FileWriterfw=null;char[]chs=newchar[1000];StringBuffersb=newStringBuffer();try{fr=newFileReader(fileName);while((count=fr.read(chs))!=-1){sb.append(chs,0,count);}fr.close();Stringstr=newString(sb);str.replaceAll("","#");fw=newFileWriter(fileName);fw.write(str.toCharArray());fw.close();}catch(Exceptione){if(fr!=null){try{fr.close();}catch(Exceptioner){e.printStackTrace();}}if(fw!=null){try{fw.close();}catch(Exceptioner){e.printStackTrace();}} }returntrue;}}第9章1、(1)C(2)D(3)B(4)C2、程序阅读(1)12distinctwordsdetected:a0b0r0a2c0o0p0o2p2s0t0q0(2)程序实现的是出一副牌中的所有的牌,先黑桃、红桃、方块,最后到梅花。每种花色的版从A出到K。3、(1)publicclassabc2{publicabc2(){}publicvoidf1(){Classcls=(newHashSet()).getClass();Field[]fls=cls.getDeclaredFields();Method[]mds=cls.getMethods();System.out.println("classname:"+cls.getName());System.out.println("packagename:"+cls.getPackage());System.out.println("superclassname:"+cls.getSuperclass().getName());for(inti=0;i"Z"|chs[i]<"a"){break;}}if(i>=100){offsetBuffer+=100;}else{readCount=pbr.read(chs,offsetReader,offsetBuffer-offsetReader+i);offsetReader+=i;offsetBuffer=offsetReader;if(pbr.read()==-1){break;}}}//while}catch(Exceptione){e.printStackTrace();}}}(3)//READER的实现,WRITER类似classC103ReaderextendsFileReader{privateintreadCount;publicC103Reader(StringreadName)throwsException{//try{super(readName);//}catch(Exceptione){//e.printStackTrace();//}}publicintCount(charch,char[]chs,intlength){Stringstr=newString(chs,0,length);StringsubStr=String.valueOf(ch); intoffset=0,count=0;while((offset=str.indexOf(subStr,offset))>0){count++;offset++;}returncount;}//可以使用这个方法进行计数publicintreadAndCount(charch,char[]chs,intoff,intlength){try{intcount=this.read(chs,off,length);returnthis.Count(ch,chs,count);}catch(Exceptione){e.printStackTrace();return0;}}}(4)//Vector对象序列化并放到一个流(testvector.obj文件)中classTestVectorimplementsjava.io.Serializable{privateVectorvect=newVector();publicvoidadd(Objectobj){this.vect.add(obj);}publicvoidprint(){System.out.println(this.vect);}publicstaticvoidmain(String[]args){try{/*将对象写到一个文件里*/FileOutputStreamobjfile=newFileOutputStream("testvector.obj");/*创建一个输出流*/ObjectOutputStreamp=newObjectOutputStream(objfile);/*创建一个TestVector对象*/TestVectortv=newTestVector();/*给Vector写入几String个对象*/tv.add("One");tv.add("Two");tv.add("Three"); p.writeObject(tv);//把tv写入流p.flush();objfile.close();//关闭文件对象}catch(Exceptione){e.printStackTrace();}}}//使用序列化的VectorclassReadVectorObj{publicstaticvoidmain(String[]args){try{/*打开文件,读取Vector存放的对象数据*/FileInputStreamobjfile=newFileInputStream("testvector.obj");ObjectInputStreamq=newObjectInputStream(objfile);/*获取对象原始数据*/TestVectormyvector=(TestVector)q.readObject();myvector.print();}catch(Exceptione){e.printStackTrace();}}}第11章1、选择题(1)BC(2)BC(3)B(4)B(5)C(6)D2、程序阅读(1)在右边添加一个控件。(2)Thelastwordisbold.(3)将在上级容器上添加一个按钮Clickme,点击这个按钮,将发出一声蜂鸣。(4)当鼠标移动或者拖拽时,将在一个textArea上添加一个消息,说明是在移动还是拖拽,当前鼠标在哪个控件上。3、程序题1、2、3题用到了同一个启动类publicclassabc3{JButtonjButton1=newJButton();publicabc3(){try{jbInit();}catch(Exceptione){ e.printStackTrace();}}privatevoidjbInit()throwsException{jButton1.setText("jButton1");}publicstaticvoidmain(Stringargs[]){Frame1a3=newFrame1();a3.show();}}(1)importjavax.swing.*;importjava.awt.*;importcom.borland.jbcl.layout.*;/***

Title:

*

Description:

*

Copyright:Copyright(c)2005

*

Company:

*@authornotattributable*@version1.0*/publicclassFrame1extendsJFrame{XYLayoutxYLayout1=newXYLayout();JButtonjButtonStart=newJButton();JButtonjButtonStop=newJButton();JLabeljLabel1=newJLabel();publicFrame1(){try{jbInit();}catch(Exceptionex){ex.printStackTrace();}}voidjbInit()throwsException{jButtonStart.setText("Start");this.getContentPane().setLayout(xYLayout1);jButtonStop.setText("Stop");xYLayout1.setWidth(279); xYLayout1.setHeight(182);jLabel1.setFont(newjava.awt.Font("Dialog",0,20));jLabel1.setForeground(Color.gray);jLabel1.setVerifyInputWhenFocusTarget(true);jLabel1.setText("PressStart");this.setDefaultCloseOperation(HIDE_ON_CLOSE);this.setEnabled(true);this.setLocale(java.util.Locale.getDefault());this.setResizable(true);this.setState(Frame.ICONIFIED);this.setTitle("");this.getContentPane().add(jButtonStart,newXYConstraints(42,34,83,24));this.getContentPane().add(jButtonStop,newXYConstraints(171,34,83,24));this.getContentPane().add(jLabel1,newXYConstraints(74,91,119,31));//this.add(newComponet(),BorderLayout.EAST);}}(2)publicclassFrame1extendsJFrame{XYLayoutxYLayout1=newXYLayout();JButtonjButtonStart=newJButton();JButtonjButtonStop=newJButton();JLabeljLabel1=newJLabel();publicFrame1(){try{jbInit();}catch(Exceptionex){ex.printStackTrace();}}voidjbInit()throwsException{jButtonStart.setText("Start");jButtonStart.addActionListener(newFrame1_jButtonStart_actionAdapter(this));jButtonStart.addActionListener(newFrame1_jButtonStart_actionAdapter(this));this.getContentPane().setLayout(xYLayout1);jButtonStop.setText("Stop");jButtonStop.addActionListener(newFrame1_jButtonStop_actionAdapter(this));xYLayout1.setWidth(279);xYLayout1.setHeight(182);jLabel1.setFont(newjava.awt.Font("Dialog",0,20));jLabel1.setForeground(Color.gray);jLabel1.setVerifyInputWhenFocusTarget(true); jLabel1.setText("PressStart");this.setDefaultCloseOperation(HIDE_ON_CLOSE);this.setEnabled(true);this.setLocale(java.util.Locale.getDefault());this.setResizable(true);this.setState(Frame.ICONIFIED);this.setTitle("");this.getContentPane().add(jButtonStart,newXYConstraints(42,34,83,24));this.getContentPane().add(jButtonStop,newXYConstraints(171,34,83,24));this.getContentPane().add(jLabel1,newXYConstraints(74,91,119,31));//this.add(newComponet(),BorderLayout.EAST);}voidjButtonStart_actionPerformed(ActionEvente){this.jButtonStart.setEnabled(false);this.jButtonStop.setEnabled(true);this.jLabel1.setText("PressStart");}voidjButtonStop_actionPerformed(ActionEvente){this.jButtonStart.setEnabled(true);this.jButtonStop.setEnabled(false);this.jLabel1.setText("PressStop");}}classFrame1_jButtonStart_actionAdapterimplementsjava.awt.event.ActionListener{Frame1adaptee;Frame1_jButtonStart_actionAdapter(Frame1adaptee){this.adaptee=adaptee;}publicvoidactionPerformed(ActionEvente){adaptee.jButtonStart_actionPerformed(e);}}classFrame1_jButtonStop_actionAdapterimplementsjava.awt.event.ActionListener{Frame1adaptee;Frame1_jButtonStop_actionAdapter(Frame1adaptee){this.adaptee=adaptee;} publicvoidactionPerformed(ActionEvente){adaptee.jButtonStop_actionPerformed(e);}}(3)将上文的函数jButtonStart_actionPerformed改为:voidjButtonStart_actionPerformed(ActionEvente){Dialog1dlg=newDialog1(this,"test",true);dlg.show();if(dlg.sayYes){this.jButtonStart.setEnabled(false);this.jButtonStop.setEnabled(true);this.jLabel1.setText("PressStart");}同时添加一个新类Dialog1:publicclassDialog1extendsJDialog{JPanelpanel1=newJPanel();JButtonjButton1=newJButton();JButtonjButton2=newJButton();XYLayoutxYLayout2=newXYLayout();JLabeljLabel1=newJLabel();publicbooleansayYes;BorderLayoutborderLayout1=newBorderLayout();publicDialog1(Frameframe,Stringtitle,booleanmodal){super(frame,title,modal);sayYes=false;try{jbInit();pack();}catch(Exceptionex){ex.printStackTrace();}}publicDialog1(){this(null,"",false);}privatevoidjbInit()throwsException{panel1.setLayout(xYLayout2);jButton1.setText("jButton1");jButton1.addActionListener(newDialog1_jButton1_actionAdapter(this));jButton2.setText("jButton1");jButton2.addActionListener(newDialog1_jButton2_actionAdapter(this));this.getContentPane().setLayout(borderLayout1); jLabel1.setForeground(Color.black);jLabel1.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION);jLabel1.setDoubleBuffered(false);jLabel1.setToolTipText("");jLabel1.setVerifyInputWhenFocusTarget(true);jLabel1.setDisplayedMnemonic("0");jLabel1.setIcon(null);jLabel1.setIconTextGap(4);jLabel1.setText("Realystartthejob?\nIt"sgoingtotakealongtime.");getContentPane().add(panel1,BorderLayout.CENTER);panel1.add(jButton2,newXYConstraints(5,195,99,40));panel1.add(jButton1,newXYConstraints(176,196,115,34));panel1.add(jLabel1,newXYConstraints(25,48,317,100));}voidjButton2_actionPerformed(ActionEvente){this.sayYes=true;this.hide();}voidjButton1_actionPerformed(ActionEvente){this.hide();}}classDialog1_jButton2_actionAdapterimplementsjava.awt.event.ActionListener{Dialog1adaptee;Dialog1_jButton2_actionAdapter(Dialog1adaptee){this.adaptee=adaptee;}publicvoidactionPerformed(ActionEvente){adaptee.jButton2_actionPerformed(e);}}classDialog1_jButton1_actionAdapterimplementsjava.awt.event.ActionListener{Dialog1adaptee;Dialog1_jButton1_actionAdapter(Dialog1adaptee){this.adaptee=adaptee;}publicvoidactionPerformed(ActionEvente){adaptee.jButton1_actionPerformed(e); }}(4)将3中的Frame1改为继承extendsJApplet。HTML代码为:第12章1、选择题(1)B(2)B(start/run)(3)ABC(4)ABD2、程序阅读(1)这三个线程将各自分别执行,哪个先执行,哪个后执行,并无确定的顺序。所谓“来早了不如来巧了”。(2)将启动一个线程,如果此线程是当前线程,将打印出当前时间,以时:分:秒显示。3、编程题(1)publicclassApplet1extendsJApplet{privatebooleanisStandalone=false;//GetaparametervaluepublicStringgetParameter(Stringkey,Stringdef){returnisStandalone?System.getProperty(key,def):(getParameter(key)!=null?getParameter(key):def);}//ConstructtheappletpublicApplet1(){}//Initializetheappletpublicvoidinit(){try{jbInit();}catch(Exceptione){e.printStackTrace();}}//ComponentinitializationprivatevoidjbInit()throwsException{this.setSize(newDimension(400,300));}//Starttheappletpublicvoidstart(){ newSimpleThread("Jamaica").start();newSimpleThread("Fiji").start();newSimpleThread("Hoho").start();}//Stoptheappletpublicvoidstop(){}//Destroytheappletpublicvoiddestroy(){}//GetAppletinformationpublicStringgetAppletInfo(){return"AppletInformation";}//GetparameterinfopublicString[][]getParameterInfo(){returnnull;}//Mainmethodpublicstaticvoidmain(String[]args){Applet1applet=newApplet1();applet.isStandalone=true;JFrameframe=newJFrame();//EXIT_ON_CLOSE==3frame.setDefaultCloseOperation(3);frame.setTitle("AppletFrame");frame.getContentPane().add(applet,BorderLayout.CENTER);applet.init();applet.start();frame.setSize(400,320);Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();frame.setLocation((d.width-frame.getSize().width)/2,(d.height-frame.getSize().height)/2);frame.setVisible(true);}//staticinitializerforsettinglook&feelstatic{try{ //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());}catch(Exceptione){}}}(2)publicclassabc12_13{publicabc12_13(){}publicstaticvoidmain(Stringargs[]){Playerp0=newPlayer(0);Playerp1=newPlayer(1);Playerp2=newPlayer(2);Playerp3=newPlayer(3);p0.start();p1.start();p2.start();p3.start();}}classPoker{publicStringnums[];publicStringcolors[];publicPoker(){nums=newString[13];colors=newString[4];nums[0]="Ace";nums[1]="Two";nums[2]="Three";nums[3]="Four";nums[4]="Five";nums[5]="Six";nums[6]="Seven";nums[7]="Eight";nums[8]="Nine";nums[9]="Ten";nums[10]="Jack";nums[11]="Queen";nums[12]="King";colors[0]="Diamonds";colors[1]="Clubs";colors[2]="Hearts";colors[3]="Spades"; }}classPlayerextendsThread{privateintdeskNo;privatePokerpokerCards;publicstaticinttoken=0;publicPlayer(intindex){this.deskNo=index;pokerCards=newPoker();}publicvoidrun(){while(token<54){if(((token==0)&&(deskNo==0))|//firstone(token%4==(deskNo==0?3:deskNo-1))){//roundSystem.out.println(deskNo+":"+pokerCards.nums[token%13]+"of"+pokerCards.colors[deskNo]);token++;}try{sleep((long)(Math.random()*1000));}catch(Exceptione){e.printStackTrace();}}}}(3)图片移动见例12.9,下面是图片动画的例子packagetest;importjava.awt.*;publicclassSimpleAnimation1extendsFrameimplementsRunnable{Threadanimation;intframeDelay=1500;inti=0;Imageframes[];intnumFrames=12;intcurrentFrame=0;intscreenWidth=800;intscreenHeight=600; booleanthreadSuspended=false;MediaTrackertracker;GraphicsoffScrGraphics;DimensionoffScrDimension;ImageoffScrImage;StringWellText="正在装载图象,请稍候......";Fontfont=newFont("宋体",Font.ITALIC,20);publicstaticvoidmain(Stringargs[]){SimpleAnimation1app=newSimpleAnimation1();}publicSimpleAnimation1(){setup();resize(screenWidth,screenHeight);show();animation=newThread(this);animation.start();}voidsetup(){Toolkitt=getToolkit();frames=newImage[numFrames];tracker=newMediaTracker(this);for(i=0;i");for(inta=1;a<=9;a++){for(intb=1;b<=9;b++){intc=a*b;pw.print(a+"*"+b+"="+c+"t");}pw.println("
");}}}(4)提示:页头和页脚用普通的HTML,然后用include命令引入。中间的格式、新闻内容可以用JSP实现。(5)<%@pagecontentType="text/html;charset=gb2312"%><%@pageerrorPage="jsp1_error.jsp"%>jsp1username:
password: <%session.setAttribute("username",username);session.setAttribute("password",password);%> (6)略。参见第5题及本章的购物车例子。'

您可能关注的文档