try {
s = new ServerSocket (5432); -----(1) } catch ( IOException e) {} while (true) {
try {
s1 = s.accept(); ------(2) s1out = s1.getOutputStream(); dos = new DataOutputStream(s1out); dos.writeUTF(sendString); -----(3) s1out.close(); s1.close();
} catch (IOException e) {} }
} }
(2)(3分)语句1的功能为构造ServerSocket对象,使服务程序在指定的5432端口侦听。
语句2的功能为调用accept()方法,该方法等待客户端来的连接请求然后返回一个新的Socket与之建立连接。
语句3调用writeUTF()方法将要发送的字符串转换为与机器平台无关的UTF-8格式然后在网上传输。 (3)(2分)程序功能:
服务程序首先构造ServerSocket对象,并在指定的5432端口侦听,当有客户端的连接请求来时先建立连接,然后向客户方发送字符串“Welcome to visit me!”。 b) 参考程序(6分) import java.net.*; import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException { }
第21页 共24页
int c; Socket s1; InputStream s1In; DataInputStream dis;
s1 = new Socket(localhost,5432); s1In = s1.getInputStream(); dis = new DataInputStream(s1In); String st = new String(dis.readUTF()); System.out.println(st); s1In.close(); s1.close()
}
------------------------------------------------------章节分割线------------------------------------------------------------ 1. 有以下程序段:(9分)
11.import java.awt.*; 12.import java.awt.event.*;
13.class X extends Frame implements MouseListener { 14. public X() {
15. this.addMouseListener(this); 16. this.setSize(300,300); 17. this.setVisible(true); 18. } 9.
public void mouseClicked(MouseEvent e) { 10. System.out.println(\11. }
12. public static void main(String[] args) { 13. 14. } 15.}
问:程序能否通过编译并运行?如能,说明程序的功能,如不能,说明原因并修改程序使之能够运行。 参考答案:
程序不能通过编译,因为一个类如要实施某个接口,就要实现该接口中的所有方法,类X并未实现MouseListener的所有方法,所以编译出错。(3分) 修改如下:(6分) import java.awt.*; import java.awt.event.*; class X extends Frame { public X() { }
public static void main(String[] args) { new X(); } }
2.若tryThis()方法抛出NullPointerException例外,则下面一段代码产生的输出是什么?(6分) try{
第22页 共24页
this.addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent e) { System.out.println(\ } });
this.setSize(300,300); this.setVisible(true);
new X();
System.out.println(\tryThis(); return;
} catch(IOException x1) { System.out.println(\} catch(Exception x2) {
System.out.println(\} finally {
System.out.println(\}
参考答案: 程序输出: before Exception exception2 finally
四.下面程序实现对自然数1到10的求和计算,编译时程序出错,请说出错原因并改正。(10分) public class Add_Sum {
public static void main(String args[]) { } }
参考答案:
变量i定义在for循环中,其作用域仅限于for循环,因此在用System.out.println语句输出时会报错。(3分) 修改如下:(7分) public class Add_Sum {
public static void main(String args[]) { } }
五.仔细阅读下列程序,完成以下要求:(10分)
5. 完成划线部分中的XXX(共4处),使程序完整。(8分) 6. 说出下列程序的功能。(2分) 服务程序:
import XXX ; ----(1) import XXX; ----(2) 第23页 共24页
int sum = 0; int i = 0;
for (i=1;i<=10;i++)
sum = sum+i;
System.out.println(\int sum = 0;
for (int i=1;i<=10;i++)
sum = sum+i;
System.out.println(\
public class URLReader {
public static void main(String[] args) throws Exception { }
参考答案:
1. 划线部分(8分)
import java.net.*; -- (1) import java.io.*;; -- (2) openStream() -- (3) readLine() -- (4) 2.功能:(2分)
程序读入华东师范大学网站首页的内容(HTML形式),并在标准输出上显示。
六.编程,从键盘上读入一行,并将其中的小写字符转换为大写字符后在屏幕上回显。上述操作循环执行,直至输入空行,程序结束。(15分) 参考程序: import java.io.*; public class Echo {
public static void main(String[] args) { BufferedReader in = new BufferedReader(
new InputStreamReader(System.in)); String s; try {
while((s = in.readLine()).length() != 0) System.out.println(s.toUpperCase()); } catch(IOException e) { e.printStackTrace(); } } }
URL ecnu = new URL(\/该URL对应华东师范大学网站首页 BufferedReader in = new BufferedReader(
new InputStreamReader( ecnu. XXX )); ----(3)
String inputLine;
while ((inputLine = in. XXX ) != null) ----(4) System.out.println(inputLine); in.close();
}
第24页 共24页