第五届全国ITAT教育工程就业技能大赛Java组复赛A卷试题答案

来源:互联网 发布:js返回顶部素材 编辑:程序博客网 时间:2024/04/28 23:19

1、 某人有53分和45分的邮票,请编写一个程序,计算由这些邮票中的1张或若干张可以得到多少种不同的邮资,并按照邮资从小到大顺序显示。(20分) 

2、 采用Java多线程技术编写程序,其中包括两个线程:AB,其中A线程准备休眠一小时,B线程每隔一秒输入3句“起床”后,吵醒休眠的线程A。(25分)

3、 利用JavaGUI编程,编写一个窗体,包含两个文本框和一个命令按钮。其中一个文本框接收用户输入的一行字符串,回车后在另一个文本框中重复输出三行,单击命令按钮可清空两个文本框的所有内容。(25分)

4、 编写一个Java应用程序,运行后,首先列出当前工作目录,然后把当前目录下面的所有后缀为java的文件取出(设置一个过滤器进行文件名后缀的过滤)。(30分)

附加题:

5、 使用堆栈结构(递归)输出,其中=2+2,=3=8。(20分)

6、 访问Access 2003数据库,在Access数据库中创建学生表(T_Student)表并配置ODBC驱动源,学生表的结构和示例数据如下所示:

T_ Student表:

字段名称

说明

数据类型

约束

备注

S_ID

学号

Integer

主键

S_Name

姓名

Varchar(10)

不允许空

S_Email

邮箱

Varchar(30)

S_Score

英语成绩

Integer

表数据示例:

S_ID

S_Name

S_Email

S_Score

1001

Jerry

Jerry@126.com

80

1002

Mike

Mike@126.com

90

1003

John

John@126.com

78

使用Java语言编写程序,要求如下:

1) 使用JDBC-ODBC桥驱动程序;

2) 查询出表中所有记录,并按照主键升序显示。

参考提示代码:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection conn=DriverManager.getConnection("jdbc:odbc:student","","");

30分)

 

 

 

T1:

package Five;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class A1 {
 public static void main(String args[]) {
  HashSet<Integer> set = new HashSet<Integer>();
  int postage;
  for (int i = 0; i <= 5; i++)
   for (int j = 0; j <= 4; j++) {
    postage = i * 3 + j * 5;
    if (postage != 0)
     set.add(postage);
   }

  System.out.println("共有邮资" + set.size() + "种分别如下:");
  ArrayList<Integer> list = new ArrayList<Integer>(set);
  Collections.sort(list);
  System.out.println(list);
 }

}
 

 

T2:

package Five;

public class A2 {
 public static void main(String[] args) {
  Object o1 = new Object();
  Thread t2 = new Thread(new Thread2(o1));
  Thread t1 = new Thread(new Thread1(o1, t2));
  t1.start();

 }

 static class Thread1 implements Runnable {
  Object o1;
  Thread t2;

  public Thread1(Object o1, Thread t2) {
   this.o1 = o1;
   this.t2 = t2;
  }

  @Override
  public void run() {
   synchronized (o1) {
    t2.start();
    while (true) {
     System.out.println("我是线程1我要休息一个小时!");
     try {
      o1.wait(3 * 60 * 60 * 1000);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     System.out.println("我被线程2吵醒!");
     o1.notify();
    }
   }

  }

 }

 static class Thread2 implements Runnable {
  Object o1;

  public Thread2(Object o1) {
   this.o1 = o1;
  }

  @Override
  public void run() {
   // TODO Auto-generated method stub
   synchronized (o1) {
    while (true) {
     try {
      Thread.sleep(1000);
      for (int i = 0; i < 3; i++)
       System.out.println("我是线程2,线程1起床" + (i + 1));

      o1.notify();
      o1.wait();
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }

  }

 }
}

 

 

T3:

package Five;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class A3 extends JFrame implements ActionListener {
 JLabel lable = new JLabel("请输入一行字符串");
 JTextField one = new JTextField(20);
 JTextArea two = new JTextArea(3, 20);
 JButton clear = new JButton("清空内容");

 public A3() {
  this.setSize(300, 400);
  this.setVisible(true);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 20));
  one.addActionListener(this);
  clear.addActionListener(this);
  this.add(lable);
  this.add(one);
  this.add(two);
  this.add(clear);
 }

 public static void main(String[] args) {
  new A3();
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if (e.getSource() == one) {
   String str = one.getText();
   two.setText(str + "\n" + str + "\n" + str);
  }
  if (e.getSource() == clear) {
   one.setText(null);
   two.setText(null);
  }

 }

}

 

T4:

package Five;

import java.io.File;
import java.io.FilenameFilter;

public class A4 {
 public static void main(String[]args){
  File f=new File("");
  String dir=f.getAbsolutePath();
  System.out.println("当前工作目录是"+dir);
  f=new File(dir); 
  String[]name=f.list(new FilenameFilter(){
   @Override
   public boolean accept(File dir, String name) {    
    return name.endsWith(".java");
   }});
  for(String str:name)
   System.out.println(str);
 
  
 }

}

 

T5:

package Five;

public class A5 {
 public static void main(String args[]) {
  int a10 = 0;
  a10 = getSum(10);
  System.out.println("a10的值是" + a10);
 }

 private static int getSum(int i) {
  if (i == 1)
   return 3;
  if (i == 2)
   return 8;
  return 2 * (getSum(i - 1) + getSum(i - 2));

 }

}

 

 

T6:


 

package Five;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class A6 {
 public static void main(String[] args) throws ClassNotFoundException,
   SQLException {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn = DriverManager.getConnection("jdbc:odbc:student");
  Statement stmt = conn.createStatement();
  String sql = "select * from T_Student order by S_ID asc";
  ResultSet rs = stmt.executeQuery(sql);
  System.out.println("学号\t姓名\t邮箱\t\t分数");
  while (rs.next()) {
   System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t"
     + rs.getString(3) + "\t" + rs.getString(4));

  }
 }

}