华东师大网络程序设计平时作业资料_第1页
华东师大网络程序设计平时作业资料_第2页
华东师大网络程序设计平时作业资料_第3页
华东师大网络程序设计平时作业资料_第4页
华东师大网络程序设计平时作业资料_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、网络程序设计一 选择题 1 假设有如下代码:1. 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 b

2、lue red yellow D. java Colors.class blue green red yellow E. java Colors.class green blue red yellow 答:C 2 当编译和运行下列代码时会产生什么情况?1. 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

3、);10. 11. 12. public static void main(String args)13. StrEq s = new StrEq();14. 15. A. 程序能通过编译并输出 Equal. B. 程序能通过编译并输出 Not Equal. C. 程序在编译时出错。 D. 程序在运行时出错。答:B 3 对下列不完整的代码来说,哪些选项的声明语句能使程序完整并通过编译?1. public class Declare 2.3. public static void main(String args) 4.5. System.out.println(The variable is

4、+ 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行 答:D,E二编程题1. 写一程序,它能接收命令行输入的2个整数,相加后将结果在屏幕上输出。 (注: 输入的两个命令行参数分别对应args0和args1,但为String类型,可用Integer类的parseInt方法来将它转换为整数后实现相加。例: String str1;int i;str1 = new String(123);

5、i = Integer.parseInt(str1);) 答:主要程序代码如下:publicclassAdd2Integerpublicstaticvoidmain(Stringargs)if(args.length!=2)System.out.println(参数个数不对!);elseinti1=Integer.parseInt(args0);inti2=Integer.parseInt(args1);inti=i1+i2;System.out.println(两参数之和为:+i);2. 写一程序,它能接收命令行输入的若干个整数,排序后按从小到大的次序在屏幕上输出。 答:主要程序代码如下:p

6、ublicstaticvoidmain(Stringargs)inttemp;intvalues;for(intn=0;nargs.length;+n)valuesn=Integer.parseInt(argsn); for(inti=0ivalues.length+i)for(intj=0;jvaluesj+1)temp=valuesj;valuesj=valuesj+1;valuesj+1=temp;for(intk=0;kvalues.length;+k)System.out.println(Index:+k+Value:+valuesk);3设名为staff的包中包含两个类,Emplo

7、yee和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姓名。答:主要

8、源代码如下:importjava.util.ArrayList;importjava.util.List;publicclassTopManagerEmploeesubmManagersubman;publicStringgetMaxManagerName(TopManagertopManager)Managermain=topManager.subman;intsum=0; Listlist=newArrayList();for(inti=0;iman.length;i+)Employeee=maini.subm;sum=0;for(intj=0;j3.length;j+)sum+=ei.g

9、rade;list.add(sum);intn=list.get(0);intindex=0;for(inti=1;ilist.size();i+)intm=list.get(i);if(nm)n=list.get(i);index=I;returntopM;作业2一. 选择题1. 有以下代码段: try throw new EOFException();catch(IOException e) System.out.println(IOException Caught.);这里,EOFException是IOException的子类。假设这段代

10、码是在某个类中,以下拿种说法是正确的?A. 代码不能通过编译B. 代码能够运行并显示IOException Caught.C. throw语句将被忽略,因为没有相应的catch语句与它对应。D. 因为throw语句与catch语句不匹配,程序终止。答:B2. 当浏览器窗口被图标化后又重新恢复原大小时会调用Applet的什么方法?(多选) A.init() B.start() C.stop() D.destory() E.paint() 答:B,E3. 下列哪些类允许你删除磁盘上的一个文件?(多选)A.File B.FileInputStream C.FileOutputStream D.Fil

11、eReader E.FileWriter 答:A,C4. 通常,URL由以下哪几个部分组成?(多选)A.协议名 B.Socket C.端口号 D.主机名 E.文件路径名 F. 页内参照答:A、C、D、E二请将下列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(

12、);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.setSiz

13、e(200,200);f.setVisible(true);答:将类继承改为Applet,去掉main函数,将其改为:publicvoidinit()go();三编制一个具有如下界面的计算器,它能接受鼠标事件并将鼠标对应键的值显示在屏幕上。答:主要程序代码如下:importjava.awt.*;importjava.awt.event.*;publicclassCalimplementsActionListenerprivateFramef;privatePanelp1;privateLabell1;privateButtonb1,b2,b3,b4,b5,b6,b7,b8;privateBut

14、tonb9,b10,b11,b12,b13,b14,b15,b16;publicstaticvoidmain(Stringargs)Calmycal=newCal();mycal.go();publicvoidgo()f=newFrame(MyCalculate);l1=newLabel(0.0,Label.RIGHT);f.add(l1,BorderLayout.NORTH);p1=newPanel();p1.setLayout(newGridLayout(4,4);b1=newButton(7);b1.addActionListener(this);b2=newButton(8);b2.a

15、ddActionListener(this);b3=newButton(9);b3.addActionListener(this);b4=newButton(+);b4.addActionListener(this);b5=newButton(4);b5.addActionListener(this);b6=newButton(5);b6.addActionListener(this);b7=newButton(6);b7.addActionListener(this);b8=newButton(-);b8.addActionListener(this);b9=newButton(1);b9.

16、addActionListener(this);b10=newButton(2);b10.addActionListener(this);b11=newButton(3);b11.addActionListener(this);b12=newButton(*);b12.addActionListener(this);b13=newButton(0);b13.addActionListener(this);b14=newButton(.);b14.addActionListener(this);b15=newButton(=);b15.addActionListener(this);b16=ne

17、wButton(/);b16.addActionListener(this);p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p1.add(b5);p1.add(b6);p1.add(b7);p1.add(b8);p1.add(b9);p1.add(b10);p1.add(b11);p1.add(b12);p1.add(b13);p1.add(b14);p1.add(b15);p1.add(b16);f.add(p1,BorderLayout.CENTER);f.addWindowListener(newMyWindowListener();f.setS

18、ize(300,300);f.pack();f.setVisible(true);publicvoidactionPerformed(ActionEvente)l1.setText(e.getActionCommand();四建立一对客户/服务程序。服务程序在8000端口侦听,如接收到客户程序送来的口令,验证是否是合法的口令1111,如是,就返回服务器的当前时间给客户程序,否则返回Invalid User!。客户程序设法连到服务程序的8000端口,并通过命令行给出口令,然后等待服务程序的返回,最后将返回的内容在标准输出上显示。(可在一台机器上调试这对程序)答:主要程序代码如下:客户端参考程序:

19、import.*;importjava.io.*;publicclassPasswdClientpublicstaticvoidmain(Stringargs)throwsIOExceptionintc;Sockets1;PrintWriterpw;BufferedReaderbrd;Stringst;s1=newSocket(localhost,8000);pw=newPrintWriter(newBufferedWriter(newOutputStreamWriter(s1.getOutputStream(),true);pw.println(args0);brd=newBufferedReader(newInputStreamReader(s1.getInputStream();st=brd.readLine();if(st!=null) System.out.println(st);elseSystem.out.println(NoDataReceived!);pw.close();brd.close();s1.close();服务器端参考程序:import.*;importjava.io.*;importjava.util.*;publi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论