Java计算机二级(上机真题)

1.
import java.io.*;
public class Java_1 {
    public static void main(String[ ] args) throws IOException {
        InputStreamReader ir;
        BufferedReader in;
        int max, x;
        String data;

        max = 0;
        ir = new InputStreamReader(System.in);
        in = new BufferedReader(ir);
        System.out.println("请输入5个正整数:");
        //*********Found**********
        for (int i = 1; i<= 5; i++) {
            data = in.readLine();
            //*********Found**********
            x = Integer.parseInt(data);
            if ( max < x )
                //*********Found**********
                max = x;
        }
        System.out.println("输入的最大值是 "+ max);
    }
}
2.文件的和递归调用
import java.io.File;

public class Java_2
{
   public static void main(String s[])
   {
      //Getting the Current Working Directory
      String curDir = System.getProperty("user.dir");
      System.out.println("当前的工作目录是:"+curDir);
		
      //*********Found**********
      File ff=new File(curDir);
      String[] files=ff.list();
      for(int i=0; i<files.length; i++)
      {
         String ss=curDir+"\\"+files[i];
         traverse(0,ss);	
      }
   }
	
   /**
   * 递归地遍历目录树
   * @param  level 目录的层次
   * @param  s     当前目录路径名
   */
   public static void traverse(int level,String s)
   {
      File f=new File(s);
      for(int i=0; i<level; i++) System.out.print("   ");
      if(f.isFile()) 
      {
         System.out.println(f.getName());
      }
      else if(f.isDirectory())
      {
         //*********Found**********
         System.out.println("<"+f.getName()+">");
         String[] files=f.list();
         level++;
         //*********Found**********
         for(int i=0; i<files.length;i++)
         {
            String ss=s+"\\"+files[i];
            //*********Found**********
            traverse(level,ss);
         }
      }
      else
      {
         System.out.println("ERROR!");
      }
   }
}
3.swing构架与AWT事件处理机制

import java.awt.*;
import java.awt.event.*;
//*********Found**********
import javax.swing.*;

public class Java_3 {

    public static void main(String[ ] args) {
        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //*********Found**********
        frame.getContentPane().add(new Change());

        frame.pack();
        frame.setVisible(true);
    }
}

class Change extends JPanel {

    int count = 200;
    JLabel l1;
    JButton b1, b2;

    public Change( ) {
        setPreferredSize(new Dimension(280, 60));
        l1 = new JLabel("200");
        b1 = new JButton("增大");
        b2 = new JButton("减小");
        add(l1);
        //*********Found**********
        add(b1);
        //*********Found**********
        add(b2);
        b1.addActionListener( new BListener( ) );
        b2.addActionListener( new BListener( ) );
    }

    //*********Found**********
    private class  BListener implements ActionListener {

        //*********Found**********
        public void  actionPerformed (ActionEvent e) {
            if (e.getSource( ) == b1) {
                count++;
            } else {
                count--;
            }
            l1.setText("" + count);
        }
    }
}

二
1.考察JOptionPane类
import javax.swing.JOptionPane;  //导入JOptionPane类

public class Java_1 {
   public static void main( String args[] )
   {
//*********Found********
      JOptionPane.showMessageDialog(
         null, "欢迎\n你\n参加\nJava\n考试!" );
      System.exit( 0 );  // 结束程序
   }
}
/* JOptionPane类的常用静态方法如下:
   showInputDialog()
   showConfirmDialog()
   showMessageDialog()
   showOptionDialog()
*/

2.
import java.util.Random;

public class Java_2
{
   public static void main(String args[]){
      Random random = new Random();
      float x = random.nextFloat();//产生0.0与1.0之间的一个符点数
      int n = Math.round(20*x);  //构造20以内的一个整数
      long f = 1 ;  //保存阶乘的结果
      int k = 1 ;  //循环变量
   //*********Found********
      do{f = k * f;
         k++;
   //*********Found********
      }while(k <= n); 	
      System.out.println(n+"!= "+f);
   }
}

3.
//考察对话框综合使用,首先继承JFrame类以及实现ActionListener接口。处理GUI事件;浮点输出格式;//showMessageDialog()显示信息
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

     //*********Found********
public class Java_3 extends JFrame implements ActionListener {
   private JTextField input1, input2, output;
   private int number1, number2;
   private double result;

   // 初始化
   public Java_3()
   {
     //*********Found********
      super( "示范异常" );

      Container c = getContentPane();
      c.setLayout( new GridLayout( 3, 2 ) );

      c.add( new JLabel( "输入分子",
                         SwingConstants.RIGHT ) );
      input1 = new JTextField( 10 );
      c.add( input1 );

      c.add(
         new JLabel( "输入分母和回车",
                     SwingConstants.RIGHT ) );
      input2 = new JTextField( 10 );
      c.add( input2 );
      input2.addActionListener( this );

      c.add( new JLabel( "计算结果", SwingConstants.RIGHT ) );
      output = new JTextField();
      c.add( output );

      setSize( 425, 100 );
      show();
   }

   //处理 GUI 事件
   public void actionPerformed( ActionEvent e )
   {
      DecimalFormat precision3 = new DecimalFormat( "0.000" );

      output.setText( "" ); // 空的JTextField输出

     //*********Found********
      try {         
         number1 = Integer.parseInt( input1.getText() );
         number2 = Integer.parseInt( input2.getText() );

         result = quotient( number1, number2 );
     //*********Found********
         output.setText(precision3.format(result));
      }
      catch ( NumberFormatException nfe ) {
         JOptionPane.showMessageDialog( this,
            "你必须输入两个整数",
            "非法数字格式",
            JOptionPane.ERROR_MESSAGE );
      }
      catch ( Exception dbze ) {
     //*********Found********
         JOptionPane.showMessageDialog( this, 
            "除法异常",
            "除数为零",
            JOptionPane.ERROR_MESSAGE );
      }
   }

   // 定义求商的方法,如遇除数为零时,能抛出异常。
     public double quotient( int numerator, int denominator )
      throws Exception
   {
      if ( denominator == 0 )
         throw new Exception();

      return ( double ) numerator / denominator;
   }

   public static void main( String args[] )
   {
      Java_3 app = new Java_3();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               e.getWindow().dispose();
               System.exit( 0 );
            }
         }
      );
   }
}
/* JOptionPane类的常用静态方法如下:
   showInputDialog()
   showConfirmDialog()
   showMessageDialog()
   showOptionDialog()
*/

三
1.考察Applet使用
//*********Found********
import java.applet.*;
import java.awt.Graphics;

//*********Found********
public class Java_1 extends Applet {  
   public void paint( Graphics g )
   {
//*********Found********
      g.drawString( "欢迎你来参加Java 语言考试!", 25, 25 );
   }
}

2.考察Applet编写
import java.awt.*;
import java.applet.*;

//*********Found********
public class Java_2 extends Applet
{
    TextArea outputArea;

    public void init()
    {
        setLayout(new BorderLayout());
        outputArea = new TextArea();
     //*********Found********
        add( outputArea );

      // 计算0至10的阶乘
        for ( long i = 0; i <= 10; i++ )
            //*********Found********
            outputArea.append(i + "! = " + factorial(i)+ "\n" );
    }
   
   // 用递归定义阶乘方法
    public long factorial( long number )
    {                  
        if ( number <= 1 )  // 基本情况
            return 1;
        else
            //*********Found********
            return number * factorial( number - 1 );
    }  
}

3.考察Swing与AWT中事件处理
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;

public class Java_3
{
   public static void main(String[] args)
   {
      FontFrame frame = new FontFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

     //*********Found********
class FontFrame extends JFrame
{
   public FontFrame()
   {
      setTitle("沁园春.雪");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
      FontPanel panel = new FontPanel();
      Container contentPane = getContentPane();
     //*********Found********
      contentPane.add(panel);
   }
   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;
}

     //*********Found********
class FontPanel extends JPanel
{
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      String message = "数风流人物,还看今朝!";
      Font f = new Font("隶书", Font.BOLD, 24);
      g2.setFont(f);
      FontRenderContext context = g2.getFontRenderContext();
      Rectangle2D bounds = f.getStringBounds(message, context);
      double x = (getWidth() - bounds.getWidth()) / 2;
      double y = (getHeight() - bounds.getHeight()) / 2;
      double ascent = -bounds.getY();
      double baseY = y + ascent;
      g2.setPaint(Color.RED);
     //*********Found********
      g2.drawString(message, (int)x, (int)(baseY));
   }
}

四.
1.
public class Java_1{
  public static void main(String[] args){
    //*********Found**********
    int[] scores = {90,80,75,67,53}; 
    int best = 0; 
    char grade; 

    // 找出这组成绩中的最高分
    //*********Found**********
    for (int i=0;i <=4; i++){
      //*********Found**********
      if (scores[i] > best)
        best = scores[i];
    }
 
    //求各分数的等级并显示
    for (int i=0; i<scores.length; i++){
      if (scores[i] >= best - 10)
        grade = 'A';
      //*********Found**********
      else if (scores[i] >= best - 20)
        grade = 'B';
      else if (scores[i] >= best - 30)
        grade = 'C';
      else if (scores[i] >= best - 40)
        grade = 'D';
      else
        grade = 'F';
      System.out.println("Student " + i + " score is " + scores[i] +
        " and grade is " + grade);
    }
  }
}

2.
public class Java_2 {
    public static void main(String[ ] args) {
        Point pt;
        //*********Found**********
        pt = new Point(2, 3);
        System.out.println(pt);
    }
}

class Point {

