1、计算机二级 JAVA-155 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是随机产生一个 110 的随机数,根据提示,用户可以猜这个随机数。例如程序随机产生的随机数是 7,如果输入 8,则提示“Try smaller”并继续提示输入;如果输出 6,则提示“Try bigger”并继续提示输入;如果输入 7,则会输出“Great!”并退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.io.*; public class basic public static void
2、main(String args) InputStreamReader it; Bur feredReader in; Ir = new InputStreamReader(System.in) In = new BufferedReader(ir); int ranNum = _; int guessNum = -1; String str; while (_) System.out.print (“Please Input:“); try _; guessNum = Integer.parseInt(str); catch (Exception e) if(guessNum ranNum)
3、 System.out.println(“Try smaller.“); else System.out.println(“Try bigger.“); System.out.println (“Great ! “); (分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题中,窗口的背景色为系统桌面的颜色,在窗口中分别画空心和实心的黑色矩形、深灰色圆角矩形和浅灰色椭圆形,并且画白色粗体的“欢迎学习 Java!”字符串。 import java.awt.*; import javax.swing.*; public class java2 public static vo
4、id main(Stringargs) FillFrame frame=new FillFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class FillFrame extends JFrame public FillFrame() setTitle(“java2“); setSize(WIDTH,HEIGHT); FillPanel panel=new FillPanel(); panel.setBackround(SystemColor.desktop); Container con
5、tentPane=getContentPane(); contentPane.add(panel); public static final int WIDTH=400; public static final int HEIGHT=250; class FillPanel extends JPanel public void paintComponent(Graphics g) _; g.setColor(new Color(10,10,10); g.drawRect(10,10,100,30); g.setColor(new Color(100,100,100); g.drawRoundR
6、ect(150,10,100,30,15,15); g.setColor(new Color(150,150,150); g.drawOval(280,10,80,30); g.setColor(new Color(10,10,10); g.fillRect(10,110,100,30); g.setColor(new Color(100,100,100); g.fillRoundRect(150,110,100,30,15,15); g.setColor(new Color(150,150,150); g.fillOval(280,110,80,30); g.setColor(Color.w
7、hite); Font f=new Font(“宋体“,_,20); g.setFont(f); g.drawstring(“欢迎学习 Java!“,150,200); (分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是:主窗口中有两个按钮“Start”和“Close”,单击按钮“Start”后会在窗口左上角生成一个黑色实心小球,小球做斜线运动,碰到面板边缘后反弹几次后停止运动,再次单击按钮“Start”后又会生成一个小球做同样的运动。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; i
8、mport 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 BounceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.show(); class BounceFrame extends JFrame public BounceFram
9、e() setSize(WIDTH,HEIGHT); setTitle(“advance“); Container contentPane = getContentPane(); Canvas = new BallCanvas(); contentPane.add(canvas,BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); addButton(buttonPanel,“Start“,new ActionListener() public void actionPerformed(ActionEvent evt) addBall
10、(); ); addButton(buttonPanel,“Close“,new ActionListener() public void actionPerformed(ActionEvent evt) 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.add
11、ActionListener(listener); public void addBall() try Ball 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 static final int WIDTH = 450; public static final int HEIGHT = 350; class
12、 BallCanvas extend JPanel public 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); private ArrayList balls = new ArrayList(); class Ball public Bal
13、l(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; dy = -dy; if (y+YSIZE = canvas.getHeight() y
14、= 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)_计算机二级 JAVA-155 答案解析(总分:100.00,做题时间:9
15、0 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是随机产生一个 110 的随机数,根据提示,用户可以猜这个随机数。例如程序随机产生的随机数是 7,如果输入 8,则提示“Try smaller”并继续提示输入;如果输出 6,则提示“Try bigger”并继续提示输入;如果输入 7,则会输出“Great!”并退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.io.*; public class basic public static void main(String args) InputStreamReader i
16、t; Bur feredReader in; Ir = new InputStreamReader(System.in) In = new BufferedReader(ir); int ranNum = _; int guessNum = -1; String str; while (_) System.out.print (“Please Input:“); try _; guessNum = Integer.parseInt(str); catch (Exception e) if(guessNum ranNum) System.out.println(“Try smaller.“);
17、else System.out.println(“Try bigger.“); System.out.println (“Great ! “); (分数:30.00)_正确答案:()解析:(int)(Math.random()*10)。 guessNum!=ranNum。 str=in.readLine()。二、2简单应用题(总题数:1,分数:40.00)2.本题中,窗口的背景色为系统桌面的颜色,在窗口中分别画空心和实心的黑色矩形、深灰色圆角矩形和浅灰色椭圆形,并且画白色粗体的“欢迎学习 Java!”字符串。 import java.awt.*; import javax.swing.*; p
18、ublic class java2 public static void main(Stringargs) FillFrame frame=new FillFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class FillFrame extends JFrame public FillFrame() setTitle(“java2“); setSize(WIDTH,HEIGHT); FillPanel panel=new FillPanel(); panel.setBackround(S
19、ystemColor.desktop); Container contentPane=getContentPane(); contentPane.add(panel); public static final int WIDTH=400; public static final int HEIGHT=250; class FillPanel extends JPanel public void paintComponent(Graphics g) _; g.setColor(new Color(10,10,10); g.drawRect(10,10,100,30); g.setColor(ne
20、w Color(100,100,100); g.drawRoundRect(150,10,100,30,15,15); g.setColor(new Color(150,150,150); g.drawOval(280,10,80,30); g.setColor(new Color(10,10,10); g.fillRect(10,110,100,30); g.setColor(new Color(100,100,100); g.fillRoundRect(150,110,100,30,15,15); g.setColor(new Color(150,150,150); g.fillOval(
21、280,110,80,30); g.setColor(Color.white); Font f=new Font(“宋体“,_,20); g.setFont(f); g.drawstring(“欢迎学习 Java!“,150,200); (分数:40.00)_正确答案:()解析:第 1 处:super.paintComponent(g) 第 2 处:Font.Bold 解析 第 1 处使用父类方法应使用 super 来引用;第 2 处为设置粗体。三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是:主窗口中有两个按钮“Start”和“Close”,单击按钮“Start”后会在窗
22、口左上角生成一个黑色实心小球,小球做斜线运动,碰到面板边缘后反弹几次后停止运动,再次单击按钮“Start”后又会生成一个小球做同样的运动。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 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 Bou
23、nceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.show(); class BounceFrame extends JFrame public BounceFrame() setSize(WIDTH,HEIGHT); setTitle(“advance“); Container contentPane = getContentPane(); Canvas = new BallCanvas(); contentPane.add(canvas,BorderLayout.CENTER); JPanel bu
24、ttonPanel = new JPanel(); addButton(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);
25、 public void addButton(Container c,String title,Listener listener) JButton button = new JButton(title); c.add(button); button.addActionListener(listener); public void addBall() try Ball b = new Ball(canvas); canvas.add(b); for (int i = 1;i = 1000; i+) b.move (); Runnable.sleep (5); catch (Interrupte
26、dException exception) private BallCanvas canvas; public static final int WIDTH = 450; public static final int HEIGHT = 350; class BallCanvas extend JPanel public void add(Ball b) balls.add(b); public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (int i =
27、 0; i balls.size(); i+) Ball b = (Ball)balls.get(i); b.draw(g2); private ArrayList balls = new ArrayList(); class Ball public 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(
28、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 final int YSIZE =
29、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。