《Java程序设计》参考答案1.doc_第1页
《Java程序设计》参考答案1.doc_第2页
《Java程序设计》参考答案1.doc_第3页
《Java程序设计》参考答案1.doc_第4页
《Java程序设计》参考答案1.doc_第5页
已阅读5页,还剩27页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

浙江工业大学软件学院Java程序设计练习题参考答案赵小敏 自编版权所有 不得转载 违者必究参考答案一、判断题1-10 对,错,对,错,错,对,错,对,对,对11-20 对,错,错,错,对,错,对,对,对,对21-30 对,对,对,错,错,对,错,对,对,对31-40 对,对,对,对,错,对,错,对,对,对41-50 对,对,错,对,错,错,对,错,错,错二、填空题1、javac,java,jar,appletview2、0.0f,false,基本,引用3、6,2484、false,35、MyPrg.java6、Interger,parseInt()7、28、抽象(或abstract),final9、“abc”10、引用(或复合),arraycopy11、package MyPackage,第一行12、implements13、new,protected14、继承,接口15、对象,interface16、abstract, 提供了一个基本类,实现代码或算法的重用17、Object,import java.util.*;18、Error和Exception19、finally,throw,throws20、InputStream,OutputStream,字节,字符21、IOException,ArrayIndexOutOfBoundsException22、new FileInputStream(“c:test.txt”),readLine()23、RandomAcessFile raf=new RandomAcessFile(“c:test.txt”,“rw”);i= raf.readInt();24、GridLayout、CardLayout25、轻量级,getContentPane(),setLayout26、JFrame,JWindow,JDialog27、Applet,MyApplet.java28、init(),destroy()29、add,GUI30、Thread,Runnable31、start,run32、synchronized,从全局考虑获得锁的顺序,并且在整个程序中都遵守这个顺序;释放锁时,要按加锁的反序释放。33、sleep,setPriority()34、Runnable,public void run(),new Thread(rt),myThread.start()35、TCP,UDP36、getPort(),getLength()37、ServerSocket,Socket38、ServerSocket,Socket39、accept(),receive()40、accept(),receive()三、选择题1-10 BCCBA DCCAA11-20 BABCD BABBC21-30 CDBCB DADBC31-40 CABBD ACCAA41-50 DBADB DCABA51-60 DADDD ADDCD61-70 DABCB ACCBB71-80 CDCBD ABDDC四、程序阅读题1、输出结果如下:Second season3th season、输出结果如下:i=1j=2、输出结果如下:234、输出结果如下:Hi!I am TomHow do you do?5、第8行编译出错,原因是第8行调用11行构造方法时,int参数不能与short类型匹配6、输出结果如下:67、第6行编译出错,原因是无法调用另一个类中private修饰的方法8、输出结果如下:HelloYava9、输出结果如下:捕获ArithmeticException异常java.lang.ArithmeticException: 没有13月份!10、输出结果如下:userException: 数组可能越界!退出返回!11、输出结果如下:捕获了一个零除异常12、1) container.put( fancyButton ); 改为container.add( fancyButton );2) container.put( plainButton );改为container.add( plainButton );3) fancyButton.addItemListener( handler );改为 fancyButton.addActionListener( handler );4) plainButton.addItemListener( handler );改为plainButton.addActionListener( handler );5) itemStateChanged改为actionPerformed13、输出结果如下:张三的工资为1000元。异常:工资不能为负!14、输出结果如下:size in Inner.doStuff(): 11size of the Inner class: 3size of the Outer class: 215、输出结果如下:15111016、有两种修改方法:(1)如果采用程序中的implements Runnable的方法,则修改的地方有以下3处,:第2行String str=new String10;改为static String str=new String10;第11行的sleep(1000);改为Thread.sleep(1000);第20行的代码修改为以下两行: Thread tt=new Thread(t); /tt的变量名可取符合java语言规范的任何标识符 tt.start();(2如果采用extends实现该线程程序,则修改的地方有以下3处: 第1行的implements Runnable修改为extends Thread第2行String str=new String10;改为static String str=new String10;第20行的t.run(); 修改为t.start();17、输出结果如下:javaiavaCYava18、18. System.out.println(str1= =str2); 输出true19. System.out.println(str2= =str3); 输出true20. System.out.println(str2= =str4); 输出false21. System.out.println(str4= =str5); 编译出错22. System.out.println(str3.equals(str4); 输出true23. System.out.println(str4.equals(str5); 输出false24. System.out.println (stringReplace (str1); 输出iavaC25. System.out.println(bufferReplace (str5); 输出iavaC26. System.out.println (str1); 输出java27. System.out.println(str5); 输出iavaC19、有两种修改方法:(1)如果采用程序中的implements Runnable的方法,则修改的地方有以下4处,每改对一处给2分,总共8分:第35行写入构造方法,并且将第3行的str.length()改为str.length,修改后的代码如下:public Test() for (int i = 0; i str.length; i+) stri=i+-; 第8行的str.length()改为str.length第9行的sleep(1000);改为Thread.sleep(1000);第18行的代码修改为以下两行: Thread tt=new Thread(t); /tt的变量名可取符合java语言规范的任何标识符 tt.start();(2如果采用extends实现该线程程序,则修改的地方有以下4处,每改对一处给2分,总共8分:第一行的implements Runnable修改为extends Thread第35行写入构造方法,并且将第3行的str.length()改为str.length,修改后的代码如下:public Test() for (int i = 0; i str.length; i+) stri=i+-; 第8行的str.length()改为str.length 第18行的t.run(); 修改为t.start();20、(1)小应用程序在加载阶段,在屏幕上打印结果是:Applet初始化Applet启动.(2)关闭这个小应用程序时,在屏幕上打印结果是:Applet停止运行.准备卸载.21、输出结果如下:My Func22、输出结果如下:eab23、输出结果如下:224、输出结果如下:The index is 10The index is 20The index is 3025、输出结果如下:1002026、输出结果如下:x=2st.y=1x=2st.y=127、输出结果如下:two028、输出结果如下:i of methodI():21i of Test11:3i of Test1:229、输出结果如下:15030、输出结果如下:13431、输出结果如下:32、输出结果如下:33、执行的顺序为:1-4-5-6-2-334、每隔1s打印一个数字和-,运行结果是:0-1-2-3-4-5-6-7-8-9-五、程序填空题1、extends JFrame调用父类JFrame的构造方法,使得窗口的标题为ConcentricsetSize(300,300);使窗口为可见的,即可在屏幕上显示出来输出结果为在窗口画8个同心圆,界面效果如下:2、创建path的File对象,对象引用名为f判断f的对象是否为文件递归调用scanFiles方法该程序的作用是扫描C盘下所有的文件,并将文件的路径打印3、abstractString getNameextendsgetDescription() String args4、extends Thread让线程休眠n毫秒t1.start();程序的输出结果是:30105、Test t=new Test();创建test.txt文件的FileInputStream对象,对象引用名为din运行结果是:two06、interface AreaMyCircle c=new MyCircle(5);MyRectangle r=new MyRectangle(2,3);程序的功能是定义一个接口Area,其中包含一个计算面积的方法CalsulateArea(),然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法CalsulateArea(),分别计算圆和矩形的面积,最后写出测试以上类和方法的程序。7、WindowListernerWindowEventaddWindowListernersetSize8、略9、servernew ServerSocket(6000,50) socketserver.accept();output = new ObjectOutputStream(socket.getOutputStream();input = new ObjectInputStream(socket.getInputStream();socket.close();catch(IOException ioException) 或catch(Exception ioException) ,ioException可取任何变量1011、略五、程序改错题1、(1)double PI; 改为double PI=Math.PI;(2)class Cycle extends Shape 改为class Cycle implements Shape(3)double area( )改为public double area( )(4)System.out.println(PI*r*r);改为 return PI*r*r;(5)在Cycle 类中增加以下方法public double perimeter( ) return 2*PI*r; 或者去掉public double perimeter( );2、第10行改为public String toString()1619行改为Person p=new Person(张三,20,男);20行改为System.out.println(p.toString();3、第4行的str.length()改为str.length第8行start ()改为run()第11行的sleep(1000);改为Thread.sleep(1000);六、编程题1、参考代码如下:class ForDemopublic static void main(String args)int n=0;for(int i=10;i=10)n=0;System.out.println();2、有两种方法可实现题目给定的getY : 方法一:若采用if语句实现,参考代码如下:public double getY(double t) double y=0.0; if(t=0&t=1&t=3&t=5&t7) y=t+1; else y=t-1; return y;方法二:若采用switch语句实现,参考代码如下:public double getY(double t) double y=0.0; switch(int)t) case 0: y=t*t-1; break; case 1: case 2: y=t*t*t-2*t-3;break; case 3: case 4: y=t*t-t*Math.cos(t);break; case 5: case 6: y=t+1;break; default: y=t-1; return y;3、略4、参考代码如下:interface AreaInterfacedouble pai=Math.PI;double area();5、参考代码如下:public class Studentprivate String name;private int age;public Student()name=无名氏;age=20;public void setName(String name)=name;public String getName()return name;public void setAge(int age)this.age=age;public int getAge()return age;public boolean isSameAge(Student s)if(s.age-this.age=0) return true;return false;public static void main(String args)Student s1=new Student();Student s2=new Student();s2.age=20;System.out.println(s1.isSameAge(s2);6:略7、参考代码如下:interface AreaInterface double pai=Math.PI; double area();class Rectangle implements AreaInterfaceprivate double x,y;public Rectangle(double x,double y)this.x=x;this.y=y;public double area()return x*y;public String toString()return 该长方形面积为:+area();class TestAreapublic static void main(String args)Rectangle r=new Rectangle(10.0,20.0);System.out.println(r.toString();810:略11、参考代码如下:public class TestArraySum int a=new int10; public TestArraySum() for(int i=0;i10;i+) ai=11+i; public int arraySum(int a) int sum=0; for(int i=0; ib?a-b:b-a ; public int multiAB() return a*b ; public double Divab() return (double)ab?a/b:b/a ; class TestSuansu public static void main(String args) Suansu s=new Suansu(); System.out.println(s.addAB(); System.out.println(s.subAB(); System.out.println(s.multiAB(); System.out.println(s.Divab(); 13、参考代码如下:class Rectangle private int width, height; public Rectangle(int w,int h) width=w; height=h; public Rectangle() width=4; height=5; public int getWidth() return width; public int getHeight() return height; public void setWidth(int w) if (w 0 & w 0 & h 30) height = h; else System.out.println(Invalid height); public int getPerimeter() return width + width + height + height; public int getArea() return width * height; public void draw() for (int rowCounter = 0; rowCounter height; rowCounter+) for (int colCounter = 0; colCounter =0) count+; index=sentence.indexOf(word,index+word.length();catch(IOException e)return count;public static void main(String args)String article=article.txt;String word=stock;WordCount w=new WordCount();System.out.println(单词+word+在文章+article+中出现的次数为:+w.getWordCount(article,word);18、参考代码如下:import java.io.*;public class N_Digitalpublic void initNum(int num) if (num = null) throw new NullPointerException(参数不能为null!); for (int i=0;inum.length;i+) numi=(int)(Math.random()*40)+10; System.out.print(numi+ ); public void sortNum(int obj) if (obj = null) throw new NullPointerException(参数不能为null!); int tmp; for (int i = 0 ;i obj.length ;i+ ) /切记,每次都要从第一个开始比。最后的不用再比。 for (int j = 0 ;j obj.length -i -1 ;j+ ) /对邻接的元素进行比较,如果后面的小,就交换 if (objjobjj+1) tmp = objj; objj = objj+1; objj+1 = tmp; /if /for /for public void writeToFile(intnum,String file)tryFile f=new File(file);if(!f.exists()f.createNewFile();PrintWriter out = new PrintWriter(new FileWriter(file);for(int i=0;inum.length;i+)out.print(numi+ );System.out.print(numi+ );out.close();catch(IOException ioe)public static void main(String args)int n=(int)(Math.random()*40)+10;int num=new intn;N_Digital nd=new N_Digital();nd.initNum(num);nd.sortNum(num);System.out.println();nd.writeToFile(num,n.txt);19、参考代码如下:import java.io.*;public class CopyFile public static void main(String args) FileInputStream from = null; FileOutputStream to = null; try from = new FileInputStream(c:/test1.txt); to = new FileOutputStream(c:/test2.txt); byte buffer = new byte4096; int bytes_read; while ( (bytes_read = from.read(buffer) != -1) to.write(buffer, 0, bytes_read); from.close(); to.close(); catch (IOException e) e.printStackTrace(); 一个完整的copy文件的参考程序如下:import java.io.*;class CopyFile public boolean copy(String fromFileName, String toFileName,boolean override) File fromFile = new File(fromFileName); File toFile = new File(toFileName); if (!fromFile.exists() | !fromFile.isFile() | !fromFile.canRead() return false; if (toFile.isDirectory() toFile = new File(toFile, fromFile.getName(); if (toFile.exists() if (!toFile.canWrite() | override = false) return false; else String parent = toFile.getParent(); if (parent = null) parent = System.getProperty(user.dir); File dir = new File(parent); if (!dir.exists() | dir.isFile() | !dir.canWrite() return false; FileInputStream from = null; FileOutputStream to = null; try from = new FileInputStream(fromFile); to = new FileOutputStream(toFile); byte buffer = new byte4096; int bytes_read; while ( (bytes_read = from.read(buffer) != -1) to.write(buffer, 0, bytes_read); return true; catch (IOException e) return false; finally if (from != null) try from.close(); catch (IOException e) if (to != null) try to.close(); catch (IOException e) public static void main(String args) CopyFile cf=new CopyFile(); if(cf.copy(1.gif,2.gif,false)=true) System.out.println(文件拷贝成功!); else System.out.println(文件未拷贝!); 20、参考代码如下:import java.io.*;class ReadFileContentpublic static void main(String args)int i=0; try BufferedReader br = new BufferedReader(new FileReader(Test.txt); String line = br.readLine(); PrintWriter out = new PrintWriter(new FileWriter(myTest.txt); while(line!=null) i+; out.println(i+ +line); line = br.readLine(); br.close(); out.close(); catch (Exception e) e.printStackTrace(); 21、参考代码如下:import java.awt.*;import java.awt.event.*;public class TestButton extends Frame implements ActionListenerButton jb1,jb2;TextField jt; public TestButton() super(TestButton); jb1=new Button(JButton1); jb2=new Button(JButton2); jt=new TextField(20); jb1.addActionListener(this); jb2.addActionListener(this); setLayout(new FlowLayout(); add(jt); add(jb1); add(jb2); setSize(250,100); setVisible(true); public void actionPerformed(ActionEvent e) if(e.getSource()=jb1) jt.setText(jb1.getActionCommand(); if(e.getSource()=jb2) jt.setText(jb2.getActionCommand(); public static void main(String args) new TestButton(); 如果用Swing组件实现,则参考代码如下:import java.awt.*;import java.awt.event.*;import javax.swin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论