第二次做業 - 我的項目

第二次做業 - 我的項目

1.GitHub地址

2.PSP表格

PSP2.1 Personal Software Process Stages 預估耗時(分鐘) 實際耗時(分鐘)
Planning 計劃
· Estimate · 估計這個任務須要多少時間 15 20
Development 開發
· Analysis · 需求分析 (包括學習新技術) 60 90
· Design Spec · 生成設計文檔 20 30
· Design Review · 設計複審 30 60
· Coding Standard · 代碼規範 (爲目前的開發制定合適的規範) 5 5
· Design · 具體設計 30 30
· Coding · 具體編碼 180 350
· Code Review · 代碼複審 30 40
· Test · 測試(自我測試,修改代碼,提交修改) 30 90
Reporting 報告
· Test Repor · 測試報告 30 50
· Size Measurement · 計算工做量 20 30
· Postmortem & Process Improvement Plan · 過後總結, 並提出過程改進計劃 30 60
合計 480 855

3.運行環境

  • 語言:Java
  • IDE: IntelliJ IDEA 2018.2.3
  • 操做系統:Windows10 64bit

4.解題思路

剛拿到題目的第一反應是利用正則表達式進行詞頻統計。 經過正則表達式進行匹配,同時正則表達式匹配空白行,正則表達式匹配ASCII字符。
整個過程的思路是這樣子:java

  1. 先讀入整個文本,將內容讀進字符串,用 \p{ASCII} 進行匹配完成字符統計。
  2. 讀入文件,每次讀一行,用 \s+ 進行匹配判斷是不是空白行。
  3. 單詞統計先將內容用 \s+ 進行分割, 在用 ^[a-zA-Z]{4,}.* 進行判斷是不是單詞。存入Map中,
    若是不存在Map中就put同時設置value爲1,若是已經在Map中那麼value++。

5.設計實現過程

CalMost: 對詞頻進行排序,返回最多的前10個
CharsCount: 完成對字符個數的統計
LinesCount: 完成對行數的統計
WordsCount: 對單詞個數進行統計,同時生成Map
git

6.代碼規範

  1. 類名首字母大寫,方法與變量採用駝峯式命名法。
  2. 註釋採用 Java 文檔註釋規範
    例如:
/**
* @param map the HashMap contain words and amount
* @return the top 10 amount of the words and amount in list
*/
public List<Map.Entry<String, Integer>> mostWords(HashMap<String, Integer> map)
  1. 行寬不超過IDEA默認行寬(120)

7.性能分析

利用VisualVM進行性能分析,如下是執行100000次的結果
CPU
內存github

能夠看出主要耗時仍是載輸出到文件這裏。後續再想辦法看看有沒有優化的空間。最近時間不夠用了正則表達式

8.代碼說明

  • 對詞頻進行排序並返回前10個,若不足10個就返回當前個數。
/**
 * @param map the HashMap contain words and amount
 * @return the top 10 amount of the words and amount in list
 */
public List<Map.Entry<String, Integer>> mostWords(HashMap<String, Integer> map) {
    // convert HashMap to list
    List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
    // sort by value then by key
    list.sort(new MapComparator());
    return list.size() < 10 ? list.subList(0, list.size()) : list.subList(0, 10);
}


/**
 * This class define how to compare the element in list
 */
private class MapComparator implements Comparator<Map.Entry<String, Integer>> {
    @Override
    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
        return o1.getValue().compareTo(o2.getValue()) != 0 ? o2.getValue().compareTo(o1.getValue()) : o1.getKey().compareTo(o2.getKey());
    }
}
  • 單詞統計:轉換成小寫以後進行統計。
/**
 * @param content the input
 */
public WordsCount(String content) {

    String[] temp = content.split("\\s+");
    String countRegex = "^[a-zA-Z]{4,}.*";
    for (String i : temp) {
        if (i.matches(countRegex)) {
            sum++;
            String lowCase = i.toLowerCase();
            if (!map.containsKey(lowCase)) {
                map.put(lowCase, 1);
            } else {
                int num = map.get(lowCase);
                map.put(lowCase, num + 1);
            }
        }
    }
}

9.測試單元

測試單元覆蓋率
覆蓋率應該算還行吧,每一個方法都有覆蓋到。
LinesCount中有個catch中的沒有覆蓋到。ide

10.總結和感想

此次看了一些測試相關的東西,以前本身寫東西都沒有用過單元測試,或者就本身直接print出來,測試幾個是否跟本身的預期符合。沒有過寫單元測試的經歷。
經過此次,瞭解了單元測試的有點,當項目較大時,經過測試更能提早發現問題。
完成做業所花的時間跟本身的預期差距也是比較大的。一開始以爲本身一直都陸陸續續有在寫Android,作這個應該不會很花時間。後面才發現...Android...Java差異仍是有的,主要緣由仍是本身對Java的掌握仍是不夠深。接下來以仍是要花些時間加深對Java的掌握。
本次做業完成的質量,我的並非很滿意,由於手頭上同時還有很多代碼要寫但願趕忙寫完,好累好累,因此時間精力並無充分投入,但願下一次做業能讓本身比較滿意。性能

相關文章
相關標籤/搜索