1、 编写一个Java应用程序,对用户输入的任意一组字符如{1,3,4,7,2,1,1,5,2},输出其中出现次数最多且数值最大的字符,并显示其出现次数。(本题20分)
2、 编写一个Java应用程序,使用Java的输入输出流技术将Input.txt的内容逐行读出,每读出一行就顺序为其添加行号(从1开始,逐行递增),并写入到另一个文本文件Output.txt中。(本题20分)
3、 编写一个Java应用程序,使用RandomAccessFile流统计Hello.txt中的单词,要求如下:
(1)计算全文中共出现了多少个单词(重复的单词只计算一次);
(2)统计出有多少个单词只出现了一次;
(3)统计并显示出每个单词出现的频率,并将这些单词按出现频率高低顺序显示在一个TextArea中。(本题30分)
4、 编写一个Java GUI应用程序,采用Java多线程技术,有两个线程,模拟垂直上抛运动和水平抛体运动:一个球垂直上抛,一个球水平抛出。(本题30分)
(垂直上抛物理公式:h=v0*t-g*t2/2 ;平抛运动物理公式:h=g*t2/2 ,x=v*t ;
h代表高度,v0代表初速度=30 m/s ,t代表时间,g代表重力加速度=9.8 m/s2 ,v代表平抛速度=30 m/s )
附加题:
5、 编写一个Java应用程序,当用户在输入对话框中输入两个日期后(日期格式为YYYYMMDD,如1999年1月12日应输入为19990112),程序将判断两个日期的先后顺序,以及两个日期之间的间隔天数(例如1999年1月1日和1999年1月2日之间的间隔是1天)。(本题20分)
6、 编写客户/服务器程序,客户端Client.java使用DatagramSocket对象将数据包发送到服务器,请求获取服务器端的图像(考生可自选图像文件)。服务器端Server.java将图像文件包装成数据包,并使用DatagramSocket对象将该数据包发送到客户端。首先将服务器端的程序编译通过,并运行起来,等待客户的请求。(本题30分)
程序的运行效果如下图所示:
客户端
服务器端
T1:
package Three;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class B1 {
public static void main(String[]args){
System.out.println("请输入一组数值数组:");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String s[]=str.split("\\W");
HashMap <String ,Integer>map=new HashMap<String,Integer>();
for(int i=0;i<s.length;i++){
String key=s[i];
if(key.length()>0){
if(map.get(key)==null){
map.put(key, 1);
}else {
int value=map.get(key).intValue();
map.put(key, ++value);
}
}
}
Set<Map.Entry<String, Integer>> entryset=map.entrySet();
int max=0;
String key="";
for(Map.Entry<String, Integer> entry:entryset){
if(entry.getValue()>max){
max=entry.getValue();
key=entry.getKey();
}else if(entry.getValue()==max){
if(entry.getKey().compareTo(key)>0){
key=entry.getKey();
max=entry.getValue();
}
}
}
System.out.println("出现次数最多且数值最大的是"+key+",共"+max+"次");
}
}
T2:
package Three;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class B2 {
public static void main(String args[]) {
BufferedReader br;
BufferedWriter bw;
try {
br = new BufferedReader(new FileReader("input.txt"));
bw = new BufferedWriter(new FileWriter("output.txt"));
String str;
int i = 0;
while ((str = br.readLine()) != null) {
str = ++i+"\t"+ str;
bw.write(str);
bw.newLine();
}
br.close();
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package Three;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class B3 {
public Map<String, Integer> howmuchword(File file) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
String str;
while ((str = raf.readLine()) != null) {
String s[] = str.split("\\W|\\d");
for (String key : s) {
if (key.length() > 0) {
if (map.containsKey(key)) {
int value = map.get(key);
map.put(key, ++value);
} else {
map.put(key, 1);
}
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
public void oneTime(Map<String, Integer> map) {
Set<Map.Entry<String, Integer>> set = map.entrySet();
int time = 0;
for (Map.Entry<String, Integer> entry : set) {
if (entry.getValue() == 1) {
time++;
}
}
System.out.println("\n出现一次的单词共有" + time + "个");
}
public void sortMap(Map<String,Integer> map){
Set<Map.Entry<String, Integer>> set=map.entrySet();
ArrayList<Map.Entry<String, Integer>> list=new ArrayList<Entry<String, Integer>>(set);
//Collections.sort(list);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1,Entry<String, Integer> o2) {
return o2.getValue()-o1.getValue();
}});
System.out.println("按照出现频率高低排顺如下:");
for(Map.Entry<String, Integer> entry:list){
System.out.println(entry.getKey()+"\t"+entry.getValue());
}
}
public static void main(String args[]) {
File file = new File("hello.txt");
B3 b3 = new B3();
Map<String, Integer> map = b3.howmuchword(file);
System.out.println("此文本共有" + map.size() + "个单词");
Set<Map.Entry<String, Integer>> set = map.entrySet();
for (Map.Entry<String, Integer> entry : set) {
System.out.print(entry.getKey()+" ");
}
b3.oneTime(map);
b3.sortMap(map);
}
}
package Three;
import java.awt.Graphics;
import javax.swing.JFrame;
public class A4 extends JFrame implements Runnable {
double x1 = 0, x2 = 0, y = 0, t = 0;
public A4() {
this.setSize(600, 450);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}
public void paint(Graphics g) {
super.paint(g);
y = 9.8 * 0.5 * t * t;
x2 =50* t;
g.fillOval((int) x1, (int) y, 40, 40);
g.fillOval((int) x2, (int) y, 40, 40);
}
public void run() {
try {
while (true) {
Thread.sleep(80);
t += 0.6;
repaint();
if(y>510) System.exit(0);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread thread = new Thread(new A4());
thread.start();
}
}
package Three;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;
public class B5 {
public static void calculate(String date1, String date2) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar time1 = Calendar.getInstance();
Calendar time2 = Calendar.getInstance();
try {
time1.setTime(format.parse(date1));
time2.setTime(format.parse(date2));
int day = 0;
String message = "";
if (time1.compareTo(time2) > 0) {
day = (int) ((time1.getTimeInMillis() - time2.getTimeInMillis()) / (24 * 60 * 60 * 1000));
message = date1 + "和" + date2 + "相隔" + day + "天";
} else {
day = (int) ((time2.getTimeInMillis() - time1.getTimeInMillis()) / (24 * 60 * 60 * 1000));
message = date2 + "和" + date1 + "相隔" + day + "天";
}
System.out.println(message);
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String date = JOptionPane
.showInputDialog("请输入YYYYMMDD格式的两个日期字符串并以空格分开");
if (date == null || date.equals("")) {
JOptionPane.showConfirmDialog(null, "输入不能为空");
System.exit(0);
}
String time[] = date.split("\\s+");
calculate(time[0],time[1]);
}
}
package Three;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class A6Client extends JFrame implements ActionListener {
private static final int port = 2345;
// private static final String address = "127.0.0.1";
private DatagramSocket client;
private DatagramPacket pack;
private ImageIcon imgico;
private JLabel img = new JLabel(imgico);
private JButton jb = new JButton("点击获取图片");
private byte[] b;
public static void main(String[] args) {
new A6Client();
}
public A6Client() {
this.setSize(500, 500);
this.add(jb, BorderLayout.NORTH);
this.add(img, BorderLayout.CENTER);
jb.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb) {
try {
client = new DatagramSocket();
b = new byte[1];
pack = new DatagramPacket(b, b.length,
InetAddress.getByName("localhost"), port);
client.send(pack);
System.out.println("成功发送请求");
b = new byte[100000];
pack = new DatagramPacket(b, b.length);
client.receive(pack);
imgico = new ImageIcon(pack.getData());
img.setIcon(imgico);
System.out.println("接收成功");
client.close();
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}
}
}
package Three;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class A6Server {
private static final int port = 2345;
public static void main(String args[]) {
DatagramSocket server = null;
FileInputStream fis;
DatagramPacket pack;
try {
server = new DatagramSocket(port);
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while (true) {
pack = new DatagramPacket(new byte[1024], 1024);
System.out.println("等待接收到请求...");
server.receive(pack);
System.out.println("成功接收到" + pack.getAddress() + "主机"
+ pack.getPort() + "端口的请求...");
fis = new FileInputStream("c:/2.jpg");
byte data[] = new byte[fis.available()];
fis.read(data);
pack = new DatagramPacket(data, data.length, pack.getAddress(),
pack.getPort());
server.send(pack);
System.out.println("成功向主机" + pack.getAddress() + "的"
+ pack.getPort() + "端口发送图片");
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}