1、二级 JAVA 机试-203 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是随机产生 50 个 0100 间的随机数,并计算 7080 间随机数的个数(包括 70,不包括 80)。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static void main(String args)int result = 0;int i = 0;int randomNum;while (i50)randomNum =_;if(_)result +;_;System
2、.out.println(“result =“ + result);(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是通过滑动条修改颜色的 RGB 值,从而控制颜色。程序中有一个面板、3 个标签和 3 个滑动条,标签和滑动条一一对应,分别对应三原色红、绿、蓝,任意拖动其中的一个滑动条,所对应的颜色值就会发生变化,面板的颜色也会对应地发生变化。滑动条值的范围是 0255。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import javax.swing.
3、*;public class simple extends JFrame implements AdjustmentListenerpublic simple()setTitle(“simple“);setSize(300,200);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)System.exit (0););Container contentPane =_;JPanel p = new JPane1();p.setLayout(new GridLayout(3,2);p.add(
4、redLabel = new JLabel(“Red 0“);p.add(red = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255);red.setBlockIncrement(16);red.addAdjustmentListener(this);p.add(greenLabel = new JLabel(“Green 0“);p.add(green = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255);green.setBlockIncrement(16);green.addAdjustmentL
5、istener(this);p.add(blueLabel = new JLabel(“Blue 0“);p.add(blue = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255);blue.setBlockIncrement(16);blue.addAdjustmentListener(this);contentPane.add(p,“South“);colorPanel = new JPanel();colorPanel.setBackground(new Color(0,0,0);contentPane.add(colorPanel,“Cen
6、ter“);public void adjustmentValueChanged(AdjustmentEvent evt)redLabel.setText(“Red “ + red.getValue();greenLabel.setText(“Green “ + green.getValue();blueLabel.setText(“Blue “ + blue.getValue();colorPanel.setBackground(new Color(red.getValue().green.getValue(),blue.getValue();_;public static void mai
7、n(String args)JFrame f = new simple();f.show();private JLabel redLabel;private JLabel greenLabel;private JLabel blueLabel;private JScrollBar red;private JScrollBar green;private JScrollBar blue;private JPanel colorPanel;(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序是一个 Applet 应用程序,功能是更改显示的图片。页面中有一个按钮“改变图
8、形”,单击该按钮,面板中显示的图片改变;继续单击,图片继续改变。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.applet.*;import java.awt.*;import java.awt.event.*;public class advance extends Applet implements ActionListenerint n = 0;Image im1,im2,showim;Button bn = new Button(“改变图形“);public void init()add (bn);_;im1 = getImage(getCo
9、deBase(),“advance_1.jpg“);im2 = getImage(getCodeBase(),“advance_2.jpg“);bn.addActionListener ( this );showim = im2;public void actionPerformed(ActionEvent e)_;if (e.getActionCommand ()= “改变“ )if(n%2 = 0)showim = im1;n+;elseshowim = im2;n+;_;public void paint (Graphics g)g.drawImage (showim,0,0,this)
10、;(分数:30.00)_二级 JAVA 机试-203 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是随机产生 50 个 0100 间的随机数,并计算 7080 间随机数的个数(包括 70,不包括 80)。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static void main(String args)int result = 0;int i = 0;int randomNum;while (i50)randomNum =_;if(_)result
11、 +;_;System.out.println(“result =“ + result);(分数:30.00)_正确答案:(int)(Math.random()*100)。randomNum=70randomNum80。i+。)解析:二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是通过滑动条修改颜色的 RGB 值,从而控制颜色。程序中有一个面板、3 个标签和 3 个滑动条,标签和滑动条一一对应,分别对应三原色红、绿、蓝,任意拖动其中的一个滑动条,所对应的颜色值就会发生变化,面板的颜色也会对应地发生变化。滑动条值的范围是 0255。请将下述程序补充完整(注意:不得改动程序的结
12、构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class simple extends JFrame implements AdjustmentListenerpublic simple()setTitle(“simple“);setSize(300,200);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)System.exit (0););Container conte
13、ntPane =_;JPanel p = new JPane1();p.setLayout(new GridLayout(3,2);p.add(redLabel = new JLabel(“Red 0“);p.add(red = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255);red.setBlockIncrement(16);red.addAdjustmentListener(this);p.add(greenLabel = new JLabel(“Green 0“);p.add(green = new JScrollBar(Adjustabl
14、e.HORIZONTAL,0,0,0,255);green.setBlockIncrement(16);green.addAdjustmentListener(this);p.add(blueLabel = new JLabel(“Blue 0“);p.add(blue = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255);blue.setBlockIncrement(16);blue.addAdjustmentListener(this);contentPane.add(p,“South“);colorPanel = new JPanel();c
15、olorPanel.setBackground(new Color(0,0,0);contentPane.add(colorPanel,“Center“);public void adjustmentValueChanged(AdjustmentEvent evt)redLabel.setText(“Red “ + red.getValue();greenLabel.setText(“Green “ + green.getValue();blueLabel.setText(“Blue “ + blue.getValue();colorPanel.setBackground(new Color(
16、red.getValue().green.getValue(),blue.getValue();_;public static void main(String args)JFrame f = new simple();f.show();private JLabel redLabel;private JLabel greenLabel;private JLabel blueLabel;private JScrollBar red;private JScrollBar green;private JScrollBar blue;private JPanel colorPanel;(分数:40.0
17、0)_正确答案:(getContentPane()。colorPanel.repaint()。)解析:三、3综合应用题(总题数:1,分数:30.00)3.本题程序是一个 Applet 应用程序,功能是更改显示的图片。页面中有一个按钮“改变图形”,单击该按钮,面板中显示的图片改变;继续单击,图片继续改变。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.applet.*;import java.awt.*;import java.awt.event.*;public class advance extends Applet implements Actio
18、nListenerint n = 0;Image im1,im2,showim;Button bn = new Button(“改变图形“);public void init()add (bn);_;im1 = getImage(getCodeBase(),“advance_1.jpg“);im2 = getImage(getCodeBase(),“advance_2.jpg“);bn.addActionListener ( this );showim = im2;public void actionPerformed(ActionEvent e)_;if (e.getActionCommand ()= “改变“ )if(n%2 = 0)showim = im1;n+;elseshowim = im2;n+;_;public void paint (Graphics g)g.drawImage (showim,0,0,this);(分数:30.00)_正确答案:(bn.setActionCommand(“改变“)。Graphics g=getGraphics()。update(g)。)解析: