1、二级 JAVA 机试-206 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序首先给一个数组赋值,然后计算该数组中下标为奇数的元素的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic 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(_
2、)sum = sum + arrayListpos;_;System.out.println(“sum =“+ sum);(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是:单击窗口左上角的 File 菜单中的 Open 命令,打开一个 GIF 图像文件,并将图像文件显示在窗口中;单击 File 菜单中的 Exit 命令,能够退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.awt
3、.image.*;import java.io.*;public class simple extends JFrame implements ActionListenerpublic simple()setTitle (“ImageViewer“ );setSize(300,400);JMenuBar mbar = new JMenuBar();JMenu m = new JMenu(“File“);openItem = new JMenuItem(“Open“);openItem,addActionListener (this);m.add (openItem);exitItem = ne
4、w JMenuItem(“Exit“);exitItem.addActionListener(this);m.add(exitItem);mbar.add (m);setJMenuBar (mbar);label = new JLabel();Container contentPane = getContentPane();contentPane.add(label,“Center“);public void actionPerformed(ActionEvent evt)Object source = evt.getSource();if (source = openItem)JFileCh
5、ooser chooser = new JFileChooser();chooser.setCurrentDirectory(new File(“.“);chooser.setFileFilter(new javax.swing.filechooser.FileFilter()public boolean accept(File f)return f.getName().toLowerCase().endsWith(“.gif“);public String getDescription()return “GIF Images“;);int r = chooser.showOpenDialog
6、(this);if(r = JFileChooser.APPROVE_OPTION)String name = chooser.getSelectedFile().getName();label.setIcon(_);elseif(source = exitItem)System.exit(0);public static void main(String args)JFrame frame = new simple();_;private JLabel label;private JMenuItem openItem;private JMenuItem exitItem;(分数:40.00)
7、_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是计算圆和三角形的面积。通过菜单“选择”可以分别进行圆和三角形面积的计算。单击菜单项“圆面积计算”,窗口中就会显示两个文本框和一个“确定”按钮,在第一个文本框中输入圆的半径,单击“确定”按钮后就可以在第二个文本框中显示圆的面积。单击菜单项“三角形面积计算”,窗口中就会显示 4 个文本框和一个“确定”按钮,在前三个文本框中分别输入三角形三个边的长度,单击“确定”按钮后,如果三个边的长度不能组成三角形,结果文本框中会给出提示信息,否则显示三角形的面积;如果输入的值不是数值,则会给出提示信息。请将下述程序补充完整(注意:不得改动程序
8、的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;class circle extends Panel implements AetionListenerdouble r,area;TextField radius = null,result = null;Button b = null;_;radius = new TextField(10);result = new TextField(10);b = new Button(“确定“);add (new Label (“输入半径“);add (radius);add(new Lab
9、el(“面积是“);add(result);add(b);b.addActionListener (this);result,setEnabled (false);public void actionPerformed(ActionEvent e)tryr = Double.parseDouble (radius.getText ();area =Math.PI*r*r;result,setText (“+area);catch (Exception ee)radius.setText (“请输入数字字符“);class triangle extends Panel implements Ac
10、tionListenerdouble a = 0,b = 0,c = 0,area;TextField border a = new TextField(6) ;TextField border b = new TextField(6) ;TextField border c = new TextField(6) ;Result = new TextField(24);Button button = new Button(“确定“);triangle ()add(new Label(“输入三边的长度“);add (border_a);add (border_b);add (border_c);
11、add(new Label(“面积是:“);add (result);add (button);button,addActionListener (this);result.setEnabled(false);public void actionPerformed(ActionEvent e)trya = Double.parseDoubleborder_a.getText();b = Double.parseDouble(border_b.getText();c = Double.parseDouble(border_c.getText();if(a+bca+cbc+ba)double p
12、= (a+b+c)/2;area = Math.sqrt(p*(p-a)*(p-b)*(p-c);result.setText(“+ area);elseresult.setText (“您输入的数字不能形成三角形“);catch(Exception ee)result.setText (“请输入数字字符“);class Win _ implements ActionListenerMenuBar bar = null;Menu menu = null;MenuItem item1,item2;circle circle;triangle trangle;Win()bar = new Menu
13、Bar(); menu = new Menu(“选择“);setSize(300,200);item1 = new MenuItem(“圆面积计算“);item2 = new MenuItem(“三角形面积计算“);menu.add(item1);menu.add(item2);bar.add(menu);setMenuBar(bar);circle = new circle();trangle = new triangle();item1.addActionListener(this);item2.addActionListener(this);setVisible(true);public
14、 void actionPerformed(ActionEvent e)if (e.getSource() = item1)removeAll();add(circle,“Center“);validate();elseif(e.getSource() = item2)removeAll ();add (trangle,“Center“);validate ();public class advancepublic static void main (String args)Win win = new Win();win.setTitle (“advance “);win.setBounds
15、(100,100,700,300);win.setVisible (true);win.addWindowListener (_)public void windowClosing(WindowEvent e)System.exit (0););(分数:30.00)_二级 JAVA 机试-206 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序首先给一个数组赋值,然后计算该数组中下标为奇数的元素的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static v
16、oid 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 + arrayListpos;_;System.out.println(“sum =“+ sum);(分数:30.00)_正确答案:(sum=0。pos%2=1。pos+。)解析:二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是:单击窗口左上角的 File 菜单中的 Open 命令,打开一个
17、 GIF 图像文件,并将图像文件显示在窗口中;单击 File 菜单中的 Exit 命令,能够退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;public class simple extends JFrame implements ActionListenerpublic simple()setTitle (“ImageViewer“ );setSize(300
18、,400);JMenuBar mbar = new JMenuBar();JMenu m = new JMenu(“File“);openItem = new JMenuItem(“Open“);openItem,addActionListener (this);m.add (openItem);exitItem = new JMenuItem(“Exit“);exitItem.addActionListener(this);m.add(exitItem);mbar.add (m);setJMenuBar (mbar);label = new JLabel();Container conten
19、tPane = getContentPane();contentPane.add(label,“Center“);public void actionPerformed(ActionEvent evt)Object source = evt.getSource();if (source = openItem)JFileChooser chooser = new JFileChooser();chooser.setCurrentDirectory(new File(“.“);chooser.setFileFilter(new javax.swing.filechooser.FileFilter(
20、)public boolean accept(File f)return f.getName().toLowerCase().endsWith(“.gif“);public String getDescription()return “GIF Images“;);int r = chooser.showOpenDialog(this);if(r = JFileChooser.APPROVE_OPTION)String name = chooser.getSelectedFile().getName();label.setIcon(_);elseif(source = exitItem)Syst
21、em.exit(0);public static void main(String args)JFrame frame = new simple();_;private JLabel label;private JMenuItem openItem;private JMenuItem exitItem;(分数:40.00)_正确答案:(new ImageIcon(name)。frame.show()。)解析:三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是计算圆和三角形的面积。通过菜单“选择”可以分别进行圆和三角形面积的计算。单击菜单项“圆面积计算”,窗口中就会显示两个文本
22、框和一个“确定”按钮,在第一个文本框中输入圆的半径,单击“确定”按钮后就可以在第二个文本框中显示圆的面积。单击菜单项“三角形面积计算”,窗口中就会显示 4 个文本框和一个“确定”按钮,在前三个文本框中分别输入三角形三个边的长度,单击“确定”按钮后,如果三个边的长度不能组成三角形,结果文本框中会给出提示信息,否则显示三角形的面积;如果输入的值不是数值,则会给出提示信息。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;class circle extends Panel implements Ae
23、tionListenerdouble r,area;TextField radius = null,result = null;Button b = null;_;radius = new TextField(10);result = new TextField(10);b = new Button(“确定“);add (new Label (“输入半径“);add (radius);add(new Label(“面积是“);add(result);add(b);b.addActionListener (this);result,setEnabled (false);public void a
24、ctionPerformed(ActionEvent e)tryr = Double.parseDouble (radius.getText ();area =Math.PI*r*r;result,setText (“+area);catch (Exception ee)radius.setText (“请输入数字字符“);class triangle extends Panel implements ActionListenerdouble a = 0,b = 0,c = 0,area;TextField border a = new TextField(6) ;TextField bord
25、er b = new TextField(6) ;TextField border c = new TextField(6) ;Result = new TextField(24);Button button = new Button(“确定“);triangle ()add(new Label(“输入三边的长度“);add (border_a);add (border_b);add (border_c);add(new Label(“面积是:“);add (result);add (button);button,addActionListener (this);result.setEnabl
26、ed(false);public void actionPerformed(ActionEvent e)trya = Double.parseDoubleborder_a.getText();b = Double.parseDouble(border_b.getText();c = Double.parseDouble(border_c.getText();if(a+bca+cbc+ba)double p = (a+b+c)/2;area = Math.sqrt(p*(p-a)*(p-b)*(p-c);result.setText(“+ area);elseresult.setText (“您
27、输入的数字不能形成三角形“);catch(Exception ee)result.setText (“请输入数字字符“);class Win _ implements ActionListenerMenuBar bar = null;Menu menu = null;MenuItem item1,item2;circle circle;triangle trangle;Win()bar = new MenuBar(); menu = new Menu(“选择“);setSize(300,200);item1 = new MenuItem(“圆面积计算“);item2 = new MenuIte
28、m(“三角形面积计算“);menu.add(item1);menu.add(item2);bar.add(menu);setMenuBar(bar);circle = new circle();trangle = new triangle();item1.addActionListener(this);item2.addActionListener(this);setVisible(true);public void actionPerformed(ActionEvent e)if (e.getSource() = item1)removeAll();add(circle,“Center“);
29、validate();elseif(e.getSource() = item2)removeAll ();add (trangle,“Center“);validate ();public class advancepublic static void main (String args)Win win = new Win();win.setTitle (“advance “);win.setBounds (100,100,700,300);win.setVisible (true);win.addWindowListener (_)public void windowClosing(WindowEvent e)System.exit (0););(分数:30.00)_正确答案:(circle()。extends Frame。new WindowAdapter()。)解析: