题目集4-6的总结

前言

题目集4应该是做PTA作业以来最难的一次了,题目量不算多,只有三道题,这次题目集难在第一题,水文数据校验及处理。
这道题目主要是在考查正则表达式的应用以及对输入数据串进行拆分等,由于输入的数据量较大以及格式要求比较多,所以使用
正则表达式时非常容易出错,导致得不到分,所以这次作业得分普遍偏低,其余两道题难度一般,中规中矩,第二题考查功能的
聚合,将前几次作业中做过的功能合起来,求下一天、前一天以及两个日期之间相差的天数,第三题考查的是类的继承。
题目集5的难度比起4来降低了一些,不过题目量变多了,有五道题目,第一道送分题,找字符串中最长的一个,通过比较每
个字符串的长度即可,第二道题合并数组,之前已经做过了,这里就不再说了,第三道题是对整型数据排序,分别使用插入排序、
选择排序及冒泡排序三种算法进行排序,算是比较简单的,第四题是统计Java程序中关键词的出现次数,涉及了java针对数组的
遍历、比较、运算等,统计关键词次数以及拆分出关键词,有一定的难度,第五题跟上一次作业基本一样,也是考查功能的聚合。
题目集6总体而言是三次作业中最简单的一次,所以题目量也是最多的,前四道题主要考查正则表达式的应用,难度不大,但
是需要细心一些,不然很容易出错,同时也要熟悉正则表达式的一些基本用法,第五题是图形的继承与多态,求面积以及进行排序,
算是这几道题目中难度最大的,第六题是实现图形接口及多态性,同样是求面积,比较简单,这里就不赘述了。

设计与分析

①题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较

聚合一



import java.util.Scanner;

//Year类
class Year{
    private int value;
    //默认构造方法
    public Year(){
    }
    //带参构造方法
    public Year(int value){
        this.value=value;
    }
    //getter
    public int getValue(){
        return value;
    }
    //setter
    public void setValue(int value){
        this.value=value;
    }
    //判断year是否为闰年
    public  boolean isLeapYear(){
        if((value%4==0&&value%100!=0)||value%400==0)
            return true;
        else
            return false;
    }
    
    public  boolean isLeapYear(int value){
        if((value%4==0&&value%100!=0)||value%400==0)
            return true;
        else
            return false;
    }
    //效验数据合法性
    public boolean validate(){
        if(value<=2050&&value>=1900)
            return true;
        else
            return false;
    }
    //年份加一
    public void yearIncrement(){
        value=value+1;
    }
    //年份减一
    public void yearReduction(){
        value=value-1;
    }
    public int daysOfYear(){
    	if(this.isLeapYear()){
    		return 366;
    	}
    	else
    		return 365;
    }
    public int daysOfYear(int year){
    	if(this.isLeapYear(year)){
    		return 366;
    	}
    	else
    		return 365;
    }
}
//Month类
class Month{
    private int value;
    private Year year;
    //默认构造方法
    public Month(){
    }
    //带参构造方法
    public Month(int yearValue,int monthValue){
        this.year=new Year(yearValue);
        this.value=monthValue;
    }
    //getter
    public int getValue(){
        return value;
    }
    public Year getYear(){
        return year;
    }
    //setter
    public void setValue(int value){
        this.value=value;
    }
    public void setYear(Year year){
        this.year=year;
    }
    //日期复位(1)
    public void resetMin(){
        value=1;
    }
    //月份设置为12
    public void resetMax(){
        value=12;
    }
    //效验数据合法性
    public boolean validate(){
        if(value>=1&&value<=12)
            return true;
        else
            return false;
    }
    //月份加一
    public void monthIncrement(){
        value=value+1;
    }
    //月份减一
    public void monthReduction(){
        value=value-1;
    }
    public int daysOfMonth(){
    	int[] mon_maxnum = new int[]{31,31,28,31,30,31,30,31,31,30,31,30,31};
    	if(this.getYear().isLeapYear())
    		mon_maxnum[2]=29;
    	return mon_maxnum[this.getValue()];
    }
}
//Day类
class Day{
    private int value;
    private Month month;
    int a[]={31,31,28,31,30,31,30,31,31,30,31,30,31};
    //默认构造方法
    public Day(){
    }
    //带参构造方法
    public Day(int yearValue,int monthValue,int dayValue){
        this.month=new Month(yearValue,monthValue);
        this.value=dayValue;
    }
    //getter
    public int getValue(){
        return value;
    }
    public Month getMonth(){
        return month;
    }
    //setter
    public void setValue(int value){
        this.value=value;
    }
    public void setMonth(Month value){
        this.month=value;
    }
    //日期复位(1)
    public void resetMin(){
        value=1;
    }
    //日期设为该月最大值
    public void resetMax(){
        value=a[month.getValue()];
    }
    //效验数据合法性
    public boolean validate(){
        if(this.getMonth().getYear().isLeapYear())
            a[2]=29;
        else a[2]=28;
        if(value>=1&&value<=a[month.getValue()])
            return true;
        else
            return false;
    }
    //日期加一
    public void dayIncrement() {
        value=value+1;
    }
    //日期减一
    public void dayReduction() {
        value=value-1;
    }
}
class DateUtil{
	private Day day;
	
