軟體測試實驗二

來源:互聯網
上載者:User

標籤:

2,

(1)寫一個程式,用於分析一個字串中各個單詞出現的頻率,並將單詞和它出現的頻率輸出顯示。(單詞之間用空格隔開,如“Hello World My First Unit Test”);

(2)編寫單元測試進行測試;

(3)用ElcEmma查看程式碼涵蓋範圍,要求覆蓋達到100%。

            import java.io.BufferedReader;

            import java.io.FileReader;

            import java.util.ArrayList;

            import java.util.Collections;

            import java.util.Comparator;

            import java.util.List;

            import java.util.Map;

            import java.util.TreeMap;

            import java.util.regex.Matcher;

            import java.util.regex.Pattern;

 

            public class Demo {

            public static void main(String[] args) throws Exception {

 

            long time1 = System.currentTimeMillis();

            BufferedReader reader = new BufferedReader(new FileReader(

            "D:\\ZMX.txt"));

            StringBuffer buffer = new StringBuffer();

            String line = null;

            while ((line = reader.readLine()) != null) {

            buffer.append(line);

            }

            reader.close();

            Pattern expression = Pattern.compile("[a-zA-Z]+");

            String string = buffer.toString();

            Matcher matcher = expression.matcher(string);

            Map<String, Integer> map = new TreeMap<String, Integer>();

            String word = "";

            int times = 0;

            while (matcher.find()) {// 是否匹配單詞

            word = matcher.group();// 得到一個單詞-樹映射的鍵

            if (map.containsKey(word)) {// 如果包含該鍵,單詞出現過

            times = map.get(word);// 得到單詞出現的次數

            map.put(word, times + 1);

            } else {

            map.put(word, 1);// 否則單詞第一次出現,添加到映射中

            }

            }

            /*

            * 核心:如何按照TreeMap 的value排序而不是key排序.將Map.Entry放在集合裡,重寫比較子,在用

            * Collections.sort(list, comparator);進行排序

            */

            List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(

            map.entrySet());

           

            Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {

            public int compare(Map.Entry<String, Integer> left,

            Map.Entry<String, Integer> right) {

            return (left.getValue()).compareTo(right.getValue());

            }

            };

            Collections.sort(list, comparator);// 排序

            int last = list.size() - 1;

            try{

            for (int i = last; i > last-10; i--) {

            String key = list.get(i).getKey();

            Integer value = list.get(i).getValue();

            System.out.print("Top"+i+" : ");

            System.out.println(key + " " + value);

            }

            }catch(Exception e){

            //System.out.println("");

            }

            long time2 = System.currentTimeMillis();

            System.out.print("耗時:");

            System.out.println(time2 - time1+"ms");

            }

}

 

3,(1)把一個英語句子中的單詞次序顛倒後輸出。例如輸入“how are you”,輸出“you are how”;

(2)編寫單元測試進行測試;

(3)用ElcEmma查看程式碼涵蓋範圍,要求覆蓋率達到100%。

            import java.util.Scanner;

            public class NewDemo {

            public static void main(String [] a){

            Scanner in=new Scanner(System.in);

            while(true){

            String s=in.nextLine();

            if(s.equalsIgnoreCase("quit")){

            System.exit(0);

            }

            String[] ss=s.split("\\s+");

            StringBuffer sb=new StringBuffer();

            for (int i=ss.length-1;i>=0;i--){

            sb.append(ss[i]+" ");

            }

            System.out.println(sb);

            }

            }

}

 

 

軟體測試實驗二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.