1、二级 JAVA 机试-225 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.请在每条横线处填写一个语句,使程序的功能完整,且输出结果为 9 1 1。注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。源程序文件代码清单如下:public class Outerpublic static void main(String args)Outer i=new Outer();i.taskInner();public class Innerprivate int size;public void doSometh
2、ing(int size)_/访问局部变量this.size+;/访问内部类的成员变量_/访问外部类的成员变量System.out.println(size+“ “+this.size+“ “+Outer.this.size);public void taskInner()_k.doSomething(8);private static int size;(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.下面的程序的功能是利用实现 Runnable 接口的方法来创建线程,并利用它来执行响应的一些操作。最后使得 m 的执行结果为 100。注意:请勿改动 main()主方法
3、和其他已有的语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:class ClassName implements Runnableint n;_tryThread.sleep(2000);n=100;catch(Exception e) public static void main(String args)tryClassName a=new ClassName();_thread1.start();thread1.join();int m=a.n;System.out.println(“m=“+m);catch(Exception e) (分数:40.00)_三、3综合应用题
4、(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是计算山顶的高度,计算方法是:该山顶由 a 点量得仰角度数为 a,由 b 点量得仰角度数为 b,且测得 a,b 点之间的距离为 c,求山的高度。要求窗口中有 3 个输入框,分别作为 a、b、c 的输入,一个按钮单击后进行计算,结果显示在另一个文本框中这个文本框不可编辑)。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。源程序文件代码清单如下:import java.io.*;import java.awt.*;import java.awt.event.*;impor
5、t java.applet.Applet;/*applet code=“ex6_3.class“width=800 height=400/applet*/public class ex6_3 extends Applet implements ActionListenerPanel pane=new Panel();Label 11=new Label(“a 点仰角:“);TextField tf1=new TextField(5);Label 12:=new Label(“b 点仰角:“);TextField tf2=new TextField(5);Label 13=new Label(“
6、a,b 之间距离:“);TextField tf3=new TextField(5);Button btn=new Button(“OK“);Label 14=new Label(“山高:“);TextField tf4=new TextField(20);ex6_3 Obj23_3;public void init()pane.setLayout(new FlowLayout(PlowLayout.LEFT,10,5);pane.add(11);pane.add(tf1);pane.add(12);pane.add(tf2);add(“North“,pane);Panel p2=new Pa
7、nel();p2.setLayout(new FlowLayout(FlowLayout.LEFT,10,5);p2.add(13);p2.add(tf3);p2.add(btn);btn.addActionListener(this);add(“Center“,p2);Panel p3=new Panel();p3.setLayout(new FlowLayout(FlowLayout.LEFT,10,5);p3.add(14);tf4.setEditable(true);p3.add(tf4);add(“South“,p3);Obj23_3=new ex6 3();public void
8、doMessure(double a1,double a2,double a3,TextField tf)double pi=Math.PI,a,b,h;a=al*pi/180.0;b=a2*pi/180.0;h=a3/(1.0/Math.tan(a)-1.0/Math.tan(b);tf.setText(Integer.toString(h);public void actionPerformed(ActionEvent ae)double a,b,c;trya=new Double(tf1.getText().doubleValue();b=new Double(tf2.getText()
9、.doubleValue();c=new Double(tf3.getText().doubleValue();obj23_3.doMessure(a,b,c, tf4;catch(NumberFormatExceptlon nfe)tf4.setText(“wrong number!“);ex6_3.htmlHTMLHEADTITLEex6_3/TITLE/HEADBODYapplet code=“ex6_3.class“width=800 height=400/applet/BODY/HTML(分数:30.00)_二级 JAVA 机试-225 答案解析(总分:100.00,做题时间:90
10、分钟)一、1基本操作题(总题数:1,分数:30.00)1.请在每条横线处填写一个语句,使程序的功能完整,且输出结果为 9 1 1。注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。源程序文件代码清单如下:public class Outerpublic static void main(String args)Outer i=new Outer();i.taskInner();public class Innerprivate int size;public void doSomething(int size)_/访问局部变量this.size+;/访问内部类的
11、成员变量_/访问外部类的成员变量System.out.println(size+“ “+this.size+“ “+Outer.this.size);public void taskInner()_k.doSomething(8);private static int size;(分数:30.00)_正确答案:(size+;Outer.this.size+;Inner k=new Inner();)解析:解析 本题主要考查内部类的概念、super、this 关键字的用法。解答本题的关键是熟练掌握super、this 关键字的用法。在本题中 size+语句是访问局部变量 size,Outer.t
12、his.size+语句的功能是访问外部类的成员变量 size,Inner K=new Inner()语句的功能是生成内部类 Inner 的对象 K。二、2简单应用题(总题数:1,分数:40.00)2.下面的程序的功能是利用实现 Runnable 接口的方法来创建线程,并利用它来执行响应的一些操作。最后使得 m 的执行结果为 100。注意:请勿改动 main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:class ClassName implements Runnableint n;_tryThread.sleep(2000);n=100;catch(Exc
13、eption e) public static void main(String args)tryClassName a=new ClassName();_thread1.start();thread1.join();int m=a.n;System.out.println(“m=“+m);catch(Exception e) (分数:40.00)_正确答案:(public void run()Thread thread1=new Thread(a);)解析:解析 本题主要考查 Java 中对线程的创建知识。解答本题的关键是熟练掌握如何创建线程的知识。一般情况下,创建线程的方法是:通过继承 T
14、hread 类创建线程:通过向 Thread()构造方法传递 Runnable 对象来创建线程。在本题中,public void run()声明语句是用来声明线程体的,这是创建一个线程的必须做的。Thread thread1=new Thread(a)语句的功能是通过向 Thread()构造方法传递 Runnable 对象 a 来生成一个对象 thread1。最后程序的输出结果如下图所示。三、3综合应用题(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是计算山顶的高度,计算方法是:该山顶由 a 点量得仰角度数为 a,由 b 点量得仰角度数为 b,且测得 a,b 点之间
15、的距离为 c,求山的高度。要求窗口中有 3 个输入框,分别作为 a、b、c 的输入,一个按钮单击后进行计算,结果显示在另一个文本框中这个文本框不可编辑)。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。源程序文件代码清单如下:import java.io.*;import java.awt.*;import java.awt.event.*;import java.applet.Applet;/*applet code=“ex6_3.class“width=800 height=400/applet*/public class ex6_3
16、extends Applet implements ActionListenerPanel pane=new Panel();Label 11=new Label(“a 点仰角:“);TextField tf1=new TextField(5);Label 12:=new Label(“b 点仰角:“);TextField tf2=new TextField(5);Label 13=new Label(“a,b 之间距离:“);TextField tf3=new TextField(5);Button btn=new Button(“OK“);Label 14=new Label(“山高:“)
17、;TextField tf4=new TextField(20);ex6_3 Obj23_3;public void init()pane.setLayout(new FlowLayout(PlowLayout.LEFT,10,5);pane.add(11);pane.add(tf1);pane.add(12);pane.add(tf2);add(“North“,pane);Panel p2=new Panel();p2.setLayout(new FlowLayout(FlowLayout.LEFT,10,5);p2.add(13);p2.add(tf3);p2.add(btn);btn.a
18、ddActionListener(this);add(“Center“,p2);Panel p3=new Panel();p3.setLayout(new FlowLayout(FlowLayout.LEFT,10,5);p3.add(14);tf4.setEditable(true);p3.add(tf4);add(“South“,p3);Obj23_3=new ex6 3();public void doMessure(double a1,double a2,double a3,TextField tf)double pi=Math.PI,a,b,h;a=al*pi/180.0;b=a2*
19、pi/180.0;h=a3/(1.0/Math.tan(a)-1.0/Math.tan(b);tf.setText(Integer.toString(h);public void actionPerformed(ActionEvent ae)double a,b,c;trya=new Double(tf1.getText().doubleValue();b=new Double(tf2.getText().doubleValue();c=new Double(tf3.getText().doubleValue();obj23_3.doMessure(a,b,c, tf4;catch(Numbe
20、rFormatExceptlon nfe)tf4.setText(“wrong number!“);ex6_3.htmlHTMLHEADTITLEex6_3/TITLE/HEADBODYapplet code=“ex6_3.class“width=800 height=400/applet/BODY/HTML(分数:30.00)_正确答案:(tf4.setEditable(false)h=a3/Math.abs(1.0/Math.tan(a)-1.0/Math.tan(b)tf.setText(Double.toString(h)解析:解析 本题主要考查 Applet 窗口编程和 AWT 基本构件的使用以及事件处理机制,解题关键是设计出计算山高的方法,并且结合事件处理机制,调用该计算方法实现程序的功能。本题中,第 1 处,由于用显示结果的文本框不可编辑,因此参数为 false;第 2 处,需要对分母进行取绝对值操作,否则最后结果会产生负数;第 3 处,由于 h 是 double 类型的变量,做数据类型转换时需要调用 Double 类的 toString()方法。