1、二级 JAVA 机试-30 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.当按下鼠标时,一个圆不断地增大半径。注意:请勿改动已有语句内容,仅在下划线处填入适当的语句。import java.applet.*;import java.awt.*;import java.awt.event.*;public class ex1 extends applet implements MouseListenerTextField text;int x;public void init()x=6;text=new TextField(30);ad
2、d(text);addMouseListener(this);public void paint(Graphics g)x= x+2;g.drawOval(10,10,x,x);public void mousePressed(MouseEvent e)Text.setText(“鼠标按下的位置是:“+e.getX()+“,“e.getY();repaint();public void mouseReleased(MouseEvent e)Text.setText(“鼠标松开的位置是:“+e.getX()+“,“e.getY();repaint();public void mouseEnter
3、ed(MouseEvent e)Text.setText(“鼠标进来的位置是:“+e.getX()+“,“e.getY();repaint();public void mouseExited(MouseEvent e)Text.setText(“鼠标退出的位置是:“+e.getX()+“,“e.getY();_;(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.下列程序要求将 source.txt 文件中的字符,通过文件输入/输出流复制到另一个 dest.txt 文件中。请将程序补充完整。注意:不改动程序结构,不得增行或删行。import java.io.*;publi
4、c class ex2public static void main(String args) throws IOExceptionFile inputFile;File outputFile;FileInputStream in;FileOutputStream out;int c;inputFile=new File(“source.txt“);outputFile=new File(“dest.txt“);in=new FileInputStream(inputFile);_(outputFile);while(c=in.read()!=-1)_;in.close();out.close
5、();(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.下列程序是整数除法计算的程序,要求在出现异常时,能抛出异常信息。考虑两种异常:(1)输入非数字除数(2)输入除法分母为零该程序运行的三种结果状态如下:(1)输入两个合法整数(2)输入非数字除数(3)输入除数为零请将程序填写完整。注意:不改动程序结构,不得增行或删行。import java.awt.event.*;public class ex3 extends _implements ActionListenerprivate JTextField input1,input2, output;private int
6、 number1,number2;private double result;public ex3()_(“示范异常“);Container c=getContentPane();c.setLayout(new GridLayout(3,2);c.add(new JLabe1(“输入分子“,SwingConstants.RIGHT);input1=new JTextField(8);c.add(input1);c.add(new JLabe1(“输入分母和回车“,SwingConstants.RIGHT);input2=new JTextField(8);c.add(input2);input
7、2.addActionListener(this);c.add(new JLabe1(”计算结果”,SwingConstants.RIGHT);output=new JTextField();c.add(output);setSize(400,100);show();public void actionPerformed(ActionEvent e)DecimalFormat precision3=new DecimalFormat(“0.000“);output.setText(“);/空的 JTextField 输出trynumber1=Integer.parseInt(input1.ge
8、tText();number2=Integer.parseInt(input2.getText();result=quotient(number1,number2);_;catch (NumberFormatException nfe)_(this,“你必须输入两个整数“,“非法数字格式“,JOptionPane.ERROR_MESSAGE);catch (Exception dbz)_(this,“除法异常“,“除数为零“,JOptionPane.ERROR_MESSAGE);/定义求商的方法,如遇除数为零时,能抛出异常。public double quotient(int numerato
9、r,int denominator)throws Exceptionif(denominator= =0)throw new Exception();return(double) numerator/denominator;public static void main(String args)Java3 app=new Java3();app.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)e.getWindow().dispose();System.exit(0););(分数:30.0
10、0)_二级 JAVA 机试-30 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.当按下鼠标时,一个圆不断地增大半径。注意:请勿改动已有语句内容,仅在下划线处填入适当的语句。import java.applet.*;import java.awt.*;import java.awt.event.*;public class ex1 extends applet implements MouseListenerTextField text;int x;public void init()x=6;text=new TextField(30);
11、add(text);addMouseListener(this);public void paint(Graphics g)x= x+2;g.drawOval(10,10,x,x);public void mousePressed(MouseEvent e)Text.setText(“鼠标按下的位置是:“+e.getX()+“,“e.getY();repaint();public void mouseReleased(MouseEvent e)Text.setText(“鼠标松开的位置是:“+e.getX()+“,“e.getY();repaint();public void mouseEnt
12、ered(MouseEvent e)Text.setText(“鼠标进来的位置是:“+e.getX()+“,“e.getY();repaint();public void mouseExited(MouseEvent e)Text.setText(“鼠标退出的位置是:“+e.getX()+“,“e.getY();_;(分数:30.00)_正确答案:(repaint()解析:讲解 本题考查对鼠标事件的掌握,鼠标事件有很多种,最常用的有 mousePressed(MouseEvent)、mouseReleased(MouseEvent)、mouseEntered(MouseEvent)、mouse
13、Exited(MouseEvent)、 mouseClicked(MouseEvent e)等。MouseEvent 有几个重要的方法,getX()、getY()、getModifiers()、 getClickCount()、getSource()等。本题目中的空白处应该填写 repaint()。二、2简单应用题(总题数:1,分数:40.00)2.下列程序要求将 source.txt 文件中的字符,通过文件输入/输出流复制到另一个 dest.txt 文件中。请将程序补充完整。注意:不改动程序结构,不得增行或删行。import java.io.*;public class ex2public
14、static void main(String args) throws IOExceptionFile inputFile;File outputFile;FileInputStream in;FileOutputStream out;int c;inputFile=new File(“source.txt“);outputFile=new File(“dest.txt“);in=new FileInputStream(inputFile);_(outputFile);while(c=in.read()!=-1)_;in.close();out.close();(分数:40.00)_正确答案
15、:(out=new FileOutputStreamout.write(c)解析:讲解 本题主要考查 Java 中的 IO 操作。第一空应填写 out=new FileOutputStream。Java 中要将一个文件中的内容写入到另一个文件中,需要知道文件读写操作。程序中已经声明了 FileInputStream 的对象 in,套接 File 类的对象 inputFile 来进行读入的操作,我们还需要声明 FileOutputStream 类的对象 out,来套接 File 类的对象 outputFile 进行读出的操作。第二空应填写 out.write(c)。程序此处要求进行文字写入。在程
16、序的前一个步骤,已经调用 FileInputStream 类的 read 方法,将文件中的内容以单字节的方式读入到流中,所以我们在这里要调用 FileOutputStream 类的 write 方法,将流中的内容写出。三、3综合应用题(总题数:1,分数:30.00)3.下列程序是整数除法计算的程序,要求在出现异常时,能抛出异常信息。考虑两种异常:(1)输入非数字除数(2)输入除法分母为零该程序运行的三种结果状态如下:(1)输入两个合法整数(2)输入非数字除数(3)输入除数为零请将程序填写完整。注意:不改动程序结构,不得增行或删行。import java.awt.event.*;public c
17、lass ex3 extends _implements ActionListenerprivate JTextField input1,input2, output;private int number1,number2;private double result;public ex3()_(“示范异常“);Container c=getContentPane();c.setLayout(new GridLayout(3,2);c.add(new JLabe1(“输入分子“,SwingConstants.RIGHT);input1=new JTextField(8);c.add(input1
18、);c.add(new JLabe1(“输入分母和回车“,SwingConstants.RIGHT);input2=new JTextField(8);c.add(input2);input2.addActionListener(this);c.add(new JLabe1(”计算结果”,SwingConstants.RIGHT);output=new JTextField();c.add(output);setSize(400,100);show();public void actionPerformed(ActionEvent e)DecimalFormat precision3=new
19、DecimalFormat(“0.000“);output.setText(“);/空的 JTextField 输出trynumber1=Integer.parseInt(input1.getText();number2=Integer.parseInt(input2.getText();result=quotient(number1,number2);_;catch (NumberFormatException nfe)_(this,“你必须输入两个整数“,“非法数字格式“,JOptionPane.ERROR_MESSAGE);catch (Exception dbz)_(this,“除法异
20、常“,“除数为零“,JOptionPane.ERROR_MESSAGE);/定义求商的方法,如遇除数为零时,能抛出异常。public double quotient(int numerator,int denominator)throws Exceptionif(denominator= =0)throw new Exception();return(double) numerator/denominator;public static void main(String args)Java3 app=new Java3();app.addWindowListener(new WindowAda
21、pter()public void windowClosing(WindowEvent e)e.getWindow().dispose();System.exit(0););(分数:30.00)_正确答案:(JFrameSuperoutput.setText(precision3.format(result);JOptionPane.showMessageDialogJOptionPane.showMessageDialog)解析:讲解 本题综合考查类的继承、图形用户界面和数据类型转换。第 1 空应填写 JFrame。通过前面的关键字 extends,我们很明确地知道这里应该填写一个类的名称。
22、从下面的程序中,我们发现使用了Container 以及 JTextField,并且 Container 调用了 getContentPane 方法,所以这里应该继承的是JFrame 类。第 2 空应填写 super。既然本类继承了 JFrame 类,那么我们显示一个名为“示范异常”的JFrame 类,只需要调用 super 关键字。第 3 空应填写 output.setText(precision3.format(result);。在程序的这一步,我们需要在文本框中显示信息,所以要调用文本框 output 的 setText 方法。但是,我们从程序的上一个步骤得到的结果 result 是一个 double 类型的数据,所以要进行数据转换,在这里,我们涉及到了 DecimalFormat 类,它是 NumberFormat 的一个子类,可以提供格式化输出数据类型的功能。DecimalFormat 类的 format 方法可以返回字符串类型的数据。第四空和第五空都应填写JOptionPane.showMessageDialog。显示信息对话框调用 JOptionPane 的 showMessageDialog 方法。