1、二级 JAVA 机试-207 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序随机产生 10 个学生的成绩(0100),要求每个学生的成绩都必须及格,同时团体的平均分不少于 70 分,这样这个团体才能通过,否则就不能通过。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static void main(String args)int score,sum = 0,i;for(i = 1;i = 10; i +)score = (int) (_);System.ou
2、t.print (score +“,“);if(score 60)_;sum = sum + score;i+;System.out.println();if (i = 10) (_)System.out.println(“This team passes!“);elseSystem.out.println(“This team doesnt pass! “);(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是:主窗口中有一个按钮,按钮的长和宽每 200ms 增加 1,当达到 100 时又恢复原来大小重新进行增加。请将下述程序补充完整(注意:不得改动程序
3、的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;public class simplepublic static void main (String args)Mywin win = new Mywin();class Mywin extends Frame _Button b = new Button(“按钮“);int x = 5;Thread bird = null;Mywin ()setBounds(100,100,400,400); setLayout(new FlowLayout();setTitle (“simple“
4、);setVisible (true);add(b);b.setBackground (Color.green);addWindowListener (new WindowAdapter () public void windowClosing(WindowEvent e)System.exit (0););bird = new Thread(this);bird.start();public _while (true)x = x+1;if(x 100)x = 5;b.setBounds(40,40,x,x);trybird.sleep (200);catch(InterruptedExcep
5、tion e) (分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是:主窗口中有两个按钮“Start”和“Close”,单击按钮“Start”后会在窗口左上角生成一个黑色实心小球,小球做斜线运动,碰到面板边缘后反弹几次后停止运动,再次单击按钮“Start”后又会生成一个小球做同样的运动。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.util.*;import javax.s
6、wing.*;public class advancepublic static void main (String args)JFrame frame = new BounceFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)frame.show();class BounceFrame extends JFramepublic BounceFrame()setSize(WIDTH,HEIGHT);setTitle(“advance“);Container contentPane = getContentPane();Can
7、vas = new BallCanvas();contentPane.add(canvas,BorderLayout.CENTER);JPanel buttonPanel = new JPanel();addButton(buttonPanel,“Start“,new ActionListener()public void actionPerformed(ActionEvent evt)addBall(););addButton(buttonPanel,“Close“,new ActionListener()public void actionPerformed(ActionEvent evt
8、)System.exit (0););contentPane.add(buttonPanel,BorderLayout.SOUTH);public void addButton(Container c,String title,Listener listener)JButton button = new JButton(title);c.add(button);button.addActionListener(listener);public void addBall()tryBall b = new Ball(canvas);canvas.add(b);for (int i = 1;i =
9、1000; i+)b.move ();Runnable.sleep (5);catch (InterruptedException exception)private BallCanvas canvas;public static final int WIDTH = 450;public static final int HEIGHT = 350;class BallCanvas extend JPanelpublic void add(Ball b)balls.add(b);public void paintComponent(Graphics g)super.paintComponent(
10、g);Graphics2D g2 = (Graphics2D)g;for (int i = 0; i balls.size(); i+)Ball b = (Ball)balls.get(i);b.draw(g2);private ArrayList balls = new ArrayList();class Ballpublic Ball(Component c)canvas = c;public void draw(Graphics2D g2)g2.fill(new Ellipse2D.Double(x,y,XSIZE,YSIZE);public void move()x += dx;y +
11、= dy;if(x 0)x = 0;dx = -dx;if(x+XSIZE = canvas.getWidth()x = canvas.getWidth() - XSIZE;dx = -dx;if(y 0)y = 0;dy = -dy;if (y+YSIZE = canvas.getHeight()y = canvas.getHeight() - YSIZE;dy = -dy;canvas.paint(canvas.getGraphics();private Component canvas;private static final int XSIZE = 15;private static
12、final int YSIZE = 15;private int x = 0;private int y = 0;private int dx = 2;private int dy = 2;(分数:30.00)_二级 JAVA 机试-207 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序随机产生 10 个学生的成绩(0100),要求每个学生的成绩都必须及格,同时团体的平均分不少于 70 分,这样这个团体才能通过,否则就不能通过。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basic
13、public static void main(String args)int score,sum = 0,i;for(i = 1;i = 10; i +)score = (int) (_);System.out.print (score +“,“);if(score 60)_;sum = sum + score;i+;System.out.println();if (i = 10) (_)System.out.println(“This team passes!“);elseSystem.out.println(“This team doesnt pass! “);(分数:30.00)_正确
14、答案:(Math.random()*100。break。(sum/10)=70。)解析:二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是:主窗口中有一个按钮,按钮的长和宽每 200ms 增加 1,当达到 100 时又恢复原来大小重新进行增加。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;public class simplepublic static void main (String args)Mywin win = new Mywin();class Mywin ex
15、tends Frame _Button b = new Button(“按钮“);int x = 5;Thread bird = null;Mywin ()setBounds(100,100,400,400); setLayout(new FlowLayout();setTitle (“simple“);setVisible (true);add(b);b.setBackground (Color.green);addWindowListener (new WindowAdapter () public void windowClosing(WindowEvent e)System.exit
16、(0););bird = new Thread(this);bird.start();public _while (true)x = x+1;if(x 100)x = 5;b.setBounds(40,40,x,x);trybird.sleep (200);catch(InterruptedException e) (分数:40.00)_正确答案:(implements Runnable。void run。)解析:三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是:主窗口中有两个按钮“Start”和“Close”,单击按钮“Start”后会在窗口左上角生成一个黑色实心小球,小
17、球做斜线运动,碰到面板边缘后反弹几次后停止运动,再次单击按钮“Start”后又会生成一个小球做同样的运动。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.util.*;import javax.swing.*;public class advancepublic static void main (String args)JFrame frame = new BounceFrame();frame.setDefa
18、ultCloseOperation(JFrame.EXIT_ON_CLOSE)frame.show();class BounceFrame extends JFramepublic BounceFrame()setSize(WIDTH,HEIGHT);setTitle(“advance“);Container contentPane = getContentPane();Canvas = new BallCanvas();contentPane.add(canvas,BorderLayout.CENTER);JPanel buttonPanel = new JPanel();addButton
19、(buttonPanel,“Start“,new ActionListener()public void actionPerformed(ActionEvent evt)addBall(););addButton(buttonPanel,“Close“,new ActionListener()public void actionPerformed(ActionEvent evt)System.exit (0););contentPane.add(buttonPanel,BorderLayout.SOUTH);public void addButton(Container c,String ti
20、tle,Listener listener)JButton button = new JButton(title);c.add(button);button.addActionListener(listener);public void addBall()tryBall b = new Ball(canvas);canvas.add(b);for (int i = 1;i = 1000; i+)b.move ();Runnable.sleep (5);catch (InterruptedException exception)private BallCanvas canvas;public s
21、tatic final int WIDTH = 450;public static final int HEIGHT = 350;class BallCanvas extend JPanelpublic void add(Ball b)balls.add(b);public void paintComponent(Graphics g)super.paintComponent(g);Graphics2D g2 = (Graphics2D)g;for (int i = 0; i balls.size(); i+)Ball b = (Ball)balls.get(i);b.draw(g2);pri
22、vate ArrayList balls = new ArrayList();class Ballpublic Ball(Component c)canvas = c;public void draw(Graphics2D g2)g2.fill(new Ellipse2D.Double(x,y,XSIZE,YSIZE);public void move()x += dx;y += dy;if(x 0)x = 0;dx = -dx;if(x+XSIZE = canvas.getWidth()x = canvas.getWidth() - XSIZE;dx = -dx;if(y 0)y = 0;d
23、y = -dy;if (y+YSIZE = canvas.getHeight()y = canvas.getHeight() - YSIZE;dy = -dy;canvas.paint(canvas.getGraphics();private Component canvas;private static final int XSIZE = 15;private static final int YSIZE = 15;private int x = 0;private int y = 0;private int dx = 2;private int dy = 2;(分数:30.00)_正确答案:(第 39 行的 public void addButton(Container c,String title,Listener listener)改为public void addButton(Container c,String title,ActionListener listener)。第 54 行的 Runnable.sleep (5)改为 Thread.sleep(5)。第 66 行的 class BallCanvas extend JPanel 改为 class BallCanvas extends JPanel。)解析: