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

来源:互联网 发布:审批流程数据库设计 编辑:程序博客网 时间:2024/05/17 04:40

1、 设计一个Student接口,以一维数组存储一个班级的学生姓名。该接口中有一个抽象方法getStudentName()。设计一个类Test,该类实现接口Student中的方法getStudentName(),功能是获取学生姓名并显示。(20分) 

2、 如下图所示,利用JavaGUI和多线程编程技术,编写一个时钟显示程序。(30分)

 

 

 

 

 

3、 定义一个可序列化的对象Student类,这个类实现了Serializable接口,类中的成员变量idnameagedepartment都可以被序列化,方法不能序列化。通过对象输出流的writeObject()方法将Student对象保存到文件data.ser中,然后通过对象输入流的readObject()方法从文件data.ser中读出保存下来的Student对象,然后将Student对象的idname输出到控制台。(25分)

4、 编写一个异常类MyException,再编写一个类Student,该类有一个产生异常的方法public void speak(int m) throws MyException,要求参数m的值大于1000时,方法抛出一个MyException对象。最后编写主类,在主类的main方法中用Student创建一个对象,让该对象调用speak方法(m参数设为1500)。(25分)

附加题:

5、 有两个集合,A集合内容为:{1234}B集合内容为:{1256},两个集合的对称差定义为A Δ B = (A − B) ∪(B − A),上述AB两集合的对称差为{3、4、5、6}。编写一个程序,用散列集求两个集合A、B的对称差集合,即求

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语言编写程序,插入3条记录,然后查询表数据并显示。要求如下:

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

2) 使用PreparedStatement语句来插入3条记录;

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

参考提示代码:

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

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

   (30分)

 

 

 

T1:

package Five;

public class B1 {
 public static void main(String[] args) {
  new Test().getStudentName();
 }

}

class Test implements Student {
 public void getStudentName() {
  for (String name : names)
   System.out.println(name + "\t");
 }
}

interface Student {
 String[] names = new String[] { "张三", "李四", "王二麻", "小明" };

 void getStudentName();
}

 

T2:

package Five;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class B2 extends JFrame implements Runnable {
 Clock clock = new Clock();
 JLabel show = new JLabel("", JLabel.CENTER);

 public B2() {
  show.setText(clock.getTime());
  this.add(clock, BorderLayout.CENTER);
  this.add(show, BorderLayout.SOUTH);
  this.setVisible(true);
  this.setLocationRelativeTo(null);
  this.setSize(300, 350);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 public void run() {
  while (true) {
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   clock.repaint();
   show.setText(clock.getTime());
  }

 }

 public static void main(String[] args) {
  new Thread(new B2()).start();

 }
}

class Clock extends JPanel {
 private int hour;
 private int minute;
 private int second;
 Calendar c = Calendar.getInstance();

 public String getTime() {
  SimpleDateFormat format = new SimpleDateFormat(
    "E MM月dd日  HH:mm:ss yyyy");
  return format.format(c.getTime());
 }

 public void setTime() {
  hour = c.get(c.HOUR);
  minute = c.get(c.MINUTE);
  second = c.get(c.SECOND);
 }

 public void paint(Graphics g) {
  super.paintComponent(g);
  int xcenter = getWidth() / 2;
  int ycenter = getHeight() / 2;
  c = Calendar.getInstance();
  setTime();

  int radiu = (int) (Math.min(getHeight(), getWidth()) * 0.8 * 0.5);
  g.drawOval(xcenter - radiu, ycenter - radiu, 2 * radiu, 2 * radiu);
  g.drawString("12", xcenter - getFont().getSize() / 2, ycenter - radiu
    + getFont().getSize());
  g.drawString("3", xcenter + radiu - getFont().getSize(), ycenter);
  g.drawString("6", xcenter, ycenter + radiu - 5);
  g.drawString("9", xcenter - radiu + 5, ycenter);

  int hlength = (int) (radiu * 0.4);
  int xhour = (int) (xcenter + Math.sin((2 * Math.PI / 12) * (hour % 12))
    * hlength);
  int yhour = (int) (ycenter - Math.cos((2 * Math.PI / 12) * (hour % 12))
    * hlength);
  g.setColor(Color.black);
  g.drawLine(xcenter, ycenter, xhour, yhour);

  int mlength = (int) (radiu * 0.6);
  int xminuter = (int) (xcenter + Math.sin((2 * Math.PI / 60) * (minute))
    * mlength);
  int yminuter = (int) (ycenter - Math.cos((2 * Math.PI / 60) * (minute))
    * mlength);
  g.setColor(Color.blue);
  g.drawLine(xcenter, ycenter, xminuter, yminuter);

  int slength = (int) (radiu * 0.8);
  int xsecond = (int) (xcenter + Math.sin((2 * Math.PI / 60) * (second))
    * slength);
  int ysecond = (int) (ycenter - Math.cos((2 * Math.PI / 60) * (second))
    * slength);
  g.setColor(Color.red);
  g.drawLine(xcenter, ycenter, xsecond, ysecond);

 }

}

 

 

T3:

package Five;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class B3 {
 public static void main(String[] args) throws FileNotFoundException,
   IOException, ClassNotFoundException {
  Students student = new Students(101344, "郑永亮", 24, "电子信息系");
  File file = new File("data.ser");
  if (!file.exists())
   file.createNewFile();
  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
    file));
  oos.writeObject(student);
  ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
  oos.close();
  Students s = (Students) ois.readObject();
  System.out.println(s.name + "的id是" + s.id);
 }

}

