1、二级 JAVA 机试-48 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题的功能是计算二维数组各个元素的和。程序中定义了二维数组 arr,arr 有 3 行 4 列共 12 个元素,程序中采用 for 循环语句的嵌套来计算数组中各个元素的和,并将结果保存在 sum 变量中,最后打印输出结果。public class java1public static void main(Stringargs)int arr=1,2,3,4,5,6,7,8,(9,10,11,12;int sum=0;int i=0,j=0;for(i=0;_)fo
2、r(_)_;System.out.println(“sum=“+sum);(分数:30.00)_二、简单应用题(总题数:1,分数:40.00)2.本题是设计一个窗体,窗口的背景色为系统桌面的颜色,在窗口中分别画了空心和实心的黑色矩形、深灰色圆角矩形和浅灰色椭圆形,并且画了白色粗体的“Java 二级考试!”字符串。Import java.awt.*;Import javax.swing.*;Public class java2Public static void main(stringargs)Fillframe frame=new fillframe();Frame.setdefaultclo
3、seoperation(jframe.exit_on_close);Frame.show();Class fillframe extends jframePublic fillframe()Settitle(“simple“);Setsize(width,height);Fillpanel panel=new fillpanel();Panel.setbackground(systemcolor.desktop);Container contentpane=getcontentpane();Contentpane.add(panel);Public static final int width
4、=400;Public static final int width=250;Class fillpanel extends jpanelPublic void paintcomponent(graphics g)_;g.setcolor(new color(10,10,10);g.drawrect(10,10,100,30);g.setcolor(new color(100,100,100);g.drawroundrect(150,10,100,30,15,15);g.setcolor(new color(150,150,150);g.drawoval(280,10,80,30);g.set
5、color(new color(10,10,10);g.fillrect(10,110,100,30);g.setcolor(new color(100,100,100);g.drawroundrect(150,110,100,30,15,15);g.setcolor(new color(150,150,150);g.filloval(280,110,80,30);g.setcolor(color.white),font f=new font(“宋体“,_,20);g.setfont(f);g.drawstring(“java 二级考试!“,150,200);(分数:40.00)_三、综合应用
6、题(总题数:1,分数:30.00)3.本题的功能是监听鼠标的操作。鼠标置于窗口中单击时(左键或右键),在单击的地方会画一个小矩形,如果将鼠标置于小矩形上,则鼠标光标状态改为小十字,按下鼠标左键可拖曳,双击鼠标左键(或右键)时,小矩形消失。import java.awt.*;import java.awt.event.*;import java.util.*;import java.awt.geom.*;import javax.swing.*;public class java3public static void main(Stringargs)MouseFrame frame=new Mo
7、useFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();class MouseFrame extends JFramepublic MouseFrame()setTitle(“java3“);setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);MousePanel panel=new MousePanel();Container contentPane=geeContentPane();contentPane.add(panel);public static final i
8、nt DEFAULT_WIDTH=300;public static final int DEFAULT_HEIGHT=200;class MousePanel extends JPanelpublic MousePanel()squares=new ArrayList();current=null;addMouseListener(new MouseHandler();addMouseMotionListener(new MouseMorionHandler();public void paintComponent(Graphics g)super.paintComponent(g);Gra
9、phics2D g2=(Graphics2D)g;for(int i=0;isquares.size();i+)g2.draw(Rectangle2D)squares.get(i);public void find(Point2D p)for(int i=0;isquares.size();i+)Rectangle2D r=(Rectangle2D)squares.get(i);if(r.contains(p)return r;return null;public void add(Point2D p)double x=p.getX();double y=p.getY();current=ne
10、w Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH),squares.add(current);repaint();public void remove(Rectangle2D s)if(s=null)return;if(s=current)current=null;squares.remove(s);repaint();private static final int SIDELENGTH=10;private ArrayList squares;private Rectangle2D curren
11、t;private class MouseHandler extends MouseActionListenerpublic void mousePressed(MouseEvent event)current=find(event.getPoint();if(current=null)add(event.getPoint();public void mouseClicked(MouseEvent event)current=find(event.getPoint();if(current!=null private class MouseMotionHandler implements Mo
12、useMotionListenerpublic void mouseMoved(MouseEvent event)if(find(event.getPoint)=null)setCursor(Cursor.getDefaultCursor();elsesetCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);public void mouseDragged(MouseEvent event)if(current!=null)int x=event.getX();int y=event.getY();current.setFram
13、e(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);repaint();(分数:30.00)_二级 JAVA 机试-48 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题的功能是计算二维数组各个元素的和。程序中定义了二维数组 arr,arr 有 3 行 4 列共 12 个元素,程序中采用 for 循环语句的嵌套来计算数组中各个元素的和,并将结果保存在 sum 变量中,最后打印输出结果。public class java1public static void main(String
14、args)int arr=1,2,3,4,5,6,7,8,(9,10,11,12;int sum=0;int i=0,j=0;for(i=0;_)for(_)_;System.out.println(“sum=“+sum);(分数:30.00)_正确答案:(第 1 处:i3;i+或 i=2;i+第 2 处:j=0;j4;j+或 j=0;j=3;j+第 3 处:sum=sum+arrij)解析:解析 第 1 处、第 2 处是由该数组是 3 行 4 列的数组而得出的遍历数组的循环条件;第 3 处是将数组元素累加并将最终累加结果赋给 sum。二、简单应用题(总题数:1,分数:40.00)2.本题是设
15、计一个窗体,窗口的背景色为系统桌面的颜色,在窗口中分别画了空心和实心的黑色矩形、深灰色圆角矩形和浅灰色椭圆形,并且画了白色粗体的“Java 二级考试!”字符串。Import java.awt.*;Import javax.swing.*;Public class java2Public static void main(stringargs)Fillframe frame=new fillframe();Frame.setdefaultcloseoperation(jframe.exit_on_close);Frame.show();Class fillframe extends jframe
16、Public fillframe()Settitle(“simple“);Setsize(width,height);Fillpanel panel=new fillpanel();Panel.setbackground(systemcolor.desktop);Container contentpane=getcontentpane();Contentpane.add(panel);Public static final int width=400;Public static final int width=250;Class fillpanel extends jpanelPublic v
17、oid paintcomponent(graphics g)_;g.setcolor(new color(10,10,10);g.drawrect(10,10,100,30);g.setcolor(new color(100,100,100);g.drawroundrect(150,10,100,30,15,15);g.setcolor(new color(150,150,150);g.drawoval(280,10,80,30);g.setcolor(new color(10,10,10);g.fillrect(10,110,100,30);g.setcolor(new color(100,
18、100,100);g.drawroundrect(150,110,100,30,15,15);g.setcolor(new color(150,150,150);g.filloval(280,110,80,30);g.setcolor(color.white),font f=new font(“宋体“,_,20);g.setfont(f);g.drawstring(“java 二级考试!“,150,200);(分数:40.00)_正确答案:(第 1 处:super.paintComponent(g);第 2 处:Font.BOLD)解析:解析 第 1 处是通过 super 语句调用父类的构造方
19、法 paintComponent(g)。第 2 处是通过字体对象的构造函数,设置“Java 二级考试!”格式,题目要求是粗体,所以大 Font.BOLD。三、综合应用题(总题数:1,分数:30.00)3.本题的功能是监听鼠标的操作。鼠标置于窗口中单击时(左键或右键),在单击的地方会画一个小矩形,如果将鼠标置于小矩形上,则鼠标光标状态改为小十字,按下鼠标左键可拖曳,双击鼠标左键(或右键)时,小矩形消失。import java.awt.*;import java.awt.event.*;import java.util.*;import java.awt.geom.*;import javax.s
20、wing.*;public class java3public static void main(Stringargs)MouseFrame frame=new MouseFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();class MouseFrame extends JFramepublic MouseFrame()setTitle(“java3“);setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);MousePanel panel=new MousePanel();
21、Container contentPane=geeContentPane();contentPane.add(panel);public static final int DEFAULT_WIDTH=300;public static final int DEFAULT_HEIGHT=200;class MousePanel extends JPanelpublic MousePanel()squares=new ArrayList();current=null;addMouseListener(new MouseHandler();addMouseMotionListener(new Mou
22、seMorionHandler();public void paintComponent(Graphics g)super.paintComponent(g);Graphics2D g2=(Graphics2D)g;for(int i=0;isquares.size();i+)g2.draw(Rectangle2D)squares.get(i);public void find(Point2D p)for(int i=0;isquares.size();i+)Rectangle2D r=(Rectangle2D)squares.get(i);if(r.contains(p)return r;r
23、eturn null;public void add(Point2D p)double x=p.getX();double y=p.getY();current=new Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH),squares.add(current);repaint();public void remove(Rectangle2D s)if(s=null)return;if(s=current)current=null;squares.remove(s);repaint();private
24、static final int SIDELENGTH=10;private ArrayList squares;private Rectangle2D current;private class MouseHandler extends MouseActionListenerpublic void mousePressed(MouseEvent event)current=find(event.getPoint();if(current=null)add(event.getPoint();public void mouseClicked(MouseEvent event)current=fi
25、nd(event.getPoint();if(current!=null private class MouseMotionHandler implements MouseMotionListenerpublic void mouseMoved(MouseEvent event)if(find(event.getPoint)=null)setCursor(Cursor.getDefaultCursor();elsesetCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);public void mouseDragged(Mous
26、eEvent event)if(current!=null)int x=event.getX();int y=event.getY();current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);repaint();(分数:30.00)_正确答案:(第 1 处:public Rectangle2D find(Point2D p)第 2 处:private class MouseHandler extends MouseAdapter第 3 处:find(event.getPoint()=null)解析:解析 第 1 处定义一个返回类型为 Rectangle2D 的 find 函数且有一个 Point2D 型的形参;第 2 处定义了继承鼠标适配器 MouseAdapt 的 MouseHandler;第 3 处判断 find 函数的返回值是否为空。