	DateUtil(){
		
	}
	DateUtil(int y,int m,int d){
		day=new Day(y,m,d);
	}
	
	Day getDay(){
		return day;
	}
	void setDay(Day d){
		day=d;
	}
	boolean checkInputValidity(){
		if(day.getMonth().getYear().validate()&&day.getMonth().validate()&&day.validate())
			return true;
		return false;
	}
    public boolean compareDates(DateUtil date) {
        if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())
            return false;
        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())
            return false;
        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
            return false;
        else
            return true;
    }
    //判定两个日期是否相等
    public boolean equalTwoDates(DateUtil date){
        if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
            return true;
        else
            return false;
    }
    //日期值格式化
    public String showDate(){
        return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();
    }
    DateUtil getNextNDays(int n){
    	while(n>=this.getDay().getMonth().getYear().daysOfYear()){
    		n-=this.getDay().getMonth().getYear().daysOfYear();
    		this.getDay().getMonth().getYear().yearIncrement();
    	}
    	while(n>=this.getDay().getMonth().daysOfMonth()){
    		n-=this.getDay().getMonth().daysOfMonth();
    		this.getDay().getMonth().monthIncrement();
    		if(this.getDay().getMonth().getValue()>12){
    			this.getDay().getMonth().getYear().yearIncrement();
    			this.getDay().getMonth().resetMin();
    		}
    	}
    	this.getDay().setValue(this.getDay().getValue()+n);
    	if(this.getDay().getValue()>this.getDay().getMonth().daysOfMonth()){
    		this.getDay().getMonth().monthIncrement();
    		this.getDay().setValue(this.getDay().getValue()-this.getDay().getMonth().daysOfMonth());
    	}
    	return this;
    }
    DateUtil getPreviousNDays(int n){
    	while(n>=this.getDay().getMonth().getYear().daysOfYear(this.getDay().getMonth().getYear().getValue()-1)){
    		n-=this.getDay().getMonth().getYear().daysOfYear(this.getDay().getMonth().getYear().getValue()-1);
    		this.getDay().getMonth().getYear().yearReduction();
    	}
    	while(n>=this.getDay().a[this.getDay().getMonth().getValue()-1]){
    		n-=this.getDay().a[this.getDay().getMonth().getValue()-1];
    		this.getDay().getMonth().monthReduction();
    		if(this.getDay().getMonth().getValue()<1){
    			this.getDay().getMonth().getYear().yearReduction();
    			this.getDay().getMonth().resetMax();
    		}
    	}
    	if(this.getDay().getValue()>n)
    		this.getDay().setValue(this.getDay().getValue()-n);
    	else{
    		this.getDay().getMonth().monthReduction();
    		this.getDay().setValue(this.getDay().getValue()+this.getDay().a[this.getDay().getMonth().getValue()]-n);
    		if(this.getDay().getMonth().getValue()<1){
    			this.getDay().getMonth().getYear().yearReduction();
    			this.getDay().getMonth().resetMax();
    		}
    	}
    	
    	return this;
    }
    public int getDaysofDates(DateUtil date){
        DateUtil b1=this;
        DateUtil b2=date;
        if(this.equalTwoDates(date)){//如果两天的日期相等
            return 0;
        }
        else if(!this.compareDates(date)){//如果日期大小不对
            b1=date;
            b2=this;
        }
        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int i,j,ts=0;
        for(i=b1.getDay().getMonth().getYear().getValue()+1;i<b2.getDay().getMonth().getYear().getValue();i++){//两个日期的年数之和
            ts=ts+365;
            if(new Year(i).isLeapYear())
                ts++;
        }
        if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()==b2.getDay().getMonth().getValue()){//年份相同,月份相同,日不同
            ts=b2.getDay().getValue()-b1.getDay().getValue();
        }
        else if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()!=b2.getDay().getMonth().getValue()){//年份相同,月份不同
            if(b1.getDay().getMonth().getYear().isLeapYear())//是闰年
                a[2]=29;
            ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();//小日期该月剩余的天数
            ts=ts+b2.getDay().getValue();//大日期的天数
            for(j=b1.getDay().getMonth().getValue()+1;j<=b2.getDay().getMonth().getValue()-1;j++)//月份天数和
                ts+=a[j];
        }
        else if(b1.getDay().getMonth().getYear().getValue()!=b2.getDay().getMonth().getYear().getValue()){//年份不同
            ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数
            ts=ts+b2.getDay().getValue();//大日期在该月已经过的天数
            for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++)//小日期在该年剩余的天数
                ts=ts+a[j];
            for(j=b2.getDay().getMonth().getValue()-1;j>0;j--)//大日期在该年已经过的天数
                ts=ts+a[j];
            if(b1.getDay().getMonth().getYear().isLeapYear()&&b1.getDay().getMonth().getValue()<=2)//如果小日期该年为闰年且该天在1月或2月
                ts++;
            if(b2.getDay().getMonth().getYear().isLeapYear()&&b2.getDay().getMonth().getValue()>2)//如果大日期该年为闰年且该天在1月或2月后
                ts++;
        }
        return ts;
    }


    
}
//主类
public class Main {
    public static void main(String[] args) {
        Scanner x=new Scanner(System.in);
        int year=0,month=0,day=0,a,b;
        a=x.nextInt();//输入判断类型
        year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日
        DateUtil c=new DateUtil(year,month,day);
        if(a==1){//求下n天
            b=x.nextInt();//输入n
            if(!c.checkInputValidity()||b<0){//如果数据不合法
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else
                System.out.println(c.getNextNDays(b).showDate());
        }
        else if(a==2){
            b=x.nextInt();//输入n
            if(!c.checkInputValidity()||b<0){//如果数据不合法
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else
                System.out.println(c.getPreviousNDays(b).showDate());
        }
        else if(a==3){
            int y1,m1,d1;
            y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日
            DateUtil d=new DateUtil(y1,m1,d1);
            if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else
                System.out.println(c.getDaysofDates(d));
        }
        else
            System.out.println("Wrong Format");
        
        x.close();
    }
}


Metrics Details For File 'Main.java'

Parameter Value
========= =====
Project Directory C:\Users\14768\workspace\Contacts\src\PTAwork/
File Name Main.java
Lines 357
Statements 210
Percent Branch Statements 25.2
Method Call Statements 211
Percent Lines with Comments 15.1
Classes and Interfaces 2
Methods per Class 17.50
Average Statements per Method 5.86
Line Number of Most Complex Method 253
Name of Most Complex Method DateUtil.getDaysofDates()
Maximum Complexity 17
Line Number of Deepest Block 278
Maximum Block Depth 5
Average Block Depth 1.73
Average Complexity 5.33


Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls

DateUtil.checkInputValidity() 4, 3, 2, 6
DateUtil.compareDates() 8, 8, 2, 40
DateUtil.DateUtil() 1, 1, 2, 0
DateUtil.DateUtil() 1, 0, 0, 0
DateUtil.equalTwoDates() 5, 4, 2, 18
DateUtil.getDay() 1, 1, 2, 0
DateUtil.getDaysofDates() 17, 25, 5, 74
DateUtil.getNextNDays() 5, 14, 4, 22
DateUtil.getPreviousNDays() 6, 17, 4, 29
DateUtil.setDay() 1, 1, 2, 0
DateUtil.showDate() 1, 1, 2, 1
Main.main() 14, 30, 5, 10


Block Depth Statements

0 57
1 45
2 41
3 36
4 26
5 5
6 0
7 0
8 0
9+ 0

类图

聚合二


import java.util.Scanner;

//Year类
class Year{
    private int value;
    //默认构造方法
    public Year(){
    }
    //带参构造方法
    public Year(int value){
        this.value=value;
    }
    //getter
    public int getValue(){
        return value;
    }
    //setter
    public void setValue(int value){
        this.value=value;
    }
    //判断year是否为闰年
    public  boolean isLeapYear(){
        if((value%4==0&&value%100!=0)||value%400==0)
            return true;
        else
            return false;
    }
    public  boolean isLeapYear(int value){
        if((value%4==0&&value%100!=0)||value%400==0)
            return true;
        else
            return false;
    }
   
    //效验数据合法性
    public boolean validate(){
        if(value<=2020&&value>=1820)
            return true;
        else
            return false;
    }
    //年份加一
    public void yearIncrement(){
        value=value+1;
    }
    //年份减一
    public void yearReduction(){
        value=value-1;
    }
    public int daysOfYear(){
    	if(this.isLeapYear()){
    		return 366;
    	}
    	else
    		return 365;
    }
    public int daysOfYear(int year){
    	if(this.isLeapYear(year)){
    		return 366;
    	}
    	else
    		return 365;
    }
    
}
//Month类
class Month{
    private int value;
    //默认构造方法
    public Month(){
    }
    //带参构造方法
    public Month(int monthValue){
        this.value=monthValue;
    }
    //getter
    public int getValue(){
        return value;
    }

    //setter
    public void setValue(int value){
        this.value=value;
    }
    //日期复位(1)
    public void resetMin(){
        value=1;
    }
    //月份设置为12
    public void resetMax(){
        value=12;
    }
    //效验数据合法性
    public boolean validate(){
        if(value>=1&&value<=12)
            return true;
        else
            return false;
    }
    //月份加一
    public void monthIncrement(){
        value=value+1;
    }
    //月份减一
    public void monthReduction(){
        value=value-1;
    }
    
}
//Day类
class Day{
    private int value;
    //默认构造方法
    public Day(){
    }
    //带参构造方法
    public Day(int dayValue){
        this.value=dayValue;
    }
    //getter
    public int getValue(){
        return value;
    }
    //setter
    public void setValue(int value){
        this.value=value;
    }

    public void dayIncrement() {
        value=value+1;
    }
    //日期减一
    public void dayReduction() {
        value=value-1;
    }
}
class DateUtil{
	private Year year;
	private Month month;
	private Day day;
	int[] mon_maxnum={31,31,28,31,30,31,30,31,31,30,31,30,31};
	DateUtil(){
		
	}
	DateUtil(int y,int m,int d){
		year=new Year(y);
		month=new Month(m);
		day=new Day(d);
	}
	
	Day getDay(){
		return day;
	}
	public Month getMonth() {
		return month;
	}
	public Year getYear() {
		return year;
	}
	
	void setDay(Day d){
		day=d;
	}
	public void setMonth(Month month) {
		this.month = month;
	}
	public void setYear(Year year) {
		this.year = year;
	}
	
	boolean checkInputValidity(){
		if(year.isLeapYear())
			mon_maxnum[2]=29;
		else
			mon_maxnum[2]=28;
		
		if(day.getValue()>=1&&day.getValue()<=mon_maxnum[month.getValue()]){
			if(year.validate()&&month.validate())
				return true;
		}
		return false;
	}
	
    public boolean compareDates(DateUtil date) {
        if(date.getYear().getValue()<this.getYear().getValue())
            return false;
        else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()<this.getMonth().getValue())
            return false;
        else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()==this.getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
            return false;
        else
            return true;
    }
    //判定两个日期是否相等
    public boolean equalTwoDates(DateUtil date){
        if(this.getDay().getValue()==date.getDay().getValue()&&this.getMonth().getValue()==date.getMonth().getValue()&& this.getYear().getValue()==date.getYear().getValue())
            return true;
        else
            return false;
    }
    //日期值格式化
    public void showDate(int year,int month,int day,int choice,int n){
    	switch(choice){
    	case 1:
    		System.out.println(year+"-"+month+"-"+day+" next "+n+" days is:"+this.year.getValue()+"-"+this.month.getValue()+"-"+this.day.getValue());
    		break;
    	case 2:
    		System.out.println(year+"-"+month+"-"+day+" previous "+n+" days is:"+this.year.getValue()+"-"+this.month.getValue()+"-"+this.day.getValue());
    		break;
    		
    	}
    	
    		
    }
    
    DateUtil getNextNDays(int n){
    	//if(n>=655535)
    		//n++;
    	int[] mon_maxnum={0,31,28,31,30,31,30,31,31,30,31,30,31};
    	while(n>=year.daysOfYear()){
    		n-=year.daysOfYear();
    		year.yearIncrement();
    	}
    	if(year.isLeapYear())
    		mon_maxnum[2]=29;
    	else
    		mon_maxnum[2]=28;
    	if(year.isLeapYear()&&month.getValue()>2)
    	{
    		this.getDay().setValue(this.getDay().getValue()-1);
    		if(day.getValue()<1){
    			month.monthReduction();
    			day.setValue(mon_maxnum[month.getValue()]);
    			if(month.getValue()<1){
    				year.yearReduction();
    				month.resetMax();
    			}	
    				
    		}
    			
    	}
    	if(year.isLeapYear())
    		mon_maxnum[2]=29;
    	else
    		mon_maxnum[2]=28;	
    	while(n>=mon_maxnum[month.getValue()]){
    		n-=mon_maxnum[month.getValue()];
    		month.monthIncrement();
    		if(month.getValue()>12){
    			year.yearIncrement();
    			month.resetMin();
    		}
    	}
    	if(year.isLeapYear())
    		mon_maxnum[2]=29;
    	else
    		mon_maxnum[2]=28;
    	this.getDay().setValue(this.getDay().getValue()+n);
    	if(day.getValue()>mon_maxnum[month.getValue()]){
    		this.getDay().setValue(this.getDay().getValue()-mon_maxnum[month.getValue()]);
    		month.monthIncrement();
    	}
    	return this;
    }
    
    DateUtil getPreviousNDays(int n){
    	while(n>=year.daysOfYear(year.getValue()-1)){
    		n-=year.daysOfYear(year.getValue()-1);
    		year.yearReduction();;
    	}
    	while(n>=mon_maxnum[month.getValue()-1]){
    		n-=mon_maxnum[month.getValue()-1];
    		month.monthReduction();
    		if(month.getValue()<1){
    			year.yearReduction();;
    			month.resetMax();;
    		}
    	}
    	if(this.getDay().getValue()>n)
    		this.getDay().setValue(this.getDay().getValue()-n);
    	else{
    		month.monthReduction();
    		this.getDay().setValue(this.getDay().getValue()+mon_maxnum[month.getValue()]-n);
    		if(month.getValue()<1){
    			year.yearReduction();;
    			month.resetMax();;
    		}
    	}
    	
    	return this;
    }
    public int getDaysofDates(DateUtil date){
        DateUtil b1=this;
        DateUtil b2=date;
        if(this.equalTwoDates(date)){//如果两天的日期相等
            return 0;
        }
        else if(!this.compareDates(date)){//如果日期大小不对
            b1=date;
            b2=this;
        }
        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        int i,j,ts=0;
        for(i=b1.year.getValue()+1;i<b2.year.getValue();i++){//两个日期的年数之和
            ts=ts+365;
            if(new Year(i).isLeapYear())
                ts++;
        }
        if(b1.year.getValue()==b2.year.getValue()&&b1.month.getValue()==b2.month.getValue()){//年份相同,月份相同,日不同
            ts=b2.getDay().getValue()-b1.getDay().getValue();
        }
        else if(b1.year.getValue()==b2.year.getValue()&&b1.month.getValue()!=b2.month.getValue()){//年份相同,月份不同
            if(b1.year.isLeapYear())//是闰年
                a[2]=29;
            ts=ts+a[b1.month.getValue()]-b1.getDay().getValue();//小日期该月剩余的天数
            ts=ts+b2.getDay().getValue();//大日期的天数
            for(j=b1.month.getValue()+1;j<=b2.month.getValue()-1;j++)//月份天数和
                ts+=a[j];
        }
        else if(b1.year.getValue()!=b2.year.getValue()){//年份不同
            ts=ts+a[b1.month.getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数
            ts=ts+b2.getDay().getValue();//大日期在该月已经过的天数
            for(j=b1.month.getValue()+1;j<=12;j++)//小日期在该年剩余的天数
                ts=ts+a[j];
            for(j=b2.month.getValue()-1;j>0;j--)//大日期在该年已经过的天数
                ts=ts+a[j];
            if(b1.year.isLeapYear()&&b1.month.getValue()<=2)//如果小日期该年为闰年且该天在1月或2月
                ts++;
            if(b2.year.isLeapYear()&&b2.month.getValue()>2)//如果大日期该年为闰年且该天在1月或2月后
                ts++;
        }
        return ts;
    }


    
}
//主类
public class Main {
    public static void main(String[] args) {
        Scanner x=new Scanner(System.in);
        int year=0,month=0,day=0,a,b;
        a=x.nextInt();//输入判断类型
        year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日
        DateUtil c=new DateUtil(year,month,day);
        if(a==1){//求下n天
            b=x.nextInt();//输入n
            if(!c.checkInputValidity()||b<0){//如果数据不合法
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else
                c.getNextNDays(b).showDate(year,month,day,a,b);
        }
        else if(a==2){
            b=x.nextInt();//输入n
            if(!c.checkInputValidity()||b<0){//如果数据不合法
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else
                c.getPreviousNDays(b).showDate(year,month,day,a,b);
        }
        else if(a==3){
            int y1,m1,d1;
            y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日
            DateUtil d=new DateUtil(y1,m1,d1);
            if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else
                System.out.println("The days between "+year+"-"+month+"-"+day+" and "+y1+"-"+m1+"-"+d1+" are:"+c.getDaysofDates(d));
        }
        else
            System.out.println("Wrong Format");
        
        x.close();
    }
}


Metrics Details For File 'Main.java'

Parameter Value
========= =====
Project Directory C:\Users\14768\workspace\Contacts\src\PTAwork/
File Name Main.java
Lines 383
Statements 230
Percent Branch Statements 28.3
Method Call Statements 122
Percent Lines with Comments 13.6
Classes and Interfaces 2
Methods per Class 16.50
Average Statements per Method 6.85
Line Number of Most Complex Method 281
Name of Most Complex Method DateUtil.getDaysofDates()
Maximum Complexity 17
Line Number of Deepest Block 224
Maximum Block Depth 5
Average Block Depth 2.10
Average Complexity 5.25


Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls

DateUtil.checkInputValidity() 7, 8, 3, 6
DateUtil.compareDates() 8, 8, 2, 24
DateUtil.DateUtil() 1, 3, 2, 0
DateUtil.DateUtil() 1, 0, 0, 0
DateUtil.equalTwoDates() 5, 4, 2, 12
DateUtil.getDay() 1, 1, 2, 0
DateUtil.getDaysofDates() 17, 25, 5, 24
DateUtil.getMonth() 1, 1, 2, 0
DateUtil.getNextNDays() 15, 35, 5, 24
DateUtil.getPreviousNDays() 6, 22, 4, 16
DateUtil.getYear() 1, 1, 2, 0
DateUtil.setDay() 1, 1, 2, 0
DateUtil.setMonth() 1, 1, 2, 0
DateUtil.setYear() 1, 1, 2, 0
DateUtil.showDate() 4, 7, 4, 2
Main.main() 14, 30, 5, 10


Block Depth Statements

0 36
1 43
2 63
3 44
4 37
5 7
6 0
7 0
8 0
9+ 0

类图

通过比较,第二种聚合设计更为简单,复杂程度更低。聚合关系的两个类处于不同的层次,一个是整体,一个是部分,同时,是一种弱的“拥有”关系。
表现为Dateutil类包含有其他类的全局对象,但是其他对象可以不在Dateutil创建的时刻创建。
聚合二较聚合一,更加精炼,删除了冗余的代码,使代码更加漂亮并且有逻辑!

②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

7-3



import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		ArrayList<Shape> shape=new ArrayList<>();
		
		int count1,count2,count3;
		count1=input.nextInt();
		count2=input.nextInt();
		count3=input.nextInt();
		if(count1<0||count2<0||count3<0) {
			System.out.println("Wrong Format");
			System.exit(0);
		}
			
		
		for(int i=0;i<count1;i++) {
			double radius;
			radius=input.nextDouble();
			shape.add(new Circle(radius));
		}
		
		for(int j=0;j<count2;j++) {
			double width,length;
			width=input.nextDouble();
			length=input.nextDouble();
			shape.add(new Rectangle(width,length));
		}
		
		for(int k=0;k<count3;k++) {
			double[] side=new double[3];
			double side1,side2,side3;
			side[0]=input.nextDouble();
			side[1]=input.nextDouble();
			side[2]=input.nextDouble();
			Arrays.sort(side);
			side1=side[0];
			side2=side[1];
			side3=side[2];
			shape.add(new Triangle(side1,side2,side3));
		}
		sortAndSum.checkValidity(shape);
		
		System.out.println("Original area:");
		for(int n=0;n<shape.size();n++) {
			System.out.print(shape.get(n).toString()+" ");
		}
		System.out.print("\n");
		
		System.out.print("Sum of area:"+sortAndSum.sum(shape)+"\n");
		
		System.out.println("Sorted area:");
		
		for(int n=0;n<sortAndSum.sort(shape).size();n++) {
			System.out.print(sortAndSum.sort(shape).get(n).toString()+" ");
		}
		System.out.print("\n");
		
		System.out.print("Sum of area:"+sortAndSum.sum(sortAndSum.sort(shape))+"\n");
		
		
	
	
	}
}

abstract class Shape{
	abstract double getArea();
	abstract boolean validate();
	
	public String toString(){
		return String.format("%.2f", getArea());
	}
	
}

class Circle extends Shape{
	double radius;
	
	public Circle() {
		// TODO 自动生成的构造函数存根
	}
	public Circle(double radius){
		this.radius=radius;
	}
	
	double getArea(){
		return Math.PI*Math.pow(radius, 2);
	}
	boolean validate(){
		if(radius<=0)
			return false;
		else
			return true;
					
	}
	
}

class Rectangle extends Shape{
	double width,length;
	public Rectangle() {
		// TODO 自动生成的构造函数存根
	}
	public Rectangle(double width,double length){
		this.length=length;
		this.width=width;
	}
	
	double getArea(){
		return width*length;
	}
	
	boolean validate(){
		if(width<0||length<0){
			return false;
		}
		else
			return true;
	}
}

class Triangle extends Shape{
	double side1,side2,side3;
	
	public Triangle() {
		// TODO 自动生成的构造函数存根
	}
	public Triangle(double side1,double side2,double side3) {
		this.side1=side1;
		this.side2=side2;
		this.side3=side3;
	}
	
	double getArea() {
		double s=(side1+side2+side3)/2;
		return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
		
	}
	boolean validate() {
		if(side1<0||side2<0||side3<0)
			return false;
		else {
			if(side1+side2<=side3)
				return false;
			return true;
		}
	}
}

class sortAndSum{
	public static ArrayList<Shape> sort(ArrayList<Shape> w){
        int i=0,j=0;
        Shape k;
            for (i = 0; i < w.size(); i++) {
                for (j = 0; j < w.size() - i - 1; j++) {
                    if (w.get(j).getArea() >= w.get(j + 1).getArea()) {
                        k = w.get(j);
                        w.set(j, w.get(j + 1));
                        w.set(j + 1, k);
                    }
                }
            }
            return w;
    }
	
	public static String sum(ArrayList<Shape> shape) {
		double sum=0;
		
		for(int i=0;i<shape.size();i++){
			sum+=shape.get(i).getArea();
		}
		
		return String.format("%.2f", sum);
	}
	
	public static void checkValidity(ArrayList<Shape> shape) {
		if(shape.isEmpty())
			return;
		if(shape.get(0).validate()&&shape.get(1).validate()&&shape.get(2).validate())
			return;
		else {
			System.out.println("Wrong Format");
			System.exit(0);
		}
	}
}

Metrics Details For File 'Main.java'

Parameter Value
========= =====
Project Directory C:\Users\14768\workspace\Contacts\src\PTAwork/
File Name Main.java
Lines 94
Statements 54
Percent Branch Statements 9.3
Method Call Statements 5
Percent Lines with Comments 2.1
Classes and Interfaces 4
Methods per Class 7.75
Average Statements per Method 1.35
Line Number of Most Complex Method 21
Name of Most Complex Method Rectangle.System.out.printf()
Maximum Complexity 4
Line Number of Deepest Block 87
Maximum Block Depth 5
Average Block Depth 2.46
Average Complexity 1.80


Most Complex Methods in 4 Class(es): Complexity, Statements, Max Depth, Calls

Circle.System.out.printf() 3, 4, 3, 0
Main.main() 3, 11, 3, 5
Rectangle.System.out.printf() 4, 4, 5, 0
System.out.printf().System.out.printf() 4, 4, 5, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 2, 4, 0
System.out.printf().System.out.printf() 3, 4, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0


Block Depth Statements

0 5
1 5
2 16
3 17
4 10
5 1
6 0
7 0
8 0
9+ 0

类图

继承二




import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		double radius,width,length;
		radius=in.nextDouble();
		width=in.nextDouble();
		length=in.nextDouble();
		
		Circle circle = new Circle(radius);
		Rectangle rectangle = new Rectangle(width, length);
		if(!circle.validate()||!rectangle.validate()) {
			System.out.println("Wrong Format");
			System.exit(0);
		}
		
		System.out.printf("%.2f\n%.2f",circle.getArea(),rectangle.getArea());
		
	}
}

