202103226-1 编程作业

这个作业属于哪个课程 计科1班软工
这个作业要求在哪里 202103226-1 编程作业
这个作业的目标 学会git的一些操作
学号 20188378

码云地址

https://gitee.com/hyh153678/project-java

PSP表格

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

代码实现

1.获取文件内容

    public static String charactersCount(String filePath) throws IOException
    {

        BufferedReader bReader=null;
        StringBuilder str=null;

        try 
        {
            FileReader reader=new FileReader(filePath);
            bReader=new BufferedReader(reader);
            str=new StringBuilder();
            int flag;

            while ((flag=bReader.read())!=-1)
            {
                str.append((char)flag);
            }
        } catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        bReader.close();

        return str.toString();
    }

字数统计

 public static int charactersNumberCount(String str)
    {

        char[] ch = str.toCharArray();
        int num = 0;
        for(int i = 0; i < ch.length; i++) {
            if(ch[i] >= 0 && ch[i] <= 127) {
                num++;
            }
        }
        
        return num;
    }

单词统计

     public static int wordsNumberCount(String str)
    {

        int num=0;
        
        String[] word=str.split("\s+");
        
        String strings="^[a-zA-Z]{4,}.*";
        for(int i=0;i<word.length;i++)
        {
            if(word[i].matches(strings))
            {
                num++;
                

                String lowCase=word[i].toLowerCase();
                

                if (wordsMap.containsKey(lowCase))
                {
                    int j=wordsMap.get(lowCase);
                    wordsMap.put(lowCase,j+1);
                }
                else 
                {
                    wordsMap.put(lowCase,1);
                }
            }
        }

        return num;
    }

计算行数

public static int linesNumberCount(String filePath)
    {
    	File file=new File(filePath);
    	int count=0;
    	
    	if(file.exists()) 
    	{
    		try
    		{
    			BufferedReader in = new BufferedReader(new FileReader(file));
    			String line;
    			while((line = in.readLine()) != null)
    			{

    				if(!line.equals("") )
    				{
    					count ++;
    				}
    			}
    			in.close();
    		}
    		catch(FileNotFoundException e)
    		{
    			e.printStackTrace();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		
    	}

        return count;
    }

获取单词频率

for(Map.Entry<String,Integer>map:wordsList)
	        {
	            wordsSort+=map.getKey()+":"+map.getValue()+"
";
	            i++;
	            if(i==10)
	            {
	                break;
	            }
	        }

设计思路

把文件数据从文件中取出,获得结果后将结果拼接成字符串
文件中字符串的长度直接使用length()方法获取,在返回字符串长度。
单词数的统计采用正则表达式来分割字符串
利用bufferedreader中的readline()方法统计行数
利用map<string,intger>对象,利用sort方法和匿名内部类的比较器进行排序,最后返回一个list对象来统计单词数

运行截图

输入believe in yourself
输出

再输入数字

多输入几行英文

学习心得

这一次作业有很多以前没接触过的知识,也让我学到了git的使用。对于软件合作开发也有了新的认识

原文地址:https://www.cnblogs.com/hy14157/p/14610961.html