【计算机类职业资格】二级JAVA机试-37及答案解析.doc

上传人:sumcourage256 文档编号:1326564 上传时间:2019-10-17 格式:DOC 页数:10 大小:40KB
下载 相关 举报
【计算机类职业资格】二级JAVA机试-37及答案解析.doc_第1页
第1页 / 共10页
【计算机类职业资格】二级JAVA机试-37及答案解析.doc_第2页
第2页 / 共10页
【计算机类职业资格】二级JAVA机试-37及答案解析.doc_第3页
第3页 / 共10页
【计算机类职业资格】二级JAVA机试-37及答案解析.doc_第4页
第4页 / 共10页
【计算机类职业资格】二级JAVA机试-37及答案解析.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

1、二级 JAVA 机试-37 及答案解析(总分: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);public static int add(_)if(n=1)_;else_;(分数:30.00)_二、简单应用题(总题数:1,分数:40.00)2.本题中定义了一个简单的计算器,可以进行基本的四则运算

2、程序中包含 16 个按钮用来表示09、+、-、*、/、=运算符和小数点,程序顶部的文本框用来显示操作数以及结果。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class java2public static void main(Stringargs)tryUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName();catch(Exception e)JFrame frame=new CalculatorFrame();fram

3、e.show();class CalculatorPanel extends JPanel implements ActionListenerprivate JTextField display;private JButton btn;private double arg=0;private String op=“=“;private boolean start=true;public CalculatorPanel()setLayout(new BorderLayout();display=new JTextField(“0“);display.setEditable(false);add(

4、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.getActionCommand();if(0=s.charAt(0)

5、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.equals(“*“)arg*=n;else if(op.equals(“/

6、)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););Container contentPane=getContentPane();contentPane.

7、add(new CalculatorPanel();(分数:40.00)_三、综合应用题(总题数:1,分数:30.00)3.本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;import javax.swing.event.*;public class java3public static vo

8、id main(Stringargs)TextTestFrame frame=new TextTestFrame();frame.setDefauhCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();class TextTestFrame extends JFramepublic TextTestFrame()setTitle(“java3“);setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);Container contentPane=getContentPane();DocumentListener listener

9、new DocurnentListener();JPanel panel=new JPanel();hourField=new JTextField(“12“,3);panel.add(hourField);hourField.getDocument().addDocumentListener(this);minuteField=new JTextField(“00“,3);panel.add(minuteField);minuteField.getDocument().addDocumentListener(listener);contentPane.add(panel,BorderLay

10、out.SOUTH);clock=new ClockPanel();contentPane.add(clock,BorderLayout.CENTER);public void setClock()tryint hours=Integer.parseInt(hourField,getText().trim();int minutes=Integer.parseInt(minuteField.getText().trim();clock.setTime(hours,minutes);catch(NumberFormatException e)public static final int DEF

11、AULT_WIDTH=300;public static final int DEFAULT_HEIGHT=300;private JTextField hourField;private JTextField minuteField;private ClockPanel clock;private class clockFieldListener extends DocumentListenerpublic void insertUpdate(DocumentEvent e)setClock();public void removeUpdate(DocumentEvent e)setCloc

12、k();public void changedUpdate(DocumentEvent e)class ClockPanel 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);d

13、rawHand(g2.hourAngle,HOUR_HAND_LENGTH);double minuteAngle=Math.toRadians(90-360*minutes/60);drawHand(g2,minuteAngle,MINUTE_HAND_LENGTH);public void drawHand(Graphics2D g2,double angle,double handLength)Point2D end=new Poim2D.Double(RADIUS+handLength*Math.cos(angle),RADIUS-handLength*Math.sin(angle);

14、Point2D center=new Point2D.Double(RADIUS,RADIUS);g2.draw(new Line2D.Double(center,end);public void setTime(int h,int m)minutesh*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;(分数:30.00)_二级 JAVA

15、 机试-37 答案解析(总分: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);public static int add(_)if(n=1)_;else_;(分数:30.00)_正确答案:(第 1 处:int n第 2 处:return 1第 3 处:return n+add(n-1)解析:解析

16、 递归方法是一种调用程序本身并采用栈结构的算法,第 1 处定义参数类型;第 2 处是递归初值;第 3 处为递归运算。二、简单应用题(总题数:1,分数:40.00)2.本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含 16 个按钮用来表示09、+、-、*、/、=运算符和小数点,程序顶部的文本框用来显示操作数以及结果。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class java2public static void main(Stringargs)tryUIManager.setLo

17、okAndFeel(UIManager.getSystemLookAndFeelClassName();catch(Exception e)JFrame frame=new CalculatorFrame();frame.show();class CalculatorPanel extends JPanel implements ActionListenerprivate JTextField display;private JButton btn;private double arg=0;private String op=“=“;private boolean start=true;pub

18、lic CalculatorPanel()setLayout(new BorderLayout();display=new JTextField(“0“);display.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);_

19、add(p,“Center“);public void actionPerformed(ActionEvent evt)String s=evt.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(d

20、ouble n)if(op.equals(“+“)arg+=n;else if(op.equals(“-“)arg-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(

21、)public void windowClosing(WindowEvent e)System.exit(0););Container contentPane=getContentPane();contentPane.add(new CalculatorPanel();(分数:40.00)_正确答案:(第 1 处:btn.addActionListener(this)第 2 处:Double.parseDouble(display.getText()解析:解析 第 1 处为按钮添加监听器;第 2 处获得输入数字并转化为 double 型。三、综合应用题(总题数:1,分数:30.00)3.本题的

22、功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;import javax.swing.event.*;public class java3public static void main(Stringargs)TextTestFrame frame=new TextTestFrame();frame.

23、setDefauhCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();class TextTestFrame extends JFramepublic TextTestFrame()setTitle(“java3“);setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);Container contentPane=getContentPane();DocumentListener listener=new DocurnentListener();JPanel panel=new JPanel();hourField=new

24、JTextField(“12“,3);panel.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,BorderLay

25、out.CENTER);public void setClock()tryint hours=Integer.parseInt(hourField,getText().trim();int minutes=Integer.parseInt(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

26、 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 ClockPanel ex

27、tends 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);double minuteAngle=Math.to

28、Radians(90-360*minutes/60);drawHand(g2,minuteAngle,MINUTE_HAND_LENGTH);public void drawHand(Graphics2D g2,double angle,double handLength)Point2D end=new Poim2D.Double(RADIUS+handLength*Math.cos(angle),RADIUS-handLength*Math.sin(angle);Point2D center=new Point2D.Double(RADIUS,RADIUS);g2.draw(new Line

29、2D.Double(center,end);public void setTime(int h,int m)minutesh*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;(分数:30.00)_正确答案:(第 1 处:DocumentListener listener=new ClockFieldListener()第 2 处:hourField.getDocument().addDocumentListener(listener)第 3 处:private class ClockFieldListener implements DocumentListener)解析:解析 第 1 处从后面程序可以看出 ClockFieldListener 类扩展了 DocumentListener,此处应使用继承后的子类;第 2 处注册窗体的监听器,参数应为事件源。第 3 处实现的是接口,应使用implements。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1