(完整word版)java设计性实验_第1页
(完整word版)java设计性实验_第2页
(完整word版)java设计性实验_第3页
(完整word版)java设计性实验_第4页
(完整word版)java设计性实验_第5页
已阅读5页,还剩12页未读 继续免费阅读

付费下载

下载本文档

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

文档简介

1、Java设计性试验报告本科学生设计性实验报告项目组长 丁雪辉学号0088231成员尚丽娟(0088234)专业软件工程 班级08软件4班实验项目名称统计文件中的字符数、单词数和行数指导教师及职称毛澄映开课学期 2010 至2011 学年2学期上课时间2011 年 06 月 02 日、实验设计方案实验名称:统计文件中的字符数、单词数和行数实验时间:2011年6月2日小组合作:是b 否O小组成员:XXX1、实验目的:通过试验(统计文件中的字符数、单词数和行数)的设计和开发,使学生了解Java程序设计在软件工程专业中的地位,掌握Java程序设计的方法,巩固他们在课堂上学习到的知识,并培养作为软件开发

2、者应该具有的合作精神,激发学生主动学习新知识和应用新知识的意识。2、实验场地及仪器、设备和材料:实验场地:Z113 ;实验设备:计算机,Eclipse环境。3、 实验思路(实验内容、数据处理方法及实验步骤等):(1) 实验内容:本实验主要是通过统计任意的一个文本中的字符数、单词数和行数,文件名是用户直接输入的, 具有普遍性。要求:(i) 单词由空格、标点符号、回车换行符等分隔。(ii) 实验中的文件为英文文件,其文件的内容大小不能少于50行。(iii) 文件名作为命令行参数传递。(2) 数据处理:用户输入文件名,程序中通过创建一个文件输入流获得内容,抛出异常处理文件没 找到和输入输出的处理,对

3、于统计出来的次数,写入到输出文件。(3) 实验步骤:首先,1.我们现在先打开eclipse,然后新建名为project的项目,然后再新建一个名为test的包,然后再新建一个名为test的类。然后输入所要写的代码import java.io.*;public class Test public static void main(String args)throws Exceptionint charNumber = 0;int wordNumber = 0; int lineNumber = 0;Stri ng txt1 = args0;File file = new File(txt1);wh

4、ile (file.createNewFile() System. out .println(”正在创建输入的文本的文件,请稍后输入相关内容到文件中);FileReader read = new FileReader(file);BufferedReader bufferread = new BufferedReader(read);while (bufferread.read()!= -1) String s = bufferread.readL in e(); charNumber += s.len gth(); wordNumber += s.split( ). length ; lin

5、 eNumber +;Stri ng s =n ewStri ng();s =字符数:+charNumber+rn +单词数:+wordNumber+rn +行数:+lineNumber ;Stri ng txt2 = args1;File file2 = new File(txt2);while (file2.createNewFile() System.out.pri ntln(正在创建输岀的文件!);FileWriter fwrite =new FileWriter(file2);BufferedWriter bufferwrite =n ewBufferedWriter(fwrite)

6、;bufferwrite.write(s);bufferwrite.close();bufferread.close();然后我们点击保存。然后把文件复制到一个磁盘的根目录下面,以便在cmd里面更方便的找到文件。其次,我们在cmd里面编译我们复制过来的java文件。在cmd里面输入E: ”这是我们java文件锁存放的盘。然后写javac Test.java 按enter键进行编译之后再根目录下就会出现一个Test.class的文件。然后我们在 cmd里面里面输入 java Test input.txt output.txt.在E盘这个根目录下面就会发现多处两个名为in put和output的t

7、xt文件。最后,我们在in put这个txt文件里面输入文本文件,里面必须有单词,字符,和超过50行的单词。然后我们在cmd里面输入java Test input.txt output.txt.然后按enter之后我们在回到 E盘查看output这个文件。里面就会显示如下数据:字符数:2940单词数:546行数:50指导老师对实验设计方案的意见:指导老师签名:年 月 日、实验结果与分析1、实验目的、场地及仪器、设备和材料、实验思路等见实验设计方案2、实验现象、数据及结果1. 实验现象及结果在命令行传参之后,可在Test.java的相对路径下创建了两个txt文件,分别是:input.txt和ou

8、tput.txt.,rest,java文本文裆截图如下:Test.dassCLASS立件2 KB utpjttxt文玉文档I KB程序在eclipse环境下没有显示错误提示的程序截图如下:世皿IM他I理目(B 运行(R庄口f世)甘帥也l Hwpw 即&E CwntLetter.jaftfaEl LshiHiqN.jWAapaiukaEv tdit - pnhlir cla-E-ir Tert.(puihlic static wid iMtianCStnnc arcs) threwj? in.t chuSluDih-si Dl int Tc-rdNuctt*! = 0 int linf-Nmib

9、tK = Q,Steanj txti = aifstt: Pile file = FilettstO:vhile Cfile cre-arteXevFilet?) !System splintltiCZ在刨建鬻入Bfl:丈幸闌文怜 询悄馬歸人相黄内客到文件中T;File-Re-adei itid = iie-t FiLeReaderifilE); Iuffriidttd* Wf*rr*d = n贞草“血审讥“机);vhi 1c haffenrc&ri. rtidX) 1= -I)St mis i = buffe rread. redLineC ); Eliai3Toil:x += s. ler

