f.writeInt(data[i]);
}
f.seek(12);
for(longi=1;i<=17;i++){
if(f.getFilePointer()!
=0){
System.out.print(““,””+f.readInt());
}else{
return;
}
}
f.close();
}
}
3、编写一个JavaGUI应用程序,窗口标题为“GridLayout”,窗口布局如下图A所示,在图A窗口中单击任意一个Button,网格的划分方式会变化为图B;在图B窗口中单击任意一个Button,网格的划分方式会变化为图A。
(25分)
图A图B
packageITAT4;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.JFrame;
classWinGridextendsFrameimplementsActionListener{
/**
wanglong
*/
privatestaticfinallongserialVersionUID=1L;
GridLayoutgrid;
Buttonone=newButton(““one””);
Buttontwo=newButton(““two””);
Buttonthree=newButton(““three””);
Buttonfour=newButton(““four””);
Buttonfive=newButton(““five””);
Buttonsix=newButton(““six””);
inti=0;
WinGrid(inta,intb){
this.setLayout(newGridLayout(a,b));
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
this.setSize(400,300);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.addWindowListener(newWindowAdapter()//为了关闭窗口
{
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente){
if(i==0){
i=1;
this.setLayout(newGridLayout(2,3));
this.setVisible(true);
}else{
i=0;
this.setLayout(newGridLayout(3,2));
this.setVisible(true);
}
}
}
publicclassExample7_15{
publicstaticvoidmain(Stringargs[]){
newWinGrid(3,2);
}
}
4、采用Java多线程技术,设计实现一个符合生产者和消费者问题的程序。
对一个对象(枪膛)进行操作,其最大容量是12颗子弹。
生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,它不断从枪膛中射出子弹。
(30分)
要求:
(1)给出分析过程说明。
(10分)
(2)程序输出,要模拟体现对枪膛的压入和射出操作;(10)
(2)设计程序时应考虑到两个线程的同步问题。
(10)
两个线程对同一个对象的同一个成员变量进行操作,要考虑到该成员变量的属性范围,同时考虑线程的同步问题。
packageITAT4;
publicclasstest4{
publicstaticvoidmain(String[]args){
xianchengx=newxiancheng();
System.out.println(““线程开始””);
x.yaru.start();
x.shechu.start();
}
}
classqiangtang{
intzidan=0;
voidadd(){
zidan++;
}
voidmove(){
zidan--;
}
intget(){
returnzidan;
}
}
classxianchengimplementsRunnable{
Threadyaru;
Threadshechu;
xiancheng(){
yaru=newThread(this);
yaru.setName(““压入””);
shechu=newThread(this);
shechu.setName(““射出””);
}
qiangtangq=newqiangtang();
publicvoidrun(){
while(true){
if(Thread.currentThread()==yaru){
//q.add();
if(q.get()<12){
q.add();
System.out.println(““加入一颗子弹,共有:
””+q.get()+““颗””);
}else{
try{
Thread.sleep(500);
}catch(InterruptedExceptione){
}
}
}elseif(Thread.currentThread()==shechu){
if(q.get()>0){
q.move();
System.out.println(““射出一颗子弹,共有””+q.get()+““颗””);
}else{
try{
Thread.sleep(500);
}catch(InterruptedExceptione){
}
}
}
}
}
}
附加题:
5、
6、
7、某企业为了促销,搞抽奖宣传活动,奖品为新款手机一部,抽奖规则如下:
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。
手机随机放在其中一个盒子中。
(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。
(m为自然数,m(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m如果有,概率是多少?
(概率=不被抽中的可能数/n)(30分)
importjava.util.HashSet;
importjava.util.Scanner;
importjava.util.Set;
publicclassDrawOutAward{
/**
wanglong
*/
publicstaticvoidmain(Stringargs[]){
newDrawOutAward();
}
publicDrawOutAward(){
//BufferedReaderbr=newBufferedReader(new
//InputStreamReader(System.in));
Scannerscan=newScanner(System.in);
intn=5;
intm=3;
booleanflag=true;
while(flag){
System.out.print(““请输入n:
””);
n=scan.nextInt();
System.out.print(““请输入m:
””);
m=scan.nextInt();
if(mflag=false;
else{
System.out.println(““m必须要小于n””);
}
}
Setset=newHashSet();
set.add(0);
System.out.print(““摸奖过程:
0,””);//起点
for(inti=0;(i+m)%n!
=0;){
intj=(i+m)%n;
System.out.print(j+““,””);
set.add(j);
i=j;
}
System.out.println(0);//终点
if(n==set.size())
System.out.println(““一定会被抽中””);
else{
floatresult=(n-set.size())/(float)n;
System.out.println(““不被抽中的概率为:
””+result);
}
}
}
8、采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。
客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。
(20分)(服务端程序和客户端程序分别命名为Server.java和Client.java)
9、
10、
服务端:
packagesever;
importjava.io.*;
import.*;
publicclassServer{
publicstaticvoidmain(Stringargs[]){
ServerSocketserver=null;
Socketyou=null;
Strings=null;
DataOutputStreamout=null;
DataInputStreamin=null;
try{
server=newServerSocket(4332);
}catch(IOExceptione1){
System.out.println(e1);
}
try{
System.out.println(““等待客户呼叫””);
you=server.accept();
out=newDataOutputStream(you.getOutputStream());
in=newDataInputStream(you.getInputStream());
s=in.readUTF();
while(true){
System.out.println(““服务器收到:
””+s);
out.writeUTF(““你好:
我是服务器””);
if(!
(newFile(““d:
/””+s).exists())){
out.writeUTF(““文件不存在””);
System.out.println(““文件不存在””);
return;
}
out.writeUTF(““文件存在””);
System.out.println(““文件已发送””);
FileInputStreamfis=newFileInputStream(newFile(““d:
/02.jpg””));
byte[]buf=newbyte[288072];
fis.read(buf);
out.write(buf,0,288072);
Thread.sleep(1000);
}
}catch(Exceptione){
System.out.println(““客户已断开””+e);
}
}
}
客户端:
packagesever;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
import.*;
importjavax.swing.JFrame;
publicclassshowtuextendsJFrame{
/**
*
*/
privatestaticfinallongserialVersionUID=9113050687899503448L;
publicstaticvoidmain(String[]args){
try{
Strings=null;
Socketmysocket;
DataInputStreamin=null;
DataOutputStreamout=null;
mysocket=newSocket(““127.0.0.1””,4332);
in=newDataInputStream(mysocket.getInputStream());
out=newDataOutputStream(mysocket.getOutputStream());
out.writeUTF(““02.jpg””);
s=in.readUTF();
System.out.println(““客户收到:
””+s);
s=in.readUTF();
System.out.println(““客户收到:
””+s);
if(s.equals(““文件不存在””)){
System.out.println(““文件不存在””);
}else{
byte[]buf=newbyte[298072];
in.readFully(buf);
FileOutputStreamfos2=newFileOutputStream(““9.jpg””);
fos2.write(buf,0,298072);
}
}catch(IOExceptionex){
}
}
}