Java第二次Blog

前言:

第四次作业难度较大,题量大,三道题,难度依次递减,知识点涉及了复杂的正则表达式,聚合,继承。第五次作业难度中等,题量大,共5道题,难度依次递增,知识点涉及了字符串的处理,合并两个有序数组,三种排序,正则表达式,升级版聚合。第六次作业难度简单,题量中等,共6道题,难度依次递增,知识点涉及了简单的正则表达式,继承和多态,接口和多态。可以看出题目一次次深入,并有对重要知识点的训练。

题目集4(7-2)源码:

import java.util.Scanner;

class DateUtil {
    Day day;

    public DateUtil(){

    }
    public DateUtil(int d, int m, int y){
        this.day = new Day(d,m,y);
    }
    public Day getDay() {
        return day;
    }
    public void setDay(Day d) {
        this.day = d;
    }

    public boolean checkInputValidity(){
        if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate())
            return true;
        return false;
    }
    
public boolean compare1(DateUtil date){
    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 true;
    else
        return false;

}
    public boolean equal(DateUtil date){
        if(equalMonth(date)){
            return false;
        }
        else
            return true;
    }
    public boolean compareDates(DateUtil date){
        if (compare1(date))
            return false;
        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;



        return true;
    }
    public boolean equalDay(DateUtil date){
        if (this.getDay().getValue()==date.getDay().getValue())
            return true;
        else
            return false;
    }
    public boolean equalMonth(DateUtil date){
        if(this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue())
   return true;
else
    return false;
    }
    public boolean equalYear(DateUtil date){
       if( this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
    return true;
       else
           return false;
    }
    public boolean equalTwoDates(DateUtil date){
        if (equalDay(date) &&equalYear(date)&& equalMonth(date))
            return true;
else
        return false;
    }
public boolean equals(DateUtil date){
        if(equalDay(date)){
            return false;
        }
        else
            return true;
}
    public String showDate(){
        return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();

    }
    public int deom(DateUtil m) {
        int n = 0;

        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
        for (int i = m.getDay().getMonth().getValue()+1; i <=12; i++) {
            n=n+mon_maxnum[i-1];
        }
        n=n+mon_maxnum[m.getDay().getMonth().getValue()-1]-m.getDay().getValue();
        if(m.getDay().getMonth().getYear().isLeapYear()&&m.getDay().getMonth().getValue()<=2)
            n++;
        return n;
    }

    public DateUtil getNextNDays(int n){
        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
        int year, month, day;
        int s = deom(this);
        if (s>n) {
            year=this.getDay().getMonth().getYear().getValue();
            int m = mon_maxnum[this.getDay().getMonth().getValue()-1];
            if (this.getDay().getMonth().getYear().isLeapYear()&&this.getDay().getMonth().getValue()==2) {
                m++;
            }
           m=m-this.getDay().getValue();
            if (m>=n) {
                month = this.getDay().getMonth().getValue();
                day = n+this.getDay().getValue();
            }
            else{
                n=n-m;
                month = this.getDay().getMonth().getValue()+1;
                int k = month;
                while(n-mon_maxnum[k-1]>0&&k<=12){
                    n -= mon_maxnum[k-1];
                    month++;
                    k++;
                }
                day = n;
            }
        }
        else {
            n=n-s;
            year = this.getDay().getMonth().getYear().getValue()+1;
            int y = 365;
            if (new Year(year).isLeapYear()) {
                y++;
            }
           for(;n>y;){
                n-=y;
                year++;
                y=365;
                if (new Year(year).isLeapYear())
                    y++;
            }
            int k = 1;
            while(n-mon_maxnum[k-1]>0&&k<=12){
                n =n- mon_maxnum[k-1];
                k++;
            }
            month = k;
            day = n;
        }

        return new DateUtil(year,month,day);

    }



    public DateUtil getPreviousNDays(int n) {
        int year, month, day;
        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};

        int d = 365 - deom(this);
        if (this.getDay().getMonth().getYear().isLeapYear()) {
           d++;
        }
        if (d> n) {
            int i=0;
            if(i!=0){
                i++;
            }
            year = this.getDay().getMonth().getYear().getValue();
            int s = this.getDay().getValue();
            if (s > n) {
                month = this.getDay().getMonth().getValue();
                day = s- n;
            } else {
                n = n-s;
                month = this.getDay().getMonth().getValue() - 1;
                if (month == 0) {
                    month = 12;
                    year = this.getDay().getMonth().getYear().getValue() - 1;
                }
                int k = month;
                while (n - mon_maxnum[k-1] > 0 && k >= 0) {
                    n =n- mon_maxnum[k-1];
                    month--;
                    k--;
                }
                day = mon_maxnum[k-1] - n;
                if (new Year(year).isLeapYear() && month == 2) {
                    day++;
                }
            }
        } else {
            n =n- d;
            year = this.getDay().getMonth().getYear().getValue() - 1;
            int y = 365;
            if (new Year(year).isLeapYear()) {
                y++;
            }
           for(;n>y;){
                n =n- y;
                year--;
                y = 365;
                if (new Year(year).isLeapYear())
                    y++;
            }
            int k = 12;
            while (n - mon_maxnum[k-1] > 0 && k >= 0) {
                n =n- mon_maxnum[k-1];
                k--;
            }
            month = k;
            day =mon_maxnum[k-1] - n;
            if (new Year(year).isLeapYear() && month == 2) {
                day++;
            }


        }
        return new DateUtil(year, month, day);

    }

    public int getDaysofDates(DateUtil date){
        DateUtil a = this;
        DateUtil b = date;
        if (this.equalTwoDates(date)) {
            return 0;
        }
        else if (!this.compareDates(date)) {
            a = date;
            b = this;
        }
        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
        int m,n,d = 0;
        for(m=a.getDay().getMonth().getYear().getValue()+1;m<b.getDay().getMonth().getYear().getValue();m++) {
            d=d+365;
            if(new Year(m).isLeapYear())
                d++;
        }

        if (a.getDay().getMonth().getYear().getValue()!=b.getDay().getMonth().getYear().getValue()) {
            for(n=1;n<b.getDay().getMonth().getValue();n++)
                d+=mon_maxnum[n-1];
            for(n=a.getDay().getMonth().getValue()+1;n<=12;n++)
                d=d+mon_maxnum[n-1];
            d+=mon_maxnum[a.getDay().getMonth().getValue()-1]-a.getDay().getValue();

            d+=b.getDay().getValue();
            if(a.getDay().getMonth().getYear().isLeapYear()&&a.getDay().getMonth().getValue()<=2)
                d++;
            if (b.getDay().getMonth().getYear().isLeapYear()&&b.getDay().getMonth().getValue()>2) {
                d++;
            }
        }
        else if(a.getDay().getMonth().getYear().getValue()==b.getDay().getMonth().getYear().getValue()&&a.getDay().getMonth().getValue()!=b.getDay().getMonth().getValue()){
            for(n=a.getDay().getMonth().getValue()+1;n<=b.getDay().getMonth().getValue()-1;n++)
                d+=mon_maxnum[n-1];
            d+=mon_maxnum[a.getDay().getMonth().getValue()-1]-a.getDay().getValue();
            d+=b.getDay().getValue();
            if(a.getDay().getMonth().getYear().isLeapYear()&&a.getDay().getMonth().getValue()<=2)
                d++;
        }
        else if(a.getDay().getMonth().getYear().getValue()==b.getDay().getMonth().getYear().getValue()&&a.getDay().getMonth().getValue()==b.getDay().getMonth().getValue()){
            d=b.getDay().getValue()-a.getDay().getValue();
        }
        return d;
    }


}


