1、(A)二级 JAVA 笔试-11 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题利用递归方法求前 n 个自然数的和(n=10)。public class java1public static void main(Stringargs)int sum=add(10);System. out. println(“1+2+9+10=“+sum);pubhc static int add(_)if(n=1)_;else_;(分数:30.00)填空项 1:_二、简单应用题(总题数:1,分数:30.00)2.本题中定义了一个简单的计算器,可以进
2、行基本的四则运算。程序中包含 16 个按钮用来表示09、+、-、*、/、=运算符和小数点,程序顶部的文本框用来显示操作数以及结果。import java. awt. * ;import java. awt. event. * ;import javax. swing. * ;public class java2public static void main(String args)tryUIManager. setLookAndFeel(UIManager. getSystemLookAndFeelClassName();catch (Exception e)JFrame frame=new
3、CalculatorFrame();frame. show();class CalculatorPanel extends JPanel implements ActionListener private JTextField display;private JButton btn;private double arg=0;private String op=“= “;private boolean start=true;public CalculatorPanel()setLayout(new BorderLayout();dispIay=new JTextField(“0“);displa
4、y, setEditable(false);add(display, “North“);JPanel p=new JPanel();p. setLayout(new GridLayout(4,4);String buttons=“789/456*123-0.=+“;for (int i=0; ibuttons, length(); i+)btn=new JButton(buttons. substring(i, i+1);p. add(btn);_;add(p, “Center“);public void actionPerformed(ActionEvent evt)String s=evt
5、. getActionCommand();if(0=s. charAt(0) else display, setText(display, getText()+s);start=false;elseif (start)if (s. equals(“-“)display. setText(s);start=false;else op=s;elsedouble x=_;calculate(x);op=s;start=true;public void calculate(double n)if (op. equals(“+“) arg+=n;else if (op. equals(“-“) arg-
6、= n;else if (op. equals(“*“) arg *=n;else if (op. equals(“/“) arg /=n;else if (op. equals(“=“) arg=n;display, setText(“+arg);class CalculatorFrame extends JFramepublic CalculatorFrame()setTitle(“java2“);setSize(220,180);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Sys
7、tem. exit(0););Container contentPane=getContentPane();contentPane, add(new CalculatorPanel();(分数:30.00)填空项 1:_三、综合应用题(总题数:1,分数:40.00)3.本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。import java. awt. * ;import java. awt. event. * ;import java. awt. geom. *
8、 ;import javax. swing. * ;import javax. swing, event. * ;public class java3public static void main(String args)TextTestFrame frame=new TextTestFrame();frame. setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);frame. show();class TextTestFrame extends JFramepublic TextTestFrame()setTitle(“java3“);setSi
9、ze(DEFAULT_WIDTH, DEFAULT_HEIGHT);Container contentPane=getContentPane();DocumentListener listener=new DocumentListen-er();JPanel panel=new JPanel();hourField=new JTextField(“12“,3);panel, add(hourField);hourField, getDocument (). addDocumentListener(this);minuteField=new JTextField(“00“,3);panel, a
10、dd(minuteField);minuteField, getDocument(), addDocumentListener(listener);contentPane, add ( panel, BorderLayout. SOUTH);clock=new ClockPanel();contentPane, add (clock, BorderLayout. CENTER);public void setClock()tryint hours= Integer. parseInt (hourField. getText(). trim();int minutes= Integer. par
11、selnt(minuteField, getText(), trim();clock, setTime(hours, minutes);catch (NumberFormatException e)public static final int DEFAULT WIDTH=300;public static final int DEFAULT_HEIGHT=300;private JTextField hourField;private JTextField minuteField;private ClockPanel clock;private class clockFieldListene
12、r extends DocumentListenerpublic void insertUpdate(DocumentEvent e) setClock();public void removeUpdate(DocumentEvent e) setClock();public void changedUpdate(DocumentEvent e) class ClockPanel extends JPanelpublic void paintComponent(Graphics g)super, paintComponent(g);Graphics2D g2=(Graphics2D)g;Ell
13、ipse2D circle=new Ellipse2D. Double(0,0,2 * RADIUS, 2 * RADIUS);g2. draw(circle);double hourAngle=Math. toRadians(90-360 * minutes/(12 * 60);drawHand (g2, hourAngle, HOUR_HAND_LENGTH);double minuteAngleMath. toRadians(90-360 * minutes/60)drawHand(g2, minuteAngle, MINUTE_HAND_LENGTH);public void draw
14、Hand(Graphics2D g2,double angle, double handLength)Point2D end=new Point2D. Double(RADIUS+handLength * Math. cos(angle),RADIUS-handLength * Math. sin(angle);Point2D center=new Point2D. Double(RADIUS, RADIUS)g2. draw(new Line2D. Double(center, end);public void setTime(int h, int m)minutes=h*60+m;repa
15、int();private double minutes=0;private double RADIUS=100;private double MINUTE_HAND_LENGTH=0.8 * RADIUS;private double HOUR_HAND_LENGTH=0.6 * RADIUS;(分数:40.00)填空项 1:_(A)二级 JAVA 笔试-11 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题利用递归方法求前 n 个自然数的和(n=10)。public class java1public static void mai
16、n(Stringargs)int sum=add(10);System. out. println(“1+2+9+10=“+sum);pubhc static int add(_)if(n=1)_;else_;(分数:30.00)填空项 1:_ (正确答案:第 1 处:int n第 2 处:return 1第 3 处:return n+add(n-1))解析:解析 递归方法是一种调用程序本身并采用栈结构的算法,第 1 处定义参数类型;第 2 处是递归初值;第 3 处为递归运算。二、简单应用题(总题数:1,分数:30.00)2.本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含 1
17、6 个按钮用来表示09、+、-、*、/、=运算符和小数点,程序顶部的文本框用来显示操作数以及结果。import java. awt. * ;import java. awt. event. * ;import javax. swing. * ;public class java2public static void main(String args)tryUIManager. setLookAndFeel(UIManager. getSystemLookAndFeelClassName();catch (Exception e)JFrame frame=new CalculatorFrame(
18、);frame. show();class CalculatorPanel extends JPanel implements ActionListener private JTextField display;private JButton btn;private double arg=0;private String op=“= “;private boolean start=true;public CalculatorPanel()setLayout(new BorderLayout();dispIay=new JTextField(“0“);display, setEditable(f
19、alse);add(display, “North“);JPanel p=new JPanel();p. setLayout(new GridLayout(4,4);String buttons=“789/456*123-0.=+“;for (int i=0; ibuttons, length(); i+)btn=new JButton(buttons. substring(i, i+1);p. add(btn);_;add(p, “Center“);public void actionPerformed(ActionEvent evt)String s=evt. getActionComma
20、nd();if(0=s. charAt(0) else display, setText(display, getText()+s);start=false;elseif (start)if (s. equals(“-“)display. setText(s);start=false;else op=s;elsedouble x=_;calculate(x);op=s;start=true;public void calculate(double n)if (op. equals(“+“) arg+=n;else if (op. equals(“-“) arg-= n;else if (op.
21、 equals(“*“) arg *=n;else if (op. equals(“/“) arg /=n;else if (op. equals(“=“) arg=n;display, setText(“+arg);class CalculatorFrame extends JFramepublic CalculatorFrame()setTitle(“java2“);setSize(220,180);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System. exit(0););C
22、ontainer contentPane=getContentPane();contentPane, add(new CalculatorPanel();(分数:30.00)填空项 1:_ (正确答案:第 1 处:btn. addActionListener(this)第 2 处:Double. parseDouble(display. getText())解析:解析 第 1 处为按钮添加监听器;第 2 处获得输入数字并转化为 double 型。三、综合应用题(总题数:1,分数:40.00)3.本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针
23、、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。import java. awt. * ;import java. awt. event. * ;import java. awt. geom. * ;import javax. swing. * ;import javax. swing, event. * ;public class java3public static void main(String args)TextTestFrame frame=new TextTestFrame();frame. setDefaultCloseOperation (J
24、Frame. EXIT_ON_CLOSE);frame. show();class TextTestFrame extends JFramepublic TextTestFrame()setTitle(“java3“);setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);Container contentPane=getContentPane();DocumentListener listener=new DocumentListen-er();JPanel panel=new JPanel();hourField=new JTextField(“12“,3);pan
25、el, add(hourField);hourField, getDocument (). addDocumentListener(this);minuteField=new JTextField(“00“,3);panel, add(minuteField);minuteField, getDocument(), addDocumentListener(listener);contentPane, add ( panel, BorderLayout. SOUTH);clock=new ClockPanel();contentPane, add (clock, BorderLayout. CE
26、NTER);public void setClock()tryint hours= Integer. parseInt (hourField. getText(). trim();int minutes= Integer. parselnt(minuteField, getText(), trim();clock, setTime(hours, minutes);catch (NumberFormatException e)public static final int DEFAULT WIDTH=300;public static final int DEFAULT_HEIGHT=300;p
27、rivate JTextField hourField;private JTextField minuteField;private ClockPanel clock;private class clockFieldListener extends DocumentListenerpublic void insertUpdate(DocumentEvent e) setClock();public void removeUpdate(DocumentEvent e) setClock();public void changedUpdate(DocumentEvent e) class Cloc
28、kPanel extends JPanelpublic void paintComponent(Graphics g)super, paintComponent(g);Graphics2D g2=(Graphics2D)g;Ellipse2D circle=new Ellipse2D. Double(0,0,2 * RADIUS, 2 * RADIUS);g2. draw(circle);double hourAngle=Math. toRadians(90-360 * minutes/(12 * 60);drawHand (g2, hourAngle, HOUR_HAND_LENGTH);d
29、ouble minuteAngleMath. toRadians(90-360 * minutes/60)drawHand(g2, minuteAngle, MINUTE_HAND_LENGTH);public void drawHand(Graphics2D g2,double angle, double handLength)Point2D end=new Point2D. Double(RADIUS+handLength * Math. cos(angle),RADIUS-handLength * Math. sin(angle);Point2D center=new Point2D.
30、Double(RADIUS, RADIUS)g2. draw(new Line2D. Double(center, end);public void setTime(int h, int m)minutes=h*60+m;repaint();private double minutes=0;private double RADIUS=100;private double MINUTE_HAND_LENGTH=0.8 * RADIUS;private double HOUR_HAND_LENGTH=0.6 * RADIUS;(分数:40.00)填空项 1:_ (正确答案:第 1 处:DocumentLislener listener=new ClockFieldListener()第 2 处:hourField. getDocument(). addDocumentListener(listener)第 3 处:private class CloekFieldListener implements DocumentListener)解析:解析 第 1 处从后面程序可以看出 ClockFieldListener 类扩展了 DocumentListener,此处应使用继承后的子类;第 2 处注册窗体的监听器,参数应为事件源。第 3 处实现的是接口,应使用implements。