博客总结作业一

前言:

第一次题目集的量比较多,但是难度较易,再发布当天就完成了。第二次题目集难度增大了很多,第一与第三题难度不大,但是第二题难度增大了很多,题目很难看懂,当理解完题目大意后,还是比较难下手,思考了很久后才有了大概的思路。第三次题目集难度最大,除了第一题都很麻烦,做起来比较吃力,用了整整两天才勉强写完。

第一次题目集考验了我们对于Java基础的数组,字符串,循环,函数,

第二次题目集加强了我们对于字符串的使用与理解。

第三次题目集让我认识到了类以及聚合的关系,让本来不知道这两个是什么东西的我理解了类与聚合的概念以及用法。

设计与分析:

题目集一:

心得:本次题目集难度容易,虽然有九道题,但都是考验我们最基础的写Java代码的能力,加深了我对于数组,函数,字符串,循环,判断等的理解与使用,并且让我们对于写Java代码的熟练度加强了很多。

题目集二 7-2:

题目:RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。

 

输入格式:

 

由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111

 

输出格式:

 

过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data",
若某个数据的结束符不为1,则输出“validate error”。
若某个数据奇偶校验错误,则输出“parity check error”。
若数据结束符和奇偶校验均不合格,输出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error

 

 

 

 

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in=new Scanner(System.in);
        String a=in.nextLine();
        int i;
        int b;
        int num=0;
        int c=0;
        int d;
        int ch;
        if(a.length()<11)//判断是否符合基本要求
               {
            System.out.println("null data");
        }
        else {
            for(i=0;i<a.length();i++) {
                if(a.charAt(i)=='0'&&(a.length()-i)<11) {
                    System.out.println("null data");
                    break;
                }
                if(a.charAt(i)=='1'&&(a.length()-i)==1) {
                    System.out.println("null data");
                    break;
                }
                
            }
            for(i=0;i<a.length();i++) {
                if(a.charAt(i)=='0'&&(a.length()-i)>=11) {
                    for(b=i+1;b<i+9;b++) {
                        if(a.charAt(b)=='1') {
                            num=num+1;
                        }
                    }
                    if((num%2!=0&&a.charAt(b)=='0'&&a.charAt(b+1)=='1')||(num%2==0&&a.charAt(b)=='1'&&a.charAt(b+1)=='1')) {
                        c++;
                        System.out.print(c+":");
                        for(d=i+1;d<b+1;d++) {
                            if(d==b)
                                System.out.printf("\n");
                            else {
                                System.out.print(a.charAt(d));
                            }
                        }
                    }
                    i=b+1;
                    if((num%2!=0&&a.charAt(b)=='1'&&a.charAt(b+1)=='1')||(num%2==0&&a.charAt(b)=='0'&&a.charAt(b+1)=='1')) {
                        c++;
                        System.out.println(c+":"+"parity check error");
                    }
                    if(a.charAt(b+1)!='1') {
                        c++;
                        System.out.println(c+":"+"validate error");
                    }
                    num=0;
                }
            }
        }
    }

}

心得本题十分考验我们对于java字符串的理解以及灵活运用,掌握了如何通过字符串来解决题目的能力。本题的题目真的挺难理解的,第一次看题目的时候真的不理解这个题是要我们做什么。但当静下心来的时候,才发现题目的意思,我认为看题目必须要冷静,越急躁就越不利于写代码。写完这题后我对于Java字符串的运用熟练了很多,并且也学到了一些规范代码的格式。并且在写题过程中我发现误差范围也是我们应该考虑到的,要不然根本过不了测试点。

题目集三 7-1:

题目:定义一个代表一元二次方程ax2+bx+c=0的类QuadraticEquation,其属性为三个系数a、b、c(均为私有属性),类中定义的方法参考main方法中的代码。

 

输入格式:

 

在一行中输入a、b、c的值,可以用一个或多个空格或回车符分开。

 

