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

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

1、计算机二级 JAVA-138及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是计算 110(包括 1和 10)中除 5以外各个自然数的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static void main (String args) int i = 1; int sum = 0; while(i = 10) if(i = 5) _; _; _; i+; System.out.println (“sum=“+sum); (分数:30.00)

2、_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是定义一个简单的计算器,可以进行基本的四则运算。程序中布局了 16个按钮用来表示数字 09 及运算符和点号,程序顶部的文本框用来显示运算数及结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class simple public static void main (String args) try UIManager.setLookAndFeel(UIMana

3、ger.getSystemLookAndFeelClassName(); catch (Exception e) JFrame frame = new 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;

4、public 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; i buttons.length(); i +) btn = new JButton(buttons.s

5、ubstring(i,i + 1); p.add(btn); _; add(p,“Center“); public void actionPerformed(ActionEvent evt) String s = evt.getActionCommand(); if (“0“ = s.charAt(0)s.charAt(0) =“9“ |s.equals(“.“) if (start) display.setText(s); else display.setText(display.getText() + s); start = false; else if (start) if (s.equ

6、als(“-“) display.setText(s); start = false; else op = s; else double 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(“/“) arg /= n; else if(op.equals(“=“) arg

7、=n; display.setText(“+ arg); class CalculatorFrame extends Jframe public CalculatorFrame() setTitle(“simple“); setSize(220,180); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); Container contentPane=getContentPane(); contentPane.add(new CalculatorPan

8、el(); (分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是用键盘上的方向键来控制直线的绘制方向。如果一直按向上的方向键,则在窗口中从焦点开始向上缓慢绘制直线,按其他方向的方向键也会向对应的方向缓慢地绘制直线。如果按下 Shift键,绘制直线的速度会加快。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.geom.*; import java.util.*; import java.awt.event.*; import javax.swing.*; public

9、class advance public static void main(String args) SketchFrame frame = new SketchFrame(); frame.setDefaultCloseOperation(JFrame.EXIT ON CLOSE); frame,show (); class SketchFrame extends JFrame public SketchFrame() setTitle(“advance“); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); SketchPanel panel = new Ske

10、tchPanel(); Container contentPane = getContentPane(); contentPane.add(panel); public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; class SketchPanel extends JPanel _; last = new Point2D.Double(100,100); lines = new ArrayList(); KeyHandler listener = new KeyHandl

11、er(); _; setFocusable(true); public void add(int dx,int dy) Point2D end = new Point2D.Double(last.getX()+ dx,last.getY() + dy); Line2D line = new Line2D.Double(last,end); lines.add(line); repaint(); last = end; public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2 = (Graphics

12、2D)g; for (int i = 0; i lines.size(); i+) g2 .draw (Line2D) lines .get (i); private Point2D last; private ArrayList lines; private static final int SMALL INCREMENT = i; private static final int LARGE_INCREMENT = 5; private class KeyHandler implements KeyListener public void keyPressed(KeyEvent event

13、) _; int d; if (event.isShiftDown() d = LARGE_INCREMENT; else d = SMALL_INCREMENT; if(keyCode = KeyEvent.VK_LEFT) add(-d,0); else if(keyCode = KeyEvent.VK_RIGHT) add(d,0); else if(keyCode = KeyEvent.VK_UP) add(0,-d); else if(keyCode = KeyEvent.VK DOWN) add(0,d); public void keyReleased(KeyEvent even

14、t) public void keyTyped(KeyEvent event) char keyChar = event.getKeyChar(); int d; if (Charaeter.isUpperCase(keyChar) d = LARGE_INCREMENT; keyChar = Character.toLowerCase(keyChar); else d = SMALL_INCREMENT; (分数:30.00)_计算机二级 JAVA-138答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是计算 110(包括

15、 1和 10)中除 5以外各个自然数的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static void main (String args) int i = 1; int sum = 0; while(i = 10) if(i = 5) _; _; _; i+; System.out.println (“sum=“+sum); (分数:30.00)_正确答案:()解析:i+。 continue。 sum=sum+i。二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是定义一个简单的计算器,可以进行

16、基本的四则运算。程序中布局了 16个按钮用来表示数字 09 及运算符和点号,程序顶部的文本框用来显示运算数及结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class simple public static void main (String args) try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName(); catch (Except

17、ion e) JFrame frame = new 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 BorderLayou

18、t(); 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; i buttons.length(); i +) btn = new JButton(buttons.substring(i,i + 1); p.add(btn); _; add(p,“Center“);

19、 public void actionPerformed(ActionEvent evt) String s = evt.getActionCommand(); if (“0“ = s.charAt(0)s.charAt(0) =“9“ |s.equals(“.“) if (start) display.setText(s); else display.setText(display.getText() + s); start = false; else if (start) if (s.equals(“-“) display.setText(s); start = false; else o

20、p = s; else double 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(“/“) arg /= n; else if(op.equals(“=“) arg=n; display.setText(“+ arg); class CalculatorFrame

21、 extends Jframe public CalculatorFrame() setTitle(“simple“); setSize(220,180); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); Container contentPane=getContentPane(); contentPane.add(new CalculatorPanel(); (分数:40.00)_正确答案:()解析:btn.addActionListener(t

22、his)。 Double.parseDouble(display.getText()。三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是用键盘上的方向键来控制直线的绘制方向。如果一直按向上的方向键,则在窗口中从焦点开始向上缓慢绘制直线,按其他方向的方向键也会向对应的方向缓慢地绘制直线。如果按下 Shift键,绘制直线的速度会加快。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.geom.*; import java.util.*; import java.awt.event.*; imp

23、ort javax.swing.*; public class advance public static void main(String args) SketchFrame frame = new SketchFrame(); frame.setDefaultCloseOperation(JFrame.EXIT ON CLOSE); frame,show (); class SketchFrame extends JFrame public SketchFrame() setTitle(“advance“); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); S

24、ketchPanel panel = new SketchPanel(); Container contentPane = getContentPane(); contentPane.add(panel); public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; class SketchPanel extends JPanel _; last = new Point2D.Double(100,100); lines = new ArrayList(); KeyHandl

25、er listener = new KeyHandler(); _; setFocusable(true); public void add(int dx,int dy) Point2D end = new Point2D.Double(last.getX()+ dx,last.getY() + dy); Line2D line = new Line2D.Double(last,end); lines.add(line); repaint(); last = end; public void paintComponent(Graphics g) super.paintComponent(g);

26、 Graphics2D g2 = (Graphics2D)g; for (int i = 0; i lines.size(); i+) g2 .draw (Line2D) lines .get (i); private Point2D last; private ArrayList lines; private static final int SMALL INCREMENT = i; private static final int LARGE_INCREMENT = 5; private class KeyHandler implements KeyListener public void

27、 keyPressed(KeyEvent event) _; int d; if (event.isShiftDown() d = LARGE_INCREMENT; else d = SMALL_INCREMENT; if(keyCode = KeyEvent.VK_LEFT) add(-d,0); else if(keyCode = KeyEvent.VK_RIGHT) add(d,0); else if(keyCode = KeyEvent.VK_UP) add(0,-d); else if(keyCode = KeyEvent.VK DOWN) add(0,d); public void

28、 keyReleased(KeyEvent event) public void keyTyped(KeyEvent event) char keyChar = event.getKeyChar(); int d; if (Charaeter.isUpperCase(keyChar) d = LARGE_INCREMENT; keyChar = Character.toLowerCase(keyChar); else d = SMALL_INCREMENT; (分数:30.00)_正确答案:()解析:public SketchPanel()。 addKeyListener(listener)。 int keyCode=event.getKeyCode()。

展开阅读全文
相关资源
猜你喜欢
  • KS J ISO 15214-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the enumeration of mesophilic lactic acid bacteria-Colony-count technique at 30 ℃《食品和动物饲料的微.pdf KS J ISO 15214-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the enumeration of mesophilic lactic acid bacteria-Colony-count technique at 30 ℃《食品和动物饲料的微.pdf
  • KS J ISO 16654-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the detection of Escherichia coli O157《食品和动物饲料的微生物学 检测大肠杆菌0157的水平方法》.pdf KS J ISO 16654-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the detection of Escherichia coli O157《食品和动物饲料的微生物学 检测大肠杆菌0157的水平方法》.pdf
  • KS J ISO 17410-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the enumeration of psychrotrophic microorganisms《食品和动物饲料的微生物学 适冷微生物记数水平方法》.pdf KS J ISO 17410-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the enumeration of psychrotrophic microorganisms《食品和动物饲料的微生物学 适冷微生物记数水平方法》.pdf
  • KS J ISO 21569-2011 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-Qualitative nucleic acid based methods《食品 基因改良有机物和衍生产品的检.pdf KS J ISO 21569-2011 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-Qualitative nucleic acid based methods《食品 基因改良有机物和衍生产品的检.pdf
  • KS J ISO 21570-2011 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-Quantitative nucleic acid based methods《食品 转基因有机体和衍生物探测分.pdf KS J ISO 21570-2011 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-Quantitative nucleic acid based methods《食品 转基因有机体和衍生物探测分.pdf
  • KS J ISO 21571-2006 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-Nucleic acid extraction《食品 基因改良有机物及其衍生产品检测的分析方法 核酸萃取》.pdf KS J ISO 21571-2006 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-Nucleic acid extraction《食品 基因改良有机物及其衍生产品检测的分析方法 核酸萃取》.pdf
  • KS J ISO 21572-2009 Foodstuffs-Methods for the detection of genetically modified organisms and derived products-Protein based methods《食品 基因改良有机物及衍生产品的检测方法 基于蛋白质的方法》.pdf KS J ISO 21572-2009 Foodstuffs-Methods for the detection of genetically modified organisms and derived products-Protein based methods《食品 基因改良有机物及衍生产品的检测方法 基于蛋白质的方法》.pdf
  • KS J ISO 24276-2006 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-General requirements and definitions《食品 基因改良有机物及其衍生产品检测的.pdf KS J ISO 24276-2006 Foodstuffs-Methods of analysis for the detection of genetically modified organisms and derived products-General requirements and definitions《食品 基因改良有机物及其衍生产品检测的.pdf
  • KS J ISO 4831-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the detection and enumeration of coliforms-Most probable number technique《食品和动物饲料微生物学 大肠杆菌菌落.pdf KS J ISO 4831-2007 Microbiology of food and animal feeding stuffs-Horizontal method for the detection and enumeration of coliforms-Most probable number technique《食品和动物饲料微生物学 大肠杆菌菌落.pdf
  • 相关搜索

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

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