網絡編程實驗報告.doc_第1頁
網絡編程實驗報告.doc_第2頁
網絡編程實驗報告.doc_第3頁
網絡編程實驗報告.doc_第4頁
網絡編程實驗報告.doc_第5頁
免費預覽已結束,剩余43頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、網絡編程技術實驗報告一實驗目的:網絡編程技術是計算機科學與技術專業、網絡工程專業、軟件工程專業的一門專業基礎課程。本課程以Java技術為主講授,Java語言是當前最流行的網絡 編程語言。 本課程是一門實用性和綜合運用性都很強的課程,實踐教學環節是教學過程中必不可少的重要內容。通過實驗,讓學生熟悉JDK中的主要內容,掌 握用JDK調試和運行程序的方法,掌握網絡編程的基本思想和開發方法、面向對象編程的思想,JAVA中的基本方法和技術,能夠熟練使用 JAVA設計、編寫 程序,特別是基于 TCP/IP的Socket 編程,并能運用這些知識方法完成C/S和B/S結構程序的設計工作。通過實驗,提高學生使用

2、Java語言程序設計開發的能 力,提高應用面向對象技術分析和解決實際問題 的能力,并在此基礎上強化學生的實踐意識、提高其分析問題、解決問題的能力以及動手能力和創新能力。二實驗要求要求學生熟悉JDK中的主要內容,掌握用 JDK調試和運行程序的方法,掌 握網 絡編程的基本思想和開發方法、面向對象編程的思想,JAVA中的基本方法 和技術,能夠熟練使用JAVA設計、編寫程序,特別是基于 TCP/IP的Socket編程,并能運用 這些知識方法完成 C/S和B/S結構程序的設計工作。要注意培養學 生良好的編程習慣, 自始至終貫徹課程中所介紹的程序設計風格。為保證盡量在統一安排的上機時間內完成程序設計任務,

3、學生應事先做問題分析,并做靜態檢查。學生應記錄實驗中所遇到的問題,并寫出詳細的實驗報告。課前準備上機程序,上機認真調試,課后撰寫實驗報告,實驗報告包括實驗目的、實驗內容、源 程序、實驗結果及分析。實驗一java 基本語法實驗目的:了解Java的數據類型,掌握各種變量的聲明方式,理解運算符的優先級,掌握Java基本數據類型、 運算符與表達式, 掌握順序結構、 選擇 結構和循環結構語法的程序設計方法。實驗要求:1、編寫一個聲明Java 不同數據類型變量的程序。2、編寫使用不同選擇結構的程序。3、編寫使用不同循環結構結構的程序。實驗內容:1、編寫一個聲明Java不同數據類型變量的程序。public

4、class DataTypespublic 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(&quo

5、t; 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 Testifpublic static void main(String args)boolean leap;int year=2014;if(ye

6、ar%4=0&&year%100!=0)|(year%400=0)/System.out.println(year+" 年是閏年");elseSystem.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;elseleap=true;if(leap=true)System.out.println(year+" 年是閏年&q

7、uot;);elseSystem.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;elseleap=false;if(leap=true)System.out.println(year+" 年是閏年");elseSystem.out.println(year+" 年不是閏年");2、編寫使用不同選擇結構的程序。/ 使用 switch

8、 語句 /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;c

9、ase '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 IOExcep

10、tionint 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;i<10;i+)for(int j=0;j<10;j+)a=(char)System.in.read();if(a='b')

11、break outer;if(a='c')continue outer;/while 循環語句/import java.io.*;class WhileTestpublic static void main(String args)throws IOExceptionchar ch;System.out.println(" 按 1/2/3 數字可獲大獎!");System.out.println(" 按空格鍵后回車可退出循環操作");while(ch=(char)System.in.read()!=' ') System.

12、in.skip(2);/ 跳過回車鍵switch(ch)case'1':System.out.println(" 恭喜你獲得大獎,一輛汽車");break;case'2':System.out.println(" 不錯呀,你得到一臺筆記本電腦");break;case '3':System.out.println(" 沒白來,你得到一臺冰箱");break;default: System.out.println(" 真不興,你沒有獎品!下次再來");/ 多重循環pub

