1、计算机二级 JAVA-31 及答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.10 位同学参加某次团队测试,要求每位同学都必须及格、同时团队平均分不少于 80 分,整个团队才能够通过。每位同学的成绩可以通过随机数产生(0100)。请在程序的每条横线处填写一条语句,是程序的功能完整。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 源程序文件代码清单如下: _; public class While public static void main(String args) int fenshu=60
2、; int sumfenshu=0; int i=1; while(fenshu=60) System. out. print in (fenshu+“ “); sumfenshu+=fenshu; i+; System.out.println(); if(_) System. out. println ( “团队通过“) else System. out.println (“团队测试不通过“); (分数:30.00)_二、B2简单应用题/B(总题数:1,分数:40.00)2.请完成以下程序,首先由一个类 Example2_3 实现 Serializable 接口,并有三个成员变量,分别为 i
3、nt型、double 型和 String 型,可以用 toString 的方法显示这三个成员变量。在 main 方法中创建这个Example2_3 的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为 TheSerial.data 的文件,并显示成员变量。最后从文件 TheSerial.data 中读出三个成员变量并显示出来。 注意:请勿改动 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable private int intVal
4、ue; private double doubleValue; private String string; TheSerial() intValue = 123; doubleValue = 12.34; string = “Serialize Test“; public void setDouble(double d) doubleValue = d; public void setInt(int i) intValue = i; public void setString(String s) string = s; public String toString() return(“int
5、=“+intValue+“double=“+doubleValue+“ string=“+string); public class Example2_3 public static void main(String argv) TheSerial e1 = new TheSerial(); TheSerial e2; try e1.setInt(Integer.parseInt(argv0); e1.setDouble(Double.parseDouble(argv1); e1.setStringargv2); catch(Exception e) e1.setString(e.getMes
6、sage); System.out.println(e1); try FileOutputStream oS = new FileOutputStream(“TheSerial.data“); ObjectOutputStream oIS = new ObjectOutputStream(oS); _; catch(IOException ioException) System.out.println(ioException.getMessage(); try FileInputStream iS = new FileInputStream(“TheSerial. data“); Object
7、InputStream oIS = new ObjectInputStream(iS); _ System.out.println(e2); catch(IOException ioException) System.out.println(ioException.getMessage(); catch(ClassNotFoundException cnfException) System.out.println(cnfException.getMessage(); (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.本程序的功能是监听对于菜单项和工具条按钮的操作。
8、主窗口中有菜单、工具条和一个文字标签,菜单中有“文件”项,“文件”菜单中包括菜单项“新建”、“保存”、“打印”、“页面设置”和“退出”,工具条上包括按钮“新建”、“保存”、“打印”、“页面设置”和“退出”。单击任何一个菜单项或任何一个工具条按钮,文字标签都会显示哪个构件被选中。请更正题中带下划线的部分。运行结果如下图所示。 (分数:30.00)_计算机二级 JAVA-31 答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.10 位同学参加某次团队测试,要求每位同学都必须及格、同时团队平均分不少于 80 分,整个团队才能够通过。每位同学
9、的成绩可以通过随机数产生(0100)。请在程序的每条横线处填写一条语句,是程序的功能完整。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 源程序文件代码清单如下: _; public class While public static void main(String args) int fenshu=60; int sumfenshu=0; int i=1; while(fenshu=60) System. out. print in (fenshu+“ “); sumfenshu+=fenshu; i+; System.out.println(); i
10、f(_) System. out. println ( “团队通过“) else System. out.println (“团队测试不通过“); (分数:30.00)_正确答案:(import javamath.*; Mathrandom()*100 (i=10) class TheSerial implements Serializable private int intValue; private double doubleValue; private String string; TheSerial() intValue = 123; doubleValue = 12.34; stri
11、ng = “Serialize Test“; public void setDouble(double d) doubleValue = d; public void setInt(int i) intValue = i; public void setString(String s) string = s; public String toString() return(“int=“+intValue+“double=“+doubleValue+“ string=“+string); public class Example2_3 public static void main(String
12、 argv) TheSerial e1 = new TheSerial(); TheSerial e2; try e1.setInt(Integer.parseInt(argv0); e1.setDouble(Double.parseDouble(argv1); e1.setStringargv2); catch(Exception e) e1.setString(e.getMessage); System.out.println(e1); try FileOutputStream oS = new FileOutputStream(“TheSerial.data“); ObjectOutpu
13、tStream oIS = new ObjectOutputStream(oS); _; catch(IOException ioException) System.out.println(ioException.getMessage(); try FileInputStream iS = new FileInputStream(“TheSerial. data“); ObjectInputStream oIS = new ObjectInputStream(iS); _ System.out.println(e2); catch(IOException ioException) System
14、.out.println(ioException.getMessage(); catch(ClassNotFoundException cnfException) System.out.println(cnfException.getMessage(); (分数:40.00)_正确答案:(oOS.writeObject(e1) e2=(TheSerial)oIS.readObject()解析:解析 本题主要考查串行化相关的方法和实现。解题中首先要掌握串行化的基本过程和反串行化的过程。串行化过程首先要创建一个输出流 FileOutputStream,通过该类的实例对文件进行访问,然后创建一个 O
15、bJectOutput Stream 对象,通过 writeObject()方法来实现对象的序列化。第一个空就是使用writeObject()实现序列化。 反序列化过程中用 FileInputStream 对象建立读取文件的连接,并使用该对象创建一个 ObiectInputStream 的实例,这个 ObjectInput Stream 实例的 readObject()方法就可以实现对象的反序列化。第二个空就是使用 readObject()实现反序列化。三、B3综合应用题/B(总题数:1,分数:30.00)3.本程序的功能是监听对于菜单项和工具条按钮的操作。主窗口中有菜单、工具条和一个文字标签
16、,菜单中有“文件”项,“文件”菜单中包括菜单项“新建”、“保存”、“打印”、“页面设置”和“退出”,工具条上包括按钮“新建”、“保存”、“打印”、“页面设置”和“退出”。单击任何一个菜单项或任何一个工具条按钮,文字标签都会显示哪个构件被选中。请更正题中带下划线的部分。运行结果如下图所示。 (分数:30.00)_正确答案:(setTitle(titleText) JMenu file=new JMenu(“文件“) cp.add(tb,BorderLayout.NORTH)或cp.add(“North“,tb)解析:解析 本题考查知识点:类的继承、AWT 和 Swing 的区别、布局管理器的使用
17、。解题思路:程序中声明了一个菜单构件、一个工具栏构件,声明了 5 个事件添加到菜单和工具栏中。子类继承父类以后,可以调用父类的 public、protected 成员变量和成员方法。子类还可以使用变量 super,访问已经被隐藏或被覆盖了的父类成员变量和方法。题中,JFrame 带一个参数的构造方法已经被子类 advance 的构造方法覆盖。原题打算使用 super 调用父类的构造方法,为框架添加变量 titleText 定义的名字。这样的操作可以实现,但是 super 变量要求必须写在其所在的方法的第一行,而本题中 super 变量前还有语句setDefaultCloseOperation。
18、因此在第 1 条下划线的位置不能使用 super,只能调用 set Title()方法,该方法可以为框架设置名字。file 是一个 AWT 构件 Menu 的对象,而它所在的容器 mb 是一个 Swing 的JMenuBar 对象,两类不同的构件,实现的方法不一样,不能使用 add 方法进行添加。因此在第 2 条下划线处需要把 file 的类型改为 JMenu。容器定义了 BorderLayout 布局管理器以后,如果添加构件时不指明添加的位置,则会把该构件添加到 Center 的位置。由于本题中不需要把名为 actionInfo 的 JLabel 构件添加到 Center 的位置,所以在第 3 条下划线处,添加 tb 构件时必须指明位置参数。本题答案中根据习惯把工具栏添加到容器的顶部,当然,添加到容器的 South、East 或 West 位置也都正确。