1. (简答题, 33.3分)
Design a train ticket simulation program Ticket.java. If there are 100 train tickets to be sold at the railway station, now there are 5 ticketing outlets selling tickets at the same time, and 5 threads are used to simulate the ticketing situation at these 5 ticketing outlets. The requirements are as follows:
(1)Output the ticket number sold at each ticketing outlet;
(2)Train tickets with the same number cannot be sold at the these ticketing outlets.
An example of the output result is shown in the figure below. The number on the left is the ticketing outlet number, and the number on the right is the number of the train ticket sold.
设计了一个火车票仿真程序ticket .java。假设火车站有100张火车票出售,现在有5个售票网点同时售票,用5个线程来模拟这5个售票网点的购票情况。具体要求如下:
(1)输出各票务网点售出的票号;
(2)相同数量的火车票不能在这些售票点出售。
输出结果的示例如下图所示。左边的号码是售票口号码,右边的号码是售出的火车票号码:
5______________100
2______________99
4______________98
1______________97
3______________96
……
4______________3
3______________2
2______________1
Please submit program code and the screenshot of the program output in the answer.
public class ticket_test {
public static void main(String[] args) {
ticket x = new ticket();
//启动线程
for(int i=1;i<=5;i++) {
new Thread(x,""+i).start();
}
}
}
public class ticket implements Runnable {
public int total_tickets=100;
public int count=100;
public void run() {
while(total_tickets>0) {
try {
Thread.sleep(101);
synchronized(this) {
if(total_tickets>1) {
count--;
System.out.println(Thread.currentThread().getName()+"______________"+count);
total_tickets--;
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
2. (简答题, 33.3分)
Create database “myDB” with JavaDB, and create table “employee” in it as table-1. Then input the corresponding data as table-2.
(1) Display all records of male employees in the “employee” table.
(2) Add a data record to “employee” table: “2017, Xing SF, female, 650”.
(3) Change the “salary” of the record where “sno” is “2017” to “900” and output the result.
使用JavaDB创建数据库“myDB”,并在其中创建表“employee”作为表1。然后输入表2所示的相应数据。
(1)显示“employee”表中所有男性员工的记录。
(2)在“员工”表中增加一条数据记录:“2017,邢顺丰,女,650”。
(3)将“sno”为“2017”的记录中的“salary”改为“900”,输出结果。
一、 安装javaDB(Derby)
首先,javaDB环境配置
1.下载derby数据库放在一个位置中,我把他放在了H:\Java\jdk-19中。
2.配置环境变量。配置’path’变量,H:\Java\jdk-19\db-derby-10.16.1.1-bin\bin是DB目录下的bin目录.
3.开始—run—cmd,输入sysinfo测试一下,会看到一些Derby信息,这就成功了。
4,我们来创建数据库,首先先选择一个路径,用来存放数据库内容的。就存放C:\Users\x\myderby目录下吧,输入 cd C:\Users\x\myderby 回车 这就可以了.然后输入ij.
C:\Users\x\myderby>ij
ij 版本 10.16
ij> connect 'jdbc:derby:project;user=root;password=123456;create=true';
ij> connect 'jdbc:derby:project;user=root;password=123456;';
ij(CONNECTION1)> create table employee(sno int primary key,name varchar(8),sex varchar(6),salary float);
ij(CONNECTION1)> insert into employee values(1001,'Zhang Y','male',675.20);
已插入/更新/删除 1 行
ij(CONNECTION1)> insert into employee values(1004,'Li X','female',842.00);
已插入/更新/删除 1 行
ij(CONNECTION1)> insert into employee values(1007,'Wang DS','male',765.00);
已插入/更新/删除 1 行
ij(CONNECTION1)> insert into employee values(1010,'Zhao YH','female',690.00);
已插入/更新/删除 1 行
ij(CONNECTION1)> select * from employee;
ij(CONNECTION1)> select * from employee where sex='male';
ij(CONNECTION1)> insert into employee values(2017,'Xing SF','female',650.00);
ij(CONNECTION1)> update employee set salary=900.00 where sno=2017;
ij(CONNECTION1)> select * from employee;
3. (简答题, 33.4分)
Design a client/server communication program based on stream sockets. The requirements are as follows:
(1) Write the server-side program and the client-side program respectively (the port number is 1888).
(2) The client sends information to the server, and the server receives the information and displays it.
(3) At the same time, the server will feedback the information to the client, and the client will receive the information and display it.
Please submit program code and the screenshot of the program output in the answer.
设计基于流套接字的客户端/服务器通信程序。要求如下:
(1)分别编写服务端程序和客户端程序(端口号为1888)。
(2)客户端向服务器发送信息,服务器接收信息并显示。
(3)同时,服务器将信息反馈给客户端,客户端接收信息并显示。
请在答案中提交程序代码和程序输出的屏幕截图。
import java.io.*;
import java.net.*;
public class Server {
private int port = 1888;
public static void main(String[] args) throws IOException{
new Server().service();
}
public void service() throws IOException{
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server is listening on port " + port);
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getRemoteSocketAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println("Received from client: " + inputLine);
// Send data back to the client
String message = "Server received your message: " + inputLine;
out.println(message);
if (inputLine.equals("bye")) break;
}
System.out.println("Connection closed!");
socket.close();
serverSocket.close();
}
}
import java.io.*;
import java.net.*;
public class Client {
private String hostname = "localhost";
private int port = 1888;
public static void main(String[] args) throws IOException{
new Client().connect();
}
public void connect() throws IOException{
Socket socket = new Socket(hostname, port);
System.out.println("Connected to Server: " + socket.getRemoteSocketAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = consoleInput.readLine()) != null){
out.println(userInput);
System.out.println("Sent to server: " + userInput);
String serverResponse = in.readLine();
System.out.println("Response from server: " + serverResponse);
if (userInput.equals("bye")) break;
}
consoleInput.close();
in.close();
out.close();
socket.close();
}
}