1、(A)二级 JAVA 笔试-5 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题中,在下画线上填写代码,指定变量 b 为字节型,变量 f 为单精度实型,变量 l 为 64 位整型。public class java1public static void main(String args)_b=49;_f=8.9f;_1=0xfedl;System. out. println(“b=“+b);System. out. println(“f=“+f);System. out. println(“1=“+1);(分数:30.00)填空项 1
2、:_二、简单应用题(总题数:1,分数:30.00)2.本题是一个表格式的成绩单,其中包括“姓名”、“英语成绩”、“数学成绩”和“总成绩”,姓名和成绩都可以进行修改,单击按钮“计算每人总成绩”,则可以统计出每个人的总成绩并显示在总成绩栏中。import javax. swing. * ;import java. awt. * ;import java. awt. event. * ;public class java2 extends JFrame implements ActionListenerJTable table; Object a;Object name=“姓名“, “英语成绩“,
3、“数学成绩“, “总成绩“;JButton button;java2()setTitle(“java2“);a=new Object84;for(int i=0; i8; i+)for(int j=0; j4; j+)if(j!=0)aij=“;elseaij=“;button=new JButton(“计算每人总成绩“);table=_;button, addActionListener(this);getContentPane(). add(new JScrollPane(table), BorderLayout. CENTER);getContentPane (). add(button
4、, BorderLayout. SOUTH);setSize(400,200);setVisible(true);validate();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System. exit(0););public void actionPerformed(ActionEvent e)for(int i=0; i8; i+)double sum=0;boolean boo=true;for(int j=l; j=2; j+)trysum=sum+Double. parse
5、Double(_);catch(Exception ee)boo=false;table. repaint();if(boo=true)ai3=“+sum;table. repaint();public static void main(String args)java2 Win=new java2();(分数:30.00)填空项 1:_三、综合应用题(总题数:1,分数:40.00)3.本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出来。import java. awt. * ;import java. awt. event. * ;i
6、mport javax. swing. * ;public class java3 extends JFrame implements ActionListenerpublic java3()setTitle(“java3“);setSize(300,300);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System. exit(0););JMenuBar mbar=new JMenuBar();setJMenuBar(bar);JMenu fileMenu=new JMenu(“Fi
7、le“);mbar. add(fileMenu);connectItem=new JMenuItem(“Connect“);connectltem. addActionListener(this);fileMenu. add(connectItem);exitItem=new JMenuItem(“Exit“);exitItem. addActionListener(this);fileMenu. add(exitItem);public void actionPerformed(ActionEvent evt)Object source=evt. getSource();if (source
8、=connectItem)ConnectInfo transfer=new ConnectInfo(“yourname“, “pw“);if(dialog=null)dialog=new ConnectDialog(this);if(dialog. showDialog(transfer)String uname=transfer. username;String pwd=transfer. password;Container contentPane=getContentPane();contentPane. add(new JLabel(“username=“+uname+“, passw
9、ord=“+pwd), “South“);validate();else if(source=exitItem)System. exit(0);public static void main(String args)JFrame f=new java3();f. show();private ConnectDialog dialog=null;private JMenuItem connectltem;private JMenuItem exitItem;class Connectlnfopublic String username;public String password;public
10、ConneetInfo(String u, String p)username=u; password=p;class ConnectDialog extends JDialog implements ActionListenerpublic ConnectDialog()super(parent, “Connect“, true);Container contentPane=getContentPane();JPanel p1=new JPanel();p1. setLayout(new GridLayout(2,2);p1. add(new JLabel(“User name:“);p1.
11、 add(username=new JTextField(“);p1. add(new JLabel(“Password:“);p1. add(password=new JPasswordField(“)contentPane. add(“Center“, p1);Panel p2=new Panel();okButton=addButton(p2,“Ok“);cancelButton=addButton(p2, “Cancel“);contentPane. add( “South“, p2);setSize(240,120);JButton addButton(Container c, St
12、ring name)JButton button=new JButton(name);button. addActionListener(this);c. add(button);return button;public void actionPerformed(ActionEvent evt)Object source=evt. getSource();if(source=okButton)ok=true;setVisible(false);else if (source=cancelButton)setVisible(faIse);public void showDialog(Connec
13、tInfo transfer)username. setText(transfer. username);password. setText(transfer. password);ok=false;show();if(ok)transfer. username=username, getText();transfer. password=new String(password. getPassword();return ok;private JTextField username;private JPasswordField password;private boolean ok;priva
14、te JButton okButton;private JButton cancelButton;(分数:40.00)填空项 1:_(A)二级 JAVA 笔试-5 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题中,在下画线上填写代码,指定变量 b 为字节型,变量 f 为单精度实型,变量 l 为 64 位整型。public class java1public static void main(String args)_b=49;_f=8.9f;_1=0xfedl;System. out. println(“b=“+b);System.
15、out. println(“f=“+f);System. out. println(“1=“+1);(分数:30.00)填空项 1:_ (正确答案:第 1 处:byte第 2 处:float第 3 处:long)解析:解析 本题考查的是数据类型。byte 为字节型;float 为单精度实型;long 为长整型。二、简单应用题(总题数:1,分数:30.00)2.本题是一个表格式的成绩单,其中包括“姓名”、“英语成绩”、“数学成绩”和“总成绩”,姓名和成绩都可以进行修改,单击按钮“计算每人总成绩”,则可以统计出每个人的总成绩并显示在总成绩栏中。import javax. swing. * ;imp
16、ort java. awt. * ;import java. awt. event. * ;public class java2 extends JFrame implements ActionListenerJTable table; Object a;Object name=“姓名“, “英语成绩“, “数学成绩“, “总成绩“;JButton button;java2()setTitle(“java2“);a=new Object84;for(int i=0; i8; i+)for(int j=0; j4; j+)if(j!=0)aij=“;elseaij=“;button=new JB
17、utton(“计算每人总成绩“);table=_;button, addActionListener(this);getContentPane(). add(new JScrollPane(table), BorderLayout. CENTER);getContentPane (). add(button, BorderLayout. SOUTH);setSize(400,200);setVisible(true);validate();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)S
18、ystem. exit(0););public void actionPerformed(ActionEvent e)for(int i=0; i8; i+)double sum=0;boolean boo=true;for(int j=l; j=2; j+)trysum=sum+Double. parseDouble(_);catch(Exception ee)boo=false;table. repaint();if(boo=true)ai3=“+sum;table. repaint();public static void main(String args)java2 Win=new j
19、ava2();(分数:30.00)填空项 1:_ (正确答案:第 1 处:new JTable(a, name)第 2 处:aij. toString())解析:解析 第 1 处初始化表格变量;第 2 处取得表格中单元格内容并转换成 Double 型计算出总成绩。三、综合应用题(总题数:1,分数:40.00)3.本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出来。import java. awt. * ;import java. awt. event. * ;import javax. swing. * ;public class ja
20、va3 extends JFrame implements ActionListenerpublic java3()setTitle(“java3“);setSize(300,300);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System. exit(0););JMenuBar mbar=new JMenuBar();setJMenuBar(bar);JMenu fileMenu=new JMenu(“File“);mbar. add(fileMenu);connectItem=n
21、ew JMenuItem(“Connect“);connectltem. addActionListener(this);fileMenu. add(connectItem);exitItem=new JMenuItem(“Exit“);exitItem. addActionListener(this);fileMenu. add(exitItem);public void actionPerformed(ActionEvent evt)Object source=evt. getSource();if (source=connectItem)ConnectInfo transfer=new
22、ConnectInfo(“yourname“, “pw“);if(dialog=null)dialog=new ConnectDialog(this);if(dialog. showDialog(transfer)String uname=transfer. username;String pwd=transfer. password;Container contentPane=getContentPane();contentPane. add(new JLabel(“username=“+uname+“, password=“+pwd), “South“);validate();else i
23、f(source=exitItem)System. exit(0);public static void main(String args)JFrame f=new java3();f. show();private ConnectDialog dialog=null;private JMenuItem connectltem;private JMenuItem exitItem;class Connectlnfopublic String username;public String password;public ConneetInfo(String u, String p)usernam
24、e=u; password=p;class ConnectDialog extends JDialog implements ActionListenerpublic ConnectDialog()super(parent, “Connect“, true);Container contentPane=getContentPane();JPanel p1=new JPanel();p1. setLayout(new GridLayout(2,2);p1. add(new JLabel(“User name:“);p1. add(username=new JTextField(“);p1. ad
25、d(new JLabel(“Password:“);p1. add(password=new JPasswordField(“)contentPane. add(“Center“, p1);Panel p2=new Panel();okButton=addButton(p2,“Ok“);cancelButton=addButton(p2, “Cancel“);contentPane. add( “South“, p2);setSize(240,120);JButton addButton(Container c, String name)JButton button=new JButton(n
26、ame);button. addActionListener(this);c. add(button);return button;public void actionPerformed(ActionEvent evt)Object source=evt. getSource();if(source=okButton)ok=true;setVisible(false);else if (source=cancelButton)setVisible(faIse);public void showDialog(ConnectInfo transfer)username. setText(trans
27、fer. username);password. setText(transfer. password);ok=false;show();if(ok)transfer. username=username, getText();transfer. password=new String(password. getPassword();return ok;private JTextField username;private JPasswordField password;private boolean ok;private JButton okButton;private JButton cancelButton;(分数:40.00)填空项 1:_ (正确答案:第 1 处:setJMenuBar(mbar)第 2 处:public ConnectDialog(JFrame parent)第 3 处:public Boolean showDialog(C0nnectlnfotransfer))解析:解析 第 1 处参数错误,bar 未定义;第 2 处从下一行的 super(parent, “Connect“, true);可以看出,这里需要的参数为父窗体;第 3 处从下面的 return ok;等可以看出,这是一个有 Boolean 型返回值的函数,故类型应为 Boolean。