java實(shí)驗(yàn)報告完結(jié)版_第1頁
java實(shí)驗(yàn)報告完結(jié)版_第2頁
java實(shí)驗(yàn)報告完結(jié)版_第3頁
java實(shí)驗(yàn)報告完結(jié)版_第4頁
java實(shí)驗(yàn)報告完結(jié)版_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

本科實(shí)驗(yàn)報告課程名稱:Java語言程序設(shè)計(jì)實(shí)驗(yàn)地點(diǎn):軟件學(xué)院209學(xué)院:軟件學(xué)院班級:1426班姓名:李鵬學(xué)號:2014006281指導(dǎo)教師:相潔成績:2016年1月13日

實(shí)驗(yàn)1Java語言基礎(chǔ)實(shí)驗(yàn)?zāi)康耐ㄟ^實(shí)驗(yàn),掌握J(rèn)ava語言程序設(shè)計(jì)的基本方法。學(xué)會Java語言中標(biāo)示符的命名、運(yùn)算符和表達(dá)式的應(yīng)用。熟練地掌握J(rèn)ava程序流程控制語句的應(yīng)用。實(shí)驗(yàn)內(nèi)容“水仙花數(shù)”是指一個3位數(shù),其個位、十位、百位上的數(shù)字的立方和等于該數(shù)本身,例如371=33+73+13,因此371是一個水仙花數(shù)。編寫程序,求所有的水仙花數(shù)。(1)關(guān)鍵算法 思路:編寫一個判斷水仙花數(shù)的函數(shù),然后對函數(shù)調(diào)用,是則返回true,反之返回false publicstaticbooleanIsShuixianhuashu(intnum,inta[]){ inttemp=num;; for(inti=0;i<3;i++){ a[i]=temp%10; temp=temp/10; } temp=a[2]*a[2]*a[2]+a[1]*a[1]*a[1]+a[0]*a[0]*a[0]; if(temp==num) returntrue; else returnfalse; } 以下程序?yàn)閙ain()函數(shù)中的關(guān)鍵程序 for(num=100;num<1000;num++) if(Is(num,a)) System.out.println(num+"="+a[2]+"^3+"+a[1]+"^3+"+a[0]+"^3");(2)運(yùn)行結(jié)果 編寫一個程序,求1-100間的素數(shù)。(1)關(guān)鍵算法思路:編寫一個判斷素數(shù)的函數(shù),然后對函數(shù)調(diào)用,是素數(shù)返回true,反之返回false publicstaticbooleanIsPrime(intx){ if(x<2)returnfalse; for(inti=2;i<x;i++) if(x%i==0) returnfalse; returntrue;}main()函數(shù)里面的主要函數(shù)intoutTime=0; for(inti=2;i<=100;i++){ if(IsPrime(i)){//調(diào)用判斷素數(shù)的函數(shù) System.out.print(i+"\t"); ++outTime; if(outTime%5==0)//五個一行 System.out.println(); }}(2)運(yùn)行結(jié)果 編寫程序,輸出從公元1900年到2100年所有閏年的年號,每輸出5個年號換一行。判斷年是否為閏年的條件是:①若年號可以被4整除,而不能被100整除,則是閏年;②若年號可以被400整除,也是閏年。(1)關(guān)鍵算法 思路:1.(i%4==0&&i%100!=0)||(i%400==0),若滿足該條件則為閏年; 2.輸出一次,計(jì)數(shù)器outTime++,如果outTime%5==0則換行。 intoutTime=0; for(inti=1900;i<=2100;i++) if((i%4==0&&i%100!=0)||(i%400==0)){ System.out.print(i+"\t"); outTime++; if(outTime%5==0) System.out.println(); } }(2)運(yùn)行結(jié)果 實(shí)驗(yàn)體會經(jīng)過第一個實(shí)驗(yàn)以后對java編程語言有了一定認(rèn)識,java語法大多與C/C++相同,編程思路也基本相同,主要還是在于算法。

實(shí)驗(yàn)2Java面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)?zāi)康恼莆認(rèn)ava面向?qū)ο蟪绦蛟O(shè)計(jì)中類、繼承、包和接口的概念與應(yīng)用,能熟練應(yīng)用方法、類、成員變量等元素。實(shí)驗(yàn)內(nèi)容創(chuàng)建一個復(fù)數(shù)類Complex,要求如下利用浮點(diǎn)變量表示此類的私有數(shù)據(jù)。提供兩個構(gòu)造方法,一個用于此類聲明的對象的初始化;一個為默認(rèn)的無參構(gòu)造方法。提供兩復(fù)數(shù)加、減、乘的運(yùn)算方法。按格式(a,b)打印復(fù)數(shù)。其中a為實(shí)部,b為虛部。(1)關(guān)鍵代碼復(fù)數(shù)類Complex: classComplex{ privatefloatRealPart=0f; privatefloatImaginaryPart=0f; //構(gòu)造方法 publicComplex(){ } publicComplex(floatreal,floatimaginary){ this.RealPart=real; this.ImaginaryPart=imaginary; } //實(shí)現(xiàn)加法 publicComplexComplexAdd(Complexc){ ComplexnewComplex=newComplex(); newComplex.RealPart=this.RealPart+c.RealPart; newComplex.ImaginaryPart=this.ImaginaryPart+c.ImaginaryPart; returnnewComplex; } //實(shí)現(xiàn)減法 publicComplexComplexSub(Complexc){ ComplexnewComplex=newComplex(); newComplex.RealPart=this.RealPart-c.RealPart; newComplex.ImaginaryPart=this.ImaginaryPart-c.ImaginaryPart; returnnewComplex; } //實(shí)現(xiàn)乘法 publicComplexComplexMul(Complexc){ ComplexnewComplex=newComplex(); newComplex.RealPart=(this.RealPart*c.RealPart) -(this.ImaginaryPart*c.ImaginaryPart); newComplex.ImaginaryPart=(this.RealPart*c.ImaginaryPart) +(this.ImaginaryPart*c.RealPart); returnnewComplex; } //按格式打印復(fù)數(shù)的實(shí)部和虛部 publicvoidPrint(){ Stringstr="("+this.RealPart+","+this.ImaginaryPart+")"; System.out.println(str); }}main()函數(shù)程序關(guān)鍵程序 //實(shí)例化 Complexc1=newComplex(1f,2f); Complexc2=newComplex(3f,4f); Complexc3=newComplex(); ... c3=c1.ComplexAdd(c2);//c1+c2 System.out.print("c1+c2="); c3.Print(); c3=c1.ComplexSub(c2);//c1-c2 System.out.print("c1-c2="); c3.Print(); c3=c1.ComplexMul(c2);//c1*c2 System.out.print("c1*c2="); c3.Print();(2)運(yùn)行結(jié)果 編程定義一個接口,實(shí)現(xiàn)三個數(shù)中求最小值和最大值的方法,并將程序存放在mymaths包中。(1)關(guān)鍵代碼 packagemymaths;importjava.util.Scanner;interfaceSeekMM{ abstractpublicintMaxNumber(inta,intb); abstractpublicintMinNumber(inta,intb);}classSeekMaxMinimplementsSeekMM{publicintMaxNumber(inta,intb){ returna>b?a:b; }publicintMinNumber(inta,intb){ returna<b?a:b; }}Main()中的部分代碼: Scannersr=newScanner(System.in); System.out.print("輸入三個int型數(shù)據(jù):"); ...sr.close(); inttempMax=seek.MaxNumber(seek.MaxNumber(x,y),z);inttempMin=seek.MinNumber(seek.MinNumber(x,y),z);(2)運(yùn)行結(jié)果 創(chuàng)建銀行賬號Account類,實(shí)現(xiàn)存款(balance)的存(deposit)、取(withdraw)和查詢(getbalance)等功能。(1)關(guān)鍵代碼菜單顯示: publicstaticvoidshowMeau(){ System.out.println("/********操作菜單********/"); System.out.println("\t1.存款\n\t2.取款\n\t3.查詢余額\n\t4.退卡");}main()函數(shù)的部分程序:Accountcustomer=newAccount("李鵬","2014006281","123456");//建立一個示例賬戶... if(strName.equals(customer.getid())&&strPass.equals(customer.getpassword())){ System.out.println("登陸成功!"); while(true){ showMeau(); System.out.print("請輸入你的操作序號:"); choose=sc.nextInt(); switch(choose){ case1: System.out.print("請輸入存款金額:"); money=sc.nextDouble(); customer.deposit(money); break; case2: System.out.print("請輸入取款金額:"); money=sc.nextDouble(); customer.withdraw(money); break; case3: customer.getbalance(); break; case4: sc.close(); System.out.println("退卡成功..."); break; default: System.out.println("輸入有誤!"); } if(choose==4) break; } } else{ System.out.println("密碼或賬號錯誤!"); sc.close();} 銀行賬號Account類: classAccount{ privateStringname; privateStringid; privateStringpassword; privatedoublebalance; publicAccount(){} publicAccount(Stringname,Stringid,Stringpassword){ =name; this.id=id; this.password=password; this.balance=0; } //(存)deposit; publicvoiddeposit(doublemoney){ this.balance+=money; System.out.println("交易成功!"); } //(取)withdraw; publicvoidwithdraw(doublemoney){ if(this.balance<money) System.out.println("對不起,您的余額不足."); else{ this.balance-=money; System.out.println("交易成功!"); } } //(查詢)getbalance; publicvoidgetbalance(){ System.out.println("當(dāng)前余額為:"+this.balance+"元"); } //獲取用戶名 publicStringgetname(){return;} //獲取id publicStringgetid(){returnthis.id;} //獲取密碼 publicStringgetpassword(){returnthis.password;} }(2)運(yùn)行結(jié)果實(shí)驗(yàn)體會 該部分的實(shí)驗(yàn)內(nèi)容相對簡單,與C/C++的整體思路基本沒有區(qū)別,在語法上面稍有不同。整體來說這部分的內(nèi)容相對容易一些。

實(shí)驗(yàn)3數(shù)組和字符串編程實(shí)驗(yàn)?zāi)康耐ㄟ^實(shí)驗(yàn),掌握J(rèn)ava語言中數(shù)組、字符串程序設(shè)計(jì)的基本方法。較熟練地掌握J(rèn)ava程序數(shù)組的應(yīng)用、字符串處理操作的方法應(yīng)用。實(shí)驗(yàn)內(nèi)容編寫一個程序,在控制臺輸入10個數(shù),按大小順序輸出。關(guān)鍵算法思路:在public類中定義函數(shù),通過調(diào)用函數(shù)對數(shù)組操作 publicfinalstaticintN=10;//輸入函數(shù) publicstaticvoidinput(doublearray[]){ System.out.println("請鍵入"+array.length+"個數(shù)字用(空格隔開):"); Scannercin=newScanner(System.in); for(inti=0;i<array.length;i++) array[i]=cin.nextDouble(); cin.close(); } //輸出函數(shù) publicstaticvoidoutput(doublearray[]){ for(inti=0;i<array.length;i++) System.out.print(array[i]+""); System.out.println(); } //冒泡排序函數(shù)(<) publicstaticvoidorder(doublearray[]){ doubletemp; System.out.println("由小到大排序結(jié)果:"); for(inti=1;i<array.length;i++) for(intj=0;j<array.length-i;j++) if(array[j]>array[j+1]){ temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; }}main()中的部分代碼:double[]input_array=newdouble[N]; input(input_array); order(input_array); output(input_array); System.out.println("Main()執(zhí)行完畢...");運(yùn)行結(jié)果輸入一段字符串,統(tǒng)計(jì)其中有多少個單詞。(單詞用空格隔開)(1)關(guān)鍵算法思路:編寫計(jì)算單詞數(shù)量的函數(shù):從字符串中依次取出一個字符,該字符非字母和下一個是字母,則單詞數(shù)量+1;最后判斷第一個字符是字母則不再+1,若第一個是字母,則單詞數(shù)量+1。 //計(jì)算單詞數(shù)量的函數(shù) publicstaticintTotalWord(Stringstr){ charthischar,nextchar; intwordnum=0; for(inti=0;i<str.length()-1;i++){ thischar=str.charAt(i); naxtchar=str.charAt(i+1); if((!((thischar>='A'&&thischar<='Z')||(thischar>='a'&&thischar<='z')))&&((nextchar>='A'&&nextchar<='Z')||(nextchar>='a'&&nextchar<='z'))){ wordnum++; } returnwordnum; } main()函數(shù)關(guān)鍵程序: System.out.println("Wordnumber:"+TotalWord(strEnglish));(2)運(yùn)行結(jié)果 求一個3*3矩陣對角元素之和。(1)關(guān)鍵算法 finalintN=3;int[][]array=newint[N][N]; System.out.println("一個隨機(jī)3*3矩陣:"); for(inti=0;i<N;i++){ for(intj=0;j<N;j++){ array[i][j]=(int)(1+Math.random()*100);//隨機(jī)數(shù)1~100 System.out.print(array[i][j]+"\t"); if(i==j)//主對角線 sum1+=array[i][j]; elseif((i+j)==(N-1))//次對角線 sum2+=array[i][j]; } System.out.println(); }(2)運(yùn)行結(jié)果 實(shí)驗(yàn)體會 通過對java的數(shù)組和字符串的應(yīng)用,java在數(shù)組和字符串的處理功能上很強(qiáng)大,利用這些可以解決很多問題,不必自己再重新編寫某些函數(shù)。

