【计算机类职业资格】(A)二级JAVA笔试-8及答案解析.doc

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

1、(A)二级 JAVA 笔试-8 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题中数组 arr 中存储了学生的成绩,分别为 87,45,56,78,67,56,91,62,82,63,程序的功能是计算低于平均分的人数,并打印输出结果。请在程序空缺部分填写适当内容,使程序能正确运行。public class java1public static void main(String args)int arr=56,91,78,67,56,87,45,62,82,63;int num=arr. length;int i=0;int sumSc

2、ore=0;int sumNum=0;double average;while(inum)sumScore=sumScore+arri;_;average=_;i=0;doif(arriaverage)sumNum+;i+;while(_);System. out. println(“average:“+average+“, belows average:“+sumNum);(分数:30.00)填空项 1:_二、简单应用题(总题数:1,分数:30.00)2.本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“Yes”和“No”,单击

3、对话框上的“Yes”和“No”按钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。import java. awt. event. * ;import java. awt. * ;class MyDialog_implements ActionListenerstatic final int YES=1, NO=0;int message=-1; Button yes, no;MyDialog(Frame f, String s, boolean b)super(f, s, b);yes=new Button(“Yes“); yes. addActionListener(this);n

4、o=new Button (“No“); no. addActionListener(this);setLayout (new FlowLayout();add(yes); add(no);setBounds(60,60,100,100);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)message=-1; setVisible(false););public void actionPerformed(ActionEvent e)if(e. getSource()=yes)message

5、=YES;setVisible(false);else if(e. getSource()=no)message=NO;setVisible(false);public int getMessage()return message;class Dwindow extends Frame implements ActionListenetTextArea text; Button button; MyDialog dialog;Dwindow(String s)super(s);text=new TextArea(5,22); button=new Button(“打开对话框“);button.

6、 addActionListener(this);setLayout(new FlowLayout();add(button); add(text);dialog=new MyDialog(this, “Dialog“, true);setBounds(60,60,300,300); setVisible(true);validate();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System. exit(0););public void actionPerformed(Action

7、Event e)if(e. getSource()=button)_;if(dialog, getMessage()=MyDialog. YES)text. append(“/n 你单机了对话框的 yes 按钮“);else if(dialog. getMessage()=MyDialog. NO)text. append(“/n 你单机了对话框的 No 按钮“);public class java2public static void main(String args)new Dwindow(“java2“);(分数:30.00)填空项 1:_三、综合应用题(总题数:1,分数:40.00)3

8、.本题的功能是监听鼠标左右键的单击,以及面板中滚动条的添加。在窗口的画板中单击鼠标左键,在单击的位置绘制一个圆,当绘制的圆大于画板的大小时,画板就添加滚动条,在画板中单击鼠标右键,则清除画板中的所有图形。import javax. swing. * ;import javax. swing. event. MouseInputAdapter;import java. awt. * ;import java. awt. event. * ;import java. util. * ;public class java3 extends JPanelprivate Dimension size;p

9、rivate Vector objects;private final Color colors=Color. red, Color. blue, Color. green, Color. orange,Color. cyan, Color. magenta, Color. darkGray,Color. yellow;private final int color_n=colors, length;JPanel drawingArea;public java3()setOpaque(true);size=new Dimension(0,0);objects=new Vector();JLab

