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

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

1、二级 JAVA 机试-69 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题中定义了一个长度为 20 的整数数组,然后将 120 分别赋给数组元素,计算该数组中所有下标为奇数的元素的和。public class java1public static void main(string args)int sum;_;int arrayLis=new int20;for(int i=0;i=19;i+)arrayListi=i+1;int pos=0;while(pos20)if(_)sum-sum+arrayListpos;_;Syste

2、m.out.println(“sum=“+sum);(分数:30.00)_二、简单应用题(总题数:1,分数:40.00)2.本题的功能是通过按钮来选择窗口显示的风格。窗口中有三个按钮:“Metal”、“Motif”和“Windows”,单击任何一个按钮,就能将窗口的风格改变为按钮名称所对应的风格。import java.awt.*;import java.awt.event.*;import javax.swing.*;class PlafPanel extends JPanei implements ActionListenerpublic_()metalButton=new JButton

3、(“Metal“);motifButton=new JButton(“Motif“);windowsButton=new JButton(“Windows“);add(metalButton);add(motifButton);add(windowsButton);metalButton.addActionListener(this);motifButton.addActionListener(this);windowsButton.addActionListener(this);public void actionPerformed(ActionEvent evt)Object source

4、=evt.getSource();String plaf=“;if(source=metalButton)plaf=“javax.swing.plaf.metal.MetalLookAndFeel“;else if(source=motifButton)plaf=“com.sun.java.swing.plaf.motif.MotifLookAndFeel“;else if(source=windowsButton)plaf=“com.sun.java.swing.plaf.windows.WindowsLookAndFeel“;tryUIManager.setLookAndFeel(_);S

5、wingUtilities.updateComponentTreeUI(this);catch(Exception e)private JButton metalButton;private JButton motifButton;private JButton windowsButton;class PlafFrame extends JFramepublic PlafFrame()setTitle(“simple“);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowE

6、vent e)System.exit(0););Container contentPane=getContentPane();contentPane.add(new PlafPanel();public class java2public static void main(Stringargs)JFrame frame=new PlafFrame();frame.show();(分数:40.00)_三、综合应用题(总题数:1,分数:30.00)3.本程序的功能是获取文本框中的文本。窗口中有两个文本框“用户名”和“密码”,以及三个按钮“登录”、“其他用户登录”和“关闭”,初始状态“用户名”文本框

7、是只读的,单击“其他用户登录”按钮后变成可写的,“密码”文本框使用的不是密码文本框,在用户键入的时候设置显示为*号。输入用户名和密码后,单击“登录”按钮后,如果输入的密码为空,则弹出提示消息框,否则后台将显示输入的用户名和密码。比如显示为“admin 用户的密码:password”(admin 为输入的用户名,password 为输入密码)。import java.awt.*;import java.awt.event.*;import javax.swing.JOptionPane;public class java3public static void main(String args)f

8、inal Frame frmFrame=new Frame();Panel pnlPanel=new Panel();Label lblUsername=new Label(“用户名“);Label lblPassword=new Label(“密码“);final TextField txtUsername=new TextField(“Student“);final TextField txtPassword=new TextField(“.8);txtUsername.setEditable(false);txtPassword.setChar(*);Button btnButton1=

9、new Button(“登录“);Button btnButton2=new Button(“其他用户登录“);Button btnButton3=new Button(“关闭“);btnButton1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(txtPassword.getText().length()=0)JOptionPane.showMessageDialog(frmFrame,“密码不能为空“);return;txtPassword.setColumns(16)

10、;System.out.println(txtUsername.getText()+“用户的密码:“+txtPassword.getPassword(););btnButton2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)txtUsername.setEnable(true););btnButton3.addActionListener(new ActionListener()public void aetionPerformed(ActionEvent e)System.ex

11、it(0););pnlPanel.add(lblUsername);pnlPanel.add(txtUsername);pnlPanel.add(lblPassword);pnlPanel.add(txtPassword);pnlPanel.add(btnButtonl);pnlPanel.add(btnButton2);pnlPanel.add(btnButton3);frmFrame.add(pnlPanel);frmFrame.setTitle(“advance“);frmFrame.pack();frmFrame.show();(分数:30.00)_二级 JAVA 机试-69 答案解析