13、lic class Mul99public static void main(String args) int i,j, n=9;System.out.print(" * |");for(i=1;i<10;i+)System.out.print(" "+i);System.out.print("n|");for(i=1;i<10;i+)System.out.print("-");System.out.println();for(i=1;i<=n;i+)System.out.print("

14、 "+i+" |");for(j=1;j<=i;j+)System.out.print(" "+i*j);System.out.println();實驗感想:實驗二 面向對象編程試驗實驗目的:通過編程和上機實驗理解 Java語言是如何體現面向對象編程基本思想,熟悉類的封裝方法以及如何創建類和對象,熟悉成員變量和成員方法的特性,熟悉類的繼承性和多態性的作用,熟悉包、接口的使用方法,掌握OOP 方式進行程序設計的方法。實驗要求:1、編寫程序實現類的定義和使用。2、編寫不同成員和不同成員方法修飾方法的程序。3、編寫體現類的繼承性(成員變量、成員

15、方法、成員變量隱藏)的程序和多態性(成員方法重載、構造方法重載)的程序。4、編寫接口的定義和使用的程序。5、編寫包的定義和使用的程序。實驗內容 -1. 日期類輸出當前日期import java.io.*;public class Date private int year,month,day;/private static thisYear;public Date(int y,int m,int d)this.year=y;this.month=m;this.day=d;public void read(int y,int m,int d) int y=System.in.read();int

