




已阅读5页,还剩42页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精选文库 网络编程技术实验报告一实验目的: 网络编程技术是计算机科学与技术专业、网络工程专业、软件工程专业的一 门专业基础课程。本课程以Java技术为主讲授,Java语言是当前最流行的网络 编程语言。本课程是一门实用性和综合运用性都很强的课程,实践教学环节是教 学过程中必不可少的重要内容。通过实验,让学生熟悉JDK中的主要内容,掌 握用JDK调试和运行程序的方法,掌握网络编程的基本思想和开发方法、面向 对象编程的思想,JAVA中的基本方法和技术,能够熟练使用JAVA设计、编写 程序,特别是基于TCP/IP的Socket编程,并能运用这些知识方法完成C/S和 B/S结构程序的设计工作。通过实验,提高学生使用Java语言程序设计开发的能 力,提高应用面向对象技术分析和解决实际问题的能力,并在此基础上强化学生 的实践意识、提高其分析问题、解决问题的能力以及动手能力和创新能力。二实验要求 要求学生熟悉JDK中的主要内容,掌握用JDK调试和运行程序的方法,掌 握网络编程的基本思想和开发方法、面向对象编程的思想,JAVA中的基本方法 和技术,能够熟练使用JAVA设计、编写程序,特别是基于TCP/IP的Socket编 程,并能运用这些知识方法完成C/S和B/S结构程序的设计工作。要注意培养学 生良好的编程习惯,自始至终贯彻课程中所介绍的程序设计风格。为保证尽量在 统一安排的上机时间内完成程序设计任务,学生应事先做问题分析,并做静态检 查。学生应记录实验中所遇到的问题,并写出详细的实验报告。课前准备上机程 序,上机认真调试,课后撰写实验报告,实验报告包括实验目的、实验内容、源 程序、实验结果及分析。 实验一 java基本语法 实验目的:了解Java的数据类型,掌握各种变量的声明方式,理解运算符 的优先级,掌握Java基本数据类型、运算符与表达式,掌握顺序结构、选择 结构和循环结构语法的程序设计方法。实验要求:1、编写一个声明Java不同数据类型变量的程序。2、编写使用不同选择结构的程序。3、编写使用不同循环结构结构的程序。实验内容:1、编写一个声明Java不同数据类型变量的程序。public class DataTypes public static void main(String args) byte b=127; short s=32767; int i=2147483647; long l=9223372036l;/为什么long表示的数比Int还小? char c=c; float f=1.23F; double d=0.9E-3; boolean bool=true; System.out.println( b=+b); System.out.println( s=+s); System.out.println( i=+i); System.out.println( l=+l); System.out.println( c=+c); System.out.println( f=+f); System.out.println( d=+d); System.out.println( bool=+bool); /public class Testif public static void main(String args) boolean leap; int year=2014; if(year%4=0&year%100!=0)|(year%400=0)/ System.out.println(year+年是闰年); else System.out.println(year+年不是闰年); /方法二/ year=2008; if(year%4!=0) leap=false; else if(year%100!=0) leap=true; else if(year%400!=0) leap=false; else leap=true; if(leap=true) System.out.println(year+年是闰年); else System.out.println(year+年不是闰年); /方法三/ year=2050; if(year%4=0) if(year%100=0) if(year%400=0) leap=true; else leap=false; else leap=false; else leap=false; if(leap=true) System.out.println(year+年是闰年); else System.out.println(year+年不是闰年); 2、编写使用不同选择结构的程序。/使用switch语句/1.编写程序用Switch语句实现从键盘上都1,2,3时,屏幕提示不同的信息import java.io.*;class SwitchTestpublic static void main(String args) throw IOExceptionchar a;System.out.println(Enter a number from 1-3:);a=(char)System.in.read();switch(a) case 1:System.out.println(win a Car!);break;case 2:System.out.println(picked the goat);break;case 3: System.out.println(get to keep your100!);break;default:System.out.println(entry);3、编写使用不同循环结构结构的程序。/for循环语句import java.io.*;class ForTestpublic static void main(String args)throws IOExceptionint fahr,cels;System.out.println(Celsius Fahrenheit);for(cels=0;cels=100;cels+=5) fahr=cels*9/5+32; System.out.println(cels+ +fahr);char a;outer:/this is the lable for the outer loopfor(int i=0;i10;i+)for(int j=0;j10;j+)a=(char)System.in.read();if(a=b)break outer;if(a=c)continue outer; /while循环语句/import java.io.*;class WhileTestpublic static void main(String args)throws IOException char ch;System.out.println(按1/2/3数字可获大奖!);System.out.println(按空格键后回车可退出循环操作);while(ch=(char)System.in.read()!= ) System.in.skip(2);/跳过回车键 switch(ch) case1: System.out.println(恭喜你获得大奖,一辆汽车);break; case2: System.out.println(不错呀,你得到一台笔记本电脑);break; case 3: System.out.println(没白来,你得到一台冰箱);break; default: System.out.println(真不兴,你没有奖品!下次再来); /多重循环public class Mul99 public static void main(String args) int i,j, n=9;System.out.print( * |);for(i=1;i10;i+) System.out.print( +i); System.out.print(n-|); for(i=1;i10;i+) System.out.print(-); System.out.println();for(i=1;i=n;i+)System.out.print( +i+ |); for(j=1;j=1&m=1&d=31) return d;else System.out.println(该日期错误); public void show( ) System.out.println(this.day+/+this.month+/+this.year); public static void main(String args)throws IOException Date s=new Date();s.read();s.set();s.show(); /2. 桌子类public class Table private String name; private int longs; private int weight; private int height; private int width; public Table(String n,int l,int we,int h,int wi) =n; this.longs=l; this.weight=we; this.height=h; this.width=wi; int Area() return this.longs*this.width; public void Display() System.out.println(桌子名称:++n+重量:+this.weight+n+桌面宽度:+this.width+n+桌面长度:+this.longs+n+桌子高度:+this.height+n+桌子面积+this.Area(); public void ChangeWeight(int s) this.weight=s; public static void main(String args) Table T=new Table(xiaozuo,9,3,5,3); T.Area(); T.Display(); T.ChangeWeight(90); T.Display(); /class StaticDemostatic int x;int y;public static int getX()return x;public static void setX(int newX)x=newX;public int getY()return y;public void setY(int newY)y=newY;public class TestDemopublic static void main(String args)System.out.println(静态变量+StaticDemo.getX();System.out.println(实例变量+StaticDemo.getY();/非法编译时将出错StaticDemo a=new StaticDemo(); StaticDemo b=new StaticDemo();a.setX(1);a.setY(2);b.setX(3);b.setY(4);System.out.println(静态变量a.x=+a.getX(); System.out.println(静态变量a.y=+a.getY(); System.out.println(静态变量b.x=+b.getX(); System.out.println(静态变量b.x=+b.getY();3. 继承和多态的作用/*Date:2014.11.23 9:56:00author:Devonfunction:功能创建Rodent (啮齿动物):Mouse (老鼠),Gerbil (沙鼠),Hamster (大频 鼠)等的一个继承分级结构。在基础类中,提供适用于所有Rodent的方法,并 在衍生类中覆盖它们,从而根据不同类型的Rodent釆取不同的行动。创建一个 Rodent数组,在其中填充不同类型的Rodent,然后调用自己的基础类方法,看 看会有什么情况发生。*/class RodentRodent r=new Rodent4;public void TowTooth()public static void main(String args) Rodent rodent=new Rodent(); Mouth mouth=new Mouth(); Gerbil gerbil=new Gerbil(); Hamster hamster=new Hamster(); r0=rodent,r1=mouth,r2=gerbil,r3=hamster; for(int i=0,i=1) & (m=1) & (d=31) ? d :1);public Test_YMD() this(0,0,0);public static int thisyear() return Calendar.getInstance().get(Calendar.YEAR);/返回当年的年份public int year() return year;/返回年份public String toString()return year+-n+month+n-+day;/返回转化为字符串的年-月-日/import Mypackage.KY4_1_YMD;/引用 Mypackage 包中的 KY4_1_YMD 类public class YMD_2 private String name;private Test_YMD birth;public static void main(String args)YMD_2 a = new YMD_2(张驰,1990,1,11);a.output();public YMD_2(String nl,Test_YMD dl)name = nl;birth = dl;public YMD_2(String nl,int y,int m,int d)this(nl,new Test_YMD(y,m,d);/初始化变量与对象public int age()/计算年龄return TESt_YMD.thisyear() - birth.yearO;/返回当前年与出生年的差即年龄public void output()System.out.println(姓名:+name);System.out.println(出生日期:+birth.toString();System.out.println(今年年龄:+age();实验感想: 实验三 异常处理程序设计实验目的: 了解Java中异常处理(exception)的作用及常用的异常类,掌握异常处理的设计方法。实验要求: 理解系统异常处理的机制和创建自定义异常的方法。实验内容:Class InsufficientFoundsException extends Exceptionprivate BankAccount m-ba;private double getAmount;InsufficientFoundsException(BankAccount ba,double dAmount)super(Insufficient founds in account);m-ba=ba;getAmount=dAmount;public String toString()StringBuffer sb=new StringBuffer();sb.append(Insufficient founds in account);sb.append(m-ba.getAccountNumber();sb.append(nBalance was);sb.append(m-ba.Balance()sb.append(ngetAmount was);sb.append(getAmount);return sb,toString();public class TestExceplpublic static void main( string args )int 1=0;String greeting= Hello, Only, Testwhile(I4)try System.out.println(greetingI); catch(ArrayIndexOutofBoundsException esystem.out.println( 越界);I = -l finallysystem, out.println(总会运行I+/public class Excep2Testpublic static void main(String args) System.out.println(这是一个异常处理的例子n); try int i=10; catch (ArithmeticException e) System.out.println(异常是:+e.getMessage(); finally System.out.println(flnally 语句被执行”); 试验感想:实验四:多线程程序设计实验目的:理解线程的概念、线程的生命周期,掌握多线程的编程:继承 Thread 类与使用 Runnable 接 口。实验要求:掌握两种创建线程的方法:一种是创建用户自己的线程子类, 另一种是在用户自己的类中实现Rimable接口。实验内容:/Thread线程class FruitThread extends Thread public FruitThread(String str)super(str);public void run()for(int i=0;i5;i+)System.out.println(i+ +getName();trysleep(int)(Math.random()*3000);catch(InterruptedException e)public class TowFruitpublic static void main(String args)FruitThread apple=new FruitThread(苹果); FruitThread banana=new FruitThread(香蕉);apple.start();banana.start();class TowFruit1 implements Runnable String name; public TowFruit1(String name)=name;public void run() for(int i=0;i3;i+) System.out.println(name);Thread.yield(); public static void main(String args) TowFruit1 apple=new TowFruit1(苹果); TowFruit1 banana=new TowFruit1(香蕉);Thread t1=new Thread(apple); Thread t2=new Thread(banana); t1.start(); t2.start();System.out.println(Hello World!);public class ThreadVSRunnable extends Thread implements Runnable String name; public ThreadVSRunnable(String str) super(str); name=Tstr;public void run() for(int i=0;i20;i+) System.out.println(第+i+Thread+getName(); / System.out.println(name); /sleep/ /*trysleep(int)(Math.random()*10000);catch(InterruptedException e)*/yeild/ Thread.yield();public static void main(String args) FruitThread apple=new FruitThread(Thread 生产的 苹果); FruitThread banana=new FruitThread(Thread 生产的 香蕉);apple.start();banana.start(); FruitThread apple1=new FruitThread(Runnable 生产的 苹果); FruitThread banana1=new FruitThread(Runnable 生产的 香蕉);Thread t1=new Thread(apple1);Thread t2=new Thread(banana1);t1.start();t2.start();/System.out.println(Hello World!);实验结果:实验感想:实验五:系统I/O程序设计实验目的:理解数据流的概念、Java流的层次结构及文件的概念;熟悉图形 用户界面基本组件的使用方法,熟悉如何使用布局管理器对组件进行管理及如何 使用Java的事件处理机制。实验要求:1、掌握字节流和字符流的基本使用方法。2、能够创建、读写、更新文件。3、掌握在Applet容器中添加组件的方法,掌握使用布局管理器对组件进行 管理的方法。4、理解Java的事件处理机制,掌握为不同组件编写事件处理程序的方法。5、掌握编写独立运行的窗口界面的方法。6、了解对话框及JavaSwing组件的使用方法。实验内容:public class IOinTest public static void main(String args) byte buffer=new byte255;System.out.println(请在下面输入一行字符:n);try System.in.read(); catch(Exception e) System.out.println(读取输入字符出错,错误信息为:+e.toString()+n); System.out.println(您刚才输入的一行字符为:n);String inputStr=new String(buffer,0);System.out.println(inputStr);/package com.devon.demo01;import java.io.*;class FileStreamsTest public static void main(String args) try FileInputStream fis = new FileInputStream(D:/einput.txt);FileOutputStream fos = new FileOutputStream(eoutput.txt);int c;while (c = fis.read() != -1) fos.write(c);fis.close();fos.close(); catch (FileNotFoundException e) System.err.println(FileStreamsTest: + e); catch (IOException e) System.err.println(FileStreamsTest: + e);/package com.devon.demo01;import java.awt.*;import java.applet.Applet;public class ButtonTest extends Applet Label ll;Button bl, b2, b3, b4, b5, b6;public void init() setLayout(new GridLayout(3, 3); / 设置网格布局(3 行3 列共9 个网格)ll = new Label(标签 1);bl = new Button(按钮 1);b2 = new Button(按钮2);b3 = new Button(按钮3);b4 = new Button(按钮4);add(ll);add(bl);add(b2);add(b3);add(new Label();add(b4);add(new Button(按钮5);add(new Button( 按钮6);add(new Label(标签2);/package com.devon.demo01;import java.awt.*;import java.awt.Color;import java.applet.Applet;public class ComponentTest extends Applet public void init() / 设置最底层# Applet容器为顺序布局setFont(new Font(Ariar, Font.PLAIN, 20);Label l = new Label(这是最底层的 Applet 容器中的标签, Label.CENTER);add(l);Panel panel1 = new Panel();add(panel1);panel1.setBackground(Color.blue);panel1.setForeground(Color.red);panel1.setLayout(new BorderLayout();/ 设置边界布局panel1.add(North, new Button(北);panel1.add(South, new Button(南);panel1.add(East, new Button(东);panel1.add(West, new Button(西);panel1.add(Center, new Label(这是在 Panell 面板中部添加的标签);Panel panel2 = new Panel();add(panel2);panel2.setLayout(new GridLayout(3, 1); / 设置网格布局Choice c = new Choice();/ 创建下拉式列表c.addItem( 北京);c.addItem(上海);c.addItem(天津);Label ll = new Label(这是在Panel2面板中的标签);Button b1 = new Button(Panel2 中的按钮);panel2.setBackground(Color.green);panel2.add(ll);panel2.add(b1);panel2.add(c);/4、从标准设备中输入若干行英文句子,直到输入”bye”结束,将这些字符串 写入文件。package com.devon.demo01;import java.io.*;public class bye public static void main(String args) throws IOException System.out.println(Please input some sentences end with bye:);BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in);String s;RandomAccessFile f = new RandomAccessFile(e:bye.txt, rw);boolean ss;while (s = keyin.readLine() != null) ss = s.endsWith(bye);if (ss)System.exit(0);elseSystem.out.println(s);f.seek(f.length();f.writeUTF(s);5、从键盘输入一个整型数,一个双精度型和一个字符串,用DataOutputStream 将这些数据输出到文件中,然后用DatalnputStream从文件中读出这些数据并打 印到标准输出设备package com.devon.demo01;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 汉字讲解家的课件
- 房地产人员工作总结14篇
- 全国内地西藏班2025届九年级下学期中考一模语文试卷(含答案)
- 河北省邯郸市第二十五中学2024-2025学年八年级下学期期中考试物理试卷(含答案)
- 2024-2025学年山东省枣庄市山亭区九年级(上)期末数学试卷(含答案)
- 0-3岁婴幼儿亲子关系与互动知到智慧树答案
- 幼儿代表发言稿
- 感恩父母发言稿(31篇)
- (19秋冬)信息技术基础知到智慧树答案
- 汉字书法课件之美
- 2025年内河船员考试(主推进动力装置2103·一类三管轮)历年参考题库含答案详解(5套)
- 感染性腹主动脉瘤护理
- 公司不交社保合作协议书
- 城市轨道交通工程监测技术
- 骨灰管理员职业技能鉴定经典试题含答案
- 火锅店股东协议合同范本
- 村流动人口管理办法细则
- 2025年4月安全生产会议记录
- 2025年江苏省苏豪控股集团有限公司校园招聘笔试备考试题及答案详解(各地真题)
- 存款保险宣传培训
- 质量检查员基础知识培训
评论
0/150
提交评论