zoukankan      html  css  js  c++  java
  • 个人项目

    WordCount(Java实现)

    Github:https://github.com/Naren-Github66/WordCount

    项目相关要求

    1. 题目描述

    Word Count

    1. 实现一个简单而完整的软件工具(源程序特征统计程序)。
    2. 进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。
    3. 进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。

    2. 功能列表

      1. 基本功能列表:
        wc.exe -c file.c  //返回文件 file.c 的字符数
        wc.exe -w file.c //返回文件 file.c 的词的数目
        wc.exe -l file.c   //返回文件 file.c 的行数

    遇到的困难及解决方法

    • 困难描述:一开始不清楚如何用java生成exe可执行文件,后来经查阅学会。
    • 做过哪些尝试:用main方法进行命令的参数获取,后来修改掉了。
    • 是否解决:是。
    • 有何收获:对做项目的分模块化更加熟练。

    关键代码or设计说明

     1 /**
     2  * 模块:获取文件词数
     3  */
     4 public class GetWord {
     5     ReadFile rf = new ReadFile();
     6     public void getWordNum(String path,String fileName) throws IOException {
     7         String content = rf.readFile(path,fileName);
     8         if (content != null) {
     9             //将全部非字母字符替换为空格,便于分割统计
    10             String temp = content.replaceAll("[^a-zA-Z]", " ");
    11             String[] words = temp.split(" +");
    12             if (words.length == 1 && words[0].equals("")) {
    13                 System.out.println("词数:0");
    14             }
    15             System.out.printf("词数:%d
    
    ",words.length - 1);
    16         }
    17     }
    18 }
     1 /**
     2  * 模块:获取文件行数
     3  */
     4 public class GetLine {
     5     ReadFile rf = new ReadFile();
     6     public void getLineNum(String path,String fileName) throws IOException {
     7         String content = rf.readFile(path,fileName);
     8         byte[] lines = content.getBytes(); //以字节数组存储
     9         int count = 0;
    10         for (int i = 0; i < lines.length; i++) {
    11             if (lines[i] == '
    ')
    12                 count++;
    13         }
    14         System.out.printf("行数:%d
    
    ",++count);
    15     }
    16 }
     1 /**
     2  * 模块:获取文件字符数(包括行内空格)
     3  */
     4 public class GetChar {
     5     ReadFile rf = new ReadFile();
     6     public void getCharNum(String path,String fileName) throws IOException {
     7             String content = rf.readFile(path,fileName);
     8             byte[] bytes = content.getBytes();
     9             int count = 0;
    10         for (int i = 0; i < bytes.length; i++) {
    11             if(bytes[i] != 10 && bytes[i] != 13) //去掉回车和换行符
    12                 count++;
    13         }
    14         System.out.printf("字符数:%d
    
    ",count);
    15     }
    16 }
     1 /**
     2  * 模块:从文件中读取内容,以字符串形式返回
     3  */
     4 public class ReadFile {
     5     public String readFile(String path,String fileName) throws IOException {
     6 
     7         try{
     8             //创建读入流
     9             File file = new File(path+fileName);
    10             FileReader fileReader = new FileReader(file);
    11 
    12             //读取文件内容
    13             String content = "";
    14             int b = 0;
    15             while((b = fileReader.read()) != -1){
    16                 content += (char)b;
    17             }
    18             //关闭流
    19             if(fileReader != null) fileReader.close();
    20 
    21             //返回文件内容
    22             return content;
    23         }catch(FileNotFoundException e){
    24             System.out.println("File cannot be found.");
    25             return null;
    26         }
    27     }
    28 }
     1 /**
     2  * 测试类
     3  */
     4 public class Test {
     5     public static void main(String[] args) throws IOException {
     6         Scanner sc = new Scanner(System.in);
     7         GetChar gc = new GetChar();
     8         GetLine gl = new GetLine();
     9         GetWord gw = new GetWord();
    10 
    11         System.out.println("请输入待查询文件所属目录:");
    12         String path = sc.next();
    13         int count = 0;
    14         while(true) {
    15             System.out.println("输入命令(按“Q”退出):");
    16             if(count == 0) sc.nextLine();
    17             String input = sc.nextLine();
    18             if(input.equalsIgnoreCase("Q"))
    19                 break;
    20             String[] lines = input.split(" ");  //分割指令
    21             if (!lines[0].equals("wc.exe") || !lines[1].equals("-w") && !lines[1].equals("-c") && !lines[1].equals("-l"))
    22                 System.out.println("无效命令!");
    23             else {
    24                 if (lines[1].equals("-w")) //统计词数
    25                     gw.getWordNum(path,lines[2]);
    26                 if (lines[1].equals("-c")) //统计行数
    27                     gc.getCharNum(path,lines[2]);
    28                 if (lines[1].equals("-l")) //统计字符数
    29                     gl.getLineNum(path,lines[2]);
    30             }
    31             count ++;
    32         }
    33     }
    34 }

    测试结果:

    测试文件:

     

    正常运行测试:

     错误处理测试:

     

    PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
    Planning 计划  20  15
    · Estimate · 估计这个任务需要多少时间  180  240
    Development 开发  282  355
    · Analysis · 需求分析 (包括学习新技术)  120  180
    · Design Spec · 生成设计文档  20  20
    · Design Review · 设计复审 (和同事审核设计文档)  15  15
    · Coding Standard · 代码规范 (为目前的开发制定合适的规范)  15  10
    · Design · 具体设计  35  40
    · Coding · 具体编码  40  45
    · Code Review · 代码复审  15  15
    · Test · 测试(自我测试,修改代码,提交修改)  20  30
    Reporting 报告  45  55
    · Test Report · 测试报告  20  25
    · Size Measurement · 计算工作量  10  15
    · Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划  15  15
      合计  425  435

    总结

           本次没有实现扩展功能,仅仅完成了基础功能部分,还有很大的拓展空间。整体来说,对于项目的模块的架构更加清晰,把按最小功能划分为相应类,同时使用了伪mvc分层的思想。学会了将jar包打成exe格式,看了优秀的同学的作品很受触动,接下来希望学习javafx表格的使用。

  • 相关阅读:
    【UVA116】 单向TSP Unidirectional TSP [动态规划]
    【luogu4408】 [NOI2003]逃学的小孩 [动态规划 树的直径]
    【POJ2631】树的直径 [动态规划 树形dp]
    【luogu 1156】 垃圾陷阱 [动态规划 背包]
    【luogu1472】 奶牛家谱 Cow Pedigrees [动态规划]
    【luogu2747】 [USACO5.4]周游加拿大Canada Tour[动态规划]
    【luogu2737】 [USACO4.1]麦香牛块Beef McNuggets [动态规划 完全背包][数学 扩展欧几里德]
    【luogu3856】【TJOI2008】公共子串 [动态规划]
    【luogu1020】 导弹拦截 [动态规划LIS]
    【luogu1439】 【模板】最长公共子序列 [动态规划][LIS最长上升子序列][离散化]
  • 原文地址:https://www.cnblogs.com/narenblogs/p/12563206.html
Copyright © 2011-2022 走看看