12、(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题中定义了一个长度为 20 的整数数组,然后将 120 分别赋给数组元素,计算该数组中所有下标为奇数的元素的和。public class java1public static void main(string args)int sum;_;int arrayLis=new int20;for(int i=0;i=19;i+)arrayListi=i+1;int pos=0;while(pos20)if(_)sum-sum+arrayListpos;_;System.out.println(“sum=

13、“+sum);(分数:30.00)_正确答案:(第 1 处:sum=0第 2 处:pos%2=1 或 pos%2!=0第 3 处:pos+或 pos+=1 或 pos=pos+1)解析:解析 第 1 处给整型变量 sum 赋初值;第 2 处为判断数组中元素下标为奇数的条件;第 3 处是元素下标加 1 继续遍历。二、简单应用题(总题数:1,分数:40.00)2.本题的功能是通过按钮来选择窗口显示的风格。窗口中有三个按钮:“Metal”、“Motif”和“Windows”,单击任何一个按钮,就能将窗口的风格改变为按钮名称所对应的风格。import java.awt.*;import java.aw

14、t.event.*;import javax.swing.*;class PlafPanel extends JPanei implements ActionListenerpublic_()metalButton=new JButton(“Metal“);motifButton=new JButton(“Motif“);windowsButton=new JButton(“Windows“);add(metalButton);add(motifButton);add(windowsButton);metalButton.addActionListener(this);motifButton.

15、addActionListener(this);windowsButton.addActionListener(this);public void actionPerformed(ActionEvent evt)Object source=evt.getSource();String plaf=“;if(source=metalButton)plaf=“javax.swing.plaf.metal.MetalLookAndFeel“;else if(source=motifButton)plaf=“com.sun.java.swing.plaf.motif.MotifLookAndFeel“;

16、else if(source=windowsButton)plaf=“com.sun.java.swing.plaf.windows.WindowsLookAndFeel“;tryUIManager.setLookAndFeel(_);SwingUtilities.updateComponentTreeUI(this);catch(Exception e)private JButton metalButton;private JButton motifButton;private JButton windowsButton;class PlafFrame extends JFramepubli

17、c PlafFrame()setTitle(“simple“);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););Container contentPane=getContentPane();contentPane.add(new PlafPanel();public class java2public static void main(Stringargs)JFrame frame=new PlafFrame();fram

18、e.show();(分数:40.00)_正确答案:(第 1 处:PlafPanel第 2 处:plaf)解析:解析 第一处由题意可知,类 PlafPanel 缺少构造函数,所以填 PlafPanel。第二处是通过String 类型的对象 plaf,和 UIManager.setLookAndFeel()方法实现显示风格的切换。三、综合应用题(总题数:1,分数:30.00)3.本程序的功能是获取文本框中的文本。窗口中有两个文本框“用户名”和“密码”,以及三个按钮“登录”、“其他用户登录”和“关闭”,初始状态“用户名”文本框是只读的,单击“其他用户登录”按钮后变成可写的,“密码”文本框使用的不是密

19、码文本框,在用户键入的时候设置显示为*号。输入用户名和密码后,单击“登录”按钮后,如果输入的密码为空,则弹出提示消息框,否则后台将显示输入的用户名和密码。比如显示为“admin 用户的密码:password”(admin 为输入的用户名,password 为输入密码)。import java.awt.*;import java.awt.event.*;import javax.swing.JOptionPane;public class java3public static void main(String args)final Frame frmFrame=new Frame();Panel

20、 pnlPanel=new Panel();Label lblUsername=new Label(“用户名“);Label lblPassword=new Label(“密码“);final TextField txtUsername=new TextField(“Student“);final TextField txtPassword=new TextField(“.8);txtUsername.setEditable(false);txtPassword.setChar(*);Button btnButton1=new Button(“登录“);Button btnButton2=ne

21、w Button(“其他用户登录“);Button btnButton3=new Button(“关闭“);btnButton1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(txtPassword.getText().length()=0)JOptionPane.showMessageDialog(frmFrame,“密码不能为空“);return;txtPassword.setColumns(16);System.out.println(txtUsername.getTe

22、xt()+“用户的密码:“+txtPassword.getPassword(););btnButton2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)txtUsername.setEnable(true););btnButton3.addActionListener(new ActionListener()public void aetionPerformed(ActionEvent e)System.exit(0););pnlPanel.add(lblUsername);pnl

23、Panel.add(txtUsername);pnlPanel.add(lblPassword);pnlPanel.add(txtPassword);pnlPanel.add(btnButtonl);pnlPanel.add(btnButton2);pnlPanel.add(btnButton3);frmFrame.add(pnlPanel);frmFrame.setTitle(“advance“);frmFrame.pack();frmFrame.show();(分数:30.00)_正确答案:(第 1 处:txtPassword.setEchoChar(*)第 2 处:txtPassword.getText()第 3 处:txtUsername.setEditable(true)解析:解析 第一处是通过 TextField 类的 setEchoChar 函数设置用户输入时,文本框显示的文本。第二处是通过 TextField 类的 getText 函数获取用户的输入,即得到密码值。第三处是使用户名文本框变为可用,使用户可以输入。

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

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

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