10、el instructionsLeft=new JLabel(“单击鼠标左键画圆.“);J Label instructionsRight=new JLabel( “单击鼠标右键清空画板.“);JPanel instructionPanel=new JPanel(new GridLayout(0,1);instructionPanel. add(instructionsLeft);instructionPanel. add(instruetionsRight);drawingArea=new JPanel()protected void paintComponent(Graphies g)su

11、per, paintComponent(g);Rectangle rect;for (int i=0; iobjects. size(); i+)rect=(Rectangle)objects. elementAt(i);g. setColor(colors(i % color_n);g. fillOval (rect. x, rect. y, rect. width, reet. height);drawingArea. setBackground(Color. white);drawingArea. addMouseListener(new MouseListener();JScrollP

12、ane scroller=new JScrollPane(drawingArea);scroller. setPreferredSize(new Dimension(200,200);setLayout(new BorderLayout();add(instructionPanel, BorderLayout. NORTH);add(scroller, BorderLayout. CENTER);class MyMouseListener extends mouselnputAdapterfinal int W=100;final int H=100;public void mouseRele

13、ased(MouseEvent e)boolean changed=false;if(SwingUtilities. isRightMouseButton(e)objects, removeAllElements();size. width=0;size. height=0;changed=true;elseint x=e. getX()-W/2;int y=e. getY()-H/2;if(x0) x=0;if(y0) y=0;Rectangle reet=new Rectangle(x, y, W, H);objects. addElement(rect);drawingArea. scr

14、ollRectToVisible(rect);int this width=(x+W+2);if (this_widthsize. width)size. width=this_width; changed=true;int this_height=(y+H+2);if (this_heightsize. height)size. height=this_height; changed=true;if (changed)drawingArea. setPreferredSize(size);drawingArea. revalidate();drawingArea. paint();publi

15、c static void main (String args)JFrame frame=new JFrame(“java3“);frame. addWindowListener (new WindowAdapter()public void windowClosing (WindowEvent e)system. exit(0););frame. setContentPane(new java3 ();frame. pack();frame. setVisible(true);(分数:40.00)填空项 1:_(A)二级 JAVA 笔试-8 答案解析(总分:100.00,做题时间:90 分钟

16、)一、基本操作题(总题数:1,分数:30.00)1.本题中数组 arr 中存储了学生的成绩,分别为 87,45,56,78,67,56,91,62,82,63,程序的功能是计算低于平均分的人数,并打印输出结果。请在程序空缺部分填写适当内容,使程序能正确运行。public class java1public static void main(String args)int arr=56,91,78,67,56,87,45,62,82,63;int num=arr. length;int i=0;int sumScore=0;int sumNum=0;double average;while(in

17、um)sumScore=sumScore+arri;_;average=_;i=0;doif(arriaverage)sumNum+;i+;while(_);System. out. println(“average:“+average+“, belows average:“+sumNum);(分数:30.00)填空项 1:_ (正确答案:第 1 处:i+或 i=i+1 或 i+=1第 2 处:(double)sumScore/num第 3 处:inum)解析:解析 本程序首先通过第一个 while 循环求得平均数,再通过 do while 循环逐一比较,判断是否及格。第 1 处为 while

18、 循环的自加;第 2 处计算平均数;第 3 处 do while 循环终止条件。二、简单应用题(总题数:1,分数:30.00)2.本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“Yes”和“No”,单击对话框上的“Yes”和“No”按钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。import java. awt. event. * ;import java. awt. * ;class MyDialog_implements ActionListenerstatic final int YES=1, NO=0;int

19、 message=-1; Button yes, no;MyDialog(Frame f, String s, boolean b)super(f, s, b);yes=new Button(“Yes“); yes. addActionListener(this);no=new Button (“No“); no. addActionListener(this);setLayout (new FlowLayout();add(yes); add(no);setBounds(60,60,100,100);addWindowListener(new WindowAdapter()public vo

20、id windowClosing(WindowEvent e)message=-1; setVisible(false););public void actionPerformed(ActionEvent e)if(e. getSource()=yes)message=YES;setVisible(false);else if(e. getSource()=no)message=NO;setVisible(false);public int getMessage()return message;class Dwindow extends Frame implements ActionListe

21、netTextArea text; Button button; MyDialog dialog;Dwindow(String s)super(s);text=new TextArea(5,22); button=new Button(“打开对话框“);button. addActionListener(this);setLayout(new FlowLayout();add(button); add(text);dialog=new MyDialog(this, “Dialog“, true);setBounds(60,60,300,300); setVisible(true);valida

22、te();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System. exit(0););public void actionPerformed(ActionEvent e)if(e. getSource()=button)_;if(dialog, getMessage()=MyDialog. YES)text. append(“/n 你单机了对话框的 yes 按钮“);else if(dialog. getMessage()=MyDialog. NO)text. append(“/n

23、 你单机了对话框的 No 按钮“);public class java2public static void main(String args)new Dwindow(“java2“);(分数:30.00)填空项 1:_ (正确答案:第 1 处:extends Dialog第 2 处:dialog. setVisible(true))解析:解析 第 1 处设定对话框的类应继承 Dialog 类;第 2 处显示对话框。三、综合应用题(总题数:1,分数:40.00)3.本题的功能是监听鼠标左右键的单击,以及面板中滚动条的添加。在窗口的画板中单击鼠标左键,在单击的位置绘制一个圆,当绘制的圆大于画板的

24、大小时,画板就添加滚动条,在画板中单击鼠标右键,则清除画板中的所有图形。import javax. swing. * ;import javax. swing. event. MouseInputAdapter;import java. awt. * ;import java. awt. event. * ;import java. util. * ;public class java3 extends JPanelprivate Dimension size;private Vector objects;private final Color colors=Color. red, Color

25、. blue, Color. green, Color. orange,Color. cyan, Color. magenta, Color. darkGray,Color. yellow;private final int color_n=colors, length;JPanel drawingArea;public java3()setOpaque(true);size=new Dimension(0,0);objects=new Vector();JLabel instructionsLeft=new JLabel(“单击鼠标左键画圆.“);J Label instructionsRi

26、ght=new JLabel( “单击鼠标右键清空画板.“);JPanel instructionPanel=new JPanel(new GridLayout(0,1);instructionPanel. add(instructionsLeft);instructionPanel. add(instruetionsRight);drawingArea=new JPanel()protected void paintComponent(Graphies g)super, paintComponent(g);Rectangle rect;for (int i=0; iobjects. size

27、(); i+)rect=(Rectangle)objects. elementAt(i);g. setColor(colors(i % color_n);g. fillOval (rect. x, rect. y, rect. width, reet. height);drawingArea. setBackground(Color. white);drawingArea. addMouseListener(new MouseListener();JScrollPane scroller=new JScrollPane(drawingArea);scroller. setPreferredSi

28、ze(new Dimension(200,200);setLayout(new BorderLayout();add(instructionPanel, BorderLayout. NORTH);add(scroller, BorderLayout. CENTER);class MyMouseListener extends mouselnputAdapterfinal int W=100;final int H=100;public void mouseReleased(MouseEvent e)boolean changed=false;if(SwingUtilities. isRight

29、MouseButton(e)objects, removeAllElements();size. width=0;size. height=0;changed=true;elseint x=e. getX()-W/2;int y=e. getY()-H/2;if(x0) x=0;if(y0) y=0;Rectangle reet=new Rectangle(x, y, W, H);objects. addElement(rect);drawingArea. scrollRectToVisible(rect);int this width=(x+W+2);if (this_widthsize.

30、width)size. width=this_width; changed=true;int this_height=(y+H+2);if (this_heightsize. height)size. height=this_height; changed=true;if (changed)drawingArea. setPreferredSize(size);drawingArea. revalidate();drawingArea. paint();public static void main (String args)JFrame frame=new JFrame(“java3“);f

31、rame. addWindowListener (new WindowAdapter()public void windowClosing (WindowEvent e)system. exit(0););frame. setContentPane(new java3 ();frame. pack();frame. setVisible(true);(分数:40.00)填空项 1:_ (正确答案:第 1 处:drawingArea. addMouseListener(new MyMouseListener()第 2 处:class MyMouseListener extends MouselnputAdapter第 3 处:dwawingArea. repaint())解析:解析 第 1 处注册监听器参数应为事件源,应为 MyMouseListener;第 2 处 Java 是大小写敏感的;第 3 处重绘构件。

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

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

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