输出格式:

 

  1. 当输入非法时,输出“Wrong Format”
  2. 当有一个实根时,输出(2行):

 

  • a=值,b=值,c=值:
  • The root is 值(保留两位小数)

 

  1. 当有两个实根时,输出(2行):

 

  • a=值,b=值,c=值:
  • The roots are 值1 and 值2(均保留两位小数)

 

 

 

import java.util.Scanner;

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

        double a = Double.parseDouble(input.next());
        double b = Double.parseDouble(input.next());
        double c = Double.parseDouble(input.next());
        
        if(a == 0){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        
        //create a QuadraticEquation object
        QuadraticEquation equation = new QuadraticEquation(a, b, c);
        //get value of b * b - 4 * a * c
        double discriminant = equation.getDiscriminant();
        
        System.out.println("a=" + equation.getA() +
                ",b=" + equation.getB() + 
                ",c=" + equation.getC()+":");

        if (discriminant < 0) {
          System.out.println("The equation has no roots.");
        }
        else if (discriminant == 0)
        {
          System.out.println("The root is " + 
                  String.format("%.2f", equation.getRoot1()));
        }
        else // (discriminant >= 0)
        {
          System.out.println("The roots are " + 
                  String.format("%.2f", equation.getRoot1()) 
            + " and " +  String.format("%.2f", equation.getRoot2()));
        }
    }
}

class QuadraticEquation{
        //your code
    private double a;
    private double b;
    private double c;
    
    public QuadraticEquation(double a,double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    
    public double getA() {
        return a;
    }
    public double getB() {
        return b;
    }
    public double getC(){
        return c;
    }
    
    public double getDiscriminant() {
        double discriminant = b*b-(4*a*c);
        return discriminant;
    }
    
    public double getRoot1() {
        if(getDiscriminant()>=0) {
            double r1;
            r1 = (-b+Math.sqrt(getDiscriminant()))/(2*a);
            return r1;
        }
        else {
            return 0;
        }
    }
    public double getRoot2() {
        if(getDiscriminant()>=0) {
            double r2;
            r2 = (-b-Math.sqrt(getDiscriminant()))/(2*a);
            return r2;
        }
        else {
            return 0;
        }
    }
        
}

心得本题是题目集三中最简单的题目,可以说是一个开胃菜,让我知道了类之间是如何调用的,这也为后面的题目打了基础,要是没有这道题,我后面的题目都无法动手。这道题的主函数可以说是很巧妙,虽然对于我来说有点难,但是给了我一个很好的学习样例。

题目集三 7-2:

题目:

参考题目集二中和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:

 

public boolean checkInputValidity();//检测输入的年、月、日是否合法
public boolean isLeapYear(int year);//判断year是否为闰年
public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期
public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期
public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等
public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数
public String showDate();//以“year-month-day”格式返回日期值

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

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() + "-" + date.getMonth() + "-" + date.getDay() + " 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() + "-" + date.getMonth() + "-" + date.getDay() + " 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 DateUtil{
    private int year;
    private int month;
    private int day;
    
    public DateUtil(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    
    public boolean checkInputValidity() {
         boolean checkInputValidity;
         int[] a=new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};
         if(!isLeapYear(year))
             a[2]=28;
         checkInputValidity=(year<=2020&&year>=1820&&month<=12&&month>=1&&day>0&&day<=a[month]);
         return checkInputValidity;
    }
    
    public boolean isLeapYear(int year) {
        boolean isLeapYear;
        isLeapYear=(year%4==0&&year%100!=0)||(year%400==0);
        return isLeapYear;
    }
    
    public int getYear() {
        return year;
    }
    public int getMonth() {
        return month;
    }
    public int getDay() {
        return day;
    }
    
    public DateUtil getNextNDays(int n) {
          int i;
          int[] b=new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};
          if(!isLeapYear(year))
            b[2]=28;
          if(checkInputValidity()){
           for(i=0;i<n;i++) {
               day++;
               if(day>b[month]) {
                   month++;
                   day=1;
               }
               if(month>12) {
                   year++;
                   if(!isLeapYear(year)) b[2]=28;
                   else b[2]=29;
                   month=1;
               }
           }
          }
          DateUtil da =new DateUtil(year,month,day);
          return da;
     }
    