interface GetArea{
	abstract double getArea();
}

class Circle implements GetArea{
	private double radius;
	
	public Circle() {
		// TODO 自动生成的构造函数存根
	}
	public Circle(double radius){
		this.radius=radius;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getArea(){
		return Math.PI*Math.pow(radius, 2);
	}
	boolean validate(){
		if(radius<=0)
			return false;
		else
			return true;
					
	}
	
}

class Rectangle implements GetArea{
	private double width,length;
	public Rectangle() {
		// TODO 自动生成的构造函数存根
	}
	public Rectangle(double width,double length){
		this.length=length;
		this.width=width;
	}
	public double getLength() {
		return length;
	}
	
	public double getWidth() {
		return width;
	}
	
	public void setLength(double length) {
		this.length = length;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	
	public double getArea(){
		return width*length;
	}
	
	boolean validate(){
		if(width<=0||length<=0){
			return false;
		}
		else
			return true;
	}
}

Metrics Details For File 'Main.java'

Parameter Value
========= =====
Project Directory C:\Users\14768\workspace\Contacts\src\PTAwork/
File Name Main.java
Lines 94
Statements 53
Percent Branch Statements 9.4
Method Call Statements 5
Percent Lines with Comments 2.1
Classes and Interfaces 4
Methods per Class 7.75
Average Statements per Method 1.35
Line Number of Most Complex Method 21
Name of Most Complex Method Rectangle.System.out.printf()
Maximum Complexity 4
Line Number of Deepest Block 87
Maximum Block Depth 5
Average Block Depth 2.51
Average Complexity 1.80


Most Complex Methods in 4 Class(es): Complexity, Statements, Max Depth, Calls

Circle.System.out.printf() 3, 4, 3, 0
Main.main() 3, 11, 3, 5
Rectangle.System.out.printf() 4, 4, 5, 0
System.out.printf().System.out.printf() 4, 4, 5, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 1, 4, 0
System.out.printf().System.out.printf() 1, 2, 4, 0
System.out.printf().System.out.printf() 3, 4, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0
System.out.printf().System.out.printf() 1, 1, 3, 0


Block Depth Statements

0 4
1 5
2 16
3 17
4 10
5 1
6 0
7 0
8 0
9+ 0

类图

题目集4(7-3)是图形继承,每个类都有构造方法,这道题目思路很简单,题目已经给出来了,按照题目要求创建所要求的几个类,然后
继承各自的类,再创建各自的构造方法,然后按照题目要求设置属性,创建getter以及setter方法传送数据,再创建不同图形类求面积的方
法即可,这道题还是比较容易的
题目集6(7-5)是图形继承与多态,相比上一道题目它多了多态, 多态可以用“三个定义和两个方法”来总结。三个定义
分别是父类定义子类构 建、接口定义实现类构建和抽象类定义实体类构建,而两个方法分别是方法重载和方法重写。本
次作 业采用的是抽象类定义,实体类构建的方式。这道题有一定的难度,对于继承和多态掌握不好的话做起来有一定的
难度,但也是一道很好的训练题,本题定义Shape为抽象类,其他图形类为实体类。
题目集6(7-6) 是实现图形接口及多态性,这道题比较简单,与第一道差不多,根据题目要求创建接口以及图形类
即可

③对三次题目集中用到的正则表达式技术的分析总结

这三次作业用到的正则表达式比较多,主要是对数字和字母进行匹配,其中水文数据校验及处理这道题中正则表达式使用的
难度较大,这道题我只得了两分,所以也不知道该总结啥,不会,主要是对输入得字符串拆分并且进行匹配,并且在输入值
不同时有不同的匹配规则,所以难度较大,其他题目比较简单,就是对输入得账号、验证码以及学号进行匹配,正则表达式
的运用可以参考百度,难度不大,但是使用起来细节比较多,做题的时候需要仔细一些。

④题目集5(7-4)中Java集合框架应用的分析总结

这道题目使用了ArrayList,ArrayList是List的一个实现类,可以实现数组大小的可变,可以很方便的进行增加和删减数组内
元素的操作。List list = new ArrayList()这种形式成为向上转型,ArrayList实现了List接口,可以看成是从List继承而来,一
个子类的对象可以指向它父类。ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。ArrayList 实现java.io.Ser
ializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。通过使用ArrayList可以很方便的对组内元素进行处理。
从而实现统计和筛选。类图如下

踩坑心得

对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
对于题目集4,只得了一半的分,水文数据校验及处理这道题不会做,说明自身水平还不太行,还需要好好学习,再接再厉,
对于正则表达式的运用还很生涩,日期问题的处理当中对于润年以及非润年、平月和大小月的处理一开始出现了一些问题,测
试点没过去,后面发现对一些日期之间的差值有几天的误差,原因是特殊日期没判断好,改正后通过了,图形的继承挺简单。
对于题目集5,这次难度还是可以的,没上次那么大,前几道题主要是排序问题,对于几种排序的方法有些陌生了,通过百度
之后记起来了,难度不大,日期这道题与上面的差不多,这里就不再赘述了,难度较其他题大些的是第四题,统计Java程序中
关键词的出现次数,这道题使用到了ArrayList,通过这道题让我学习到了如何使用,还是非常有帮助的

改进

有些题目的代码写的比较复杂,还不够精炼,可读性不好,虽然说功能都实现了,但还是需要很多改进的,比如几道图形处理的
问题,要求用不同的方法写,接口啊抽象类这些,对于多态的运用还是有些模糊,该创建些什么类,定义什么方法如何调用等方
面还有许多问题

总结

对本阶段三次题目集的综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。
通过这几次作业,我学会了java的多态、继承和接口的概念,学会了正则表达式的常用方法。让我进一步学会了多态的使用方法,当然,印象最深的还是正则表达式的使用,这是一大难度,需要不
断练习,正则表达式的使用很广,很多项目中都需要使用,一定要仔细,不然很容易出错,老师上课讲的很清楚,但是毕竟上课
时间有限,很多东西需要我们去自学,

posted @ 2021-11-13 19:33  beuihe  阅读(49)  评论(0)    收藏  举报