1、二级 JAVA 机试 54 及答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下列 Application 程序中,指定 as 为字符串数组类型,先创建一个 HashSet 对象并赋值,然后在屏幕输出 s,请将程序补充完整。 import java.util.*; public class test17_1 public static void main(String args) _s=new HashSet(); s.add(“Hello“); s.add(“World“); s.add(new Character(我); s.a
2、dd(new Integer(23); s.add(“Hello“); _as=“W“,“o“,“r“,“1“,“d“; s.add(as); s.add(null); s.add(new Integer(23); s.add(null); System.out.println(_);(分数:30.00)_二、B2简单应用题/B(总题数:1,分数:40.00)2.请完成下列 Java 程序。程序的功能是复制文件并显示文件,将每个字符读入,并写入另一个文件,同时显示出来(注意:在本题中,当前目录下的 README.txt 文件打印在屏幕上,并写入另一个文件temp.txt 中)。 注意:请勿改动
3、 main()主方法和其他已有的语句内容,仅在下画线处填人适当的语句。 import java.io.*; public class FileCopyBy public static void main(String args) try FileReader input=new FileReader(“README.txt“); FileWriter output=new FileWriter(“temp.txt“);int c=input.read(); while(_) _ System.out.print(char)c); c=input.read(); input.close(); o
4、utput.close(); catch(IOException e) Systemoutprintln(e); (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是进行整数加法运算,要求有 3 个文本区域,2 个作为输入, 1 个作为输出,并且由异常处理来判断输入的数字是否为整型,如果是则进行运算,否则抛出异常并显示在第 3个文本区域中。请改正程序中的错误(有下画线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import java.awt.event.*
5、; import java.applet.Applet; /* applet code=“ex17_3.class“width=800 height=400 /applet */ public class ex17_3 extends Applet implements ActionListener TextField tf1=new TextField(5);TextField tf2=new TextField(5); TextField tf3=new TextField(5); Label l1=new Label(“+“); Label l2=new Label(“=“); Butt
6、on btn=new Button(“相加“); public void init() Uadd(l1);/U add(tf1); add(tf2); add(l2); add(tf3); add(btn); btn.addActionListener(this); public void actionPerformed(ActionEvent ae) try int a=Integer.parseInt(tfi.getText(); int b=Integer.parseInt(tf2.getText(); int x=a+b; tf2.setText(Integer.toString(x)
7、; catch(NumberFormatException nfe) tf3.setText(“error!“); ex17_3.html HTML HEAD TITLEex17_3/TITLE /HEAD BODY applet code=“ex17_3.class“width=800 height=400 /applet /BODY /HTML(分数:30.00)_二级 JAVA 机试 54 答案解析(总分:100.00,做题时间:90 分钟)一、B1基本操作题/B(总题数:1,分数:30.00)1.下列 Application 程序中,指定 as 为字符串数组类型,先创建一个 HashS
8、et 对象并赋值,然后在屏幕输出 s,请将程序补充完整。 import java.util.*; public class test17_1 public static void main(String args) _s=new HashSet(); s.add(“Hello“); s.add(“World“); s.add(new Character(我); s.add(new Integer(23); s.add(“Hello“); _as=“W“,“o“,“r“,“1“,“d“; s.add(as); s.add(null); s.add(new Integer(23); s.add(n
9、ull); System.out.println(_);(分数:30.00)_正确答案:()解析:Set String s二、B2简单应用题/B(总题数:1,分数:40.00)2.请完成下列 Java 程序。程序的功能是复制文件并显示文件,将每个字符读入,并写入另一个文件,同时显示出来(注意:在本题中,当前目录下的 README.txt 文件打印在屏幕上,并写入另一个文件temp.txt 中)。 注意:请勿改动 main()主方法和其他已有的语句内容,仅在下画线处填人适当的语句。 import java.io.*; public class FileCopyBy public static v
10、oid main(String args) try FileReader input=new FileReader(“README.txt“); FileWriter output=new FileWriter(“temp.txt“);int c=input.read(); while(_) _ System.out.print(char)c); c=input.read(); input.close(); output.close(); catch(IOException e) Systemoutprintln(e); (分数:40.00)_正确答案:()解析:c! = -1 output.
11、write(c);三、B3综合应用题/B(总题数:1,分数:30.00)3.下面是一个 Applet 程序,其功能是进行整数加法运算,要求有 3 个文本区域,2 个作为输入, 1 个作为输出,并且由异常处理来判断输入的数字是否为整型,如果是则进行运算,否则抛出异常并显示在第 3个文本区域中。请改正程序中的错误(有下画线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 import java.awt.*; import java.awt.event.*; import java.applet.Applet; /* applet code=“ex17_3.class“w
12、idth=800 height=400 /applet */ public class ex17_3 extends Applet implements ActionListener TextField tf1=new TextField(5);TextField tf2=new TextField(5); TextField tf3=new TextField(5); Label l1=new Label(“+“); Label l2=new Label(“=“); Button btn=new Button(“相加“); public void init() Uadd(l1);/U add
13、(tf1); add(tf2); add(l2); add(tf3); add(btn); btn.addActionListener(this); public void actionPerformed(ActionEvent ae) try int a=Integer.parseInt(tfi.getText(); int b=Integer.parseInt(tf2.getText(); int x=a+b; tf2.setText(Integer.toString(x); catch(NumberFormatException nfe) tf3.setText(“error!“); ex17_3.html HTML HEAD TITLEex17_3/TITLE /HEAD BODY applet code=“ex17_3.class“width=800 height=400 /applet /BODY /HTML(分数:30.00)_正确答案:()解析:add(tf1) add(l1) tf3.setText(Integer.toString(x)