    public DateUtil getPreviousNDays(int n) {
          int num;
          int i;
          int[] b=new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};
        if(!isLeapYear(year))
            b[2]=28;
          for(i=n;i>0;i--) {
              day--;
              if(day<1) {
                  month--;
                  if(month<1) {
                  year--;
                 if(!isLeapYear(year)) b[2]=28;
                   else b[2]=29;
                  month=12;
              }
                  day=b[month];
              }
              
          }
            DateUtil da =new DateUtil(year,month,day);
            return da;
    }
    
    public boolean compareDates(DateUtil date) {
        boolean f = false;
        if(date.getYear()<year) {
            f = true;
        }
        else if(date.getYear()==year) {
            if(date.getMonth()<month) {
                f = true;
            }
            else if(date.getMonth() == month) {
                if(date.getDay()<day) {
                    f = true;
                }
                else f = false;
            }
            else f = false;
        }
        else f = false;
        return f;
    }
    
    public boolean equalTwoDates(DateUtil date) {
        boolean f = false;
        if(date.getYear()==year&&date.getMonth()==month&&date.getDay()==day) {
            f = true;
        }
        return f;
    }
    
    public int getDaysofDates(DateUtil date) {
        int num=0;
        int i,a=0;
        int sum=0;
        if(compareDates(date)==true) {
             for(i=date.getYear();i<year;i++) {
             if(isLeapYear(i))
             {
                num=num+366;
             }
             else num=num+365;
             }
             for(i=1;i<month;i++)
             {
                   if(i==4||i==6||i==9||i==11)
                       num=num+30;
                   else if(isLeapYear(year)==false&&i==2)
                       num=num+28;
                   else if(isLeapYear(year)&&i==2)
                       num=num+29;
                   else num=num+31;
             }
             for(i=1;i<date.month;i++)
             {
                   if(i==4||i==6||i==9||i==11)
                        a=a+30;
                   else if(isLeapYear(year)==false&&i==2)
                        a=a+28;
                   else if(isLeapYear(year)&&i==2)
                        a=a+29;
                   else a=a+31;
             }
             sum = num+day-a-date.getDay();
        }
        if(compareDates(date)==false) {
             for(i=year;i<date.getYear();i++) {
                 if(isLeapYear(i))
                 {
                    num=num+366;
                 }
                 else num=num+365;
                 }
                 for(i=1;i<date.month;i++)
                 {
                       if(i==4||i==6||i==9||i==11)
                           num=num+30;
                       else if(isLeapYear(date.year)==false&&i==2)
                           num=num+28;
                       else if(isLeapYear(date.year)&&i==2)
                           num=num+29;
                       else num=num+31;
                 }
                 for(i=1;i<month;i++)
                 {
                       if(i==4||i==6||i==9||i==11)
                            a=a+30;
                       else if(isLeapYear(year)==false&&i==2)
                            a=a+28;
                       else if(isLeapYear(year)&&i==2)
                            a=a+29;
                       else a=a+31;
                 }
                 sum = num+date.getDay()-a-day;
        }
        return sum;
    }
    
    public String showDate() {
        String a = "";
        String b = "";
        String c = "";
        a = ""+this.year;
        b = ""+this.month;
        c = ""+this.day;
        String s = a+"-"+b+"-"+c;
        return s;
    }
}

心得:这道题是我学习Java以来第一次写超过两百行的代码,起初看到题目时我很开心,因为是之前写过的,然后我在类的基础上将前面题目集的代码一点点复制过来,并且加以修改。在自信满满提交的时候,评测结果吓了我一跳,分数很低,然后我发现算前n天以及后n天的算法在这道题并不适用,然后我就重新想算法修修改改几个小时才得到了满分。我意识到写代码不能投机取巧,题目要看清,要不然一股脑写完以后发现错了再去改的时候就会花费更多时间。

