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

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

1、计算机二级 JAVA-137及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序首先给一个数组赋值,然后计算该数组中下标为奇数的元素的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static void main (String args) int sum; _; int arrayList = new int20; for(int i = 0; i = 19; i +) arrayListi = i + i; int pos = 0; while(pos

2、 20) if(_) sum = sum + arrayListpos; _; System.out.println(“sum =“+ sum); (分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.请完成以下程序,首先由一个类 Example2_3实现 Serializable接口,并有三个成员变量,分别为 int型、double 型和 String型,可以用 toString的方法显示这三个成员变量。在 main方法中创建这个Example2_3的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为 TheSerial.data的文

3、件,并显示成员变量。最后从文件 TheSerial.data中读出三个成员变量并显示出来。 注意:请勿改动 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable private int intValue; private double doubleValue; private String string; TheSerial() intValue = 123; doubleValue = 12.34; string = “Serialize Test“; public

4、void setDouble(double d) doubleValue = d; public void setInt(int i) intValue = i; public void setString(String s) string = s; public String toString() return(“int=“+intValue+“double=“+doubleValue+“ string=“+string); public class Example2_3 public static void main(String argv) TheSerial e1 = new TheS

5、erial(); TheSerial e2; try e1.setInt(Integer.parseInt(argv0); e1.setDouble(Double.parseDouble(argv1); e1.setStringargv2); catch(Exception e) e1.setString(e.getMessage); System.out.println(e1); try FileOutputStream oS = new FileOutputStream(“TheSerial.data“); ObjectOutputStream oIS = new ObjectOutput

6、Stream(oS); _; catch(IOException ioException) System.out.println(ioException.getMessage(); try FileInputStream iS = new FileInputStream(“TheSerial. data“); ObjectInputStream oIS = new ObjectInputStream(iS); _ System.out.println(e2); catch(IOException ioException) System.out.println(ioException.getMe

7、ssage(); catch(ClassNotFoundException cnfException) System.out.println(cnfException.getMessage(); (分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是:主窗口中有两个按钮“Start”和“Close”,单击按钮“Start”后会在窗口左上角生成一个黑色实心小球,小球做斜线运动,碰到面板边缘后反弹几次后停止运动,再次单击按钮“Start”后又会生成一个小球做同样的运动。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import

8、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 BounceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.show(); class BounceFrame extends JFrame publ

9、ic BounceFrame() 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

10、 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 title,Listener listener) JButton button = new JButton(title); c.add(button

11、); 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 (InterruptedException exception) private BallCanvas canvas; public static final int WIDTH = 450; public static final int HEIGHT

12、 = 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 = 0; i balls.size(); i+) Ball b = (Ball)balls.get(i); b.draw(g2); private ArrayList balls = new ArrayList(); class Ba

13、ll 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(x+XSIZE = canvas.getWidth() x = canvas.getWidth() - XSIZE; dx = -dx; if(y 0) y = 0; dy = -dy; if (y+YSIZE = canvas.g

14、etHeight() 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)_计算机二级 JAVA-137答案解析(总分:1

15、00.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序首先给一个数组赋值,然后计算该数组中下标为奇数的元素的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static void main (String args) int sum; _; int arrayList = new int20; for(int i = 0; i = 19; i +) arrayListi = i + i; int pos = 0; while(pos 20) if(_) sum = sum + a

16、rrayListpos; _; System.out.println(“sum =“+ sum); (分数:30.00)_正确答案:()解析:sum=0。 pos%2=1。 pos+。二、2简单应用题(总题数:1,分数:40.00)2.请完成以下程序,首先由一个类 Example2_3实现 Serializable接口,并有三个成员变量,分别为 int型、double 型和 String型,可以用 toString的方法显示这三个成员变量。在 main方法中创建这个Example2_3的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为 TheSerial

17、.data的文件,并显示成员变量。最后从文件 TheSerial.data中读出三个成员变量并显示出来。 注意:请勿改动 main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable private int intValue; private double doubleValue; private String string; TheSerial() intValue = 123; doubleValue = 12.34; string = “Serialize Test“;

18、public void setDouble(double d) doubleValue = d; public void setInt(int i) intValue = i; public void setString(String s) string = s; public String toString() return(“int=“+intValue+“double=“+doubleValue+“ string=“+string); public class Example2_3 public static void main(String argv) TheSerial e1 = n

19、ew TheSerial(); TheSerial e2; try e1.setInt(Integer.parseInt(argv0); e1.setDouble(Double.parseDouble(argv1); e1.setStringargv2); catch(Exception e) e1.setString(e.getMessage); System.out.println(e1); try FileOutputStream oS = new FileOutputStream(“TheSerial.data“); ObjectOutputStream oIS = new Objec

20、tOutputStream(oS); _; catch(IOException ioException) System.out.println(ioException.getMessage(); try FileInputStream iS = new FileInputStream(“TheSerial. data“); ObjectInputStream oIS = new ObjectInputStream(iS); _ System.out.println(e2); catch(IOException ioException) System.out.println(ioExceptio

21、n.getMessage(); catch(ClassNotFoundException cnfException) System.out.println(cnfException.getMessage(); (分数:40.00)_正确答案:()解析:oOS.writeObject(e1) e2=(TheSerial)oIS.readObject() 解析 本题主要考查串行化相关的方法和实现。解题中首先要掌握串行化的基本过程和反串行化的过程。串行化过程首先要创建一个输出流 FileOutputStream,通过该类的实例对文件进行访问,然后创建一个 ObJectOutput Stream对象,

22、通过 writeObject()方法来实现对象的序列化。第一个空就是使用 writeObject()实现序列化。 反序列化过程中用 FileInputStream对象建立读取文件的连接,并使用该对象创建一个 ObiectInputStream的实例,这个 ObjectInput Stream实例的 readObject()方法就可以实现对象的反序列化。第二个空就是使用 readObject()实现反序列化。三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是:主窗口中有两个按钮“Start”和“Close”,单击按钮“Start”后会在窗口左上角生成一个黑色实心小球,小球做斜线

23、运动,碰到面板边缘后反弹几次后停止运动,再次单击按钮“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 BounceFrame(); frame.se

24、tDefaultCloseOperation(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 buttonPanel = new JPan

25、el(); 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); public void addButt

26、on(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 (InterruptedException exception

27、) 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 = 0; i balls.size();

28、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(x+XSIZE = canvas.get

29、Width() 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 = 15; private int x =

30、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。

展开阅读全文
相关资源
猜你喜欢
相关搜索

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

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