OO 第二次总结

一、前言

题目集4

  第一次接触到类的设计,还涉及了字符串相关应用、封装性以及String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。

  题目较多,但是大部分题目思路比较简单。

题目集5

  训练了正则表达式的使用以及使用聚合关系进行不同面向对象设计。除7-6外难度不大。题目量也较小。

题目集6

  仅有一道题目作为之前菜单设计的迭代升级版,但是类与类间的关系较复杂,难度很大。

二、设计与分析

题目集5 7-5及7-6

7-5

 

import java.util.Scanner;

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

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(day, month, year);
            if(!date.getDay().getMonth().validate()){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            if (!date.checkInputValidity()) {
                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 (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(day, month, year);
            if(!date.getDay().getMonth().validate()){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            if (!date.checkInputValidity()) {
                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 (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(day, month, year);
            DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);
            if(!fromDate.getDay().getMonth().validate()||!toDate.getDay().getMonth().validate()){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println(fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}
class Year {
    private 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(){
        return (value % 4 == 0 && value % 100 != 0) || value % 400 == 0;
    }

    public boolean validate(){
        return value>=1900&&value<=2050;
    }

    public void yearIncrement(){
        value++;
    }

    public void yearReduction(){
        value--;
    }
}
class Month {
    private int value;
    private Year year;
    public Month(){

    }

    public Month(int yearValue,int monthValue){
        this.value=monthValue;
        year = new Year(yearValue);
    }

    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(){
        return value <= 12 && value >= 1;
    }

    public void monthIncrement(){
        value++;
        if(value>12){
            value=1;
            year.yearIncrement();
        }
    }

    public void monthReduction(){
        value--;
        if(value==0){
            value=12;
            year.yearReduction();
        }
    }
}
class Day {
    private int value;
    private Month month;
    private 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;
        month = new Month(yearValue,monthValue);
    }

    public int getValue() {
        return value;
    }

    public Month getMonth() {
        return month;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void setMonth(Month month) {
        this.month = month;
    }

    public void resetMin(){
        value=1;
    }

    public void resetMax(){
        value=mon_maxnum[month.getValue()-1];
    }

    public boolean validate(){
        if(month.getYear().isLeapYear())
            mon_maxnum[1]=29;
        else
            mon_maxnum[1]=28;
        return value >= 1 && value <= mon_maxnum[month.getValue() - 1];
    }

    public void dayIncrement(){
        value++;
        if(month.getYear().isLeapYear())
            mon_maxnum[1]=29;
        else
            mon_maxnum[1]=28;
        if(value>mon_maxnum[month.getValue()-1]){
            value=1;
            month.monthIncrement();
        }
    }

    public void dayReduction(){
        value--;
        if(month.getYear().isLeapYear())
            mon_maxnum[1]=29;
        else
            mon_maxnum[1]=28;
        if(value==0){
            month.monthReduction();
            value=mon_maxnum[month.getValue()-1];
        }
    }
}
class DateUtil {
    private Day day;
    public DateUtil(){

    }

    public DateUtil(int d,int m,int y){
        day = new Day(y,m,d);
    }

    public Day getDay() {
        return day;
    }

    public void setDay(Day day) {
        this.day = day;
    }


    public boolean checkInputValidity(){
        return day.validate() && day.getMonth().validate() && day.getMonth().getYear().validate();
    }

    public boolean equalTwoDates(DateUtil date){
        return date.getDay().getValue() == day.getValue() && date.getDay().getMonth().getValue() == day.getMonth().getValue() && date.getDay().getMonth().getYear().getValue() == day.getMonth().getYear().getValue();
    }

    public boolean compareDates(DateUtil date){
        if(day.getMonth().getYear().getValue()>date.getDay().getMonth().getYear().getValue())
            return true;
        else if(day.getMonth().getYear().getValue()<date.getDay().getMonth().getYear().getValue())
            return false;
        else{
            if(day.getMonth().getValue()>date.getDay().getMonth().getValue())
                return true;
            else if(day.getMonth().getValue()<date.getDay().getMonth().getValue())
                return false;
            else{
                return day.getValue() > date.getDay().getValue();
            }
        }
    }
    public String showDate(){
        return day.getMonth().getYear().getValue()+"-"+day.getMonth().getValue()+"-"+day.getValue();
    }

    public int getDaysofDates(DateUtil date){
        int sum=0;
        while(!equalTwoDates(date)){
            if(compareDates(date))
                day.dayReduction();
            else
                day.dayIncrement();
            sum++;
        }
        return sum;
    }

    public DateUtil getNextNDays(int n){
        int i;
        for(i=0;i<n;i++){
            day.dayIncrement();
        }
        return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
    }

    public DateUtil getPreviousNDays(int n){
        int i;
        for(i=0;i<n;i++){
            day.dayReduction();
        }
        return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
    }
}

  本题难度较小,所写的方法都比较简短,许多方法也在之前的日期问题中有写过。仅有日期、月份的增减方法需要注意,在达到上、下限时,日期(月份)需要增1、减1。

7-6

 

import java.util.Scanner;

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

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            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()) {
                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.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            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()) {
                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.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() +
                        " and " + toDate.showDate() + " are:"
                        + fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}
class Year {
    private 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(){
        return (value % 4 == 0 && value % 100 != 0) || value % 400 == 0;
    }

    public boolean validate(){
        return value>=1820&&value<=2020;
    }

    public void yearIncrement(){
        value++;
    }

    public void yearReduction(){
        value--;
    }
}
class Month {
    private int value;
    public Month(){

    }

    public Month(int value){
        this.value=value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void resetMin(){
        value=1;
    }

    public void resetMax(){
        value=12;
    }

    public boolean validate(){
        return value <= 12 && value >= 1;
    }

    public void monthIncrement(){
        value++;
    }

    public void monthReduction(){
        value--;
    }
}
class Day {
    private int value;

    public Day(){

    }

    public Day(int value){
        this.value=value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void dayIncrement(){
        value++;
    }

    public void dayReduction() {
        value--;
    }
}
class DateUtil {
    private Day day;
    private Month month;
    private Year year;
    private int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
    public DateUtil(){

    }

    public DateUtil(int y,int m,int d){
        day = new Day(d);
        month = new Month(m);
        year = new Year(y);
    }

    public Day getDay() {
        return day;
    }

    public void setDay(Day day) {
        this.day = day;
    }

    public Year getYear() {
        return year;
    }

    public Month getMonth() {
        return month;
    }

    public void setMonth(Month month) {
        this.month = month;
    }

    public void setYear(Year year) {
        this.year = year;
    }

    private void setDayMax(){
        if(year.isLeapYear())
            mon_maxnum[1]=29;
        else
            mon_maxnum[1]=28;
        day = new Day(mon_maxnum[month.getValue()-1]);
    }

    private void setDayMin(){
        day = new Day(1);
    }

    public boolean checkInputValidity(){
        boolean flag=false;
        if(year.isLeapYear())
            mon_maxnum[1]=29;
        else
            mon_maxnum[1]=28;
        if(day.getValue() >= 1 && day.getValue() <= mon_maxnum[month.getValue() - 1])
            flag=true;
        return  flag && month.validate() && year.validate();
    }

    public boolean equalTwoDates(DateUtil date){
        return date.day.getValue() == day.getValue() && date.month.getValue() == month.getValue() && date.year.getValue() == year.getValue();
    }

    public boolean compareDates(DateUtil date){
        if(year.getValue()>date.year.getValue())
            return true;
        else if(year.getValue()<date.year.getValue())
            return false;
        else{
            if(month.getValue()>date.month.getValue())
                return true;
            else if(month.getValue()<date.month.getValue())
                return false;
            else{
                return day.getValue() > date.day.getValue();
            }
        }
    }
    public String showDate(){
        return year.getValue()+"-"+month.getValue()+"-"+day.getValue();
    }

    public int getDaysofDates(DateUtil date){
        int sum=0;
        while(!equalTwoDates(date)){
            if(compareDates(date)) {
                if(date.year.isLeapYear())
                    mon_maxnum[1]=29;
                else
                    mon_maxnum[1]=28;
                date.day.dayIncrement();
                if(date.day.getValue()>mon_maxnum[month.getValue()-1]){
                    date.day.setValue(1);
                    date.month.monthIncrement();
                    if(date.month.getValue()>12){
                        date.month.setValue(month.getValue()-12);
                        date.year.yearIncrement();
                    }
                }
            } else {
                if(year.isLeapYear())
                    mon_maxnum[1]=29;
                else
                    mon_maxnum[1]=28;
                day.dayIncrement();
                if(day.getValue()>mon_maxnum[month.getValue()-1]){
                    day.setValue(1);
                    month.monthIncrement();
                    if(month.getValue()>12){
                        month.setValue(month.getValue()-12);
                        year.yearIncrement();
                    }
                }
            }
            sum++;
        }
        return sum;
    }

    public DateUtil getNextNDays(int n){
        if(year.isLeapYear())
            mon_maxnum[1]=29;
        else
            mon_maxnum[1]=28;
        day.setValue(day.getValue()+n);
        while (day.getValue()>mon_maxnum[month.getValue()-1]){
            day.setValue(day.getValue()-mon_maxnum[month.getValue()-1]);
            month.monthIncrement();
            if(month.getValue()>12){
                month.setValue(month.getValue()-12);
                year.yearIncrement();
            }
            if(year.isLeapYear())
                mon_maxnum[1]=29;
            else
                mon_maxnum[1]=28;
        }
        return new DateUtil(year.getValue(), month.getValue(), day.getValue());
    }

    public DateUtil getPreviousNDays(int n){
        if(year.isLeapYear())
            mon_maxnum[1]=29;
        else
            mon_maxnum[1]=28;
        day.setValue(day.getValue()-n);
        while (day.getValue()<=0){
            month.monthReduction();
            if(month.getValue()<=0){
                month.setValue(month.getValue()+12);
                year.yearReduction();
            }
            day.setValue(day.getValue()+mon_maxnum[month.getValue()-1]);
            if(year.isLeapYear())
                mon_maxnum[1]=29;
            else
                mon_maxnum[1]=28;
        }
        return new DateUtil(year.getValue(), month.getValue(), day.getValue());
    }
}

  本题相较上题是一个降低耦合度的版本,Day、Month、Year类都只与DateUtil类打交道,与上一题区别较大,没有了年月日的增减方法。在很多方法里都要重复判断日月是否在合法范围内,会麻烦很多。这道题我也很惭愧的没有拿到满分。

关于题目集4的7-1与题目集6的7-1

  对于这两道题,我确实不够上心,第一次做到题目集4的7-1的时候时间不足,就放弃了这道题,题目集关闭后也没有去构思。到题目集6的7-1,看到题目所需时间20+h又听到身边的同学说题目多难,就甚至没有尝试便放弃了。我需要反思端正我的学习态度。

三、踩坑心得

题目集4

7-3 去掉重复的数据

  尝试了数组、arrayList类等方法写 n很大的测试点都会运行超时,与同学交流后才知道需要使用容器。

7-4 单词统计与排序

  先是使用split()方法分割函数,然后用冒泡排序对每个单词进行比较。无法通过全部测试点。与同学交流后知道需要用到java自带的compareToIgnoreCase()方法。

四、改进建议

  深刻理解了设计的重要性,不同的设计在书写代码时难度完全不同。

  需要多与同学交流,许多问题和同学交流后就能迎刃而解。

  阅题时要细心。

五、总结

  遇到困难不能退缩。对于题目集里的每一道题都要认真对待,不能放弃任何一道题,否则面对下次题目集的迭代版本的时候就只能干瞪眼,雪球越滚越大。

  对java的基本知识认知不全面,在面对问题时往往只能想到一种办法,无法通过测试点也不知道该怎么修改。

posted @ 2023-04-30 23:08  LastXuan  阅读(16)  评论(0)    收藏  举报