class Students implements Serializable {
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 int id;
 String name;
 int age;
 String department;

 public Students(int id, String name, int age, String department) {
  super();
  this.id = id;
  this.name = name;
  this.age = age;
  this.department = department;
 }

}

 

 

T4:

package Five;

public class B4 {
 
 public static void main(String[]args) throws MyException{
 studentss students=new studentss();
 
  students.speak(1500);
 
 }
 }

 

class MyException extends Exception{
 
 public MyException(String message){
  super(message);
  //System.out.println(message);
  
 }
}

class studentss{
 public void speak(int m) throws MyException{
  if(m>1000)throw new MyException("这位学生不要再说话了");
  else System.out.println("上课正常");
 }
}

 

 

T5:

package Five;

import java.util.HashSet;
import java.util.Iterator;

public class B5 {
  public static void main(String[] args) { 
        // TODO Auto-generatedmethod stub  
         HashSet<Integer> hashsetA=new  HashSet<Integer>(); 
        HashSet<Integer> hashsetB=new HashSet<Integer>(); 
           HashSet<Integer> hashset=new HashSet<Integer>(); 
           hashsetA.add(1); 
          hashsetA.add(3); 
          hashsetA.add(2); 
          hashsetA.add(4); 
    
           hashsetB.add(1); 
          hashsetB.add(8); 
          hashsetB.add(5); 
           hashsetB.add(6); 
          Iterator<Integer>iteA=hashsetA.iterator(); 
           Iterator<Integer>iteB=hashsetB.iterator(); 
          while(iteA.hasNext()) 
          { 
             Integer a=iteA.next(); 
             if(!hashsetB.contains(a)) 
                 hashset.add(a); 
          } 
          while(iteB.hasNext()) 
          { 
              Integer b=iteB.next(); 
              if(!hashsetA.contains(b)) 
                 hashset.add(b); 
          } 
         System.out.println(hashset); 
      }
 

}

 

 

T6:

package Five;

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

public class B6 {
 public static void main(String args[]) throws ClassNotFoundException, SQLException{
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn=DriverManager.getConnection("jdbc:odbc:student");
  String sql="insert into T_Student values(?,?,?,?)";
  PreparedStatement ps=conn.prepareStatement(sql);
  ps.setInt(1,1004);
  ps.setString(2,"郑永亮");
  ps.setString(3,"kingzyl@yeah.net");
  ps.setInt(4,99);
  ps.executeUpdate();
  ps=conn.prepareStatement("select * from T_Student");
  ResultSet rs=ps.executeQuery();
  System.out.println("学号\t姓名\t邮箱\t\t分数");
  while(rs.next()){
   System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4));
  }
  ps.close();
  conn.close();
 }
 

}

 

原创粉丝点击