System.out.print(name[i]+"");
}
}
}
interfaceStudent{
String[]name={"张三","李四","王二","赵武","胡说","刘能"};
abstractvoidgetStudentName();
}输出结果为:
王张徐李刘莫
2、如下图所示,利用Java的GUI和多线程编程技术,编写一个时钟显示程序。
(30分)
importjava.util.*;
importjava.awt.*;
importjava.applet.*;
importjava.text.*;
publicclassTest6extendsAppletimplementsRunnable{
privatevolatileThreadtimer;//Thethreadthatdisplaysclock
privateintlastxs,lastys,lastxm,
lastym,lastxh,lastyh;//Dimensionsusedtodrawhands
privateSimpleDateFormatformatter;//Formatsthedatedisplayed
privateStringlastdate;//Stringtoholddatedisplayed
privateFontclockFaceFont;//Fontfornumberdisplayonclock
privateDatecurrentDate;//Usedtogetdatetodisplay
privateColorhandColor;//Colorofmainhandsanddial
privateColornumberColor;//Colorofsecondhandandnumbers
privateintxcenter=80,ycenter=55;//Centerposition
publicvoidinit(){
intx,y;
lastxs=lastys=lastxm=lastym=lastxh=lastyh=0;
formatter=newSimpleDateFormat("EEEMMMddhh:
mm:
ssyyyy",
Locale.getDefault());
currentDate=newDate();
lastdate=formatter.format(currentDate);
clockFaceFont=newFont("Serif",Font.PLAIN,14);
handColor=Color.blue;
numberColor=Color.darkGray;
//背景色
try{
setBackground(newColor(Integer.parseInt(getParameter("bgcolor"),
16)));
}catch(NullPointerExceptione){
}catch(NumberFormatExceptione){
}
//前景色
try{
handColor=newColor(Integer.parseInt(getParameter("fgcolor1"),
16));
}catch(NullPointerExceptione){
}catch(NumberFormatExceptione){
}
//字体颜色
try{
numberColor=newColor(Integer.parseInt(getParameter("fgcolor2"),
16));
}catch(NullPointerExceptione){
}catch(NumberFormatExceptione){
}
resize(300,300);//设置窗口大小
}
//Paintisthemainpartoftheprogram
publicvoidupdate(Graphicsg){
intxh,yh,xm,ym,xs,ys;
ints=0,m=10,h=10;
Stringtoday;
currentDate=newDate();
formatter.applyPattern("s");
try{
s=Integer.parseInt(formatter.format(currentDate));
}catch(NumberFormatExceptionn){
s=0;
}
formatter.applyPattern("m");
try{
m=Integer.parseInt(formatter.format(currentDate));
}catch(NumberFormatExceptionn){
m=10;
}
formatter.applyPattern("h");
try{
h=Integer.parseInt(formatter.format(currentDate));
}catch(NumberFormatExceptionn){
h=10;
}
//Setpositionoftheendsofthehands
xs=(int)(Math.cos(s*Math.PI/30-Math.PI/2)*45+xcenter);
ys=(int)(Math.sin(s*Math.PI/30-Math.PI/2)*45+ycenter);
xm=(int)(Math.cos(m*Math.PI/30-Math.PI/2)*40+xcenter);
ym=(int)(Math.sin(m*Math.PI/30-Math.PI/2)*40+ycenter);
xh=(int)(Math.cos((h*30+m/2)*Math.PI/180-Math.PI/2)*30
+xcenter);
yh=(int)(Math.sin((h*30+m/2)*Math.PI/180-Math.PI/2)*30
+ycenter);
//Getthedatetoprintatthebottom
formatter.applyPattern("EEEMMMddHH:
mm:
ssyyyy");
today=formatter.format(currentDate);
g.setFont(clockFaceFont);
//Eraseifnecessary
g.setColor(getBackground());
if(xs!
=lastxs||ys!
=lastys){
g.drawLine(xcenter,ycenter,lastxs,lastys);
g.drawString(lastdate,5,125);
}
if(xm!
=lastxm||ym!
=lastym){
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!
=lastxh||yh!
=lastyh){
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
//Drawdateandhands
g.setColor(numberColor);
g.drawString(today,5,125);
g.drawLine(xcenter,ycenter,xs,ys);
g.setColor(handColor);
g.drawLine(xcenter,ycenter-1,xm,ym);
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);
lastxs=xs;lastys=ys;
lastxm=xm;lastym=ym;
lastxh=xh;lastyh=yh;
lastdate=today;
currentDate=null;
}
publicvoidpaint(Graphicsg){
g.setFont(clockFaceFont);
//Drawthecircleandnumbers
g.setColor(handColor);
g.drawArc(xcenter-50,ycenter-50,100,100,0,360);
g.setColor(numberColor);
g.drawString("9",xcenter-45,ycenter+3);
g.drawString("3",xcenter+40,ycenter+3);
g.drawString("12",xcenter-5,ycenter-37);
g.drawString("6",xcenter-3,ycenter+45);
//Drawdateandhands
g.setColor(numberColor);
g.drawString(lastdate,5,125);
g.drawLine(xcenter,ycenter,lastxs,lastys);
g.setColor(handColor);
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
publicvoidstart(){
timer=newThread(this);
timer.start();
}
publicvoidstop(){
timer=null;
}
publicvoidrun(){
Threadme=Thread.currentThread();
while(timer==me){
try{
Thread.currentThread().sleep(100);
}catch(InterruptedExceptione){
}
repaint();
}
}
publicStringgetAppletInfo(){
return"Title:
AClock\n"
+"Author:
RachelGollub,1995\n"
+"Ananalogclock.";
}
publicString[][]getParameterInfo(){
String[][]info={
{"bgcolor","hexadecimalRGBnumber",
"Thebackgroundcolor.Defaultisthecolorofyourbrowser."},
{"fgcolor1","hexadecimalRGBnumber",
"Thecolorofthehandsanddial.Defaultisblue."},
{"fgcolor2","hexadecimalRGBnumber",
"Thecolorofthesecondhandandnumbers.Defaultisdarkgray."}
};
returninfo;
}
}
3、定义一个可序列化的对象Student类,这个类实现了Serializable接口,类中的成员变量id,name,age,department都可以被序列化,方法不能序列化。
通过对象输出流的writeObject()方法将Student对象保存到文件data.ser中,然后通过对象输入流的readObject()方法从文件data.ser中读出保存下来的Student对象,然后将Student对象的id和name输出到控制台。
(25分)
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;
publicclassTest{
publicstaticvoidmain(Stringarg[]){
try{
Studentstudent=newStudent(2010030721,"胡思娥",21,"软件工程系");
ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream("data.ser"));
oos.writeObject(student);
ObjectInputStreamois=newObjectInputStream(newFileInputStream("data.ser"));
Students=(Student)ois.readObject();
s.display();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
classStudentimplementsSerializable{
privateintid;
privateStringname;
privateintage;
privateStringdepartment;
publicStudent(intid,Stringname,intage,Stringdepartment){
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
publicvoiddisplay(){
System.out.println("id:
"+id+"name:
"+name);
}
}
4、编写一个异常类MyException,再编写一个类Student,该类有一个产生异常的方法publicvoidspeak(intm)throwsMyException,要求参数m的值大于1000时,方法抛出一个MyException对象。
最后编写主类,在主类的main方法中用Student创建一个对象,让该对象调用speak方法(m参数设为1500)。
(25分)
importjava.util.Scanner;
classMyExceptionextendsException
{
Stringmessage;
MyException(intm)
{
message="thenumber"+m+"greatthan1000";
}
publicStringtoString()
{
returnmessage;
}
}
classStudent
{
publicvoidspeak(intm)throwsMyException
{
if(m>1000)
{
MyExceptionex=newMyException(m);
throwex;
}
else
{
System.out.println("thenumberis:
"+m);
}
}
};
publicclassTest{
publicstaticvoidmain(Stringargs[])
{
Students=newStudent();
try
{
System.out.print("Pleaseinputnumber:
");
Scannernum=newScanner(System.in);
s.speak(num.nextInt());
}catch(MyExceptionmye)
{
System.out.println(mye.toString());
}
}
};
附加题:
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分)
importjava.util.*;
publicclassT{
publicstaticHashSeths1=newHashSet();
publicstaticHashSeths2=newHashSet();
publicstaticvoidinit()
{
Integer[]A={1,2,3,4};
Integer[]B={1,2,5,6};
for(inti=0;i{
hs1.add(A[i]);
}
for(inti=0;i{
hs2.add(B[i]);
}
}
publicstaticvoidmain(Stringargs[])
{
init();
for(Integeri:
hs1)
{
if(hs2.contains(i))
{
hs2.remove(i);
}
else
{
hs2.add(i);
}
}
System.out.println(hs2);
}
}
6访问Access2003数据库,在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@
80
1002
Mike
Mike@
90
1003
John
John@
78
使用Java语言编写程序,插入3条记录,然后查询表数据并显示。
要求如下:
1)使用JDBC-ODBC桥驱动程序;
2)使用PreparedStatement语句来插入3条记录;
3)查询出表中所有记录,并按照主键升序显示。
参考