1、 定义一个Student类,拥有姓名和托福成绩属性,编写一个程序,完成以下操作:
(1)把下列学生对象存放在一个链表中,姓名和托福成绩如下所示:
姓名:“张小明”,“刘志强”,“李云环”,“孙志华”,“唐德凯”,“杜天”,“张禹”
托福成绩:86,91,72,86,84,82,86
(2)输入一个新的学生对象,包含姓名和托福成绩,将新学生的托福成绩与链表 对象进行对比,然后输出相同托福成绩的学生姓名,具体显示结果如下所示:
新学生——王琳的托福成绩(86)与下列学生:
张小明(86)
孙志华(86)
张禹(86)
的托福成绩相同
【分 数】15分
import java.util.Iterator;
import java.util.LinkedList;
public class T1 {
public static void main(String[]args){
LinkedList<Student> list=new LinkedList<Student>();
list.add(new Student("张小明",86));
list.add(new Student("刘志强",91));
list.add(new Student("李云环",72));
list.add(new Student("孙志华",86));
list.add(new Student("唐德凯",84));
list.add(new Student("杜天",82));
list.add(new Student("张属",86));
Student student=new Student("王琳",86);
Iterator<Student> it=list.iterator();
System.out.println("新学生——"+student.name+"的托福成绩("+student.score+")与下列学生");
while(it.hasNext()){
Student stu=it.next();
if(stu.score==student.score){
System.out.println(stu.name+"("+stu.score+")");
}
}
System.out.println("的托福成绩相同");
}
}
class Student{
String name;//学生姓名
int score;//学生托福成绩
public Student(String name,int score){
this.name=name;
this.score=score;
}
}
2、 使用Java的应用编程接口,编写一个程序,实现显示当前日期和时间,并显示当月日历的效果。在日历显示中,当前日期后加“*”突出,具体显示结果如下图所示:
【分 数】15分
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class Calendars {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
System.out.println("北京时间:" + sdf.format(calendar.getTime()));
showCalendar(calendar, calendar.get(Calendar.DAY_OF_MONTH));
}
private static void showCalendar(Calendar calendar, int day) {
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
int maxday = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
DateFormatSymbols dfs = new DateFormatSymbols(Locale.CHINESE);
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 1; i < dayofweek; i++)
System.out.print("\t");
String str = null;
for (int i = 0; i < maxday; i++) {
if (i + 1 == day)
str = "*";
else
str = "";
if (dayofweek % 7 == 0) {
System.out.println(i + 1 + str);
dayofweek = 1;
} else {
System.out.print((i + 1) + str + "\t");
dayofweek++;
}
}
}
}
3、 假设有一数组,包含12个整数,已经按升序排好。请编写一个程序,使其能从指定位置开始的n(0<n<12)个数按逆序重新排列并输出新的完整数组。
例如:原数组为5,9,11,15,16,19,21,32,45,48,72,83,要求把从第5个数开始的4个数按逆
序重新排列,则得到的新数组为5,9,11,15,32,21,19,16,?45,48,72,83。
【分 数】15分
public class T3 {
public static void main(String[] args) {
int[] array = new int[] { 5, 9, 11, 15, 16, 19, 21, 32, 45, 48, 72, 83 };
System.out.println("原数组为");
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + "\t");
int start = 4;// 指定位置
int n = 5;// 多少个数
for (int i = start - 1; i < start + n - 1; i++)
for (int j = i + 1; j < start + n - 1; j++) {
if (array[i] < array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
System.out.println("\n得到新数组为");
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + "\t");
}
}
4、 使用RandomAccessFile流,编程统计所给素材文件“NEWSPAPER.txt”中的英文单词出现的几种情况,具体要求如下:
(1)统计出现的单词个数;
(2)统计互不相同的单词数量;
(3)统计每个单词出现的频率,并将这些单词按频率从大到小的顺序显示在一个 TextArea中(频率相同的单词显示顺序无要求)。
程序运行效果如下图所示:
【分 数】15分
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class T4 extends JFrame implements ActionListener {
File file = new File("NEWSPAPER.txt");
RandomAccessFile raf = null;
ArrayList<String> list = new ArrayList<String>();
HashMap<String, Integer> map = new HashMap<String, Integer>();
String str = null;// 读取的一行文本
String strs[] = null;// 一行中单词数组
JButton open = new JButton("Open File");
JTextArea info = new JTextArea("");
public T4() {
this.setSize(350, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
open.addActionListener(this);
this.setLocationRelativeTo(null);
this.add(open, BorderLayout.NORTH);
this.add(info, BorderLayout.CENTER);
}
public static void main(String[] args) {
new T4();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == open) {
try {
raf = new RandomAccessFile(file, "rw");
while ((str = raf.readLine()) != null) {
strs = str.split("\\W|\\d");
for (int i = 0; i < strs.length; i++) {
if (strs[i].length() > 0) {
list.add(strs[i]);
if (map.containsKey(strs[i])) {
int value = map.get(strs[i]);
map.put(strs[i], ++value);
} else {
map.put(strs[i], 1);
}
}
}
}
info.append(file.getPath() + "文本中共有" + list.size() + "个英文单词");
info.append("\n其中有" + map.size() + "个互不相同的单词");
Set<Map.Entry<String, Integer>> set = map.entrySet();
List lists = new ArrayList(set);
Collections.sort(lists,
new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
// List
return o2.getValue() - o1.getValue();
}
});
Iterator<Map.Entry<String, Integer>> its = lists.iterator();
while (its.hasNext()) {
Map.Entry<String, Integer> entry = its.next();
String key = entry.getKey();
Integer count = entry.getValue();
info.append("\n" + key + ":" + count + "/" + list.size()
+ "=" + (float) count / list.size());
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
5、 用Socket编程实现客户端读取服务器端对象的交互过程,即客户端将服务器端的文本区(TextArea)对象读取到客户端,并添加到窗口中。需要编程实现2个程序文件,一 个是服务器端程序文件:Server.java,另一个是客户端程序文件:ClientA.java。服务器端程序编译通过后,创建一个ServerSocket在端口4331监听客户请求,等待客户端呼叫,一旦接收到客户端请求,则启动一个新线程, 输出一个文本区对象并发送到客户端,客户端接收服务器端传过来的文本区对象,并显示其内容。
【分 数】20分
Client-->
import java.awt.BorderLayout;
import java.awt.TextArea;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import javax.swing.JFrame;
public class ClientA extends JFrame{
TextArea info=new TextArea("");
public ClientA(){
this.setVisible(true);
this.setSize(350, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.add(info, BorderLayout.CENTER);
client();
}
public void client(){
try{
Socket client=new Socket("127.0.0.1",4331);
InputStream is=client.getInputStream();
byte[] data=new byte[1024];
while((is.read(data))!=-1){
info.append(new String(data));
}
System.out.println("获取成功");
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String []args){
new ClientA();
}
}
Server-->
import java.awt.BorderLayout;
import java.awt.TextArea;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
public class Server extends JFrame {
ServerSocket server = null;
TextArea info = new TextArea("等待客户端的请求并把此信息发送给对方");
public Server() {
this.setVisible(true);
this.setSize(350, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.add(info, BorderLayout.CENTER);
service();
}
public void service() {
try {
server = new ServerSocket(4331);
while(true){
new Sent(server,server.accept(),info.getText()).start();
System.out.println("成功与客户端连接");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
}
class Sent extends Thread{
ServerSocket sev;
String info;
Socket socket;
public Sent(ServerSocket ser,Socket socket,String info){
this.sev=ser;
this.socket=socket;
this.info=info;
}
public void run(){
try{
OutputStream out=socket.getOutputStream();
out.write(info.getBytes());
}catch(IOException ex){
ex.printStackTrace();
}
}
}
6、 在Access2003中创建一个新数据库,名为员工库.mdb。配置ODBC驱动源,连接到员工库上。编写程序,使用JDBC-ODBC桥驱动程序,在Access数据库中创建员工表,并通过程序向表中插入如下数据。表的结构和示例数据如下所示:
员工表:
使用Java语言编写程序,在员工库中创建员工表,插入上述3条记录,然后查询表数据并显示。具体要求如下:
(1)使用JDBC-ODBC桥驱动程序;
(2)通过程序,在员工库中创建员工表,表结构如上所示;
(3)通过程序读取表结构信息并显示;
(4)使用PreparedStatement语句来插入3条记录;
(5)查询出表中所有记录,并按照主键升序显示。
参考提示代码:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
Connection conn=DriverManager.getConnection("jdbc:odbc:employees","","")
【分 数】20分
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 T6 {
public static Connection conn;
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:employees");
createTable();
String sql = "insert into employee values(1001,'Jerry','jerry@126.com',3000,'1982-05-18')";
insert(sql);
sql = "insert into employee values(1002,'Mike','Mike@126.com',3400,'1985-08-18')";
insert(sql);
sql = "insert into employee values(1003,'John','John@126.com',3200,'1982-09-18')";
insert(sql);
sql = "select * from employee order by emloyee_id asc";
query(sql);
}
public static void createTable() throws SQLException {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "create table employee(emloyee_id Integer primary key,name varchar(10) not null,email varchar(30),salary Integer,dates Date)";
boolean result = stmt.execute(sql);
if (result)
System.out.println("成功创建员工表");
else
System.out.println("创建员工表失败");
}
public static void insert(String sql) throws SQLException {
PreparedStatement ps = conn.prepareStatement(sql);
int result = ps.executeUpdate();
if (result > 0)
System.out.println("插入数据成功");
else
System.out.println("插入数据成功");
}
public static void query(String sql) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("员工号\t员工姓名\t员工邮箱\t\t员工工资\t员工生日");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t"
+ rs.getString(3) + "\t" + rs.getInt(4) + "\t"
+ rs.getString(5));
}
}
}