OO前三次作业总结

1、前言(对前三次作业的总结)

  前三次作业分别考察了:1.对java基本结构的运用以及数组的运用。2.对类中方法的理解与调用。3.对私有属性的修改与提取以及对复杂方法的思考。

  题量:逐级递增,对时间的规划要求不断的提高。

  难度:逐级递增,作业三7-3未能在规定时间内达到理想状态。(实在是菜的真实


2、设计与分析

  题目集一:7-8

import java.util.Scanner;
import java.util.Arrays;
public class Main
{
    public static void main(String[] args)
    {
        int i;
        Scanner input = new Scanner(System.in);
        double[] a = new double[3];
        for(i=0;i<3;i++)
            a[i] = input.nextDouble();
        Arrays.sort(a);
        if((0<a[0]&&a[0]<201)&&(0<a[1]&&a[1]<201)&&(0<a[2]&&a[2]<201))
        {
            if((a[0]+a[1])>a[2])
            {
                if((a[0] == a[1])&&(a[1] == a[2]))
                    System.out.println("Equilateral triangle");
                else if((a[0] == a[1]) && ((a[0]*a[0] + a[1]*a[1]) - (a[2]*a[2]) <0.000001))
                    System.out.println("Isosceles right-angled triangle");
                else if(a[2] == a[1] || a[2] == a[0] || a[0] == a[1])
                    System.out.println("Isosceles triangle");
                else if((a[0]*a[0] + a[1]*a[1]) - (a[2]*a[2]) <0.000001)
                    System.out.println("Right-angled triangle");
                else
                    System.out.println("General triangle");
            }
            else
                System.out.println("Not a triangle");
        }
        else
            System.out.println("Wrong Format");
    }
}

 

  题目的难度还算简单,但是在写的过程中从同学那听说了Array的库函数,这省下了我还要自己写一遍冒泡排序的功夫(java真是太强了与此同时我一直都会因为运行超时而无法过测试点在询问班上的大佬后知道是因为java的double属性不像C的double类型会自动给你取小数的后六位,所以我被迫手动确定运算的速度的范围这个运行超时是在是耗费了我不少的时间。(但是库函数的存在似乎也减少了我所需要的时间,塞翁失马焉知非福啊

  题目集二:7-4

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        int month = input.nextInt();
        int day = input.nextInt();
        if(!checkInputValidity(year, month, day))
        {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        nextDate(year,month,day);
        System.exit(0);
    }

    public static boolean isLeapYear(int year)
    {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
    public static boolean checkInputValidity(int year,int month,int day)//判断输入日期是否合法,返回布尔值
    {
        return year >= 1820 && year <= 2020 && month >= 1 && month <= 12 && day >= 1 && day <= 31 && (isLeapYear(year) || month != 2 || day <= 28);
    }
    public static void nextDate(int year,int month,int day)  //求输入日期的下一天
    {
        switch (month) {
            case 1, 3, 5, 7, 8, 10 -> {
                if (day == 31)
                    System.out.println("Next date is:" + year + "-" + (month + 1) + "-1");
                else
                    System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
            }
            case 2 -> {
                if (isLeapYear(year)) {
                    if (day == 29)
                        System.out.println("Next date is:" + year + "-" + (month + 1) + "-1");
                    else
                        System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
                } else {
                    if (day == 28)
                        System.out.println("Next date is:" + year + "-" + (month + 1) + "-1");
                    else
                        System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
                }
            }
            case 4, 6, 9, 11 -> {
                if (day == 30)
                    System.out.println("Next date is:" + year + "-" + (month + 1) + "-1");
                else
                    System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
            }
            case 12 -> {
                if (day == 31)
                    System.out.println("Next date is:" + (year + 1) + "-1-1");
                else
                    System.out.println("Next date is:" + (year + 1) + "-" + month + "-" + (day + 1));
            }
        }
    }
}

 

  题目的难度还算简单,但是在日期的情况的考虑过程中耗费了我不少的心思,因为闰年与平年以及二月还有跨年的存在,而我在这里使用的是switch的不断讨论,使我所用的时间大量的增长(要是先写第三题就好了),而这题是我第一次在java中使用多个方法,我发现对于方法的调用实际上与C是相差无几的(C中叫做子函数),与此同时请允许我吐槽一句:idea真是太强了各种帮忙纠错以及改进代码使其更加的简洁,我从中了解了不少新的语句比如:(最开始我写的是if else样式的,被idea简化了)

return year >= 1820 && year <= 2020 && month >= 1 && month <= 12 && day >= 1 && day <= 31 && (isLeapYear(year) || month != 2 || day <= 28);

  题目集二:7-5
 1 import java.util.Scanner;
 2 
 3 public class Main
 4 {
 5     public static void main(String[] args)
 6     {
 7         Scanner input = new Scanner(System.in);
 8         int year = input.nextInt();
 9         int month = input.nextInt();
10         int day = input.nextInt();
11         int n = input.nextInt();
12         if(!checkInputValidity(year, month, day, n))
13         {
14             System.out.println("Wrong Format");
15             System.exit(0);
16         }
17         int[] month_day = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
18         int t;
19         if (isLeapYear(year)) month_day[1] = 29;
20         if(n>=0){
21             if(month==1&&day<=n){
22                 year--;
23                 month=12;
24                 if (isLeapYear(year)) month_day[1] = 29;
25                 else month_day[1]=28;
26                 day=day+month_day[11]-n;
27             }
28             else if(day<=n){
29                 if (isLeapYear(year)) month_day[1] = 29;
30                 else month_day[1]=28;
31                 month--;
32                 day=day+month_day[month-1]-n;
33             }
34             else {
35                 day=day-n;
36             }
37         }
38         else {
39             int a=n;
40             n*=-1;
41             if(month==12&&month_day[11]-day<n){
42                 year++;
43                 month=1;
44                 day=-month_day[11]+day+n;
45             }
46             else if(month_day[month-1]-day<n){
47                 t=month_day[month-1]-day;
48                 n-=t;
49                 day=n;
50                 month++;
51             }
52             else {
53                 day=day+n;
54             }
55             n=a;
56         }
57         System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
58     
59         System.exit(0);
60     }
61 
62     public static boolean isLeapYear(int year)
63     {
64         return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
65     }
66     public static boolean checkInputValidity(int year,int month,int day,int n)
67     {
68         return year >= 1820 && year <= 2020 && month >= 1 && month <= 12 && day >= 1 && day <= 31 && (isLeapYear(year) || month != 2 || day <= 28)&&n < 11&&n > -11;
69     }
70     
71 }

  题目的难度与前一题相比有一定的提升,因为我一开始的时候一直在用7-4的switch堆叠进行不断的讨论,最后我发现实在计算量太大,被迫放弃。但是在我思考了很久题目给我们的month数组的存在意义的时候我灵稽一动()的发现我使用switch实在是太蠢了,于是改成了代码中的使用month数组进行定性讨论的方法。通过对于month数组的不断提取以及判定,同时巧妙的引用其他的变量,极大的减少了我的工作量。

  题目集三:7-2

 

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4    private int year = 0;
  5    private int month = 0;
  6    private int day = 0;
  7    int[] mon_maxnum = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
  8 
  9     public static void main(String[] args)
 10     {
 11         Scanner input = new Scanner(System.in);
 12         Main daily = new Main();
 13         daily.setYear(input.nextInt());
 14         daily.setMonth(input.nextInt());
 15         daily.setDay(input.nextInt());
 16         if(!checkInputValidity(daily.getYear(), daily.getMonth(), daily.getDay()))
 17         {
 18             System.out.println("Date Format is Wrong");
 19             System.exit(0);
 20         }
 21         getNextDate(daily.getYear(), daily.getMonth(), daily.getDay());
 22     }
 23 
 24     public int getDay() {
 25         return day;
 26     }
 27 
 28     public void setDay(int day) {
 29         this.day = day;
 30     }
 31 
 32     public int getYear() {
 33         return year;
 34     }
 35 
 36     public void setYear(int year) {
 37         this.year = year;
 38     }
 39 
 40     public int getMonth() {
 41         return month;
 42     }
 43 
 44     public void setMonth(int month) {
 45         this.month = month;
 46     }
 47 
 48     public int[] getMon_maxnum() {
 49         return mon_maxnum;
 50     }
 51 
 52     public void setMon_maxnum(int[] mon_maxnum) {
 53         this.mon_maxnum = mon_maxnum;
 54     }
 55     public static boolean isLeapYear(int year)  //判断闰年
 56     {
 57         return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
 58     }
 59     public static boolean checkInputValidity(int year,int month,int day)//判断输入日期是否合法,返回布尔值
 60     {
 61         if(year < 1900||year > 2000||month < 1||month > 12||day < 1||day > 31)
 62             return false;
 63         return (!isLeapYear(year) || month != 2 || day <= 29) && (isLeapYear(year) || month != 2 || day <= 28);
 64     }
 65     public static void getNextDate(int year,int month,int day)  //求输入日期的下一天
 66     {
 67         switch (month) {
 68             case 1:
 69             case 3:
 70             case 5:
 71             case 7:
 72             case 8:
 73             case 10: {
 74                 if (day == 31)
 75                     System.out.println("Next day is:" + year + "-" + (month + 1) + "-1");
 76                 else
 77                     System.out.println("Next day is:" + year + "-" + month + "-" + (day + 1));
 78             }break;
 79             case 2 : {
 80                 if (isLeapYear(year)) {
 81                     if (day == 29)
 82                         System.out.println("Next day is:" + year + "-" + (month + 1) + "-1");
 83                     else
 84                         System.out.println("Next day is:" + year + "-" + month + "-" + (day + 1));
 85                 } else {
 86                     if (day == 28)
 87                         System.out.println("Next day is:" + year + "-" + (month + 1) + "-1");
 88                     else
 89                         System.out.println("Next day is:" + year + "-" + month + "-" + (day + 1));
 90                 }
 91             }break;
 92             case 4:
 93             case 6:
 94             case 9:
 95             case 11: {
 96                 if (day == 30)
 97                     System.out.println("Next day is:" + year + "-" + (month + 1) + "-1");
 98                 else
 99                     System.out.println("Next day is:" + year + "-" + month + "-" + (day + 1));
100             }break;
101             case 12 : {
102                 if (day == 31)
103                     System.out.println("Next day is:" + (year + 1) + "-1-1");
104                 else
105                     System.out.println("Next day is:" + year + "-" + month + "-" + (day + 1));
106             }break;
107         }
108     }
109 }
  这题实际上就是题目集二7-3的翻版,主要考察了我们对于私有属性的运用,以及set与get方法的使用。在一开始接触私有属性的时候我确实没有搞懂
set与get的实际使用方法,但在看了下idea对其的解释后基本了解了其作用。

  题目集三:7-3
import java.math.BigInteger;
import java.util.Scanner;
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 come = input.nextLine();
        String cleaned = clean(come);
        if(equal(cleaned))
            change(cleaned);
        else
        {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
    public static String clean(String come)  //清除存在的空格
    {
        String regEx=" ";
        Pattern p1 = Pattern.compile(regEx);
        Matcher m1 = p1.matcher(come);
        come = m1.replaceAll( "" );
        return come;
    }


    public static boolean equal(String str)  //判断每个段的存在性
    {
        if (str.matches("[+\\-]?"))
            return true;
        else if (str.matches("[\\s]*([1-9][\\d]*[\\s]*\\*[\\s]*x[\\s]*\\^[\\s]*([+\\-]|[\\s]*)[1-9][\\d]*[\\s]*)"))
            return true;
        else if (str.matches("[\\s]*([1-9][\\d]*[\\s]*\\*[\\s]*x[\\s]*)"))
            return true;
        else if (str.matches("[\\s]*([\\s]*x[\\s]*\\^[\\s]*([+\\-]|[\\s]*)[1-9][\\d]*[\\s]*)"))
            return true;
        else if (str.matches("[\\s]*(x[\\s]*)"))
            return true;
        else return str.matches("[\\s]*([1-9][\\d]*)");
    }


    public static void change(String cleaned)
    {
        StringBuilder empty = new StringBuilder();
        Pattern pattern = Pattern.compile("([+\\-]?[1-9][\\d]*\\*x\\^[+\\-]?[1-9][\\d]*)|([+\\-]?[1-9][\\d]*\\*x)|([+\\-]?x\\^[+\\-]?[1-9][\\d]*)|([+\\-]?x)|([+\\-]?[1-9][\\d]*)");
        Matcher matcher = pattern.matcher(cleaned);
        BigInteger str1,str2;
        StringBuilder cleanedBuilder = new StringBuilder(cleaned);
        while (matcher.find())
        {
            String getIt = cleanedBuilder.substring(matcher.start(),matcher.end());
            if (condition1(getIt))
            {
                str1 = BigInteger.valueOf(Long.parseLong(cleanedBuilder.toString().split("\\^")[1]));
                if (getIt.charAt(0) == '-')
                    str2 = BigInteger.valueOf(-1);
                else
                    str2 = BigInteger.valueOf(1);
                if (str2.multiply(str1).compareTo(BigInteger.valueOf(0)) > 0 && !cleanedBuilder.toString().equals(""))
                    cleanedBuilder.append("+");
                if(str1.subtract(BigInteger.valueOf(1)).compareTo(BigInteger.valueOf(1)) > 0 )
                    cleanedBuilder.append(str2.multiply(str1)).append("*x^").append(str1.subtract(BigInteger.valueOf(1)));
                else
                    cleanedBuilder.append(str2.multiply(str1)).append("*x");
            }
            if (condition2(getIt))
            {
                if (!getIt.matches("-x"))
                {
                    if (!empty.toString().equals(""))
                        empty.append("+");
                    empty.append("1");
                }
                else
                    empty.append("-1");
            }
            if (condition4(getIt))
            {
                str2= BigInteger.valueOf(Integer.parseInt(getIt.split("\\*")[0]));
                str1= BigInteger.valueOf(Integer.parseInt(getIt.split("\\^")[1]));
                if (str2.multiply(str1).compareTo(BigInteger.valueOf(0)) > 0&&!empty.toString().equals(""))
                    empty.append("+");
                empty.append(str2.multiply(str1));
                if (str1.subtract(BigInteger.valueOf(1)).compareTo(BigInteger.valueOf(1)) == 0)
                    empty.append("*x");
                else
                    empty.append("*x^").append(str1.subtract(BigInteger.valueOf(1)));
            }
        }
    }
    public static boolean condition1(String input)
    {
        if (input.matches("([+\\-])?x\\^([+\\-])?([1-9][\\d]*)"))
            return true;
        else
            return false;
    }
    public static boolean condition2(String input)
    {
        if (input.matches("([+\\-])?x"))
            return true;
        else
            return false;
    }
    public static boolean condition3(String input)
    {
        if (input.matches("([+\\-])?([1-9][\\d]*)\\*x"))
            return true;
        else
            return false;
    }
    public static boolean condition4(String input)
    {
        if (input.matches("([+\\-])?(([1-9][\\d]*)\\*x\\^([+\\-])?([1-9][\\d]*))"))
            return true;
        else
            return false;
    }
}
  这题是我感觉既遗憾又不遗憾的一题,在我思考这题的解法的3天内,我思考了不少理论上可行的方法,包括:1.强行通过split(+-)拆分出各种字符串
然后一个一个的进行检测与运算然后再重组,但是这种因为难以检测错误输入而且实在太复杂被丢垃圾桶。2.将输入的拆分成一个一个字符并且重组为形式符
合要求的然后再计算,但是因为重组的过程态复制而丢垃圾桶。最后我选择了我先在所展示的这种方法,拆分出 +- 前后的字符,然后通过四种正确的形式进
行判断,然后再单独的讨论以及重组。其中运用到了
import java.util.regex.Matcher; import java.util.regex.Pattern; import java.math.BigInteger;这三个库,matcher与pattern都是通过正则表达式来进行大量的判断。compile(String regex) 将正则表达式编译到Pattern中,该方法是个静态方法pattern()
返回正则表达式的字符串形式,其实就是返回Pattern.complile(String regex)的regex参数。在我大致的了解到他们的使用方法,并且与同学进行了深入而深刻
的交流后我便用一天的时间完成了上述的代码,但是其中计算的部分并不完善,而我却无法通过调试来检查到我的错误,说明我的代码中肯定存在某些我现有的
知识无法理解的错误。这便是我最大的遗憾,但是我不遗憾的是我尽了最大的努力,并且学会了不少课外的姿势。(结论:菜得真实
  

3、踩坑心得
  1.java拥有巨量而强大的库函数而我并没有去大量的了解,这使得我不少的时候走了弯路,需要我多多通过查询资料以及询问身边的大佬来了解我的错误。

 

 

 

 

 


  2.我的代码有些时候还是不能跳出C的简单思维,比如喜欢将所有的方法与类写在一段代码中最后形成了一段极其长的代码,在我询问了老师后了解到我应
该要学会多分类并通过类之间的调用,能大大提高可读性。
  3.写代码的使用经常顾此失彼,比如下图的讨论的过程中,我太极限的想要降低自己的内存占用,导致可读性很低

 

 

在多种情况的讨论时我也经常会少考虑不少东西,下图的二月的部分就花了我不少的时间进行调试,最后的结果都是我自己没有考虑完美

 

 

4、改进建议
  
针对题目一:7-8下图的代码实际上可以通过控制条件以及通过偏向C的代码来进行改进可读性

 

 

 

 

 

题目集二:7-4、7-5如果可以使用java中强大的日期类库的话可以用更少的代码完成功能并且做到大区间的大量计算而且性价比比我的本身的多重判断更加高。
题目集三:7-2可以增加一个类来专门来对日期进行计算与计数,而有益于增加其他的功能。
题目集三:7-3我应该可以通过简化我的main来增加可读性,并且通过增加更多的condition来实现更加复杂的多项式求导。

5、总结
通过三次题目集的综合性学习,我对java的基本操作以及面向对象的思想有了基本的理解,在日后的学习中我应该加强我对java库函数的了解范围,使自己的
工作效率得到提高,同时跟随老师的步伐一步一个脚印的深入学习java的多方面知识协调共进,查漏补缺。
同时我希望老师能适当的放宽作业的提交期限,因为有些时候时间实在是不够。对于老师上课所讲的我都感觉非常的深入而深刻,对我学习java,查漏补缺方面
都有很大的作用,同时我希望老师实验课的时候能多观察我们编程时碰到的难点,然后有针对性的提出建设性的建议。
posted @ 2021-04-04 22:03  peaceful_light  阅读(100)  评论(0)    收藏  举报