class Year{
    int value;
    public Year() {

    }
    public Year(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public boolean isLeapYear(){
        if (((value % 4 == 0) && (value % 100 != 0)) || (value % 400 == 0))
            return true;
        else
            return false;
    }
    public boolean validate() {
        if(value>2050||value<1900)
            return false;
        else
            return true;
    }
    public void yearIncrement() {
        value++;
    }
    public void yearReduction() {
        value--;
    }
}
class Month{
    int value;
    Year year;
    public Month() {

    }
    public Month(int yearValue,int monthValue) {
        this.year = new Year(yearValue);
        this.value = monthValue;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public Year getYear() {
        return year;
    }
    public void setYear(Year year) {
        this.year = year;
    }
    public void resetMin() {
        value=1;
    }
    public void resetMax() {
        value=12;
    }
    public boolean validate() {
        if(value>12||value<1)
            return false;
        else
            return true;

    }
    public void monthIncrement() {
        value++;
    }
    public void monthReduction() {
        value--;
    }
}
class Day{
    int value;
    Month month;
    int mon_maxnum[]= {31,28,31,30,31,30,31,31,30,31,30,31};
    public Day() {
    }
    public Day(int yearValue,int monthValue,int dayValue) {
        this.value = dayValue;
        this.month = new Month(yearValue,monthValue);

    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public Month getMonth() {
        return month;
    }
    public void setMonth(Month value) {
        this.month = value;
    }
    public void resetMin() {
        value=1;
    }
    public void resetMax() {
        value=mon_maxnum[month.getValue()-1];
    }
    public boolean validate() {
        if(this.getMonth().getYear().isLeapYear())
            mon_maxnum[1]=29;
        if(value>mon_maxnum[ month.getValue()-1]||value<1)
            return false;
        else
            return true;
    }
    public void dayIncrement() {
        value++;
    }
    public void dayReduction() {
        value--;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year,month,day;


        int num= input.nextInt();

        if (num == 1) {
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (date.checkInputValidity()==false) {
                System.out.println("Wrong Format");
                System.exit(0);

            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            System.out.println(date.getNextNDays(m).showDate());
        } else if (num == 2) {
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (date.checkInputValidity()==false) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (num == 3) {
            int y= Integer.parseInt(input.next());
            int m = Integer.parseInt(input.next());
            int d = Integer.parseInt(input.next());
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil f = new DateUtil(year, month, day);
            DateUtil t = new DateUtil(y, m,d);
            if (f.checkInputValidity() && t.checkInputValidity()) {
                System.out.println(f.getDaysofDates(t));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        } else {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

 报表:

 

 类图:

 

 踩坑心得:刚开始看不懂类中传入的参数,后来才明白这样做可以才可以让本类做好自己的工作,日期的算法不止一种,第一种一天一天算可能会超时,我改进后的代码就提高效率。

改进建议:改进日期的常规算法,避免一天一天进行计算。

题目集5(7-5)源码:

import java.util.Scanner;

class DateUtil {
    Day day;

    public DateUtil(){

    }
    public DateUtil(int d, int m, int y){
        this.day = new Day(d,m,y);
    }
    public Day getDay() {
        return day;
    }
    public void setDay(Day d) {
        this.day = d;
    }

    public boolean checkInputValidity(){
        if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate())
            return true;
        return false;
    }

    public boolean compare1(DateUtil date){
        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 true;
        else
            return false;

    }
    public boolean equal(DateUtil date){
        if(equalMonth(date)){
            return false;
        }
        else
            return true;
    }
    public boolean compareDates(DateUtil date){
        if (compare1(date))
            return false;
        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;



        return true;
    }
    public boolean equalDay(DateUtil date){
        if (this.getDay().getValue()==date.getDay().getValue())
            return true;
        else
            return false;
    }
    public boolean equalMonth(DateUtil date){
        if(this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue())
            return true;
        else
            return false;
    }
    public boolean equalYear(DateUtil date){
        if( this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
            return true;
        else
            return false;
    }
    public boolean equalTwoDates(DateUtil date){
        if (equalDay(date) &&equalYear(date)&& equalMonth(date))
            return true;
        else
            return false;
    }
    public boolean equals(DateUtil date){
        if(equalDay(date)){
            return false;
        }
        else
            return true;
    }
    public String showDate(){
        return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();

    }
    public int deom(DateUtil m) {
        int n = 0;

        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
        for (int i = m.getDay().getMonth().getValue()+1; i <=12; i++) {
            n=n+mon_maxnum[i-1];
        }
        n=n+mon_maxnum[m.getDay().getMonth().getValue()-1]-m.getDay().getValue();
        if(m.getDay().getMonth().getYear().isLeapYear()&&m.getDay().getMonth().getValue()<=2)
            n++;
        return n;
    }

    public DateUtil getNextNDays(int n){
        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
        int year, month, day;
        int s = deom(this);
        if (s>n) {
            year=this.getDay().getMonth().getYear().getValue();
            int m = mon_maxnum[this.getDay().getMonth().getValue()-1];
            if (this.getDay().getMonth().getYear().isLeapYear()&&this.getDay().getMonth().getValue()==2) {
                m++;
            }
            m=m-this.getDay().getValue();
            if (m>=n) {
                month = this.getDay().getMonth().getValue();
                day = n+this.getDay().getValue();
            }
            else{
                n=n-m;
                month = this.getDay().getMonth().getValue()+1;
                int k = month;
                while(n-mon_maxnum[k-1]>0&&k<=12){
                    n -= mon_maxnum[k-1];
                    month++;
                    k++;
                }
                day = n;
            }
        }
        else {
            n=n-s;
            year = this.getDay().getMonth().getYear().getValue()+1;
            int y = 365;
            if (new Year(year).isLeapYear()) {
                y++;
            }
            for(;n>y;){
                n-=y;
                year++;
                y=365;
                if (new Year(year).isLeapYear())
                    y++;
            }
            int k = 1;
            while(n-mon_maxnum[k-1]>0&&k<=12){
                n =n- mon_maxnum[k-1];
                k++;
            }
            month = k;
            day = n;
        }

        return new DateUtil(year,month,day);

    }



    public DateUtil getPreviousNDays(int n) {
        int year, month, day;
        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};

        int d = 365 - deom(this);
        if (this.getDay().getMonth().getYear().isLeapYear()) {
            d++;
        }
        if (d> n) {
            int i=0;
            if(i!=0){
                i++;
            }
            year = this.getDay().getMonth().getYear().getValue();
            int s = this.getDay().getValue();
            if (s > n) {
                month = this.getDay().getMonth().getValue();
                day = s- n;
            } else {
                n = n-s;
                month = this.getDay().getMonth().getValue() - 1;
                if (month == 0) {
                    month = 12;
                    year = this.getDay().getMonth().getYear().getValue() - 1;
                }
                int k = month;
                while (n - mon_maxnum[k-1] > 0 && k >= 0) {
                    n =n- mon_maxnum[k-1];
                    month--;
                    k--;
                }
                day = mon_maxnum[k-1] - n;
                if (new Year(year).isLeapYear() && month == 2) {
                    day++;
                }
            }
        } else {
            n =n- d;
            year = this.getDay().getMonth().getYear().getValue() - 1;
            int y = 365;
            if (new Year(year).isLeapYear()) {
                y++;
            }
            for(;n>y;){
                n =n- y;
                year--;
                y = 365;
                if (new Year(year).isLeapYear())
                    y++;
            }
            int k = 12;
            while (n - mon_maxnum[k-1] > 0 && k >= 0) {
                n =n- mon_maxnum[k-1];
                k--;
            }
            month = k;
            day =mon_maxnum[k-1] - n;
            if (new Year(year).isLeapYear() && month == 2) {
                day++;
            }


        }
        return new DateUtil(year, month, day);

    }

    public int getDaysofDates(DateUtil date){
        DateUtil a = this;
        DateUtil b = date;
        if (this.equalTwoDates(date)) {
            return 0;
        }
        else if (!this.compareDates(date)) {
            a = date;
            b = this;
        }
        int []mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
        int m,n,d = 0;
        for(m=a.getDay().getMonth().getYear().getValue()+1;m<b.getDay().getMonth().getYear().getValue();m++) {
            d=d+365;
            if(new Year(m).isLeapYear())
                d++;
        }

        if (a.getDay().getMonth().getYear().getValue()!=b.getDay().getMonth().getYear().getValue()) {
            for(n=1;n<b.getDay().getMonth().getValue();n++)
                d+=mon_maxnum[n-1];
            for(n=a.getDay().getMonth().getValue()+1;n<=12;n++)
                d=d+mon_maxnum[n-1];
            d+=mon_maxnum[a.getDay().getMonth().getValue()-1]-a.getDay().getValue();

            d+=b.getDay().getValue();
            if(a.getDay().getMonth().getYear().isLeapYear()&&a.getDay().getMonth().getValue()<=2)
                d++;
            if (b.getDay().getMonth().getYear().isLeapYear()&&b.getDay().getMonth().getValue()>2) {
                d++;
            }
        }
        else if(a.getDay().getMonth().getYear().getValue()==b.getDay().getMonth().getYear().getValue()&&a.getDay().getMonth().getValue()!=b.getDay().getMonth().getValue()){
            for(n=a.getDay().getMonth().getValue()+1;n<=b.getDay().getMonth().getValue()-1;n++)
                d+=mon_maxnum[n-1];
            d+=mon_maxnum[a.getDay().getMonth().getValue()-1]-a.getDay().getValue();
            d+=b.getDay().getValue();
            if(a.getDay().getMonth().getYear().isLeapYear()&&a.getDay().getMonth().getValue()<=2)
                d++;
        }
        else if(a.getDay().getMonth().getYear().getValue()==b.getDay().getMonth().getYear().getValue()&&a.getDay().getMonth().getValue()==b.getDay().getMonth().getValue()){
            d=b.getDay().getValue()-a.getDay().getValue();
        }
        return d;
    }


}


class Year{
    int value;
    public Year() {

    }
    public Year(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public boolean isLeapYear(){
        if (((value % 4 == 0) && (value % 100 != 0)) || (value % 400 == 0))
            return true;
        else
            return false;
    }
    public boolean validate() {
        if(value>2020||value<1820)
            return false;
        else
            return true;
    }
    public void yearIncrement() {
        value++;
    }
    public void yearReduction() {
        value--;
    }
}
class Month{
    int value;
    Year year;
    public Month() {

    }
    public Month(int yearValue,int monthValue) {
        this.year = new Year(yearValue);
        this.value = monthValue;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public Year getYear() {
        return year;
    }
    public void setYear(Year year) {
        this.year = year;
    }
    public void resetMin() {
        value=1;
    }
    public void resetMax() {
        value=12;
    }
    public boolean validate() {
        if(value>12||value<1)
            return false;
        else
            return true;

    }
    public void monthIncrement() {
        value++;
    }
    public void monthReduction() {
        value--;
    }
}
class Day{
    int value;
    Month month;
    int mon_maxnum[]= {31,28,31,30,31,30,31,31,30,31,30,31};
    public Day() {
    }
    public Day(int yearValue,int monthValue,int dayValue) {
        this.value = dayValue;
        this.month = new Month(yearValue,monthValue);

    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public Month getMonth() {
        return month;
    }
    public void setMonth(Month value) {
        this.month = value;
    }
    public void resetMin() {
        value=1;
    }
    public void resetMax() {
        value=mon_maxnum[month.getValue()-1];
    }
    public boolean validate() {
        if(this.getMonth().getYear().isLeapYear())
            mon_maxnum[1]=29;
        if(value>mon_maxnum[ month.getValue()-1]||value<1)
            return false;
        else
            return true;
    }
    public void dayIncrement() {
        value++;
    }
    public void dayReduction() {
        value--;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year,month,day;


        int num= input.nextInt();

        if (num == 1) {
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (date.checkInputValidity()==false) {
                System.out.println("Wrong Format");
                System.exit(0);

            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            System.out.print(date.getDay().getMonth().getYear().value + "-" + date.getDay().getMonth().value + "-" + date.getDay().value + " next " + m + " days is:");

            System.out.println(date.getNextNDays(m).showDate());
        } else if (num == 2) {
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (date.checkInputValidity()==false) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
           System.out.print(date.getDay().getMonth().getYear().value + "-" + date.getDay().getMonth().value + "-" + date.getDay().value + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (num == 3) {
            int y= Integer.parseInt(input.next());
            int m = Integer.parseInt(input.next());
            int d = Integer.parseInt(input.next());
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil f = new DateUtil(year, month, day);
            DateUtil t = new DateUtil(y, m,d);
            if (f.checkInputValidity() && t.checkInputValidity()) {
                System.out.println("The days between " +t.showDate() + " and " +  f.showDate() + " are:"
                        + f.getDaysofDates(t));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        } else {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

报表:

类图:

 

 踩坑心得:弄清输入格式谁是先输入的日期谁是后输入的日期。

改进建议:

日期算法的简化,做到不超时。

两种日期类聚合优劣比较:

第一种聚合优缺点:

它采用的是依次往下找的形式,如果我要达到某个地方,那我一定要经过之前的位置,这样的结构可以使程序的整体性更强,执行起来更加有章法。其缺点是如果类很多,那么程序依次往下走就会很费时间,代码很长,有时太过复杂容易出错。

第二中聚合优缺点:

它采用的是一个类控制其他所有类的聚合关系,每一个类各管个的,只需要一个类来调用他们并使用里面的属性。有一个明显的优点就是显得更加简洁,非常容易让人理解,缺点是类与类之间几乎无关联,所以在写代码就忘了这个类的作用。

题目集4(7-3)源代码:

import java.util.Scanner;

class Shape
{

    public Shape()
    {
        System.out.println("Constructing Shape");
    }
    public double getArea()
    {
        return 0.0;
    }
}

class Circle extends Shape
{
    private double radius;
    public Circle()
    {
        System.out.println("Constructing Circle");
    }


    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public double getArea() {

        return Math.PI*radius*radius;
    }

}
class Rectangle extends Shape
{
    private double length;
    private double width;
    public Rectangle()
    {
        System.out.println("Constructing Rectangle");
    }
    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }
    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
    public double getArea() {

        return 1.0*width*length;
    }

}
class Ball extends Circle
{
    public Ball()
    {
        System.out.println("Constructing Ball");
    }
    public double getArea() {

        return 4.0*Math.PI*getRadius()*getRadius();
    }
    public double getVolume()
    {

        return 4.0/3.0*Math.PI*getRadius()*getRadius()*getRadius();
    }

}
class Box extends Rectangle
{
    private double height;
    public Box()
    {
        System.out.println("Constructing Box");
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getHeight() {
        return height;
    }
    public double getArea() {


        return 2*(getWidth()*getLength()+getLength()*height+getWidth()*height);
    }
    public double getVolume()
    {
        return height*getLength()*getWidth();
    }

}
public class Main {

    public static void main(String[] args) {
        int num;
        Scanner scanner=new Scanner(System.in);
       num=scanner.nextInt();
        switch(num)
        {
            case 1:
                double r=scanner.nextDouble();
                if(r<0) {
                    System.out.println("Wrong Format");
                }
                else {
                    Circle circle=new Circle();
                    circle.setRadius(r);
                    System.out.printf("Circle's area:%.2f\n",circle.getArea());
                }
                break;

            case 2:
                double length=scanner.nextDouble();
                double width=scanner.nextDouble();

                if(width<0||length<0) {
                    System.out.println("Wrong Format");
                }
                else {
                    Rectangle rectangle=new Rectangle();
                    rectangle.setLength(length);
                    rectangle.setWidth(width);
                    System.out.printf("Rectangle's area:%.2f\n",rectangle.getArea());
                }
                break;
            case 3:
                double r2=scanner.nextDouble();
                if(r2<0) {
                    System.out.println("Wrong Format");
                }
                else {
                    Ball ball=new Ball();
                    ball.setRadius(r2);
                    System.out.printf("Ball's surface area:%.2f\n",ball.getArea());
                    System.out.printf("Ball's volume:%.2f\n",ball.getVolume());
                }
                break;
            case 4:
                double width2=scanner.nextDouble();
                double length2=scanner.nextDouble();
                double height=scanner.nextDouble();
                if(width2<0||length2<0||height<0) {
                    System.out.println("Wrong Format");
                }
                else {
                    Box box=new Box();
                    box.setHeight(height);
                    box.setWidth(width2);
                    box.setLength(length2);
                    System.out.printf("Box's surface area:%.2f\n",box.getArea());
                    System.out.printf("Box's volume:%.2f\n",box.getVolume());
                }
                break;
            default:
                System.out.println("Wrong Format");
        }
    }
}

  报表:

 

 类图:

 

 

踩坑心得:

继承关键字extends,数据类型是double,别忘了系数前面是乘以1.0。

改进建议:

利用好重写方法,简化代码。

题目集6(7-5)源码:

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

import static java.lang.System.exit;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int ci, tr, re;
        ci= sc.nextInt();
        re = sc.nextInt();
        tr = sc.nextInt();
        if (ci < 0 || tr < 0 || re < 0) {
            System.out.println("Wrong Format");
            exit(0);
        }
        Circle []a=new Circle[ci];
        Rectangle []b=new Rectangle[re];
        Triangle []c=new Triangle[tr];
        double[] area=new double [ci+tr+re];
        double sumArea=0;
        int num=0;
        for(int i=0;i<ci;i++){
            double radius=sc.nextDouble();
            a[i]=new Circle(radius);
            area[num]=a[i].getArea();
           num++;
            if(a[i].validate()==false){
                System.out.println("Wrong Format");
                exit(0);
            }
        }
        for(int i=0;i<re;i++){
            double width=sc.nextDouble();
            double length=sc.nextDouble();
            b[i]=new Rectangle(width,length);
            area[num]=b[i].getArea();
            num++;
            if(b[i].validate()==false){
                System.out.println("Wrong Format");
                exit(0);
            }
        }
        for(int i=0;i<tr;i++){
            double m=sc.nextDouble(),n=sc.nextDouble(),k=sc.nextDouble();
            c[i]=new Triangle(m,n,k);
            area[num]=c[i].getArea();
            num++;
            if(c[i].validate()==false){
                System.out.println("Wrong Format");
                exit(0);
            }
        }
        System.out.println("Original area:");
        for(int i=0;i<ci+tr+re;i++){
            System.out.printf("%.2f ",area[i]);
            sumArea+=area[i];
        }
        System.out.printf("\n");
        System.out.printf("Sum of area:");
        System.out.printf("%.2f\n",sumArea);

        System.out.println("Sorted area:");
        Arrays.sort(area);
        for(int i=0;i<ci+tr+re;i++){
            System.out.printf("%.2f ",area[i]);
        }
        System.out.printf("\n");
        System.out.printf("Sum of area:%.2f",sumArea);
    }
}
class Circle extends Shape{
    private double radius;
   public Circle(){

   }
   public Circle(double radius){
       this.radius=radius;
   }
    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public double getArea() {

        return Math.PI*radius*radius;
    }
    public boolean validate(){
       if(radius<=0)
           return false;
       return  true;
    }

}
    class Triangle extends Shape {
        double side1,side2,side3;
        public Triangle(double a, double b, double c) {
            this.side1 = a;
            this.side2 = b;
            this.side3 = c;
        }
    
        public double getArea() {
            double p = (side1 + side2 + side3) / 2;
            return 1.0*Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
        }
        public boolean validate() {
            if (side1 <= 0 || side2 <= 0 || side3 <=0) {
                return false;
            }
            if(side1+side2>side3&&side1+side3>side2&&side3+side2>side1)
          return  true;
            else
                return false;
        }
    }
class Rectangle extends Shape{
    private double length;
    private double width;
    public Rectangle()
    {

    }
    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public Rectangle(double width, double length) {
        this.width=width;
        this.length = length;
    }
    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
    public double getArea() {

        return 1.0*width*length;
    }
    public boolean validate(){
        if(width<=0||length<=0){
            return false;
        }
        return true;
    }

}

abstract class Shape {
    public abstract boolean validate ();
    public abstract double getArea();
}

报表:

 

 类图:

 

 踩坑心得:

应该注意到不能被显而易见的测试点,如创造0个矩形对象,三角形判断。

改进建议:

海伦公式求面积的应用,和调用Math.

题目集6(7-6)源码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double s=sc.nextDouble();
        double a=sc.nextDouble();
        double b=sc.nextDouble();
        GetArea i=new Circle(s);
        GetArea j=new Rectangle(a,b);
        if(s<=0){
            System.out.println("Wrong Format");
        }
        
        else if(a<=0||b<=0) {
            System.out.println("Wrong Format");
        }
        else{
            System.out.printf("%.2f",i.getArea());
            System.out.print("\n");
            System.out.printf("%.2f",j.getArea());
        }
    }
}
class Circle implements GetArea{
    private double radius;
   public Circle(){

   }
   public Circle(double radius){
       this.radius=radius;
   }
    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public double getArea() {

        return Math.PI*radius*radius;
    }

}
class Rectangle implements GetArea{
    private double length;
    private double width;
    public Rectangle()
    {

    }
    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public Rectangle(double width, double length) {
        this.width=width;
        this.length = length;
    }
    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
    public double getArea() {

        return 1.0*width*length;
    }

}
interface GetArea {
    public default double getArea(){
        return 1.0;
    }
}

  

报表:

 

 类图:

 

 踩坑心得:

接口关键字是 implements,在这个接口里面定义一个抽象方法,当这些类对这个接口进行调用后自行对这个接口进行重写,与正常的类一样。

改进建议:

对于可重写的东西可以是接口来表示。

三种渐进式图形设计的思路与技术运用:

题目集4(7-3)中circle类和rectangle类继承shape类,重写shape类的getArea()方法,ball类继承circle类,重写getArea()方法并定义求定义求体积方法。box类继承rectangle类的属性和getArea()方法,并定义新的属性height,重写父类的getArea()方法,并定义求立方体体积方法。各个子类封装自己的属性,属性无法被继承,只能重新定义。

题目集6(7-5)中circle类、rectangle类和triangle类继承同一个父类shape类,父类未定义属性,各个子类封装自己的属性,属性无法被继承,只能重新定义。

题目集6(7-6)中使用接口GetArea,定义方法getArea(),通过implements关键字实现接口,circle类和rectangle类重写getArea()方法。再circle类和rectangle类中定义它们自己的私有属性。

三次正则表达式的分析总结:

1、第一个正则表达式是水文数据,储存到固定的地址就显得十分复杂,我的写法是先将其按“|”分开再按“/”分开,每个数据两边匹配零个或多个空格,多次使用split对分好的字符串进行储存,实现算法,正则表达式如下:

String pi1=" *([1-9]\\d{0,3}/([1-9]|1[0-2])/([1-9]|1[0-2]|3[0-1])) (([02468]|1[02468]|2[02]):[0][0]) *";

String pi2=" *(([1-9]\\d{0,2}.[0-9]\\d{0,2})|([1-9]\\d{0,2})) *";

String pi3=" *([1-9].[0-9][0-9]) *";

String pi4=" *([1-9]\\d{0,1}.([0-9]\\d{0,2})|([1-9]\\d{0,2})) *";

2、第二个是关键字,需要匹配两种注释和字符串。

String pi1=("(.*)//(.*)");

String pi2=("\"(.*?)\"");

String pi3=("/\\*(.*?)\\*/");

3、第三个多种简单匹配

   Pattern p=Pattern.compile("[^0]([0-9]{4,14})");

 Pattern p=Pattern.compile("[a-z|A-Z|0-9]{4}");

  Pattern p=Pattern.compile("2020(1[1234567]|61|7[123]|8[12])(([1-3][0-9])|40|01|02|03|04|05|06|07|08|09)");

题目集5(7-4)源码:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {

    public static void main(String[] args) {

        Scanner input=new Scanner(System.in);

        String str;
        StringBuilder ss=new StringBuilder();
        Map map=new HashMap();
        String []key= { "abstract","assert","boolean","break","byte","case","catch",
                "char","class","const","continue","default","do","double","else",
                "enum","extends","false","final","finally","float",
                "for","goto","if","implements","import","instanceof",
                "int","interface","long","native","new","null","package",
                "private","protected","public","return","short","static",
                "strictfp","super","switch","synchronized","this","throw",
                "throws","transient","true","try","void","volatile","while"

        };


     
       
        for(int i=0;;i++) {
            str=input.nextLine();
            if(str.equals("exit"))
                break;

           if(str.matches("(.*)//(.*)"))
            {String a[]=str.split("//");
                ss.append(a[0]+" ");
            }
            else
            {ss.append(str+" ");

            }
        }
        int l;
           int j=0;
        int num=0;
        String s=ss.toString();

        Pattern p=Pattern.compile("\"(.*?)\"");
        Matcher m=p.matcher(s);
         while(m.find()==true){

            s=s.replace(m.group()," ");
            m=p.matcher(s);
        }


         p=Pattern.compile("/\\*(.*?)\\*/");
        m=p.matcher(s);
         if(j>0){ j=0; }
       
        while(m.find()==true){
            s=s.replace(m.group()," ");
            m=p.matcher(s);
            }
        
            p=Pattern.compile("\"(.*?)\"");//字符串中关键字
            m=p.matcher(s);
        
    while(m.find()==true)
    {
        s=s.replace(m.group()," ");
        m=p.matcher(s);
    }
        panduan d=new panduan();
        if(d.empty(s)==false)
        {System.out.println("Wrong Format");
            System.exit(0);
        }
        else
            l=1;
         s=s.replace("["," ");
        s=s.replace("]"," ");
          s=s.replace("-"," ");
        s=s.replace("+"," ");
        s=s.replace("*"," ");
        s=s.replace("/"," ");
        s=s.replace(">"," ");
         s=s.replace("<"," ");
        s=s.replace("="," ");
        s=s.replace("!"," ");
        s=s.replace(":"," ");
        s=s.replace("\\"," ");
       
        s=s.replace("[,"," ");
           s=s.replace(",]"," ");
           s=s.replace("[,]"," ");
           s=s.replace("[],"," ");
           s=s.replace("],[,"," ");
    
        s=s.replaceAll("[^a-zA-Z]", " ");

     String []aa=s.split("\\s");
        for(int i=0;i<aa.length;i++)
        {
            for( j=0;j<key.length;j++)
                if(aa[i].equals(key[j]))
                {
                    map.put(key[j], 0);
                }
            if(key.length<0){
                i++;
            }
        }
        for( int i = 0;i<aa.length;i++)
        {
            for( j=0;j<key.length;j++)
                if(aa[i].equals(key[j]))
                {	num=(int)map.get(key[j]);
                    map.put(key[j], num+1);
                 
                }
        }
   
        Set t=map.keySet();
        Object[] arr=t.toArray();
        Arrays.sort(arr);
        for(Object k:arr){
            System.out.println(map.get(k)+"\t"+k);
        }
    }
}
class panduan{
    boolean empty(String s){
        if(s.isEmpty())
            return  false;
        return true;
    }
}

  报表:

 

 

踩坑心得:

如何提取两种注释里面的内容,遇到后如何处理?遇到字符串又怎么处理?这些让我想了好长时间,换行注释怎么处理。

改进建议:

用空格来代替注释或者双引号等等。

Java集合框架应用分析总结:

用map集合进行处理,两个参数键值对和数值一一对应,用到了map的get方法和put方法对元素进行处理。

总结:

学到了如何灵活运用正则表达式,由易到难,如何深入理解聚合,对于java面向对象的特点有了更深的了解。学到了多态,继承,接口如何使用。对于更复杂的正则表达式,我还需要多练习,多于类的设计,我还需多思考如何做的最好。希望老师课上可以讲解pta里面需要讲的题,保证大家都要学会,没有疑问,不要拖着问题,作业难度和题量适中。

 

posted @ 2021-05-01 20:49  sxh1111  阅读(62)  评论(0)    收藏  举报