题目集三 7-3:

题目:参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] 。

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

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.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.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(fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }        
    }
}

class Day {
    private int value;

    public Day() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Day(int value) {
        super();
        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 Month{
    private int value;

    public Month() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Month(int value) {
        super();
        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() {
        if(value<1||value>12)
            return false;
        else return true;
    }
    
    public void monthIncrement() {
        value++;
    }
    
    public void monthReduction() {
        value--;
    }
    
}

class Year{
    private int value;


    public Year(int value) {
        super();
        this.value = value;
    }

    public Year() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public int getValue() {
        return value;
    }

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

class DateUtil{
    private Year year = new Year();
    private Month month= new Month();
    private Day day = new Day();
    private int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
    public DateUtil() {
        super();
        // TODO 自动生成的构造函数存根
    }
    public DateUtil(int i, int j, int k) {
        super();
        this.year.setValue(i);
        this.month.setValue(j);;
        this.day.setValue(k);
        
    }
    public Year getYear() {
        return year;
    }
    public void setYear(Year year) {
        this.year = year;
    }
    public Month getMonth() {
        return month;
    }
    public void setMonth(Month month) {
        this.month = month;
    }
    public Day getDay() {
        return day;
    }
    public void setDay(Day day) {
        this.day = day;
    }
    
    public boolean checkInputValidity() {
        if(year.validate()==false)
            return false;
        else if(month.validate()==false)
            return false;
        else {
            if(day.getValue()>mon_maxnum[month.getValue()-1])
                return false;
            else if(!year.isLeapYear()&&month.getValue()==2&&day.getValue()==29) {
                return false;
            }
        }
        return true;
    }
    
    public DateUtil getNextNDays(int n) {
          int i;
          int[] b=new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};
          if(!year.isLeapYear())
            b[2]=28;
          if(checkInputValidity()){
           for(i=0;i<n;i++) {
               day.dayIncrement();
               if(day.getValue()>b[month.getValue()]) {
                   month.monthIncrement();
                   day.setValue(1);
               }
               if(month.getValue()>12) {
                   year.yearIncrement();
                 if(!year.isLeapYear()) b[2]=28;
                 else b[2]=29;
                   month.resetMin();
               }
           }
          }
          DateUtil da =new DateUtil(year.getValue(),month.getValue(),day.getValue());
          return da;
   }
    
    public DateUtil getPreviousNDays(int n) {
          int i;
          int[] b=new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};
      if(!year.isLeapYear())
            b[2]=28;
          for(i=n;i>0;i--) {
              day.dayReduction();
              if(day.getValue()<1) {
                  month.monthReduction();
                if(month.getValue()<1) {
                  year.yearReduction();
               if(!year.isLeapYear()) b[2]=28;
                 else b[2]=29;
                  month.resetMax();
              }
                  day.setValue(b[month.getValue()]);
              }
              
          }
            DateUtil da =new DateUtil(year.getValue(),month.getValue(),day.getValue());
            return da;
    }
    
    public boolean compareDates(DateUtil date) {
        boolean f = false;
        if(date.year.getValue()<year.getValue()) {
            f = true;
        }
        else if(date.month.getValue()==year.getValue()) {
            if(date.month.getValue()<month.getValue()) {
                f = true;
            }
            else if(date.month.getValue() == month.getValue()) {
                if(date.day.getValue()<day.getValue()) {
                    f = true;
                }
                else f = false;
            }
            else f = false;
        }
        else f = false;
        return f;
    }
    
    public boolean equalTwoDates(DateUtil date) {
        boolean f = false;
        if(date.year.getValue()==year.getValue()&&date.month.getValue()==month.getValue()&&date.day.getValue()==day.getValue()) {
            f = true;
        }
        return f;
    }
    
    public int getDaysofDates(DateUtil date) {
        int num=0;
        int i,a=0;
        int sum=0;
        if(compareDates(date)==true) {
             for(i=date.year.getValue();i<year.getValue();i++) {
             if((i%4==0&&i%100!=0)||i%400==0)
             {
                num=num+366;
             }
             else num=num+365;
             }
             for(int j=1;j<month.getValue();j++)
             {
                   if(j==4||j==6||j==9||j==11)
                       num=num+30;
                   else if(i%4!=0&&j==2)
                       num=num+28;
                   else if(((i%4==0&&i%100!=0)||i%400==0)&&j==2)
                       num=num+29;
                   else num=num+31;
             }
             for(int j=1;j<date.month.getValue();j++)
             {
                   if(j==4||j==6||j==9||j==11)
                        a=a+30;
                   else if(i%4!=0&&j==2)
                        a=a+28;
                   else if((i%4==0&&i%100!=0)||i%400==0&&j==2)
                        a=a+29;
                   else a=a+31;
             }
             sum = num+day.getValue()-a-date.day.getValue();
        }
        if(compareDates(date)==false) {
             for(i=year.getValue();i<date.year.getValue();i++) {
                 if((i%4==0&&i%100!=0)||i%400==0)
                 {
                    num=num+366;
                 }
                 else num=num+365;
                 }
             for(int j=1;j<date.month.getValue();j++)
             {
                   if(j==4||j==6||j==9||j==11)
                       num=num+30;
                   else if(i%4!=0&&j==2)
                       num=num+28;
                   else if(((i%4==0&&i%100!=0)||i%400==0)&&j==2)
                       num=num+29;
                   else num=num+31;
             }
             for(int j=1;j<month.getValue();j++)
             {
                   if(j==4||j==6||j==9||j==11)
                        a=a+30;
                   else if(i%4!=0&&j==2)
                        a=a+28;
                   else if((i%4==0&&i%100!=0)||i%400==0&&j==2)
                        a=a+29;
                   else a=a+31;
             }
                 sum = num+date.day.getValue()-a-day.getValue();
        }
        return sum;
    }
    
    public String showDate() {
        String a = "";
        String b = "";
        String c = "";
        a = ""+year.getValue();
        b = ""+month.getValue();
        c = ""+day.getValue();
        String s = a+"-"+b+"-"+c;
        return s;
    }

}

心得:这道题真的让我痛不欲生,第一次写这么多行的代码,由于我对聚合不是很了解,所以写这道题之前先学习了一下聚合的知识。不过这道题已经给了我们类图,我们照着类图写就行。在写这道题的时候我发现聚合真的是个好东西,在主方法中可以调用其他类的方法,这样就不用在主函数里面写一堆方法,看着也清爽很多。这道题真的让我体验到了Java的难度,我也想到了后面的题目集难度会越来越大。

题目集三 7-4:

题目:参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] 

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

心得:这道题与第三题很相似,但是我认为这道题的聚合比第三题更加合理,也更容易写一点,每个类之间的联系更加合理。年,月,日都和主函数有关系,在调用的时候更加好用,我认为这是老师在出题时特意这么安排的,先写不那么合理的聚合关系,再来一个很合理的聚合,这样有助于我们更好理解聚合,也会让我们在以后写代码前去认真考虑怎么可以更合理地去写。

