1、二级 JAVA 机试-89 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题中定义了长度为 20 的一维整型数组 a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。public class java1(public static void main(Stringargs)int a=_;int i;for(_;i+)ai=i;for(i=0;i20;i+)if(_)System.out.print(“a“+i+“=“+ai+“,“);(分数:30.00)_二、简单应用题(总题数:1,分数:40.00)2.本题中
2、定义了一个树型的通信录,窗口左侧是一个树,右侧是一个文本域,单击树的结点,则在右侧文本域中显示相关信息,如果单击的是树结点,则显示对应名字的电话信息。import javax.swing.*;import javax.swing.tree.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;class Mytree2 extends JFrameJTree tree=null;JTextArea text=new JTextArea(20,20);Mytree2()Container con=getCon
3、tentPane();DefaultMutableTreeNode root=new DefauhMutableTreeNode(“同学通信录“);DefaultMutableTreeNode t1=new DefauhMutableTreeNode(“大学同学“);DefaultMutableTreeNode t2=new DefaultMutableTreeNode(“研究生同学“);DefaultMutableTreeNode t1_1=new DefauhMutableTreeNode(“陈艳“);DefaultMutableTreeNode t1_2=new DefauhMutabl
4、eTreeNode(“李小永“);DefaultMutableTreeNode t2_1=new DefauhMutableTreeNode(“王小小“);DefaultMutableTreeNode t2_2=new DefauhMutableTreeNode(“董小“);setTitle(“java2“);root.add(t1);root.add(t2);t1.add(t1_1);t1.add(t1_2);t2.add(t2_1);t2.add(t2_2);tree=new ITree(root);JScrollPane scrollpane=new JScrollPane(text);
5、JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,tree,scrollpane);tree.addTreeSelectionListener(this);con.add(splitpane);addWindowListener(Flew WindowAdapter()public void windowClosing(WindowEvent e)System.ext(0););setVisible(true);setBounds(70,80,200,300);public void valueChange
6、d(TreeSelectionEvent e)if(e.getSource()=tree)DefaultMutableTreeNode node=(DefauhMutableTreeNode)tree.getLastSelectedPathComponent();if(node.isLeaf()String str=_;if(str.equals(“陈艳“)text.setText(str+“:联系电话:0411-4209876“);else if(str.equals(“李小永“)text.setText(str+“:联系电话:010-62789876“);else if(str.equal
7、s(“王小小“)text.setText(str+“:联系电话:0430-63596677“);else if(str.equals(“董小“)text.setText(str+“:联系电话:020-85192789“);)elsetext.setText(node.getUserObject().toString();public class java2public static void main(String args)Mytree2 win=new Mytree2();win.pack();(分数:40.00)_三、综合应用题(总题数:1,分数:30.00)3.本题中,鼠标在窗口中单击
8、一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。import java.awt.*;import java.awt.event.*;import javax.swing.*;class MousePanel extends JPanel extends MouseMotionListenerpublic MousePanel()addMouseListener(new MouseAdapter()public void mousePressed(MouseEvent evt)int x=evt.getX();int y=evt.getY();current=fin
9、d(x,y);if(current0)add(x,y);public void mouseClicked(MouseEvent evt)int x=evt.getX();int y=evt.getY();if(evt.getClickCount()=2)remove(current););addMouseMorionListener(this);public void paintComponent(Graphics g)(super.paintComponent();for(int i=0;insquares;i+)draw(g,i);public int find(int x,int y)f
10、or(int i=0;insquares;i+)if(squaresi.x-SQUARELENGTH/2=xreturn-1;public void draw(Graphics g,int i)g.drawRect(squaresi.x-SQUARELENGTH/2,squaresi.y-SQUARELENGTH/2,SQUARELENGTH,SQUARELENGTH);public void add(int x,int y)if(nsquaresMAXNSQUARES)squaresnsquares=new Point(x,y);current=nsquares;nsquares+;repa
11、int();public void remove(int n)if(n0 | n=nsquares)return;nsquares-;squaresn=squaresnsquares;if(current=n)current=-1;repaint();public void mouseMoved(MouseEvent evt)public void mouseDragged(MouseEvent evt)private static final int SQUARELENGTH=10;private static final int MAXNSQUARES=100;private Point
12、squares=new PointMAXNSQUARES;private int nsquares=0;private int current=-1;class MouseFrame extends JFramepublic MouseFrame()setTitle(“java3“);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););Container contentPane=getContentPane();content
13、Pane.add(MousePanel();public class java3public static void main(Stringargs)JFrame frame=new MouseFrame();frame.show();(分数:30.00)_二级 JAVA 机试-89 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题中定义了长度为 20 的一维整型数组 a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。public class java1(public static void main(Strin
14、gargs)int a=_;int i;for(_;i+)ai=i;for(i=0;i20;i+)if(_)System.out.print(“a“+i+“=“+ai+“,“);(分数:30.00)_正确答案:(第 1 处:new int20第 2 处:i=0;i20第 3 处:i%2! =0)解析:解析 第 1 处定义了长度为 20 的一维整型数组 a;第 2 处的 for 循环将数组元素的下标值赋给数组元素;第 3 处判断数组各个元素下标是否为奇数。二、简单应用题(总题数:1,分数:40.00)2.本题中定义了一个树型的通信录,窗口左侧是一个树,右侧是一个文本域,单击树的结点,则在右侧文本
15、域中显示相关信息,如果单击的是树结点,则显示对应名字的电话信息。import javax.swing.*;import javax.swing.tree.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;class Mytree2 extends JFrameJTree tree=null;JTextArea text=new JTextArea(20,20);Mytree2()Container con=getContentPane();DefaultMutableTreeNode root=new
16、DefauhMutableTreeNode(“同学通信录“);DefaultMutableTreeNode t1=new DefauhMutableTreeNode(“大学同学“);DefaultMutableTreeNode t2=new DefaultMutableTreeNode(“研究生同学“);DefaultMutableTreeNode t1_1=new DefauhMutableTreeNode(“陈艳“);DefaultMutableTreeNode t1_2=new DefauhMutableTreeNode(“李小永“);DefaultMutableTreeNode t2_
17、1=new DefauhMutableTreeNode(“王小小“);DefaultMutableTreeNode t2_2=new DefauhMutableTreeNode(“董小“);setTitle(“java2“);root.add(t1);root.add(t2);t1.add(t1_1);t1.add(t1_2);t2.add(t2_1);t2.add(t2_2);tree=new ITree(root);JScrollPane scrollpane=new JScrollPane(text);JSplitPane splitpane=new JSplitPane(JSplitP
18、ane.HORIZONTAL_SPLIT,true,tree,scrollpane);tree.addTreeSelectionListener(this);con.add(splitpane);addWindowListener(Flew WindowAdapter()public void windowClosing(WindowEvent e)System.ext(0););setVisible(true);setBounds(70,80,200,300);public void valueChanged(TreeSelectionEvent e)if(e.getSource()=tre
19、e)DefaultMutableTreeNode node=(DefauhMutableTreeNode)tree.getLastSelectedPathComponent();if(node.isLeaf()String str=_;if(str.equals(“陈艳“)text.setText(str+“:联系电话:0411-4209876“);else if(str.equals(“李小永“)text.setText(str+“:联系电话:010-62789876“);else if(str.equals(“王小小“)text.setText(str+“:联系电话:0430-635966
20、77“);else if(str.equals(“董小“)text.setText(str+“:联系电话:020-85192789“);)elsetext.setText(node.getUserObject().toString();public class java2public static void main(String args)Mytree2 win=new Mytree2();win.pack();(分数:40.00)_正确答案:(第 1 处:implements TreeSelectionListener第 2 处:node.toString()解析:解析 第 1 处实现了一
21、个 JTree 的监听器接口;第 2 处将 node 转换成 String 型。三、综合应用题(总题数:1,分数:30.00)3.本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。import java.awt.*;import java.awt.event.*;import javax.swing.*;class MousePanel extends JPanel extends MouseMotionListenerpublic MousePanel()addMouseListener(new MouseAdapter()public v
22、oid mousePressed(MouseEvent evt)int x=evt.getX();int y=evt.getY();current=find(x,y);if(current0)add(x,y);public void mouseClicked(MouseEvent evt)int x=evt.getX();int y=evt.getY();if(evt.getClickCount()=2)remove(current););addMouseMorionListener(this);public void paintComponent(Graphics g)(super.pain
23、tComponent();for(int i=0;insquares;i+)draw(g,i);public int find(int x,int y)for(int i=0;insquares;i+)if(squaresi.x-SQUARELENGTH/2=xreturn-1;public void draw(Graphics g,int i)g.drawRect(squaresi.x-SQUARELENGTH/2,squaresi.y-SQUARELENGTH/2,SQUARELENGTH,SQUARELENGTH);public void add(int x,int y)if(nsqua
24、resMAXNSQUARES)squaresnsquares=new Point(x,y);current=nsquares;nsquares+;repaint();public void remove(int n)if(n0 | n=nsquares)return;nsquares-;squaresn=squaresnsquares;if(current=n)current=-1;repaint();public void mouseMoved(MouseEvent evt)public void mouseDragged(MouseEvent evt)private static fina
25、l int SQUARELENGTH=10;private static final int MAXNSQUARES=100;private Point squares=new PointMAXNSQUARES;private int nsquares=0;private int current=-1;class MouseFrame extends JFramepublic MouseFrame()setTitle(“java3“);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(
26、WindowEvent e)System.exit(0););Container contentPane=getContentPane();contentPane.add(MousePanel();public class java3public static void main(Stringargs)JFrame frame=new MouseFrame();frame.show();(分数:30.00)_正确答案:(第 1 处:extends JPanel implements MouseMotionListener第 2 处:super.paintComponent(g)第 3 处:contentPane.add(new MousePanel()解析:解析 第 1 处是继承 Jpanel 实现鼠标移动监听器接口;第 2 处以 g 为参数重新绘制组件;第 3 处在 contentPane 内容面板中添加一个 MousePanel 鼠标面板。