1、二级 JAVA 机试 48 及答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下列程序中,用户使用 JOptionPane 输入一个二维数组的行数,程序随机生成每一行的列数,并对其赋值。最后显示这个二维数组。请填写横线处的内容。 注意:请勿改动 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import javax.swing.*; public class Examplel_10 public static void main (String argv) String sDimU = JOptionPane. s
2、howInputDialog (“请输入数组的行数“); int iDimU = _ (sDimU); int numbers = new intiDimU ; for(int i = 0; i iDimU; i+) int k = (int) (Math.random() *5+1); numbers i = _; for(int i = O; i iDimU; i+) for(int j = 0; _; j+) numbersi j = (int) (Math.random()*100); System.out.print(numbersi j + “ “); System.out.pri
3、ntln(); System.exit(O); (分数:30.00)_二、B2简单应用题/B(总题数:1,分数:40.00)2.本程序的功能是,根据用户输入的文件名,在相应的文件内容中查找匹配给定模式的字符串,并将这些字符串显示出来。模式串为“href=“”。请填写横线处的内容。 注意:请勿改动 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; import java.util.regex.*; import javax.swing.*; public class Example2_10 public static void main(Stri
4、ng argv) final String patternString = “href/s*=/s*(/“/“*/“|/s)/s*; String fileName ; try System. out. print ( “请输入 html 文件的文件名: “); InputStreamReader in = new InputStreamReader(System.in); BufferedReader imput = new BufferedReader(in); fileName = imput.readLine(); if(fileName.equals(“ “) return; Str
5、ingBuffer buffer = new StringBuffer(); File file = new File(fileName); FileInputStream readfile = new FileInputStream(file); for(int c = 0; (c = readfile.read() != -1; ) buffer.append(char)c); Pattern pattern = Ppile( _ Pattern.CASE_INSENSITIVE); Matcher matcher =_; while (marcher. find () int start
6、 = matcher.start(); int end = matcher.end(); String match = buffer.substring(start, end); System.out.println (match); catch (Exception excption) System. out.println (excption. getMessage (); System.exit(O); (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.下面程序的目的是在屏幕上显示当前目录下的文件信息。文件信息通过表格 JTable 的实例显示。请改正程序中
7、的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import javax.swing.*; import java.util. Date; import javax.swing.table.*; import java.applet.*; import java.io.*; public class Example3_10 Uextends JApplet, JFrame/U public void init() FileModel fm = new FileModel(); JTable jt = new U
8、JTable()/U; jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jt.setColumnSelectionAllowed(true); JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER); public static void main(String args) Example3_10 ft = new Example3_10(); ft.init(); JFrame f = new JFrame(); f.setDefau
9、ltCloseOperation(JFrame.EXIT ON CLOSE); f.getContentPane() .add(ft.getContentPane(); f.setSize (300, 400); f.show(); class FileModel extends AbstractTableModel String columnName = new String “文件名“, “大小“, “最后修改时间“ Object data; public FileModel() this(“.“); public FileModel(String dir) File file = new
10、 File(dir); String files = file.list(); data = new Object files.length columnName.length; for (int i=0; i files.length; i+) File tmp = new File (filesi); datai 0 = tmp.getName(); datai 1 = new Long(tmp.length(); datai 2 = new Date(tmp.lastModified() ); public int UgetColumnNumber/U() return columnNa
11、me.length; public int getRowCount() return data. length; public String getColumnName(int col) return columnNamecol; public Object getValueAt(int row, int col) return datarow col; public Class getColumnClass(int c) return getValueAt (0, c) .getClass(); (分数:30.00)_二级 JAVA 机试 48 答案解析(总分:100.00,做题时间:90
12、分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下列程序中,用户使用 JOptionPane 输入一个二维数组的行数,程序随机生成每一行的列数,并对其赋值。最后显示这个二维数组。请填写横线处的内容。 注意:请勿改动 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import javax.swing.*; public class Examplel_10 public static void main (String argv) String sDimU = JOptionPane. showInputDialog (“请输入数组的行数“); int iDimU
13、 = _ (sDimU); int numbers = new intiDimU ; for(int i = 0; i iDimU; i+) int k = (int) (Math.random() *5+1); numbers i = _; for(int i = O; i iDimU; i+) for(int j = 0; _; j+) numbersi j = (int) (Math.random()*100); System.out.print(numbersi j + “ “); System.out.println(); System.exit(O); (分数:30.00)_正确答
14、案:()解析:Integer,parseInt new intk jnumbersi.length 解析 本题考查知识点:本数据类型包装类的使用,数组的使用 解题思路:首先,JOptionPane 显示了一个可以输入数据的对话框,用户通过该对话框输入对话框的行数。第一个循环是对每一行分配存储空间,第二个循环是对已经分配好空间的二维数据进行赋值,并在赋值的过程中将数组显示出来。 第一个空考察字符串和数值之间的相互转换。每一种基本数据类型的包装类都有各自的方法将字符串转换为相应的基本数据类型,转换为 int 型的方法是IntegerparseInt()。 第二个空考察多维数组的基本概念,多维数组的
15、高维标识低维的名字,但是低维必须分配自己的存储空间。因此,此处需要用“new”关键字对每一行动态分配列空间。 第三空,由于多维数组的高维标识低维的名字,所以高维可以直接用来表示低维部分,使用方法跟普通数组一样。如本题中“numbers0”就表示一个用“numbers0”命名的一维数组。所以此处应该填“j numbersilength”。二、B2简单应用题/B(总题数:1,分数:40.00)2.本程序的功能是,根据用户输入的文件名,在相应的文件内容中查找匹配给定模式的字符串,并将这些字符串显示出来。模式串为“href=“”。请填写横线处的内容。 注意:请勿改动 main()主方法和其他已有语句内
16、容,仅在横线处填入适当语句。 import java.io.*; import java.util.regex.*; import javax.swing.*; public class Example2_10 public static void main(String argv) final String patternString = “href/s*=/s*(/“/“*/“|/s)/s*; String fileName ; try System. out. print ( “请输入 html 文件的文件名: “); InputStreamReader in = new InputSt
17、reamReader(System.in); BufferedReader imput = new BufferedReader(in); fileName = imput.readLine(); if(fileName.equals(“ “) return; StringBuffer buffer = new StringBuffer(); File file = new File(fileName); FileInputStream readfile = new FileInputStream(file); for(int c = 0; (c = readfile.read() != -1
18、; ) buffer.append(char)c); Pattern pattern = Ppile( _ Pattern.CASE_INSENSITIVE); Matcher matcher =_; while (marcher. find () int start = matcher.start(); int end = matcher.end(); String match = buffer.substring(start, end); System.out.println (match); catch (Exception excption) System. out.println (
19、excption. getMessage (); System.exit(O); (分数:40.00)_正确答案:()解析:patternString patternmatcher(buffer) 解析 本题考查知识点:输入输出流和正则表达式解题思路:程序首先使用 InputStreamReader 的实例“in”从标准输入中获取用户输入的文件名,并将结果存放在“fileName”字符串中。if 语句用来判断用户输入的文件名是否为空,如果为空则退出程序,不做任何处理。然后根据文件名读取相应的文件内容存放在 StringBuffer 的实例“buffer”中。然后建立起与正则表达式对应的模式对象
20、“pattem”,并与“buffer”帮定,生成一个匹配器“matcher”。最后使用 while 循环查找到相应的字符串。 Patter 的 compile 方法用来将正则表达式编译成模式对象。compile 方法的第一个参数是正则表达式字符串,所以第一个空的答案是“patternString”。 第二个空用来建立一个匹配器。模式对象建立匹配器的方法是 matcher(string),其中 string 是需要做模式匹配的兑现,本题中需要做模式匹配的对象是“buffer”。三、B3综合应用题/B(总题数:1,分数:30.00)3.下面程序的目的是在屏幕上显示当前目录下的文件信息。文件信息通过
21、表格 JTable 的实例显示。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import javax.swing.*; import java.util. Date; import javax.swing.table.*; import java.applet.*; import java.io.*; public class Example3_10 Uextends JApplet, JFrame/U public void init() FileModel fm = new FileMode
22、l(); JTable jt = new UJTable()/U; jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jt.setColumnSelectionAllowed(true); JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER); public static void main(String args) Example3_10 ft = new Example3_10(); ft.init(); JFrame f = ne
23、w JFrame(); f.setDefaultCloseOperation(JFrame.EXIT ON CLOSE); f.getContentPane() .add(ft.getContentPane(); f.setSize (300, 400); f.show(); class FileModel extends AbstractTableModel String columnName = new String “文件名“, “大小“, “最后修改时间“ Object data; public FileModel() this(“.“); public FileModel(Strin
24、g dir) File file = new File(dir); String files = file.list(); data = new Object files.length columnName.length; for (int i=0; i files.length; i+) File tmp = new File (filesi); datai 0 = tmp.getName(); datai 1 = new Long(tmp.length(); datai 2 = new Date(tmp.lastModified() ); public int UgetColumnNumb
25、er/U() return columnName.length; public int getRowCount() return data. length; public String getColumnName(int col) return columnNamecol; public Object getValueAt(int row, int col) return datarow col; public Class getColumnClass(int c) return getValueAt (0, c) .getClass(); (分数:30.00)_正确答案:()解析:exten
26、ds JFrame JTable(fm) getColumnCount 解析 本题考查知识点:JTable 构件的使用、小应用程序的安全机制解题思路:FileModel 定义了一个 JTable 的模型,在这个模型中,“File file=new File(dir)”语句获得当前目录下的文件信息,然后将这些文件信息存储在模型的“data”变量中。 Applet 的沙箱模型规定,未授权的小应用程序不能访问本地资源,当然也就不能读、写本地计算机的文件系统。本程序需要获得当前目录的信息,因此本题程序只能作为应用程序运行,所以第一处下划线应该去掉 JApplet。 JTable 的主要功能是将数据以二维表格的方式显示出来。本题采用的是 MVC 模式,FileModel 实现了表格的模型。第二条下划线处使用 JTable 的构造方法将模型赋予表格“jt”。 AbstractTableModel 所有的抽象方法都需要实现。第三条下划线处的方法在功能上与抽象方法 getColumnCount()相同,但是并没有实现该抽象方法。因此需要更正。