1、计算机二级 JAVA-150 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题中定义了长度为 20 的一维整型数组 a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。 public class java1( public static void main(Stringargs) int a=_; int i; for(_;i+) ai=i; for(i=0;i20;i+) if(_) System.out.print(“a“+i+“=“+ai+“,“); (分数:30.00)_二、2简单应用题(总题数:1,分
2、数:40.00)2.本题程序的功能是主窗口有一个按钮、一个文本域和一个复选框,初始时窗口的大小是不能调整的,勾选复选框后,窗口大小就可以进行调整,如果取消勾选复选框,则窗口的大小又不能调整,单击按钮可以关闭程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; class MyFrame extends Frame_ Checkbox box; TextArea text; Button button; MyFrame(String s) super (s); box = new Chec
3、kbox(“设置窗口是否可调整大小“); text = new TextArea(12,12); button = new Button(“关闭窗口“); button.addActionListener(this); box.addItemListener(this); setBounds(100,100,200,300); setVisible(true); add(text,BorderLayout.CENTER); add(box,BorderLayout.SOUTH); add(button,BorderLayout.NORTH); _; validate (); public vo
4、id itemStateChanged(ItemEvent e) if (box.getState() = true) setResizable(true); else setResizable(false); public void actionPerformed(ActionEvent e) dispose(); class simple public static void main(String args) new MyFrame(“simple“); (分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是求两个交叉图形的并、减、交及异或。窗口中有
5、4 个单选按钮和一个图形面板,面板中有两个交叉的图形,选中其中一个单选按钮,图形面板中会以黑色填充的方式显示运算的结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class advance public static void main(String args) JFrame frame = new AreaTestFrame(); fr
6、ame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class AreaTestFrame extends JFrame public AreaTestFrame() setTitle(“advance“); setSize(WIDTH,HEIGHT); area1 = new Area(new Ellipse2D.Double(100,100,150,100); area2 = new Area(new Rectangle2D.Double(150,150,150,100); _; panel = new JPa
7、nel() public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.draw(area1); g2.draw(area2); if (area ! = null) g2.fill(area); contentPane.add(panel,BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); ButtonGroup group = new ButtonGroup(); JRadioButton ad
8、dButtoh = new JRadioButton(“并“,false); buttonPanel.add(addButton); group.add(addButton); addButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); area.add(area2); panel.repaint(); ); JRadioButton subtractButton = new JRadioBu
9、tton(“减“,false); buttonPanel.add(subtractButton); group.add(subtractButton); subtractButton.addActionListener(new ActienListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(areal); _; panel.repaint(); ); JRadioButton intersectButton = new JRadioButton(“交“,false); but
10、tonPanel.add(intersectButton); group.add(intersectButton); intersectButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); area.intersect(area2); panel.repaint(); ); JRadioButten exclusiveOrButton = new JRadioButton(“异或“,false
11、); buttonPanel.add(exclusiveOrButton); group.add(exclusiveOrButton); exclusiveOrButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); _; panel,repaint (); ); contentPane.add(buttonPanel,BorderLayout.NORTH); private JPanel pan
12、e1; private Area area; private Area area1; private Area area2; private static final int WIDTH = 400; private static final int HEIGHT = 400; (分数:30.00)_计算机二级 JAVA-150 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题中定义了长度为 20 的一维整型数组 a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。 public class java1( pub
13、lic static void main(Stringargs) int a=_; int i; for(_;i+) ai=i; for(i=0;i20;i+) if(_) System.out.print(“a“+i+“=“+ai+“,“); (分数:30.00)_正确答案:()解析:第 1 处:new int20 第 2 处:i=0;i20 第 3 处:i%2! =0 解析 第 1 处定义了长度为 20 的一维整型数组 a;第 2 处的 for 循环将数组元素的下标值赋给数组元素;第 3 处判断数组各个元素下标是否为奇数。二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是
14、主窗口有一个按钮、一个文本域和一个复选框,初始时窗口的大小是不能调整的,勾选复选框后,窗口大小就可以进行调整,如果取消勾选复选框,则窗口的大小又不能调整,单击按钮可以关闭程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; class MyFrame extends Frame_ Checkbox box; TextArea text; Button button; MyFrame(String s) super (s); box = new Checkbox(“设置窗口是否可调整大小“
15、); text = new TextArea(12,12); button = new Button(“关闭窗口“); button.addActionListener(this); box.addItemListener(this); setBounds(100,100,200,300); setVisible(true); add(text,BorderLayout.CENTER); add(box,BorderLayout.SOUTH); add(button,BorderLayout.NORTH); _; validate (); public void itemStateChange
16、d(ItemEvent e) if (box.getState() = true) setResizable(true); else setResizable(false); public void actionPerformed(ActionEvent e) dispose(); class simple public static void main(String args) new MyFrame(“simple“); (分数:40.00)_正确答案:()解析:implements ItemListener,ActionListener。 setResizable(false)。三、3综
17、合应用题(总题数:1,分数:30.00)3.本题程序的功能是求两个交叉图形的并、减、交及异或。窗口中有 4 个单选按钮和一个图形面板,面板中有两个交叉的图形,选中其中一个单选按钮,图形面板中会以黑色填充的方式显示运算的结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class advance public static void main
18、(String args) JFrame frame = new AreaTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class AreaTestFrame extends JFrame public AreaTestFrame() setTitle(“advance“); setSize(WIDTH,HEIGHT); area1 = new Area(new Ellipse2D.Double(100,100,150,100); area2 = new Area(new Rec
19、tangle2D.Double(150,150,150,100); _; panel = new JPanel() public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.draw(area1); g2.draw(area2); if (area ! = null) g2.fill(area); contentPane.add(panel,BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); Bu
20、ttonGroup group = new ButtonGroup(); JRadioButton addButtoh = new JRadioButton(“并“,false); buttonPanel.add(addButton); group.add(addButton); addButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); area.add(area2); panel.repa
21、int(); ); JRadioButton subtractButton = new JRadioButton(“减“,false); buttonPanel.add(subtractButton); group.add(subtractButton); subtractButton.addActionListener(new ActienListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(areal); _; panel.repaint(); ); JRadioButto
22、n intersectButton = new JRadioButton(“交“,false); buttonPanel.add(intersectButton); group.add(intersectButton); intersectButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); area.intersect(area2); panel.repaint(); ); JRadioBu
23、tten exclusiveOrButton = new JRadioButton(“异或“,false); buttonPanel.add(exclusiveOrButton); group.add(exclusiveOrButton); exclusiveOrButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); _; panel,repaint (); ); contentPane.add
24、(buttonPanel,BorderLayout.NORTH); private JPanel pane1; private Area area; private Area area1; private Area area2; private static final int WIDTH = 400; private static final int HEIGHT = 400; (分数:30.00)_正确答案:()解析:Container contentPane = getContentPane()。 area.subtract(area2)。 area.exclusiveOr(a rea2)。