网络的程序设计平时作业.docx
- 文档编号:17837656
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:15
- 大小:20.87KB
网络的程序设计平时作业.docx
《网络的程序设计平时作业.docx》由会员分享,可在线阅读,更多相关《网络的程序设计平时作业.docx(15页珍藏版)》请在冰点文库上搜索。
网络的程序设计平时作业
《网络程序设计》
一.选择题
[1]假设有如下代码:
:
1.publicclassColors{
2.publicstaticvoidmain(Stringargs[]){
3.intn=1;
4.System.out.println("TheColoris"+args[n]);
5.}
6.}
设程序已经通过编译并生成文件Colors.class,运行以下哪一条命令会产生输出"TheColorisblue"?
答:
C
A.Colorsredgreenblueyellow
B.javaColorsbluegreenredyellow
C.javaColorsgreenblueredyellow
D.javaColors.classbluegreenredyellow
E.javaColors.classgreenblueredyellow
[2]当编译和运行下列代码时会产生什么情况?
1.publicclassStrEq{
2.privateStrEq(){
3.Strings="Bob";
4.Strings2=newString("Bob");
5.if(s==s2){
6.System.out.println("Equal");
7.}
8.else{
9.System.out.println("Notequal");
10.}
11.}
12.publicstaticvoidmain(Stringargs[]){
13.StrEqs=newStrEq();
14.}
15.}
答:
B
A.程序能通过编译并输出"Equal".
B.程序能通过编译并输出"NotEqual".
C.程序在编译时出错。
D.程序在运行时出错。
[3]对下列不完整的代码来说,哪些选项的声明语句能使程序完整并通过编译?
1.publicclassDeclare{
2.
3.publicstaticvoidmain(String[]args){
4.
5.System.out.println("Thevariableis"+x);
6.}
7.}
答:
D,E
A."intx;"放在第2行
B."intx;"放在第4行
C."intx=5;"放在第2行
D."intx=5;"放在第4行
E."staticintx;"放在第2行
F."intx=newint();"放在第4行
二.编程题
1.写一程序,它能接收命令行输入的2个整数,相加后将结果在屏幕上输出。
(注:
输入的两个命令行参数分别对应args[0]和args[1],但为String类型,可用Integer类的parseInt方法来将它转换为整数后实现相加。
例:
Stringstr1;
inti;
str1=newString("123");
i=Integer.parseInt(str1);
)
答:
主要程序代码如下:
PublicclassAddInteger{
publicstaticvoidmain(String[]args){
if(args.length!
=2)
System.out.println(“参数个数不对!
”);
else{
int i1 = Integer.parseInt(args[0]);
int i2 = Integer.parseInt(args[1]);
int i = i1+i2;
System.out.println("两参数之和为:
"+i);
}
}
}
2.写一程序,它能接收命令行输入的若干个整数,排序后按从小到大的次序在屏幕上输出。
答:
主要程序代码如下:
public static void main(String[] args){
int temp;
int[] values;
for(int n=0;n values[n]= Integer.parseInt(args[n]); } for(int i=0 i < values.length ++i) { for(int j=0; j if(values[j] > values[j + 1]){ temp = values[j]; values[j] = values[j + 1]; values[j + 1] = temp; } } } for(int k=0; k< values.length; ++k){ System.out.println("Index: " + k + "Value: " + values[k]); } } 3.设名为staff的包中包含两个类,Employee和Manager,其中Manager为Employee的子类,定义如下: classEmployee{ Stringname;//雇员姓名 intage;//年龄 chargrade;//业绩评等,从高到低为A,B,C,D四等。 } classManagerextendsEmployee{ Stringdepartment;//Manager所管辖部门 Employee[]subm;//下属雇员 } 请在包中定义一个名为TopManager的类,在该类中定义若干Employee和Manager的样本值,然后写一方法,找出所有Manager中其下属雇员平均业绩评等最高的Manager姓名。 答: 主要程序代码如下: importjava.util.ArrayList; importjava.util.List; publicclassTopManager{ Emploee[]subm Manager[]subman; publicStringgetMaxManagerName(TopManagertopManager){ Manager[]main=topManager.subman; intsum=0; List for(inti=0;i Employee[]e=main[i].subm; sum=0; for(intj=0;j<3.length;j++){ sum+=e[i].grade; list.add(sum); } } intn=list.get(0); intindex=0; for(inti=1;i intm=list.get(i); if(n n=list.get(i); index=I; } returntopManager.submain[index].name; } } 作业2 一.选择题 1.有以下代码段: try{ thrownewEOFException(); } catch(IOExceptione){ System.out.println("IOExceptionCaught."); } 这里,EOFException是IOException的子类。 假设这段代码是在某个类中,以下拿种说法是正确的? 答: B A.代码不能通过编译 B.代码能够运行并显示"IOExceptionCaught." C.throw语句将被忽略,因为没有相应的catch语句与它对应。 D.因为throw语句与catch语句不匹配,程序终止。 2.当浏览器窗口被图标化后又重新恢复原大小时会调用Applet的什么方法? (多选) A.init() B.start() C.stop() D.destory() E.paint() 答: B,E 3.下列哪些类允许你删除磁盘上的一个文件? (多选) A.File B.FileInputStream C.FileOutputStream D.FileReader E.FileWriter 答: A,C 4.通常,URL由以下哪几个部分组成? (多选) A.协议名 B.Socket C.端口号 D.主机名 E.文件路径名 F.页内参照 答: A,C,D,E 二.请将下列JavaApplication改写为Applet。 importjava.awt.*; publicclassExGui2{ privateFramef; privateButtonbn,bs,bw,be,bc; publicstaticvoidmain(Stringargs[]){ ExGui2guiWindow2=newExGui2(); guiWindow2.go(); } publicvoidgo(){ f=newFrame("BorderLayout"); bn=newButton("B1"); bs=newButton("B2"); be=newButton("B3"); bw=newButton("B4"); bc=newButton("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); } } 答: 主要程序代码如下: importjava.awt.*; publicclassExGui2extendsApplet{ privateFramef; privateButtonbn,bs,bw,be,bc; publicvoidinit(){ go(); } publicvoidgo(){ f=newFrame("BorderLayout"); bn=newButton("B1"); bs=newButton("B2"); be=newButton("B3"); bw=newButton("B4"); bc=newButton("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); } } 三.编制一个具有如下界面的计算器,它能接受鼠标事件并将鼠标对应键的值显示在屏幕上。 答: 主要程序代码如下: importjava.awt.*; importjava.awt.event.*; publicclassCalimplementsActionListener{ privateFramef; privatePanelp1; privateLabell1; privateButtonb1,b2,b3,b4,b5,b6,b7,b8; privateButtonb9,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.addActionListener(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.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=newButton("/"); 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.setSize(300,300); f.pack(); f.setVisible(true); } publicvoidactionPerformed(ActionEvente){ l1.setText(e.getActionCommand()); } } 四.建立一对客户/服务程序。 服务程序在8000端口侦听,如接收到客户程序送来的口令,验证是否是合法的口令1111,如是,就返回服务器的当前时间给客户程序,否则返回"InvalidUser! "。 客户程序设法连到服务程序的8000端口,并通过命令行给出口令,然后等待服务程序的返回,最后将返回的内容在标准输出上显示。 (可在一台机器上调试这对程序) 答: 主要程序代码如下: import.*; importjave.io.*; publicclassPasswdClient{ publicstaticvoidmain(Stringargs[])throwIOException{ intc; Sockets1; PrintWriterpw; BufferedReaderbrd; Stringst; s1=newSocket("localhost",8000); pw=newPrintWriter(newBufferedWriter( newOutputStreamWriter(s1.getOutputStream())),true); pw.println(args[0]); brd=newBufferedReader( newInputStreamReader( s1.getInputStream())); st=brd.readLine(); if(st! =null)System.out.println(st); elseSystem.out.println("NoDataReceived! "); pw.close(); brd.clost(); s1.clost(); } } //服务器端参考程序 import.*; importjava.io.*; importjava.util.*; publicclassPasswdServer{ publicstaticvoidmain(Stringargs[]){ ServerSockets=null; Sockets1; Stringpasswd,outString; BufferedReaders1in; PrintWriterpw; try{ s=newServerSocket(8000); }catch(IOExceptione){ System.out.println("ServerSockerCreateError! "); } while(true){ try{ s1=s.accept(); s1in=newBufferedReader( newInputStreamReader(s1.getInputStream())); passwd=s1in.readLine(); if(passwd! =null){ passwd.trim(); if(passwd.equals("1111")){ Dated=newDate(); outString=d.toString(); }else{ outString="InvalidUser! "; } pw=newPrintWriter( newBufferedWriter( newOutputStreamWriter( s1.getOutputStream())),true); pw.println(outString); pw.close(); }else{ System.out.println("NoDataReceived! "); } s1in.close(); s1.close(); }catch(IOExceptione){} } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 网络 程序设计 平时 作业