Java复习题(三)阅读程序题_软件.docx
Java复习题(三)阅读程序题_软件(JAVA程序设计)温习题之三阅读程序题三、程序阅读题1阅读下面程序importjava.io.*;publicclassReverse2publicstaticvoidmain(Stringargs)inti,n=10;inta=newint10;tryBufferedReaderbr=newBufferedReader(newInputStreamReader(System.in);ai=Integer.parseInt(br.readLine();catch(IOExceptione);for(i=n-1;i>=0;i=i-2)System.out.print(ai+"");System.out.println();请写出该程序的功能:该程序使用字符缓冲输入流从键盘输入10个数,然后倒序并间隔打印出来。2阅读下面程序importjava.io.*;publicclassabcpublicstaticvoidmain(Stringargs)inti,s=0;inta=20,30,40,50,60,70,80,90,100,110,120;for(i=0;irandf.writelnt(data_arri);randf.writeUTF("Goodmorning!");'for(inti=data_arr.length-l;i>=0;i=i-2)randf.seek(i*4);System,out.print(""+randf.readInt();randf.seek(40);System.out.println(randf.readUTF();randf.close();catch(IOExceptione)System.out.println("Fileaccesserror:"+e);该程序的输出结果是:996512366Goodmorning!4、阅读下面程序并填空。class_extendsExceptionStringmymsg="我本人定义的异常!"doublemynum=2.0;MyException()super("首字母不能为A!");MyException(Stringmsg)_/调用父类构造方法,参数为msgpublicvoiddisplayme()System.out.println(mymsg);publicdoublemymethod()returnMath.sqrt(mynum);classExceptionTestpublicstaticvoidmain(Stringargs)tryif(argsO.charAt(O)='A')MyExceptione=newMyException();System.out.println("kkkk:"+e.mymethod();e.displayme();Systemoutprintln("*intry*");_;/抛出异常eelseif(argsO.charAt(O)='B')thrownewMyException("第一个字符不应是B!");elseSystem.out.println(args0);catch(_)System.out.println(aaa.getMessage();aaa.displayme();System.out.println(""+aaa.mymethod();catch(_)System.out.println("命令行参数个数错!");程序填空:MyExceptionsuper(msg)throweMyExceptionaaaArrayIndexOutOfBoundsException5、阅读下面程序importjava.io.*;publicclassTestpublicstaticvoidmain(Stringargs)SubSubClassm=newSubSubClass(3,6,6);m.show();classSuperClassinta,b;SuperClass(intx,inty)a=x;b=y;classSubClassextendsSuperClassintc;SubClass(intaa,intbb,intcc)super(aa,bb);c=cc;classSubSubClassextendsSubClassinta;SubSubClass(intaa,intbb,intcc)super(aa,bb,cc);a=aa+bb+cc;voidshow()System.out.println("a="+a+"nb="+b+"nc="+c);请写出该程序的运行结果:a=60b=20c=306、阅读下面程序importjava.io.*;publicclassabcpublicstaticvoidmain(Stringargs)Stringsl="Hello!"Strings2=newString("World!");System.out.println(sl.concat(s2);请写出该程序的运行结果:Hello!World!7、阅读下面程序importjava.io.*;publicclassClass1publicstaticvoidmain(Stringargs)inti,max,min;inta=12,67,8,98,23,56,124,55,99,100);max=min=a0;for(i=1;iSystem.out.println(max+""+min);System.out.println();请写出该程序完成的功能:在数组中查找并输出最大值和最小值。8、阅读下面程序importjava.awt.*;importjava.applet.Applet;publicclassDrawMylmageextendsAppletImagemyImage;/定义一个图像类Image的对象myImagepublicvoidinit()myImage=getImage(getDocumentBase(),"pica.jpg");publicvoidpaint(Graphicsg)g.drawImage(myImage,0,0,this);请写出该程序的功能:在Applet界面中显示当前文件夹下名为“pica.jpg的图像。9、阅读下面程序并填空。importjava.awt.*;importjava.applet.*;import:/docsj/doc/38f1b636cdbff121dd36a32d7375a417866fc138.html.*;publicclassMypicture_AppletImageimage;publicvoid_()tryimage=getlmage(newURL(getCodeBase(),'image.gif');_(MalformedURLExceptione)publicvoidpaint(Graphicsg)g.drawlmage(image,0,0,_);publicvoidstart()_();程序填空题:extendsinitcatchthisrepaint10、阅读下面程序:publicclassSumpublicstaticvoidmain(Stringargs)doublesum=0.0;for(inti=1;ivoidshow()System.out.println("c="+c+"na="+a+"nb="+b);classSubSubClassextendsSubClassinta;SubSubClass(intaa,intbb,intcc)super(aa,bb,cc);a=aa+bb+cc;voidshow()System.out.println("a="+a+"nb="+b+"nc="+c);classtestpublicstaticvoidmain(Stringargs)SuperClassp=newSubSubClass(10,20,30);p.show();该程序的输出结果是:a=60b=20c=3012、阅读下面程序:importjava.io.*;publiicclassTestpublicstaticvoidmain(Stringargs)ABs=newAB("Hello!","IloveJava.");System.out.println(s.toString();classABStringsl;Strings2;AB(Stringstrl,Stringstr2)sl=str1;s2=str2;publicStringtoString()returnsl+s2;该程序的输出结果是:Hello!IloveJava.13、阅读下面程序,并填空。import_classMyCopypublicstaticvoidmain(Stringoargs)intch;FileInputStreamfin;_fout;tryfin=newFileInputStream(args0);fout=newFileOutputStream(_);ch=fin.read();while(ch!=-1)_ch=fin.read();fin.close();fout.close();catch(_e1)System.out.println("使用格式错误!正确格式为:javamycopy源文件名目的文件名");System.exit(0);catch(FileNotFoundExceptione3)System.out.println("文件没有找到!");catch(IOExceptione2)System.out.println("流错误!");程序填空:importjava.io.*;FileOutputStreamargs0fout.write(ch);ArrayIndexOutOfBoundsException14、阅读下面程序importjava.io.*;publicclassReversepublicstaticvoidmain(Stringargs)inti,n=10;inta=newint10;for(i=0;iBufferedReaderbr=newBufferedReader(newInputStreamReader(System.in);ai=Integer.parseInt(br.readLine();/输入一个整数catch(IOExceptione);for(i=n-1;i>=0;i-)System.out.print(ai+"");System.out.println();请写出此程序功能:程序运行时从键盘输入10个整数,然后倒序输出。15、阅读下面程序importjava.io.*;publicclassTestpublicstaticvoidmain(Stringargs)inti,s=0;inta=10,20,30,40,50,60,70,80,90;for(i=0;inumber=n;t=newThread(this);t.start();publicvoidrun()one.display(number);publicclassSynchpublicstaticvoidmain(Stringargs)throwsInterruptedExceptionOneone=newOne();intdigit=100;Twos1=newTwo(one,digit);Twos2=newTwo(one,digit);Twos3=newTwo(one,digit);Twos4=newTwo(one,digit);s1.t.join();s2.t.join();s3.t.join();s4.t.join();System.out.println("Synch结束!");此程序的输出结果是:two100完成two100完成two100完成two100完成Synch结束!17、阅读下面程序,并填空。import_;classFileTypepublicstaticvoidmain(Stringargs)_;tryFileReaderfis=new_(args0);BufferedReaderreader=newBufferedReader();Strings;while(s=reader.readLine()!=_)System.out.println(""+(i+)+":"+s);fis.close();reader.close();catch(IOExceptione)System.out.println(e);catch(_e1)System.out.println(“缺少命令行参数!);程序填空:java.io.*;inti;FileReaderfisnullArrayIndexOutOfBoundsException18、阅读下面程序:publicclassSumpublicstaticvoidmain(Stringargs)doublesum=0.0:for(inti=1;i该程序的输出结果是:10001000120、阅读下面程序:publicclassTest2staticbooleanfoo(charc)System.out.print(c);returntrue;publicstaticvoidmain(Stringargs)inti=0:for(foo('A');foo('B')&foo('C')i+;foo('D');此程序的输出结果是:"ABDCBDCB"21、阅读下面程序,并填空。import_;importjava.awt.event.*;publicclassOvalDrawerextendsFrameimplementsRunnableprivateColorcolors=Color.RED,Color.BLACK,Color.BLUE,Color.GREEN,Color.DARK_GRAY;privateColorcolor;privateintx=l0,y=10,width=10,height=10;publicOvalDrawer(Stringtitle)super(title);_;/设置窗口大小为300*300setVisible(true);_;/创立线程并启动publicvoidrun()while(true)x=(int)(Math.random0*300);y=(int)(Math.random0*300);width=(int)(Math.random()*100);height=(int)(Math.random()*100);color=colors(int)(Math.random()*(colors.length-1);