10、i(th.(! ;u s, split C length. liJieWuMfcSi r:s B :B ,*_chaiNmibflE+, EnP+4i-i!; P+woidN,ij*bei+ i nB +i +1 inpftiinbe r .String txt = axji I J; File filZ nev File (txt .while Cfile2. tNtvFile ()System, oirf.茁)祇k广正住创凰騙出囱交愕! J .问JS Mgc声则晏EKtu : 示的圮利白.在命令行中编译和运行文件并且创建两个txt文件截图如下:degree in whichfioLlene

11、sspresence.condEnmationdegree in whichnoblenesscondemnation.degree in uhichnffililenepresence.condpnniaticin,degree m uhichnoblenessLn 50/Col 20input.txt文件就会出现以下异常情况,统计的结果全当创建好了的输入和输出两个文件都正常创建并且存在的话,统计出了正确的结果,截图如下:i&curriens and Settings AdlnilnIstratopE:jauac lest.jauaJava Test input .txt: (JuiXpLi

12、t.txt当在Test.java的相对路径下删除刚刚创建的 部为0,截图如下:part of all healtni/ human nature, nf it is not an assurEd ground ofh java. Test input frxJt outpuit . t xt器瞬貓加咲输入相关楠到文件中an assured ground of goodness of heart- ujariable sign of in proportion to the degree in ujhich it is felt and beauty of character will t)Q

13、attaineicl Natural beamityi is all-prudOingi presence. The uniuerse is its temple, dingxuehiii sh angli juanThe love mF ibeautji is anpart nF all healthiy hynan naturet is a mural quality Tlie absenee* of it is not but th& pr&senc of it an iiii/ariabl sigii of In proportlorh to trim degree in iuhici

14、hi it is felt and beaiuty of character will De attained.Natural beaiuty is. an all-ppruadingThe uniuerse is its tempipThe JLdue of beauty is an essential It is a moral quality. The absence but the presence oF it is an inuariable sign of goodness of heart In proportion to the degree in which it is fe

15、lt wiU probably be the and beauty of character nill be attained natural beauty is an all-pervading presence.The universe is Its tenple.The lone of beauty is essential part oF 11 healthy hunan nature. It is a mordl quaLity. The absence of it is not an assured ground of but the presenc.e of it is -an

16、invariHaihle1 sign o-F gnndness nF hert, Iri 卩rapnrtion tn the degree in mhiclh it i百 felt mil probably he the and beaut: uF Chirac I: Er uill bB attainiPd.Hatural bp心uty is. an dll-perudingThe uiniu&r&e is itsTTio loue of bauti/ is 21) sspntial It is a moral quality. The absencensnFt UiAdous MP5-l-

17、36nPll版斤有 198-2001 NicroeoFt Coi*p_Input.txt文件中输入50行英文文章截图如下:立件(日編翔或稻弍查着帮助(出 字符数诃单词数诃行数油F口 1回渥文件(日腐!植贰fO)査看(如帮肋C H)Eurtdepi nE I: ion,丈件(E)扁辑 榕式(查看他帮取比2.实验数据输入的代码:import java.io.*;public class Test public static void main(String args)throws Exceptionint charNumber = 0;int wordNumber = 0;int lineNumb

18、er = 0;Stri ng txt1 = args0;File file = new File(txt1);while (file.createNewFile() System. out .println(”正在创建输入的文本的文件,请稍后输入相关内容到文件中);FileReader read = new FileReader(file);BufferedReader bufferread = new BufferedReader(read);while (bufferread.read()!= -1) String s = bufferread.readL in e(); charNumb

19、er += s.len gth();wordNumber += s.split( ). length ;lin eNumber +;Stri ng s =n ewStri ng();s =字符数:+charNumber+rn+单词数:+wordNumber+rn +行数:+lineNumberStri ng txt2 = args1;File file2 = new File(txt2);while (file2.createNewFile() System. out.pri ntln(正在创建输岀的文件!);FileWriter fwrite =new FileWriter(file2);B

20、ufferedWriter bufferwrite =n ewBufferedWriter(fwrite);bufferwrite.write(s);bufferwrite.close();bufferread.close();Text里面需要我们输入的数据:The love of beauty is an esse ntial part of all healthy huma n n ature.It is a moral quality. The absence of it is not an assured ground of condemnation,but the prese nce

21、 of it is an in variable sig n of good ness of heart.In proporti on to the degree in which it is felt will probably be the degree in which n oble ness and beauty of character will be atta in ed.Natural beauty is an all-pervadi ng prese nce.The uni verse is its temple.The love of beauty is an esse nt

22、ial part of all healthy huma n n ature.It is a moral quality. The absence of it is not an assured ground of condemnation,but the prese nce of it is an in variable sig n of good ness of heart.In proporti on to the degree in which it is felt will probably be the degree in which n oble ness and beauty

23、of character will be atta in ed.Natural beauty is an all-pervadi ng prese nce.The uni verse is its temple.The love of beauty is an esse ntial part of all healthy huma n n ature.It is a moral quality. The absence of it is not an assured ground of condemnation,but the prese nce of it is an in variable

24、 sig n of good ness of heart.In proporti on to the degree in which it is felt will probably be the degree in which n oble ness and beauty of character will be atta in ed.Natural beauty is an all-pervadi ng prese nce.The uni verse is its temple.The love of beauty is an esse ntial part of all healthy

25、huma n n ature.It is a moral quality. The absence of it is not an assured ground of condemnation,but the prese nce of it is an in variable sig n of good ness of heart.In proporti on to the degree in which it is felt will probably be the degree in which n oble ness and beauty of character will be att

26、a in ed.Natural beauty is an all-pervadi ng prese nce.The uni verse is its temple.The love of beauty is an esse ntial part of all healthy huma n n ature.It is a moral quality. The absence of it is not an assured ground of condemnation,but the prese nce of it is an in variable sig n of good ness of h

27、eart.In proporti on to the degree in which it is felt will probably be the degree in which n oble ness and beauty of character will be atta in ed.Natural beauty is an all-pervadi ng prese nce.The uni verse is its temple.The love of beauty is an esse ntial part of all healthy huma n n ature.It is a m

28、oral quality. The absence of it is not an assured ground of condemnation,but the prese nce of it is an in variable sig n of good ness of heart.In proporti on to the degree in which it is felt will probably be the degree in which n oble ness and beauty of character will be atta in ed.Natural beauty i

29、s an all-pervadi ng prese nce.The uni verse is its temple.The love of beauty is an esse ntial part of all healthy huma n n ature.It is a moral quality. The abse nce of it is not an assured ground of condemn ati on.but the prese nee of it is an in variable sig n of good ness of heart.In proporti on t

30、o the degree in which it is felt will probably be the degree in which n oble ness and beauty of character will be atta in ed.Natural beauty is an all-pervadi ng prese nee.The uni verse is its temple.Din gxuehui sha nglijua n3、对实验现象、数据及观察结果的分析与讨论:1. 在开始的时候,我们把复制过来的 Test.java文件在cmd里面进行编译就会出现以下的现象根本无法创

31、建两个文件。其原因是因为我们没有把引入的 java文件里面Package test ”这行 代码给去掉。因为在cmd里面无法引入这个包文件2. 在此次过程。我们注意到, main方法的声明与众不同。它具有String类型的参数args。很明显,参数args是一个字符串数组。Main方法与带有参数的普通方法一样,可以通过传递实参来调用一个普通方法,能够 main传递参数。3. 并且在编写代码的时候这句是非常重要的Stri ng s =new Stri ng();s =字符数:+charNumber+ rn+单词数:+wordNumber+rn +行数:+lineNumber ;开始的 时候我们打

32、算用 System,out,println 字符数:+charNumber+ rn+单词数:+wordNumber+rn +行数:+lineNumber )这条语句可以代替,以免麻烦,后来了解到在 输入的文件方法需要调用这条语句进行分辨里面单词数和行数等等。4、结论:成功完成实验,成功用cmd命令创建两个输入和输出文件,在试验的结果输出的文件能准确的计算出字符,单词以及行数的统计。在试验过程中,遇到了很多问题,经过老师和同学的指导能够顺利 的得到结果,因为对于利用文件名作为命令行参数传递这部分还不是很熟悉,但是在最后成功得到自己想要的结果,也掌握其中的一些技巧和知识。5、实验总结在试验的开始,

33、在cmd输入命令的时候,可以找到我们的java文件并且可以进行编 译,但是总是无法创建两个输入和输出文件。试验中,让我们更加的了解到可以从命令行给main方法传递参数。也让我们更加的明白我们的学习的东西还不是很全面。1本次实验成败之处及其原因分析:1. 1.在开始的时候,我们把复制过来的Test.java文件在cmd里面进行编译就会出现以下的现象根本无法创建两个文件。在最后我们了解到 关闭进行读取的输入流和关闭进行写入的输出流也是非常重要。2本实验的关键环节及改进措施:IC-DocLinents andinjysMIdnin istratorJE:-Jaw E:Ljvac Test R Jav

34、ajav* Test inputvtKtTest, java JAVA文件ZK0input.txt 文本文档1 KBTest,匚 la 弗CLASS文件2 KBinput .txt nutinit.txt髓鮒讓鬱文件曲嵐入联巒到文件中jaiva . langr. Class Loa. die v. A&f i_ne Class Hat; tvs Het: hod. Jaiva. larigf. ClassLdader. def ineClassCoiiidCClassLoader -JaMa 632Javi .lng. ClasRLnarier.def ineClr*KSClassLnJdiv

35、a B 3 cquf lit y -.Securedef incCla *CGcGu.r-cC193 Lohdcirv jakra =14output.txt 文本立档 0 KBJai .Rfit.UBLfilassLnailRr.def inGlissClIRLClasrinader.jfivaXRl J Java.nct .URLCasLocid&r accc3$B06 J.URLClasLod&rlrun jAuaj|. AcreTCnntrallFM*.eIihPfiui lp(|fedNaitiue Mg tbndlJdiVd ric t .URLClasLaid&r

36、f indClassURLCl Jaiya. lang. ClassLosider* loadlCls&Cla?&Loder jau;30?) eun .mice .Launch*rAppCLafrLoadoir loadClaci? J曰a. 1 any ClisLoader! LunlC las sCClaLo de J vAl;atatAtditatatCouLd. not find the main class: TestB Ppoar良徜 will exitaCCy19HS-2001HiewraoitCorp -java文件里面Package test ”这行代码给去掉。因为在 cm

37、dosaFt Vlndawi? XP 5 v 1,26001 版权所有 1985-2001 Microsoft Corp.其原因是因为我们没有把引入的里面无法引入这个包文件。2.在试验过程,因为没有考虑到代码的最后两行:bufferwrite.close();bufferread.close();我们就直接进行编译以及对命令行进行传参。在进行到对命令行输入创建两个文件的命令就会发现输岀文件里面什么都没有,并且在对in put这个文件进行输入文本的时候在在cmd里面进行输入命令的时候也发现in put文件和output文件里面什么都没有。本实验的关键环节:1在这个试验中,最关键的环节就是对运行时

38、,从命令行给 main方法进行传递参数。我们在eclipse环境下编写代码的时候也是关键的时候。如果这两个步骤不出错其他都没有什么问题。其实个人感觉还是要编程能力要强,不然很难实现其结果。2在实验过程中 public static void mai n( Stri ng args) throws Exception这句本来是实现抛出异常。包含throws语句放在程序头部来声明抛出异常,而throw子句是放在程序体内用来显示异常的。就是告诉编译器 我这个可能要抛出异常但是我只用了 throws和throw。在程序当中使用throw子句是 人为的故意制造一个异常,从而以便其他处理器获得处理异常的机

39、会,这个也是在实验过程当中一个关键的操作。本实验的改进措施:1在实验过程如果是在 eclipse中输入和输出文件的话可能会比较方便的,因为程序在eclipse中可以检错和差错,这样的话可以更好调试好程序从而达到实验题目结果。2如果本实验题目的程序能够结合其他包的作用,比如嵌入其他包中的类的功能实现更多的接 口,从而可以优化程序。但是在命令行中实现多个包的嵌入编译会比较麻烦,所以本小组成员讨论后 一致决定不使用多个包的嵌入,但是多个包的嵌入能够使程序更加饱满和更加完善的思想还是值得改 进值得采纳的。3对实验的自我评价:小组长主要承担的工作:1在本次实验中,本人承担工作首先是跟小组成员一起讨论编写

40、程序从而实现实验目的和实验功 能。接着是利用讨论出来的正确结果编写程序,根题目要求,文件的文件名使用命令行参数传递,也 就是使用Stringagrs作为参数来传递要统计的文章的文章名参数。在程序中通过创建一个文件输入 流FileReader类获得文章中的英文内容。再创建出一个缓冲的文件输入流来获得具体的内容,再通过 程序中的具体操作,使用一个输出流文件来实现统计文件的字符数、单词数和行数。最后是将输入输 出文件的方法关闭。根据这个思想来编写出来本实验题目的程序。2做本实验还是花了蛮多时间的,可能是平时有关命令行传参的上机题目实践的比较少的原因, 所以刚开始说要通过命令行来传文件名参数的时候感觉

41、有点不知所措,但是后来经过自己反复的看课本和参考资料,最后还是实现了实验要求。还有通过本实验,使我更进一步的认识了eclipse环境和命令行环境的联系和差异,这样实现一个相对完成程序的功能对自己以后在实际编程工作也打下了一个坚实的基础。3在实验的过程当中,虽然出现了一些错误,还调试了蛮长的时间,但是这从另外一个角度来说 也是锻炼了自己调试程序的能力。使用命令行传参的过程没有调试的过程,当程序出现了问题的时候我机把程序放到eclipse环境下去调试运行。这样问题就迎刃而解了。4最后个人觉得团结就是力量,在实验的过程中,出现问题可以和组员一起讨论一起解决,问题 都不再是问题了,而且还满足了自己合作

42、带来的愉悦感。小组成员主要承担的工作:在此次试验中,我承担的工作和小组长讨论在试验过程中出现的错误,并且予以改正因为我们 两个人的编程能力不是很足。因此在网上搜索了一些资料和翻阅大量的书籍资料。组长主要负责设计思路以及具体程序中出现的调用方法的编写,在实现结果的步骤上主要我负责。我们两个人经过努力,团结合作也让我们明白 JAVA编译的强大。通过这次实验让我对java的一些编程思想有了一个比较深刻的了解,在Java学习的过程中我还存在很多的不足之处,对于类的继承和方法的实现以及类中的定义不是很了解,虽然本题使用类的继承方面用的不是很多,但是java的编程思想还是使我在编程方面有了一个比较大的提高

43、。要对Java有更深一步的探索, 就要发大力度去学习类中定义和调用继承等,通过这次实验也锻炼了自己编写程序的能力,在编写程序时要时刻考虑程序的结构等,同时在实验的过程中让我把平时上课学到了一些Java知识运用到了编程中。指导老师评语及得分:签名:毛澄映2011 年 6月25日附录(程序代码清单) :3. 实验数据 输入的代码:import java.io.*;public class Test public static void main(String args) throws Exceptionint charNumber = 0;int wordNumber = 0;int lineNu

44、mber = 0;String txt1 = args0;File file =new File(txt1);while (file.createNewFile() System. out .println( 正在创建输入的文本的文件, 请稍后输入相关内容到文 件中 );FileReader read = new FileReader(file);BufferedReader bufferread =new BufferedReader(read);while (bufferread.read()!= -1) String s = bufferread.readLine(); charNumb

45、er += s.length() ; wordNumber += s.split( ). length ;lineNumber +;String s = new String();s = 字符数 : +charNumber+ rn +单词数 : +wordNumber+rn + 行数 : +lineNumber ;String txt2 = args1;File file2 = new File(txt2);while (file2.createNewFile() System. out .println( 正在创建输出的文件! );FileWriter fwrite =new FileWri

46、ter(file2);BufferedWriter bufferwrite =new BufferedWriter(fwrite);bufferwrite.write(s);bufferwrite.close();bufferread.close();Text 里面需要我们输入的数据:The love of beauty is an essential part of all healthy human nature.It is a moral quality. The absence of it is not an assured ground of condemnation, but th

47、e presence of it is an invariable sign of goodness of heart.In proportion to the degree in which it is felt will probably be the degree in which nobleness and beauty of character will be attained.Natural beauty is an all-pervading presence.The universe is its temple.The love of beauty is an essentia

48、l part of all healthy human nature.It is a moral quality. The absence of it is not an assured ground of condemnation, but the presence of it is an invariable sign of goodness of heart.In proportion to the degree in which it is felt will probably be the degree in which nobleness and beauty of charact

49、er will be attained.Natural beauty is an all-pervading presence.The universe is its temple.The love of beauty is an essential part of all healthy human nature.It is a moral quality. The absence of it is not an assured ground of condemnation, but the presence of it is an invariable sign of goodness of heart.In proportion to the degree in which it is felt will probably be the degree in which nobleness and beauty of character will be attained.Natural beauty is an all-pervading presence.The universe is its temple.The love of beauty is an essential part of all healthy

温馨提示

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

评论

0/150

提交评论