JAVA语言程序设计教程 第9章
- 格式:ppt
- 大小:2.67 MB
- 文档页数:336
第9章网络通信【1】java提供了哪几种网络通信模式?[解答]:基于TCP/IP协议的面向连接的通信模式,基于UDP协议的面向无连接的通信模式。
【2】java的套接字网络通信方式分为哪几种?[解答]:基于TCP/IP协议:客户端套接字,服务器端套接字。
基于UDP协议:用户数据报套接字,广播数据报套接字。
【3】什么是socket,怎样建立socket连接?建立连接时,客户端和服务器端有什么不同?[解答]:Socket就是套接字,是IP地址和端口号的组合。
当两个网络程序需要通信时,它们可以通过使用Socket类建立套接字连接。
服务器端建立Socket以后,要执行一个等待的方法,以方便客户端来连接。
客户端建立Socket以后即与服务器端进行连接,成功握手以后,双方产生同样的Socket对象。
【4】请列举常用的协议及其端口号。
[解答]:ftp 21/tcptelnet 23/tcpsmtp 25/tcphttp 80/tcppop3 110/tcpsnmp 161/udphttps 443/tcphttps 443/udppop3 110/tcp【5】试描述用Socket建立连接的基本程序框架。
[解答]:(1)客户端建立套接字对象,指定服务器IP和端口号。
(2)服务器端建立套接字,并指定端口号。
(3)服务器端监听本机的端口的状态:执行accept()方法。
(4)客户端程序在对象产生以后以及,服务器端的程序监听到有连接以后都会产生一个Socket类的实例。
(5)对这两个实例中对应的输入流和输出流进行操作,即完成通信过程。
【6】说明客户端如何与服务器端进行连接。
[解答]:TCP/IP的方式是:客户端产生Socket对象的同时产生与对应端口号的服务器连接的动作。
UDP数据报的方式是:客户端建立DatagramSocket对象,建立报文DatagramPacket对象,并指定发送的IP地址,调用socket对象的send方法进行连接并发送数据。
测验-9.2窗体一、多选题 (共100.00分)1.使用Swing编程时,如果要保证线程安全,需要使用哪几种线程:A.初始化线程B.事件调度线程C.工作线程正确答案:A B C测验-9.3菜单一、判断题 (共100.00分)1.快捷键是为了提升效率而设,菜单项一定要设置快捷键。
A.正确B.错误正确答案:B2.助记符是为了提升效率而设,菜单项一定要设置助记符。
A.正确B.错误正确答案:B测验-9.4布局管理一、判断题 (共100.00分)1.为了实现复杂的界面布局,JFrame的内容区可以设置多个Layout。
A.正确B.错误正确答案:B答案解析:只能设置一个。
可以添加JPanel,然后在JPanel上再设置其他布局。
测验-9.5常用组件一、判断题 (共100.00分)1.密码框类JPasswordField是文本框类JTextField的子类。
文本框用JTextField.getText()获取文本内容,密码框可以使用JPasswordField.getText()获取密码文本。
A.正确B.错误正确答案:B答案解析:应该使用JPasswordField.getPassword()测验-9.6常用对话框一、单选题 (共100.00分)1.String s = JOptionPane.showInputDialog(frame,"请输入您所在城市名称:");上面的代码执行后,会弹出一个输入对话框,如果用户点击对话框右上角上的“X”图标(关闭按钮),那么返回值s是:A.""B.nullC.NULLD.false正确答案:B测验-9.7事件处理一、多选题 (共100.00分)1.下面哪些是正确的?A.MouseAdapter是一个类B.MouseAdapter是一个接口C.MouseListener是一个类D.MouseListener是一个接口正确答案:A D。
Chapter9Strings and Text I/O1.s1==s2=>trues2==s3=>falses1.equals(s2)=>trues2.equals(s3)=>truepareTo(s2)=>0pareTo(s3)=>0s1==s4=>trues1.charAt(0)=>Ws1.indexOf('j')=>-1s1.indexOf("to")=>8stIndexOf('a')=>14stIndexOf("o",15)=>9s1.length()=>16s1.substring(5)=>me to Javas1.substring(5,11)=>me tos1.startsWith("Wel")=>trues1.endsWith("Java")=>trues1.toLowerCase()=>welcome to javas1.toUpperCase()=>WELCOME TO JAVA"Welcome".trim()=>Welcomes1.replace('o','T')=>WelcTme tT Javas1.replaceAll("o","T")=>WelcTme tT Javas1.replaceFirst("o","T")=>WelcTme tT Javas1.toCharArray()returns an array of characters consisting of W,e,l,c,o,m,e,,t,o,,J,a,v,a (Note that none of the operation causes the contents of a string to change)2.String s=new String("new string");Answer:CorrectString s3=s1+s2;Answer:CorrectString s3=s1-s2;Answer:Incorrects1==s2Answer:Corrects1>=s2Answer:IncorrectpareTo(s2);Answer:Correctint i=s1.length();Answer:Correctchar c=s1(0);Answer:Incorrectchar c=s1.charAt(s1.length());Answer:Incorrect:it's out of bounds,even if the preceding problem is fixed.3.The output isWelcome to JavaWelcabcme tabc JavaHint:No method in the String class can change the content of the string.String is an immutable class.4.∙Check whether s1is equal to s2and assign the result to a Boolean variable isEqual.boolean isEqual=s1.equals(s2);∙Check whether s1is equal to s2ignoring case and assign the result to a Boolean variable isEqual.boolean isEqual=s1.equalsIgnoreCase(s2);∙Compare s1with s2and assign the result to an int variable x.int x=pareTo(s2);∙Compare s1with s2ignoring case and assign the result to an int variable x.int x=pareToIgnoreCase(s2);∙Check whether s1has prefix"AAA"and assign the result to a Boolean variable b.boolean b=s1.startsWith("AAA");∙Check whether s1has suffix"AAA"and assign the result to a Boolean variable b.boolean b=s1.endsWith("AAA");∙Assign the length of s1to an int variable x.int x=s1.length();∙Assign the first character of s1to a char variable x. char x=s1.charAt(0);∙Create a new string s3that combines s1with s2.String s3=s1+s2;∙Create a substring of s1starting from index 1.String s3=s1.substring(1);∙Create a substring of s1from index1to index 4. String s3=s1.substring(1,5);∙Create a new string s3that converts s1to lowercase. String s3=s1.lowercase();∙Create a new string s3that converts s1to uppercase. String s3=s1.uppercase();∙Create a new string s3that trims blank spaces on both ends of s1.String s3=s1.trim();∙Replace all occurrence of character e with E in s1and assign the new string to s3.String s3=s1.replaceAll(‘e’,‘E’);∙Split"Welcome to Java and HTML"into an array tokens using delimited by a space.String[]tokens="Welcome to Java and HTML".split(‘‘);∙Assign the index of the first occurrence of charactere in s1to an int variable x.int x=s1.indexOf(‘e‘);Assign the index of the last occurrence of string abc in s1to an int variable x.int x=stIndexOf(“abc”);5.No.6.0.e the overloaded static valueOf method in the String class.8.The text is declared in Line2as a data field,but redeclared in Line5as a localvariable.The local variable is assigned with the string passed to the constructor,but the data field is still null.In Line10,test.text is null,which causesNullPointerException when invoking the toLowerCase()method.9.The constructor is declared incorrectly.It should not have void.10.A lowercase letter is between‘a’and‘z’.You can use the staticisLowerCase(char)method in the Character class to test if a character is inlowercase.An uppercase letter is between‘A’and‘Z’.You can use the staticisUpperCase(char)method in the Character class to test if a character is inuppercase.11.An alphanumeric character is between‘0’and‘9’,or‘A’and‘Z’,or‘a’and‘z’.You can use the static isLetterOrDigit(char ch)method in the Character class totest if a character is a digit or a letter.12.The StringBuilder class,introduced in JDK1.5,is similar to StringBuffer exceptthat the update methods in StringBuffer are synchronized.e the StringBuilder’s constructor to create a string buffer for a string,and usethe toString method in StringBuilder class to return a string from a StringBuilder.14.StringBuilder sb=new StringBuilder(s);sb.reverse();s=sb.toString();15.StringBuilder sb=new StringBuilder(s);sb.delete(4,10);s=sb.toString();16.Both string and string buffer use arrays to hold characters.The array in a string isfixed once a string is created.The array in a string buffer may change if the buffer capacity is changed.To accommodate the change,a new array is created.17.(1)Java is fun(2)JavaHTML(3)Jais funva(4)JHTMLava(5)v(6)4(7)Jav(8)Ja(9)avaJ(10)JComputera(11)av(12)va18.The output isJavaJava and HTMLNOTE:Inside the method,the statement s=s+"and HTML"creates a new String objects,which is different from the original String object passed to the change(s,buffer)method.The original String object has not been changed.Therefore,the printoutfrom the original string is Java.Inside the method,the content of the StringBuilder object is changed to Java andHTML.Therefore,the printout from buffer is Java and HTML.19.public static void main(String[]args)can be replaced bypublic static void main(String args[])public static void main(String[]x)public static void main(String x[])but notstatic void main(String x[])because it is not public.20.(1)Number of strings is4Ihaveadream(2)Number of strings is1123(3)Number of strings is0(4)Number of strings is1*(5)Number of strings is(the number of files and directory from where the commandis executed)Displays all files and directory names in the directory where the command isexecuted.21.The\is a special character.It should be written as\\in Java using the Escapesequence.e exists()in the File class to check whether a file e delete()in the Fileclass to delete this e renameTo(File)to rename the name for this file.Youcannot find the file size using the File class.23.No.The File class can be used to obtain file properties and manipulate files,butcannot perform I/O.24.To create a PrintWriter for a file,use new PrintWriter(filename).This statementmay throw an exception.Java forces you to write the code to deal with exceptions.One way to deal with it is to declare throws Exception in the method declaration.If the close()method is not invoked,the data may not be saved properly.25.The contents of the file temp.txt is:amount is32.320000 3.232000e+01amount is32.3200 3.2320e+01falseJava26.To create a Scanner for a file,use new Scanner(new File(filename)).This statementmay throw an exception.Java forces you to write the code to deal with exceptions.One way to deal with it is to declare throws Exception in the method declaration.If the close()method is not invoked,the problem will run fine.But it is a goodpractice to close the file to release the resource on the file.27.If you attempt to create a Scanner for a nonexistent file,an exception will occur.Ifyou attempt to create a Formatter for an existing file,the contents of the existingfile will be gone.28.No.The line separator on Windows is\r\n.29.intValue contains45.doubleValue contains57.8,andline contains'','7','8','9'.30.intValue contains45.doubleValue contains57.8,andline is empty.。