1、 水仙花数是指其个位、十位、百位三个数的立方和等于这个数本身。编写一个Java应用程序,求出所有水仙花数。(20分)
2、 编写一个Java应用程序,利用RandomAccessFile类往某个文本文件中写入20个整数(0~19),然后从该文件的第12个字节开始,将后面所有的数据(对应写入的整数)读出。(25分)
3、 编写一个Java GUI应用程序,窗口标题为“GridLayout”,窗口布局如下图A所示,在图A窗口中单击任意一个Button,网格的划分方式会变化为图B;在图B窗口中单击任意一个Button,网格的划分方式会变化为图A。(25分)
图A
图B
4、 采用Java 多线程技术,设计实现一个符合生产者和消费者问题的程序。对一个对象(枪膛)进行操作,其最大容量是12颗子弹。生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,它不断从枪膛中射出子弹。(30分)
要求:
(1)给出分析过程说明。(10分)
(2)程序输出,要模拟体现对枪膛的压入和射出操作;(10)
(2)设计程序时应考虑到两个线程的同步问题。(10)
附加题:
5、 某企业为了促销,搞抽奖宣传活动,奖品为新款手机一部,抽奖规则如下:
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。手机随机放在其中一个盒子中。(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自然数,m<n)
(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m<n),程序分析手机有没有不被抽中的机会?如果有,概率是多少? (概率=不被抽中的可能数/n)(30分)
6、 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。(20分)(服务端程序和客户端程序分别命名为Server.java和Client.java)
T1:
package Four;
public class B1 {
public static void main(String[]args){
System.out.println("所有的水仙花数如下");
shuixianhuashu();
}
private static void shuixianhuashu() {
int a,b,c;
for(int i=100;i<1000;i++){
a=i%10;
b=i%100/10;
c=i/100;
if((a*a*a+b*b*b+c*c*c)==i){
System.out.print(i+"\t");
}
}
}
}
T2:
package Four;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class B2 {
public static void main(String[]args) throws FileNotFoundException, IOException{
File file=new File("");
RandomAccessFile raf=new RandomAccessFile(file.getCanonicalPath()+"\\random.txt","rw");
for(int i=0;i<20;i++)raf.writeInt(i);
raf.seek(12);
for(int i=12;i<=raf.length()-4;i+=4)
System.out.println(raf.readInt());
}
}
T3:
package Four;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class B3 extends JFrame implements ActionListener {
GridLayout lay1 = new GridLayout(2, 3, 3, 3);
GridLayout lay2 = new GridLayout(3, 2);
JButton bt1 = new JButton("one");
JButton bt2 = new JButton("two");
JButton bt3 = new JButton("three");
JButton bt4 = new JButton("four");
JButton bt5 = new JButton("five");
JButton bt6 = new JButton("six");
boolean bool = true;
public B3() {
super("GridLayout");
this.setSize(300, 300);
this.setLayout(lay1);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt4);
this.add(bt5);
this.add(bt6);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
B3 b = new B3();
}
@Override
public void actionPerformed(ActionEvent e) {
if (bool) {
this.setLayout(lay2);
this.validate();
bool = false;
} else {
this.setLayout(lay1);
this.validate();
bool = true;
}
}
}
T4:
package Four;
public class B4 {
public static int i = 0;
public static void main(String[] args) {
B4 b4 = new B4();
Content c = b4.new Content();
Thread t1 = new Thread(b4.new Fire(c));
t1.start();
Thread t2 = new Thread(b4.new Push(c));
t2.start();
}
class Content {
public synchronized void push() {
if (i > 12) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
i++;
System.out.println("装弹一个子弹");
notify();
}
}
public synchronized void fire() {
if (i > 0) {
i--;
System.out.println("射出一个子弹");
notify();
} else
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Fire implements Runnable {
Content c;
public Fire(Content c) {
this.c = c;
}
@Override
public void run() {
while (true) {
c.fire();
}
}
}
class Push implements Runnable {
Content c;
public Push(Content c) {
this.c = c;
}
@Override
public void run() {
while (true) {
c.push();
}
}
}
}
T5:
package Four;
import java.util.LinkedHashSet;
import java.util.Scanner;
public class B5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m, n;
System.out.println("请随机输入两个整数n,m(m<n)");
System.out.print("n=");
n = sc.nextInt();
System.out.print("m=");
m = sc.nextInt();
luckyDraw(n, m);
}
private static void luckyDraw(int n, int m) {
// TODO Auto-generated method stub
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
int[] box = new int[n];
for (int i = 0; i < box.length; i++)
box[i] = i;
set.add(box[0]);
int index = 0, num = 0;
while (true) {
num++;
index++;
if (index == box.length)
index = 0;
if (num % m == 0) {
if (index == 0)
break;
set.add(box[index]);
num = 0;
}
}
System.out.println("抽中的盒子有");
for (int i : set) {
System.out.print(i + " ");
}
System.out.println("\n手机不被抽中的概率是" + (1 - set.size() / (float) n));
}
}
T6:
Client-->
package Four;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class B6Client {
public static void main(String[]args) throws IOException{
byte[] file=args[0].getBytes();
DatagramSocket client=new DatagramSocket();
DatagramPacket dp=new DatagramPacket(file,file.length,InetAddress.getByName("127.0.0.1"),4000);
client.send(dp);
System.out.println("发送成功");
dp=new DatagramPacket(new byte[1024],1024);
while(true){
client.receive(dp);
System.out.println(new String(dp.getData()));
}
}
}
Server-->
package Four;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class B6Server {
public static void main(String[] args) throws IOException {
DatagramSocket server = new DatagramSocket(4000);
DatagramPacket dp = new DatagramPacket(new byte[100], 100);
server.receive(dp);
String str = new String(dp.getData());
File file=new File(str.trim());
System.out.println("收到的文件名是"+file.getName());
if(file.exists()){
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[1024];
while (fis.read(b) != -1) {
dp = new DatagramPacket(b, b.length, dp.getAddress(), dp.getPort());
server.send(dp);
}
System.out.println("发送成功");
}else System.out.println("该文件不存在");
server.disconnect();
server.close();
}
}