 踩坑心得:

题目集一:

 

心得:由于双精度浮点数计算是有误差,因此将边界值范围增大

 

心得:把double类型强制转换为float类型,因为double类型在计算时有误差。

心得:未控制非法输入格式,加上控制输入的判断。

题目集二:

7-1:

心得:又是未考虑非法输入的情况,加上控制非法输入的格式。

7-2:

 

心得:输入三种数据的情况都出现了问题,因此重新修改了判断的格式,这道题的情况还是有点多的,因此我在草稿纸上将这些情况写了下来,然后根据问题优化了算法,才完成了这道题。

题目集三:

7-2:

 

心得前n天的两个测试点都错了,我将测试用例进行测试发现求前n天的方法中未对闰年进行判断,才导致错误,因此在求前n天的方法中加上了判断是否是闰年,这样就完成了修改。

7-3:

 

心得:天数差的两个测试点都出现了错误,我当时十分疑惑,因为我的算法跟第二题是一样的,第二题都测试过了,按理来说不应该错。然后我仔细一想发现未判断两个日期的大小,加上判断后对了一个测试点,最小值与最大值还是有错误,于是我又进行了优化才全对。

总体心得:

这三次题目集的难度是逐级递增的,我发现无论难易,我最容易踩的坑就是审题不清,不审完题就开始做,这样的作法是不对的,只会事倍功半。其次,double类型的精度,计算时会有误差,这个坑真的是我必踩。还有就是格式错误的判断,每次都注意不到,提交后才发现错误。还有就是写代码时最好一气呵成,不能中断,当思路一断再来写的时候就会很乱,甚至不知道在写什么。还有就是我发现现在pta不对我们开放测试点,有时候自己输入数据测试时根本找不到错误,因此也锻炼了我们的能力。

 

 

 

改进建议:前三个题目集各有各的特点,在写代码时我觉得应该要注意代码的格式,听老师的建议,把代码写规范。并且写代码时专心一点,一气呵成。对于题目集一的九道题来说,我认为我是按c语言的逻辑去写的,只是用上了Java的语法而已,这是我应该改进的。题目集二的7-2,我认为我判断输入合不合法可以更加简略,精简,并且不分那么多情况,可以写成一个类,在判断时直接调用就好了。题目集三7-2,我觉得判断前n天以及后n天的算法可以优化,并且有很多未调用的参数在其中,这就是老师所说的“垃圾代码”,我认为我要改进这点,减少无用的代码,并且做到低耦合性。对于题目集三的7-3,我认为自己已经尽了最大能力去写正确,但是我认为我还是c语言的思路去写,这点是我最应该改进的。题目集三的7-4,聚合关系是我应该学习的,当我们有个清楚的类图,再去写代码就会事半功倍。其次我认为我应该改正对于作业应付的问题,写慢一点也没事,但是要从中学到东西,才能发挥pta的作用。

总结:

对于本阶段的三次题目集,我认为我清楚学会了Java的判断、循环、数组、函数、字符串等知识点的运用,以及类与类间的关系,聚合的理解。我认为类以及聚合是我接下来最该学习的,并且我要规范写代码的格式,eclipse这个编译器与devc++区别还是挺大的,它可以在我们写代码的时候及时提醒我们有何错误,这样对于我这种粗心的人是很好的,因为如果少了个}之类的问题,要去找是很难的。其次我认为我要加大对于Java的重视程度,不能像之前学c语言一样随便应付,Java还真得认真学,毕竟难度比c语言大很多。对于段老师,最开始对他的印象就是严厉,认真。开始上他的课的时候,我真的有点怕,因为感觉他很严,并且听他说完后,我感觉自己还没学就要挂科了,但随着时间推移,我觉得他真是一个好老师,是为了我们能学到东西的。对于Java这们课程,我认为挺难的,因为对于我来说,对计算机是不感兴趣的,所以我认为我学软件是挺吃力的。但是硬着头皮也得学,没有多少个人是学自己喜欢的专业的,所以唯有坚持了。对于作业,我觉得第三次是挺难的,我写起来比较吃力,所以我希望后面的作业可以简单一点,要不然有点打击我的自信心。对于实验,我觉得可以稍微放宽一点,就是这周三个东西加到一起压力是挺大的,这几天几乎都是全天面对着电脑。我希望后面的任务不要堆在一起,给我们时间宽松一点,这两周真的是很累,毕竟还有三门数学作业,这个学期真的挺难的,还是希望老师最好不要在一周内同时搞pta,实验和blog,有点压得喘不过气。对于课程,我认为挺好的,线上与线下相结合,并且老师也上心,所以这个学期遇到了一个好的专业课老师。我也希望在后面的Java学习过程中有进步,努力学到东西,而不是应付。

posted @ 2022-04-10 17:34  HanHan-30  阅读(47)  评论(0)    收藏  举报