【计算机类职业资格】(A)二级JAVA笔试-7及答案解析.doc

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

1、(A)二级 JAVA 笔试-7 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题的功能是求 1100 的自然数的累加,并打印输出计算结果。public class java1public static void main(String args)int sum=0;int i=l;for(;)if(_)sum=sum+i;else_;_;System. out. println(“sum=“+sum);(分数:30.00)填空项 1:_二、简单应用题(总题数:1,分数:30.00)2.本题主窗口中包括一个文本框和一个文本域,在上面的文

2、本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。import java. awt. event. * ;import java. awt. * ;import javax. swing. JOptionPane;class Dwindow extends Frame implements ActionListenerTextField inputNumber;TextArea show;Dwindow(String s)super(s);inputNumber=new TextField(22);inputNumber

3、. addActionListener(this);show=new TextArea();add(inputNumber, BorderLayout. NORT H);add(show, BorderLayout. CENTER);setBounds(60,60,300,300); setVisible(true);validate();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System. exit(0););public void actionPerformed(Action

4、Event e)boolean boo=false;if(e. getSource()=inputNumber)String s=_;char a=s. toCharArray();for(int i=0; ia. length;i+)if(!(Character. isDigit(ai)boo=true;if(boo=true)JOptionPane. showMessageDialog (this, “您输入了非法字符“, “警告对话框“,_);inputNumber. setText(null);else if(boo=false)int number=Integer. parselnt

5、(s);show. append(“/n“+number+“平方:“+(num-ber*number);public class java2public static void main(String args)new Dwindow(“java2“);(分数:30.00)填空项 1:_三、综合应用题(总题数:1,分数:40.00)3.本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。import javax. swing. * ;import javax. swing. event. * ;impor

6、t java. awt. * ;public class java3 extends JFramepublic static JTextPane textPane;public static JScrollPane scrollPane;JPanel panel;public java3()super(“java3()“);panel=new JPanel();panel. setLayout(new BorderLayout();panel. setBorder (BorderFactory. createEmptyBorder(20,20,20,20);textPane=new JText

7、Pane();textPane. setFont(new Font(“monospaced“,Font. PLAIN, 12);scrollPane=new JScrollPane(textPane);panel. add(scrollPane);scrollPane. setPreferredsize(new Dimension(300,250);setContentPane(panel);setCloseOperation(JFrame. EXIT_ON_CLOSE);LineNumber lineNumber=new LineNumber();scrollPane. setRowHead

8、erView(lineNumber);public static void main(String args)java3 ttp=new java3();ttp. pack();ttp. setVisible(true);class LineNumber extends JTextPaneprivate final static Color DEFAULT_BACKGROUN=Color. gray;private final static Color DEFAULT_FOREGROUND=Color. black;private final static Font DEFAULTFONT=n

9、ew Font(“monospaced“, Font. PLAIN, 12);private final static int HEIGHT=Integer. MAX_VALUE-1000000;private final static int MARGIN=5;private FontMetrics fontMetrics;private int IineHeight;private int eurrentRowWidth;private JComponent component ;private int componentFontHeight;private int eomponentFo

10、ntAscent;public LineNumber(JComponent component)if(component=null)setBackground(DEFAULT_BACKGROUND);setForeground(DEFAULTFOREGROUND);setFont(DEFAULT_FONT);this. componen =this;elsesetBackground(DEFAULT_BACKGROUND);setForeground(component, getForeground();setFont(component, getFont();this. component=

11、component;componentFontHeight=component, getFontMettics(component, getFont(). getHeight();componentFontAscent=component, getFontMetrics(component, getFont(). getAscent();setPreferredWidth(9999);public void setPreferredWidth(int row)int width=fontMetrics, stringWidth( String. valueOf(row);if (current

12、RowWidthwidth)currentRowWidth=width;setPreferredSize(new Dimension(2*MARGIN+width, HEIGHT);public void setFont(Font font)super, setFont (font);fontMetrics=getFontMetrics(getFont();public int getLineHeight()if (lineHeight=0)return componentFontHeight;elsereturn IineHeight;public void setLineHeight(in

13、t lineHeight)if (lineHeight0)this. lineHeight-lineHeight;public int getStartOffset()return component, getInsets(). top+componentFontAscent;public void paintComponent(Graphics g)int lineHeight=getLineHeight();int startOffset=getStartOffset();Rectangle drawHere=g. getClipBounds();g. setColor(getBackgr

14、ound();g. fillRect(drawHere. x, drawHere. y, drawHere. width, drawHere. height);g. setColor(getForeground();int startLineNumber=(drawHere. y/lineHeight)+1;int endLineNumber=startLineNumber+(drawHere. height/lineHeight)int start=(drawHere. y/lineHeight)*lineHeight+startOffset;for (int i=startLineNumb

15、er; i=endLineNumber; i+)String lineNumber=String. valueOf(i);int width=fontMetrics. stringWidth(lineNumber);g. drawString(lineNumber, MARGIN+currentRowWidth-width, start);start+=lineHeight;setPreferredWidth(endLineNumber);(分数:40.00)填空项 1:_(A)二级 JAVA 笔试-7 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30

16、.00)1.本题的功能是求 1100 的自然数的累加,并打印输出计算结果。public class java1public static void main(String args)int sum=0;int i=l;for(;)if(_)sum=sum+i;else_;_;System. out. println(“sum=“+sum);(分数:30.00)填空项 1:_ (正确答案:第 1 处:i=100第 2 处:break第 3 处:i+或 i=i+1 或 i+=1)解析:解析 for(;)可以构成无限循环,所以第 1 处和第 2 处分别为跳出循环的条件和跳出循环;第3 处为循环条件的

17、自加操作。二、简单应用题(总题数:1,分数:30.00)2.本题主窗口中包括一个文本框和一个文本域,在上面的文本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。import java. awt. event. * ;import java. awt. * ;import javax. swing. JOptionPane;class Dwindow extends Frame implements ActionListenerTextField inputNumber;TextArea show;Dwindow(Stri

18、ng s)super(s);inputNumber=new TextField(22);inputNumber. addActionListener(this);show=new TextArea();add(inputNumber, BorderLayout. NORT H);add(show, BorderLayout. CENTER);setBounds(60,60,300,300); setVisible(true);validate();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent

19、 e)System. exit(0););public void actionPerformed(ActionEvent e)boolean boo=false;if(e. getSource()=inputNumber)String s=_;char a=s. toCharArray();for(int i=0; ia. length;i+)if(!(Character. isDigit(ai)boo=true;if(boo=true)JOptionPane. showMessageDialog (this, “您输入了非法字符“, “警告对话框“,_);inputNumber. setTe

20、xt(null);else if(boo=false)int number=Integer. parselnt(s);show. append(“/n“+number+“平方:“+(num-ber*number);public class java2public static void main(String args)new Dwindow(“java2“);(分数:30.00)填空项 1:_ (正确答案:第 1 处:inputNumber. getText()第 2 处:JOptionPane. WARNING_MESSAGE)解析:解析 第 1 处读取用户在文本框的输入内容;第 2 处设

21、定对话框类型为警告对话框。三、综合应用题(总题数:1,分数:40.00)3.本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。import javax. swing. * ;import javax. swing. event. * ;import java. awt. * ;public class java3 extends JFramepublic static JTextPane textPane;public static JScrollPane scrollPane;JPanel panel

22、;public java3()super(“java3()“);panel=new JPanel();panel. setLayout(new BorderLayout();panel. setBorder (BorderFactory. createEmptyBorder(20,20,20,20);textPane=new JTextPane();textPane. setFont(new Font(“monospaced“,Font. PLAIN, 12);scrollPane=new JScrollPane(textPane);panel. add(scrollPane);scrollP

23、ane. setPreferredsize(new Dimension(300,250);setContentPane(panel);setCloseOperation(JFrame. EXIT_ON_CLOSE);LineNumber lineNumber=new LineNumber();scrollPane. setRowHeaderView(lineNumber);public static void main(String args)java3 ttp=new java3();ttp. pack();ttp. setVisible(true);class LineNumber ext

24、ends JTextPaneprivate final static Color DEFAULT_BACKGROUN=Color. gray;private final static Color DEFAULT_FOREGROUND=Color. black;private final static Font DEFAULTFONT=new Font(“monospaced“, Font. PLAIN, 12);private final static int HEIGHT=Integer. MAX_VALUE-1000000;private final static int MARGIN=5

25、;private FontMetrics fontMetrics;private int IineHeight;private int eurrentRowWidth;private JComponent component ;private int componentFontHeight;private int eomponentFontAscent;public LineNumber(JComponent component)if(component=null)setBackground(DEFAULT_BACKGROUND);setForeground(DEFAULTFOREGROUND

26、);setFont(DEFAULT_FONT);this. componen =this;elsesetBackground(DEFAULT_BACKGROUND);setForeground(component, getForeground();setFont(component, getFont();this. component=component;componentFontHeight=component, getFontMettics(component, getFont(). getHeight();componentFontAscent=component, getFontMet

27、rics(component, getFont(). getAscent();setPreferredWidth(9999);public void setPreferredWidth(int row)int width=fontMetrics, stringWidth( String. valueOf(row);if (currentRowWidthwidth)currentRowWidth=width;setPreferredSize(new Dimension(2*MARGIN+width, HEIGHT);public void setFont(Font font)super, set

28、Font (font);fontMetrics=getFontMetrics(getFont();public int getLineHeight()if (lineHeight=0)return componentFontHeight;elsereturn IineHeight;public void setLineHeight(int lineHeight)if (lineHeight0)this. lineHeight-lineHeight;public int getStartOffset()return component, getInsets(). top+componentFon

29、tAscent;public void paintComponent(Graphics g)int lineHeight=getLineHeight();int startOffset=getStartOffset();Rectangle drawHere=g. getClipBounds();g. setColor(getBackground();g. fillRect(drawHere. x, drawHere. y, drawHere. width, drawHere. height);g. setColor(getForeground();int startLineNumber=(dr

30、awHere. y/lineHeight)+1;int endLineNumber=startLineNumber+(drawHere. height/lineHeight)int start=(drawHere. y/lineHeight)*lineHeight+startOffset;for (int i=startLineNumber; i=endLineNumber; i+)String lineNumber=String. valueOf(i);int width=fontMetrics. stringWidth(lineNumber);g. drawString(lineNumbe

31、r, MARGIN+currentRowWidth-width, start);start+=lineHeight;setPreferredWidth(endLineNumber);(分数:40.00)填空项 1:_ (正确答案:第 1 处:scrollpane. setPreferredSize(new Dimension(300,250)第 2 处:setDefaultCloseoperation(J Frame. EXIT_ON_CLOSE)第 3 处:LineNumber LineNumber=new LineNumber(textPane))解析:解析 第 1 处 Java 是大小写敏感的;第 2 处设置窗口关闭方式应使用seDefaultCloseOperation()方法;第 3 处从下面的 public LineNumber(JComponent component)可以看出 LineNumber()需要 JComponent 型参数。

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

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

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