1、二级 JAVA 机试-227 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.下面程序的功能是从键盘读取一行文本并将该文本存储到文件中。当用户运行该程序时,需要输入一行文本并按下回车键。然后在 DOS 系统提示符处输入 type file.txt 并按下 Enter 键,这时屏幕会显示type file.txt 的内容。通过这个命令可以验证该程序的功能。请在每条横线处填写一条语句,使程序的功能完整。注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。源程序文件代码清单如下:import java.io.*;
2、public class DoFilepublic static void main(String args)byte buffername=_80;trySystem.out.println(“/nEnter a line to be saved to disk:“);int bytesbleck= _;FileOutputStream f=new FileOutputStream(“file.txt“);_;catch(Exception e)e.printStackTrace();(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.请完成下列 Java 程序。程序的
3、功能是显示用户在命令行方式下指定的任意驱动器文件夹的内容。提示:public string()list();/将文件夹中所有文件名保存在字符数组中返回。注意:请勿改动 main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:import java.io.*;public class FindDirectoriespublic static void main(String args)if(args.length=0)args=new String()“;try_;String fileName=pathName.list();for(int i=0;ifil
4、eName.length;i+)File f=new File(pathName.getPath(),fileNamei);if(_)System.out.println(f.getCanonicalPath();main(new Stringf.getPath();catch(IOException e)e.printStackTrace();(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是有两个按钮,分别为 First 和 Second,以及一个 Label 构件。要求单击 First 时能在 Label 中显示出“Comm
5、and:First“,而单击 Second 时能显示出“Command:Second“,要求只能重载一次 actionPerformed()方法,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。源程序文件代码清单如下:import java.awt.*;import java.awt.event.*;import java.applet.*;/*applet code=ex04_3.class width=800 height=400/applet*/Public class ex04_3 extends Applet implements
6、 ActionListenerprivate String str=“ok“;private Label l;private Button btn;public void init()setLayout(null);l=new Label(str);l.reshape(10,10,100,30);add(l);btn=new Button(“First“);btn.reshape(10,50,60,20);l.addActionListene(this);add(btn);btn=new Button(“Second“);btn.reshape(10,100,60,20);btn.setAct
7、ionCommand(“First“);btn.addActionListener(this);add(btn);public void actionPerformed(ActionEvent ae)str=“Command:“+ae.getActionCommand();btn.setText(str);ex04_3.htmlHTMLHEADTITLEex04_3/TITLE/HEADBODYapplet code=“ex2_3.class“ width=800 height=400/applet/BODY/HTML(分数:30.00)_二级 JAVA 机试-227 答案解析(总分:100.
8、00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.下面程序的功能是从键盘读取一行文本并将该文本存储到文件中。当用户运行该程序时,需要输入一行文本并按下回车键。然后在 DOS 系统提示符处输入 type file.txt 并按下 Enter 键,这时屏幕会显示type file.txt 的内容。通过这个命令可以验证该程序的功能。请在每条横线处填写一条语句,使程序的功能完整。注意:请勿改动 main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。源程序文件代码清单如下:import java.io.*;public class DoFilepublic st
9、atic void main(String args)byte buffername=_80;trySystem.out.println(“/nEnter a line to be saved to disk:“);int bytesbleck= _;FileOutputStream f=new FileOutputStream(“file.txt“);_;catch(Exception e)e.printStackTrace();(分数:30.00)_正确答案:(new byteSystem.in.read(buffername)f.write(buffername,0,bytesblock
10、)解析:解析 本题主要考查基本数据类型、对文件的操作知识。解答本题的关键是熟练掌握基本数据类型、对文件的操作知识。在本题中,byte buffemame=byte100;语句的功能是定义含 100 个整型元素的数组 buffername, System.in.read(buffername);语句的功能是获得标准控制台的输入数据,f.write(buffemame,0, bytesblock)语句的功能是将从标准控制台获得的输入数据写入文件输出流中。二、2简单应用题(总题数:1,分数:40.00)2.请完成下列 Java 程序。程序的功能是显示用户在命令行方式下指定的任意驱动器文件夹的内容。提
11、示:public string()list();/将文件夹中所有文件名保存在字符数组中返回。注意:请勿改动 main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。源程序文件代码清单如下:import java.io.*;public class FindDirectoriespublic static void main(String args)if(args.length=0)args=new String()“;try_;String fileName=pathName.list();for(int i=0;ifileName.length;i+)File f=new Fil
12、e(pathName.getPath(),fileNamei);if(_)System.out.println(f.getCanonicalPath();main(new Stringf.getPath();catch(IOException e)e.printStackTrace();(分数:40.00)_正确答案:(File pathName=new File(args0)f.isDirectory()解析:解析 本题主要考查文件操作的基本知识。解答本题的关键是熟练掌握对文件操作的基本知识。其中,File pathName=new File(args0)语句的功能是生成 File 类对象
13、pathName; if(f.isDirectory()语句的功能是判断生成的 f 对象是否为文件夹。三、3综合应用题(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是有两个按钮,分别为 First 和 Second,以及一个 Label 构件。要求单击 First 时能在 Label 中显示出“Command:First“,而单击 Second 时能显示出“Command:Second“,要求只能重载一次 actionPerformed()方法,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。源程序文件代码清单如
14、下:import java.awt.*;import java.awt.event.*;import java.applet.*;/*applet code=ex04_3.class width=800 height=400/applet*/Public class ex04_3 extends Applet implements ActionListenerprivate String str=“ok“;private Label l;private Button btn;public void init()setLayout(null);l=new Label(str);l.reshape
15、(10,10,100,30);add(l);btn=new Button(“First“);btn.reshape(10,50,60,20);l.addActionListene(this);add(btn);btn=new Button(“Second“);btn.reshape(10,100,60,20);btn.setActionCommand(“First“);btn.addActionListener(this);add(btn);public void actionPerformed(ActionEvent ae)str=“Command:“+ae.getActionCommand
16、();btn.setText(str);ex04_3.htmlHTMLHEADTITLEex04_3/TITLE/HEADBODYapplet code=“ex2_3.class“ width=800 height=400/applet/BODY/HTML(分数:30.00)_正确答案:(btn.addActionListener(this)btn.setActionCommand(“second“)l.setText(str)解析:解析 本题主要考查 Java 语言中高级事件 AcfionEvent 和 AWT 基本构件 Label 的常用方法的使用。解题关键是熟练掌握动作事件 ActionEvent 和 Label 构件的常用方法。在本题中,第 1 处,明确注册的事件监听器是监听按钮的,而不是 Label;第 2 处,调用 ActionEvent 的 setActionCommand()方法改变了 ActionCommand,使按下 Second 按钮时显示 “Command:second“,而不是“Command:First“;第 3 处,调用 Label 的 setText()方法,而不是 Button 的方法。程序运行结果如下图所示。