16、 m=System.in.read();int d=System.in.read(); public void set(int y,int m,int d)if(m>=1&&m<=12) return m;elseSystem.out.println(" 該日期錯誤");if(d>=1&&d<=31) return d;else System.out.println(" 該日期錯誤"); public void show( )System.out.println(this.day+"/&q

17、uot;+this.month+"/"+this.year); public static void main(String args)throws IOExceptionDate s=new Date();s.read();s.set(); s.show();/2. 桌子類public class Table private String name;private int longs;private int weight;private intheight;private intwidth;public Table(String n,int l,int we,int h,

18、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"+"

19、; 桌面長度:"+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();/cla

20、ss 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("

21、; 實例變量"+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.ou

22、t.println(" 靜態變量b.x="+b.getY();3. 繼承和多態的作用/*Date:2014.11.23 9:56:00author:Devon function: 功能?創建Rodent (嚙齒動物): Mouse (老鼠), Gerbil (沙鼠), Hamster (大頻 鼠)等的一個繼承分級結構。在基礎類中,提供適用于所有Rodent 的方法,并在衍生類中覆蓋它們,從而根據不同類型的Rodent采取不同的行動。創建一個 Rodent數組,在其中填充不同類型的Rodent,然后調用自己的基礎類方法,看看會有什么情況發生。*/class RodentRod

23、ent 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<r.lenth,i+) ri.TowTooth(); class Mouae extends Rodent cla

24、ss Gerbil extends Mouse class Hamster extends Gerbil interfaceTest.java4、接口的定義和使用public class InterfaceTest public static void main(String args)double x;circle y= circle;y.circle(2);x=y.calculate.area();System.out.println("n 面積為:"+x+"n");interface cal_area double PI=3.14;double c

25、laculate_area(); class circle implements cla_area double r; circle(double r) this.r=r;/ 實現接口中的抽象方法,求圓面積 public double calculate_area() return PI*r*r;5、包的定義和使用?創建自定義包Mypackage/ package Mvpackage; / 聲明存放類的包 import java.util. * ;/ 引用 java.util 包public class Test_YMD private int year,month,day;public st

26、atic void main(String args) public Test_YMD(int y,int m,int d) year = y;month = (m>=1) & (m<=12) ? m :1); day = (d>=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 yea

27、r;/ 返回年份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.

28、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.pr

29、intln(" 出生日期:"+birth.toString();System.out.println(" 今年年齡:"+age();實驗感想:實驗三 異常處理程序設計實驗目的:了解Java中異常處理(exception)的作用及常用的異常類,掌握異常處理的設計方 法。實驗要求:理解系統異常處理的機制和創建自定義異常的方法。實驗內容:Class InsufficientFoundsException extends Exceptionprivate BankAccount m-ba;private double getAmount;Insufficient

30、FoundsException(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&qu

31、ot;);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", "Test"while(I<4)trySystem.out.println(greetingI);catch(

32、ArrayIndexOutofBoundsException esystem.out.println( " 越界 ");I = -l finally system, out.println(" 總會運行"I+/public class Excep2Testpublic static void main(String args) System.out.println(" 這是一個異常處理的例子n");tryint i=10;catch (ArithmeticException e) System.out.println(" 異

33、常是:"+e.getMessage();finally System.out.println("flnally 語句被執行”);試驗感想:實驗四:多線程程序設計實驗目的:理解線程的概念、線程的生命周期,掌握多線程的編程:繼承Thread 類與使用Runnable 接 口。實驗要求:另一種是在用戶自己的掌握兩種創建線程的方法:一種是創建用戶自己的線程子類,類中實現Rimable 接口。實驗內容:/Thread 線程class FruitThread extends Threadpublic FruitThread(String str)super(str);public vo

34、id run()for(int i=0;i<5;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(" 香蕉 ");

35、apple.start();banana.start();class TowFruit1 implements Runnable String name;public TowFruit1(String name)=name;public void run()for(int i=0;i<3;i+)System.out.println(name);Thread.yield();public static void main(String args) TowFruit1 apple=new TowFruit1(" 蘋果 ");TowFruit1 banan

36、a=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 vo

37、id run()for(int i=0;i<20;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("Thr

38、ead 生產的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);t

39、1.start();t2.start();/System.out.println("Hello World!");實驗結果:實驗感想:實驗五:系統I/O 程序設計實驗目的:理解數據流的概念、Java 流的層次結構及文件的概念;熟悉圖形用戶界面基本組件的使用方法,熟悉如何使用布局管理器對組件進行管理及如何使用Java的事件處理機制。實驗要求:1、掌握字節流和字符流的基本使用方法。2、能夠創建、讀寫、更新文件。3、 掌握在 Applet 容器中添加組件的方法, 掌握使用布局管理器對組件進行管理的方法。4、理解Java的事件處理機制,掌握為不同組件編寫事件處理程序的方法5、掌握編

40、寫獨立運行的窗口界面的方法。6、了解對話框及Java Swing 組件的使用方法。實驗內容:public class IOinTestpublic 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.p

41、rintln(" 您剛才輸入的一行字符為: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 = n

42、ew 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.d

43、emo01;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&qu

44、ot;);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

45、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)

46、;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(" 西 &

47、quot;);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 L

48、abel("這是在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 arg

49、s ) throws IOException System. out .println( "Please input some sentences end with 'bye':");BufferedReaderkeyinnew BufferedReader( new InputStreamReader(System. in );String s;RandomAccessFile f = new RandomAccessFile( "e:bye.txt" , "rw"); boolean ss; while ( s =

50、 keyin .readLine() != null ) ss = s .endsWith( "bye");if ( ss) System. exit (0);else System. out .println( s);f .seek( f.length();f.writeUTF( s);5、從鍵盤輸入一個整型數,一個雙精度型和一個字符串,用 DataOutputStream將這些數據輸出到文件中,然后用DatalnputStream從文件中讀出這些數據并打印到標準 輸出設備package com.devon.demo01;import java.io.BufferedRe

51、ader;import java.io.DataOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.IOException;public class TestDataOutputStream public static void main(String args ) throws IOException BufferedReader bf = new

52、 BufferedReader( new InputStreamReader(System. in );File newFile = new File("e:TestDataOutputStream.txt");try FileOutputStreamfOut = new FileOutputStream(newFile );DataOutputStreamdOut = new DataOutputStream(fOut );System. out .println( "Please input Integer:");int i = Integer. p

53、arseInt ( bf .readLine();System. out .println( "Please input Float:" );float f = Float. parseFloat ( bf .readLine();System. out .println( "Please input Double:" );double d = Double. parseDouble ( bf .readLine(););System. out .println( "Please input Boolean: boolean b = new Boolean( bf .readLine().booleanValue();dOut .writeInt( i);dOut .writeFloat( f);dOut .writeDouble( d);dOut .writeBoolean( b);dOut .close(); catch (FileNotFoundException e) System. out .println(e); catch (IOException e) System. out .println(e);6、一個窗口中

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論