實(shí)驗(yàn)4圖形用戶界面編程實(shí)驗(yàn)?zāi)康恼莆瘴谋窘M件、按鈕和單、復(fù)選按鈕組件的使用;掌握列表的使用,鼠標(biāo)、鍵盤事件的處理;掌握布局控制的方法。實(shí)驗(yàn)內(nèi)容試設(shè)計(jì)一窗口,內(nèi)含一個按鈕。開始運(yùn)行時,按鈕顯示“ClickMe”字樣,當(dāng)按鈕按下時,按鈕顯示為“ClickMeAgain”字樣,再按一次,則按鈕顯示“ClickMe”字樣,依次循環(huán)。(1)關(guān)鍵算法 思路:利用JButton類中的方法getText()獲得按鈕的內(nèi)容字符串,然后利用類String中的方法equals(Stringstr)判斷是否和要求字符串相等,如果不相等替換。 classclickDlgextendsJFrame{ JButtonbtn; JPaneljp; clickDlg(){ btn=newJButton("ClickMe"); jp=newJPanel(); } publicvoidload(){ btn.addActionListener(newlsr()); jp.add(btn); this.add(jp,BorderLayout.CENTER); this.setTitle("ClickButton"); this.setBounds(100,100,300,80); this.setVisible(true); } classlsrimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ Stringstr=btn.getText(); if(str.equals("ClickMe")) btn.setText("ClickMeAgain"); else btn.setText("ClickMe"); } } }(2)運(yùn)行結(jié)果完成圖6-1所示的GUI布局管理,不需要實(shí)現(xiàn)功能。(刪除)(1)關(guān)鍵算法(2)運(yùn)行結(jié)果實(shí)驗(yàn)體會 通過對該實(shí)驗(yàn)的代碼編寫之后,學(xué)習(xí)到了GUI設(shè)計(jì),java界面編程的初步知識,明顯提升的對該部分內(nèi)容的興趣。 在編寫代碼的過程中,發(fā)現(xiàn)圖形界面的編程過程并沒有想象的容易,因?yàn)樗械慕M件都要靠敲代碼來實(shí)現(xiàn),所以要界面的整個美觀性相對差一點(diǎn)。

實(shí)驗(yàn)5輸入與輸出實(shí)驗(yàn)?zāi)康恼莆認(rèn)ava兩種基本輸入/輸出流類的使用。掌握J(rèn)ava文件類型的使用。掌握文件的輸入、輸出方法。實(shí)驗(yàn)內(nèi)容編寫程序,利用Java字符流文件輸入/輸出類,實(shí)現(xiàn)將源程序復(fù)制到另一個文件中。(1)關(guān)鍵算法 packageExperiment7; importjava.io.*; publicclasscopyfile{ publicstaticvoidmain(String[]args)throwsIOException{ Stringfilepath="E:/JavaProjects/Experiment/src/Experiment7/copyfile.java"; if((newFile(filepath).exists())){ BufferedReaderbr=newBufferedReader(newFileReader(filepath)); Stringfilepathwrite="E:/J

溫馨提示

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

評論

0/150

提交評論