【计算机类职业资格】(A)二级JAVA笔试-11及答案解析.doc

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

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。

展开阅读全文
相关资源
猜你喜欢
  • ETSI TS 123 251-2017 Universal Mobile Telecommunications System (UMTS) LTE Network sharing Architecture and functional description (V14 1 0 3GPP TS 23 251 version 14 1 0 Release 14.pdf ETSI TS 123 251-2017 Universal Mobile Telecommunications System (UMTS) LTE Network sharing Architecture and functional description (V14 1 0 3GPP TS 23 251 version 14 1 0 Release 14.pdf
  • ETSI TS 123 251-2018 Universal Mobile Telecommunications System (UMTS) LTE Network sharing Architecture and functional description (V15 0 0 3GPP TS 23 251 version 15 0 0 Release 15.pdf ETSI TS 123 251-2018 Universal Mobile Telecommunications System (UMTS) LTE Network sharing Architecture and functional description (V15 0 0 3GPP TS 23 251 version 15 0 0 Release 15.pdf
  • ETSI TS 123 259-2016 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Personal Network Management (PNM) Procedures .pdf ETSI TS 123 259-2016 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Personal Network Management (PNM) Procedures .pdf
  • ETSI TS 123 259-2017 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Personal Network Management (PNM) Procedures .pdf ETSI TS 123 259-2017 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Personal Network Management (PNM) Procedures .pdf
  • ETSI TS 123 259-2017 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Personal Network Management (PNM) Procedures _1.pdf ETSI TS 123 259-2017 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Personal Network Management (PNM) Procedures _1.pdf
  • ETSI TS 123 261-2016 Universal Mobile Telecommunications System (UMTS) LTE IP flow mobility and seamless Wireless Local Area Network (WLAN) offload Stage 2 (V13 0 0 3GPP TS 23 261 .pdf ETSI TS 123 261-2016 Universal Mobile Telecommunications System (UMTS) LTE IP flow mobility and seamless Wireless Local Area Network (WLAN) offload Stage 2 (V13 0 0 3GPP TS 23 261 .pdf
  • ETSI TS 123 261-2017 Universal Mobile Telecommunications System (UMTS) LTE IP flow mobility and seamless Wireless Local Area Network (WLAN) offload Stage 2 (V14 0 0 3GPP TS 23 261 .pdf ETSI TS 123 261-2017 Universal Mobile Telecommunications System (UMTS) LTE IP flow mobility and seamless Wireless Local Area Network (WLAN) offload Stage 2 (V14 0 0 3GPP TS 23 261 .pdf
  • ETSI TS 123 261-2018 Universal Mobile Telecommunications System (UMTS) LTE IP flow mobility and seamless Wireless Local Area Network (WLAN) offload Stage 2 (V15 0 0 3GPP TS 23 261 .pdf ETSI TS 123 261-2018 Universal Mobile Telecommunications System (UMTS) LTE IP flow mobility and seamless Wireless Local Area Network (WLAN) offload Stage 2 (V15 0 0 3GPP TS 23 261 .pdf
  • ETSI TS 123 271-2016 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Functional stage 2 description of Location Se.pdf ETSI TS 123 271-2016 Digital cellular telecommunications system (Phase 2+) (GSM) Universal Mobile Telecommunications System (UMTS) LTE Functional stage 2 description of Location Se.pdf
  • 相关搜索

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

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