第五届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(B卷)答案
来源:互联网 发布:jsp连接access数据库 编辑:程序博客网 时间:2024/04/28 11:54
1、 设计一个Student接口,以一维数组存储一个班级的学生姓名。该接口中有一个抽象方法getStudentName()。设计一个类Test,该类实现接口Student中的方法getStudentName(),功能是获取学生姓名并显示。(20分)
【Student.java】//Student接口package programming; public interface Student { public static final String[] StudentName={"Jacob","Cullen","Fox","Harry","Marria","Victoria"}; public void getStudentName();} 【InterfaceTest.java】//Student接口的实现类 package programming; public class InterfaceTest implements Student{ public void getStudentName() { for(int i=0;i<StudentName.length;i++) System.out.println(StudentName[i]); } public static void main(String[] args) { // TODO Auto-generatedmethod stub new InterfaceTest().getStudentName(); } }
2、 如下图所示,利用Java的GUI和多线程编程技术,编写一个时钟显示程序。(30分)
package programming; import java.awt.Graphics;import java.util.Calendar;import java.util.GregorianCalendar; import javax.swing.JFrame;import javax.swing.JPanel; public class MultiThreadClock extends JFrame { public MultiThreadClock() { ClockPanel jp_clock=new ClockPanel(); add(jp_clock); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,400); this.setLocationRelativeTo(null); setVisible(true); Thread ClockRun=new Thread(jp_clock); ClockRun.start(); } public static void main(String[] args) { // TODO Auto-generatedmethod stub new MultiThreadClock(); }}class ClockPanel extends JPanelimplements Runnable{ private double CenterX,CenterY; private double Radius=150; private double HPointX,HPointY,MPointX,MPointY,SPointX,SPointY; private int Year,Month,Day,WeekDay,Hour,Minute,Second; private final double PI=3.1415926; public synchronized void paintComponent(Graphics g) { super.paintComponent(g); CenterX=this.getWidth()/2; CenterY=this.getHeight()/2; double HRadian=(PI/6)*Hour+(PI/360)*Minute+(PI/21600)*Second; double MRadian=(PI/30)*Minute+(PI/1800)*Second; double SRadian=(PI/30)*Second; HPointX=CenterX+Radius*0.6*Math.sin(HRadian); HPointY=CenterY-Radius*0.6*Math.cos(HRadian); MPointX=CenterX+Radius*0.8*Math.sin(MRadian); MPointY=CenterY-Radius*0.8*Math.cos(MRadian); SPointX=CenterX+Radius*Math.sin(SRadian); SPointY=CenterY-Radius*Math.cos(SRadian); g.drawOval((int)(CenterX-Radius), (int)(CenterY-Radius), (int)(2*Radius),(int)(2*Radius)); g.drawString("12",(int)(CenterX),(int)(CenterY-Radius)+10); g.drawString("3",(int)(CenterX+Radius)-6 , (int)(CenterY)); g.drawString("6", (int)(CenterX), (int)(CenterY+Radius)); g.drawString("9",(int)(CenterX-Radius), (int)(CenterY)); g.drawLine((int)(CenterX), (int)(CenterY), (int)(SPointX), (int)(SPointY)); g.drawLine((int)(CenterX), (int)(CenterY), (int)(MPointX), (int)(MPointY)); g.drawLine((int)(CenterX), (int)(CenterY), (int)(HPointX), (int)(HPointY)); g.drawString(WeekDay+" "+Month+" "+Day+" "+Hour+":"+Minute+":"+Second+" "+Year, 20, 350); } @Override public void run() { // TODO Auto-generatedmethod stub while(true) { try { setCurrentTime(); repaint(); Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void setCurrentTime() { Calendar calendar=newGregorianCalendar(); Year=calendar.get(Calendar.YEAR); Month=calendar.get(Calendar.MONTH); Day=calendar.get(Calendar.DAY_OF_MONTH); WeekDay=calendar.get(Calendar.DAY_OF_WEEK); Hour=calendar.get(Calendar.HOUR); Minute=calendar.get(Calendar.MINUTE); Second=calendar.get(Calendar.SECOND); }}
3、 定义一个可序列化的对象Student类,这个类实现了Serializable接口,类中的成员变量id,name,age,department都可以被序列化,方法不能序列化。通过对象输出流的writeObject()方法将Student对象保存到文件data.ser中,然后通过对象输入流的readObject()方法从文件data.ser中读出保存下来的Student对象,然后将Student对象的id和name输出到控制台。(25分)
package programming; 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; /* 串行化只能保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量, 而且串行化保存的只是变量的值,对于变量的任何修饰符都不能保存。 */ class StudentSerial implements Serializable{ private int id; private String name; private int age; private String department; public StudentSerial(int id,String name,int age,String department) { this.id=id; this.name=name; this.age=age; this.department=department; } public void display() { System.out.println("id:"+id+" "+"name:"+name); }}public class DisplayObject{ public static void main(String[] args) { try { StudentSerial stu=new StudentSerial(80116,"丁懿",20,"IT"); ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("data.ser")); oos.writeObject(stu); oos.close(); ObjectInputStreamois= new ObjectInputStream(new FileInputStream("data.ser")); StudentSerial ss=(StudentSerial)(ois.readObject()); ss.display(); } catch (ClassNotFoundException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } catch(FileNotFoundException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } }}
4、 编写一个异常类MyException,再编写一个类Student,该类有一个产生异常的方法public void speak(int m) throws MyException,要求参数m的值大于1000时,方法抛出一个MyException对象。最后编写主类,在主类的main方法中用Student创建一个对象,让该对象调用speak方法(m参数设为1500)。(25分)
package programming; public class TestException { public static void main(String[] args) { // TODO Auto-generatedmethod stub StudentA stu=new StudentA(); try { stu.speak(1500); } catch (MyException e) { // TODO Auto-generatedcatch block System.out.println(e.getExceptionMsg()); } }}class MyException extends Exception{ public String getExceptionMsg() { return "参数值大于1000,抛出异常"; }}class StudentA{ public void speak(int m) throws MyException { if(m>1000) throw new MyException(); }}
附加题:
5、 有两个集合,A集合内容为:{1、2、3、4},B集合内容为:{1、2、5、6},两个集合的对称差定义为AΔ B = (A − B) ∪(B − A),上述A、B两集合的对称差为{3、4、5、6}。编写一个程序,用散列集求两个集合A、B的对称差集合,即求。
(20分)
package programming; import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashSet; public class Hashset { public static void main(String[] args) { // TODO Auto-generatedmethod stub HashSet<Integer> hashsetA=newHashSet<Integer>(); HashSet<Integer> hashsetB=newHashSet<Integer>(); HashSet<Integer> hashset=newHashSet<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()) { Integera=iteA.next(); if(!hashsetB.contains(a)) hashset.add(a); } while(iteB.hasNext()) { Integerb=iteB.next(); if(!hashsetA.contains(b)) hashset.add(b); } System.out.println(hashset); } }
1、 访问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");
Connectionconn=DriverManager.getConnection("jdbc:odbc:student","","");
(30分)
package programming; import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement; public class InsertOperate { private static PreparedStatement ps; private static Connection conn; private static Statement st; private ResultSet rs; public InsertOperate() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn=DriverManager.getConnection("jdbc:odbc:DS_MyDB"); ps=conn.prepareStatement("insert into T_Student(S_ID,S_Name,S_Email,S_Score)" + "values(?,?,?,?)"); } catch (ClassNotFoundException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generatedmethod stub InsertOperate iop=new InsertOperate(); iop.insert(1004, "Eli", "Eli@126.com", 95); iop.insert(1005, "Eimmy", "Eimmy@126.com", 89); iop.insert(1006, "Justin", "Justin@126.com", 96); iop.show(); //必须关闭,否则插入数据失败 try { ps.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public void insert(int id,String name,String email,int score) { try { ps.setInt(1, id); ps.setString(2,name); ps.setString(3, email); ps.setInt(4, score); System.out.println("成功插入"+ps.executeUpdate()+"条数据"); } catch (SQLException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } } public void show() { try { st=conn.createStatement(); rs=st.executeQuery("select * from T_Studentorder by S_ID"); while(rs.next()) { for(int i=1;i<=rs.getMetaData().getColumnCount();i++) System.out.print(rs.getString(i)+"\t"); System.out.print("\n"); } } catch (SQLException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } } }
- 第五届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(B卷)答案
- 第五届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(A卷)答案
- 第三届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(B卷)答案
- 第六届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(B卷)答案
- 第三届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(A卷)答案
- 第四届全国ITAT教育工程就业技能大赛复赛试题Java组A卷答案
- 第五届全国ITAT教育工程就业技能大赛Java组复赛B卷试题答案
- 第五届全国ITAT教育工程就业技能大赛Java组复赛A卷试题答案
- 第四届全国ITAT教育工程就业技能大赛Java组复赛B卷试题答案
- 第五届全国ITAT教育工程就业技能大赛复赛试题 C语言程序设计
- ITAT教育工程就业技能大赛复赛试题A卷第一题
- 第四届全国ITAT教育工程就业技能大赛决赛试题----Java程序设
- 第三届全国ITAT教育工程就业技能大赛决赛试题----Java程序设
- 全国ITAT教育工程就业技能大赛的试题解析
- 全国ITAT教育工程就业技能大赛预赛
- ITAT 第五届c复赛b卷附加题答案
- 第五届全国信息水平设计大赛C语言复赛B卷答案
- 第五届ITAT C语言复赛试题A卷
- 如何读H.264的标准和代码
- 鼠标的位置
- CSS网页布局教程:绝对定位和相对定位
- 十进制转化为二进制
- C语言的面向对象设计-对X264/FFMPEG架构探讨
- 第五届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(B卷)答案
- 函数指针在C和C++使用的区别
- [线性表|算法设计题]第1--3题
- Android中常用的Intent启动服务
- 国内计算机类三大中文学报投稿体会
- JAVA WEB_javascript的初步(4)
- 回调函数
- 编程的本质
- ToolHelp32 函数