    //*********Found**********
    private int x;
    private int y;

    //*********Found**********
    public Point (int a, int b) {
        x = a;
        y = b;
    }

    int getX( ) {
        return x;
    }

    int getY( ) {
        return y;
    }

    void setX(int a) {
        x = a;
    }

    void setY(int b) {
        y = b;
    }

    //*********Found**********
    public String toString ( ) {
        return "( " + x + "," + y + " ) ";
    }
}

3.
import java.awt.*;
import java.awt.event.*;    
//*********Found**********
import javax.swing.*;
   
//*********Found**********
public class Java_3 extends JPanel{     
   
  private int counter = 0;    
   
  private JButton closeAllButton;    
   
  public Java_3() {    
    JButton newButton = new JButton("New");    
    //*********Found**********
    add(newButton);
    newButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt){
        CloseFrame f = new CloseFrame();    
        counter++;    
        f.setTitle("窗体 " + counter);    
        f.setSize(200, 150);    
        f.setLocation(30 * counter, 30 * counter);    
        //*********Found**********
        f.setVisible(true);    
        closeAllButton.addActionListener(f); 
      }  
    });
        
    closeAllButton = new JButton("Close all");    
    add(closeAllButton);    
  }      
   
  public static void main(String[ ] args) {    
    JFrame frame = new JFrame();    
    frame.setTitle("多窗体测试");    
    frame.setSize(300, 200);    
    frame.addWindowListener(new WindowAdapter() {    
      public void windowClosing(WindowEvent e) {    
        System.exit(0);    
      }    
    });    
   
    Container contentPane = frame.getContentPane();    
    contentPane.add(new Java_3());   
  
    frame.setVisible(true) ;    
  } 
} 
   
//*********Found**********
class CloseFrame extends JFrame implements ActionListener {    
  public void actionPerformed(ActionEvent evt) {     
    setVisible(false);    
  }    
}

1.矩阵运算
public class Java_1 {

    public static void main(String args[]) {
        int a[][] = {{2, 3, 4}, {4, 6, 5}};
        int b[][] = {{1, 5, 2, 8}, {5, 9, 10, -3}, {2, 7, -5, -18}};
        int c[][] = new int[2][4];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 4; j++) {
                //*********Found********
                c[i][j] = 0 ;
                //*********Found********
                for (int k = 0; k < 3; k++) 
                    //*********Found********
                    c[i][j] += a[i][k]*b[k][j];
                System.out.print(c[i][j] + "  ");
            }
            System.out.println();
        }
    }
}

 

二级java 公共基础知识部分30分 专业语言部分 70分 Java语言程序设计 基本要求: 1. 掌握Java语言的特点,实现机制和体系结构。 2. 掌握Java语言中面向对象的特性。 3. 掌握Java语言提供的数据类型和结构。 4. 掌握Java语言编程的基本技术。 5. 会编写Java用户界面程序。 6. 会编写Java简单应用程序。 7. 会编写Java小应用程序(Applet)。 8. 了解Java的应用。 考试内容: 一、 Java语言的特点和实现机制 二、 Java体系结构 1. JDK目录结构。 2. Java的API结构。 3. 开发环境设置。 4. Java程序结构。 三、 Java语言中面向对象的特性。 1. 面向对象编程的基本要领和特征。 2. 类的基本组成和使用。 3. 对象的生成、使用和删除。 4. 接口与包。 5. Java类库中常用类和接口。 四、 Java简单数据类型及运算 1. 变量和常量。 2. 基本数据类型及转换。 3. Java类库中对简单数据类型的类包装。 4. 运算符和表达式运算。 5. 数组和字符串。 五、 Java语言的基本语句 1. 表达式语句。 2. 条件语句。 3. 循环语句。 4. 注释语句。 5. 异常处理。 六、 Java编程技术基础 1. 线程的要领和使用。 2. 同步与共享。 3. 串行化要领和目的。 4. 串行化方法。 5. 串行化的举例。 6. 基于文本的应用。 7. 文件和文件I/O。 8. 汇集(collections)接口。 七、 编写用户界面程序 1. 图形用户界面。 2. AWT库简介。 3. SwingF简介。 4. AWT与Swing比较。 八、 编写小应用程序(Applet) 1. 小应用程序概念。 2. 安全机制。 3. Applet执行过程。 4. Applet的图形绘制。 5. Applet的窗口。 6. Applet的工作环境。 7. Java Application 和Applet。 九、 Java的应用 十、 J2DK的下载和操作。 考试方式: 笔试:90分钟,满分100分,其中含公共基础知识部分的30分。 上机操作:90分钟,满分100分。 上机题目类型要求: (1) 基本操作。 (2) 简单应用。 (3) 综合应用。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值