网络程序设计作业答案

上传人:仙*** 文档编号:37908046 上传时间:2021-11-05 格式:DOC 页数:9 大小:93KB
返回 下载 相关 举报
网络程序设计作业答案_第1页
第1页 / 共9页
网络程序设计作业答案_第2页
第2页 / 共9页
网络程序设计作业答案_第3页
第3页 / 共9页
点击查看更多>>
资源描述
网络程序设计一 选择题 1 假设有如下代码::B1. public class Colors 2. public static void main(String args) 3. int n = 1;4. System.out.println(The Color is + argsn);5. 6. 设程序已经通过编译并生成文件Colors.class,运行以下哪一条命令会产生输出 The Color is blue? A. Colors red green blue yellow B. java Colors blue green red yellow C. java Colors green blue red yellow D. java Colors.class blue green red yellow E. java Colors.class green blue red yellow 2 当编译和运行下列代码时会产生什么情况? C1. public class StrEq 2. private StrEq() 3. String s = Bob;4. String s2 = new String(Bob);5. if (s = s2)6. System.out.println(Equal);7. 8. else9. System.out.println(Not equal);10. 11. 12. public static void main(String args)13. StrEq s = new StrEq();14. 15. A. 程序能通过编译并输出 Equal. B. 程序能通过编译并输出 Not Equal. C. 程序在编译时出错。 D. 程序在运行时出错。 3 对下列不完整的代码来说,哪些选项的声明语句能使程序完整并通过编译? DE1. public class Declare 2.3. public static void main(String args) 4.5. System.out.println(The variable is + x);6. 7. A. int x; 放在第2行 B. int x; 放在第4行 C. int x = 5; 放在第2行 D. int x = 5; 放在第4行 E. static int x; 放在第2行 F. int x = new int(); 放在第4行 二编程题1. 写一程序,它能接收命令行输入的2个整数,相加后将结果在屏幕上输出。 (注: 输入的两个命令行参数分别对应args0和args1,但为String类型,可用Integer类的parseInt方法来将它转换为整数后实现相加。例: String str1;int i;str1 = new String(123);i = Integer.parseInt(str1);) public void add(String a,String b)System.out.print(Integer.parseInt(a)+ Integer.parseInt(b);2. 写一程序,它能接收命令行输入的若干个整数,排序后按从小到大的次序在屏幕上输出。 public static void main(String args)throws Exception int temp=0;for(int j=0;jn)temp=m;m=n;n=temp;for(int j=0;jargs.length;j+)System.out.println(argsj);3设名为staff的包中包含两个类,Employee和Manager,其中Manager为Employee的子类,定义如下: class Employee String name; /雇员姓名int age; /年龄char grade; /业绩评等,从高到低为A,B,C,D四等。class Manager extends Employee String department; /Manager所管辖部门Employee subm; /下属雇员请在包中定义一个名为TopManager的类,在该类中定义若干Employee和Manager的样本值,然后写一方法,找出所有Manager中其下属雇员平均业绩评等最高的Manager姓名。import java.util.ArrayList;import java.util.List;public class TopManager Employee subm; Manager subman;public String getMaxManagerName(TopManager topManager) Manager man = topManager.subman;int sum = 0;List list = new ArrayList();for (int i = 0; i man.length; i+) Employee e = mani.subm;sum = 0;for (int j = 0; j e.length; j+) sum += ei.grade;list.add(sum);int n = list.get(0);int index=0;for (int i = 1; i list.size(); i+) int m = list.get(i);if(nm)n=list.get(i);index=i;return topManager.submanindex.name;作业2一. 选择题1. 有以下代码段: Atry throw new EOFException();catch(IOException e) System.out.println(IOException Caught.);这里,EOFException是IOException的子类。假设这段代码是在某个类中,以下拿种说法是正确的?A. 代码不能通过编译B. 代码能够运行并显示IOException Caught.C. throw语句将被忽略,因为没有相应的catch语句与它对应。D. 因为throw语句与catch语句不匹配,程序终止。2. 当浏览器窗口被图标化后又重新恢复原大小时会调用Applet的什么方法?(多选) BEA.init() B.start() C.stop() D.destory() E.paint() 3. 下列哪些类允许你删除磁盘上的一个文件?(多选) ACA.File B.FileInputStream C.FileOutputStream D.FileReader E.FileWriter 4. 通常,URL由以下哪几个部分组成?(多选) CDEA.协议名 B.Socket C.端口号 D.主机名 E.文件路径名 F. 页内参照二请将下列Java Application改写为Applet。import java.awt.*;public class ExGui2 private Frame f;private Button bn,bs,bw,be,bc;public static void main(String args) ExGui2 guiWindow2 = new ExGui2();guiWindow2.go();public void go() f = new Frame(Border Layout);bn = new Button(B1);bs = new Button(B2);be = new Button(B3);bw = new Button(B4);bc = new Button(B5);f.add(bn,BorderLayout.NORTH);f.add(bs,BorderLayout.SOUTH);f.add(be,BorderLayout.EAST);f.add(bw,BorderLayout.WEST);f.add(bc,BorderLayout.CENTER);f.setSize(200,200);f.setVisible(true);将类继承Applet,去掉main函数将其改为,public void init() go();三编制一个具有如下界面的计算器,它能接受鼠标事件并将鼠标对应键的值显示在屏幕上。import java.awt.BorderLayout;import java.awt.Button;import java.awt.Font;import java.awt.Frame;import java.awt.GridLayout;import java.awt.Panel;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener; public class Calculator Frame f=new Frame(Calculator); TextField tf=new TextField(30); Panel p=new Panel(); String btn=0,1,2,3,4,5,6,7,8,9,+,-,*,/,=,.; Button b=new Button16; public void go() f.setSize(300,200); Font ft=new Font(Times,Font.PLAIN,24); p.setFont(ft); p.setLayout(new GridLayout(4,4); tf.setFont(ft); f.add(tf,BorderLayout.NORTH); for (int i=0; ibtn.length; i+) bi=new Button(btni); bi.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) tf.setText(e.getActionCommand(); ); p.add(bi); f.add(p,BorderLayout.CENTER); f.setVisible(true); public static void main(String args) Calculator Cal1=new Calculator(); Cal1.go(); 四建立一对客户/服务程序。服务程序在8000端口侦听,如接收到客户程序送来的口令,验证是否是合法的口令1111,如是,就返回服务器的当前时间给客户程序,否则返回Invalid User!。客户程序设法连到服务程序的8000端口,并通过命令行给出口令,然后等待服务程序的返回,最后将返回的内容在标准输出上显示。(可在一台机器上调试这对程序)package server;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import .Socket;import java.util.Date;/* * 服务器端类 * author Xia * */public class Server implements Runnableprivate Socket s=null;public Server(Socket s)this.s=s;public void run()tryInputStream is=s.getInputStream();OutputStream os=s.getOutputStream();BufferedReader br=new BufferedReader(new InputStreamReader(is);PrintWriter pw=new PrintWriter(os,true);while(true)String str=br.readLine();if(str.equalsIgnoreCase(quit)break;if(1111.equals(str)Date date=new Date();pw.println(date.getHours() + : + date.getMinutes() + : + date.getSeconds();elsepw.println(Invalid User);pw.close();br.close();s.close();catch(Exception es)es.printStackTrace();package server;import .ServerSocket;import .Socket;/* * 服务器端运行程序 * author Xia * */public class ServerDemo public static void main(String args) tryServerSocket ss=new ServerSocket(8001);boolean flag=true;while(flag)Socket s=ss.accept();new Thread(new Server(s).start();ss.close();catch(Exception ex)ex.printStackTrace();package server;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import .InetAddress;import .Socket;/* * 客户端运行程序 * author Xia * */public class Client public static void main(String args) trySocket s=new Socket(InetAddress.getByName(127.0.0.1),8001);InputStream is=s.getInputStream();OutputStream os=s.getOutputStream();BufferedReader br=new BufferedReader(new InputStreamReader(is);PrintWriter pw=new PrintWriter(os,true);BufferedReader keybord=new BufferedReader(new InputStreamReader(System.in);while(true)String str=keybord.readLine();pw.println(str);if(str.equalsIgnoreCase(quit)break;System.out.print(br.readLine();keybord.close();pw.close();br.close();s.close();catch(Exception ex)ex.printStackTrace();注:必须先运行服务器端ServerDemo类,在运行Client类
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!