OOP第一次博客总结

发布时间 2023-03-26 21:24:09作者: ts13

目录

  1、前言

  2、设计与分析

  3、踩坑心得

  4、改进建议

  5、总结

 

题目集1:

1、计算年利率

2、身体质量指数测算

3、九九乘法表(双循环)

4、快递运费

5、去掉重复字符

6、统计一个子串在整串中出现的次数

7、有重复数据

8、从一个字符串中移除包含在另一个字符串中的字符

9、Prime Numbers

10、GPS数据处理

11、求定积分

12、列出最简真分数序列*

题目集2

1、长度质量计算单位换算

2、奇数求和

3、房产税计算2022

4、游戏角色选择

5、学号识别

6、巴比伦法求平方根近似值

7、二进制数值提取

8、判断三角形类型

9、求下一天

题目集3

1、创建圆类型

2、创建账户类Account

3、定义日期类

4、日期类设计

 

1、前言

  这是我第一次用写Java的总结博客,也是刚接触Java这门编译语言。因为对Java的不了解,刚开始敲代码时很不适应,但Java与c语言又有部分相似,所以基础的语法这部分可以说是一点就通。尽管我接触Java还没到一个月,但是在这接近一个月的学习过程里,我能清楚的感觉到自己水平的提升,可以说是“痛并快乐着”。

  题目集1有十二题,题量挺大的,虽然在现在看来题目较为基础,但在当时却确确实实是给了刚接触java的我一个下马威,让我快速适应c语言与Java编译语言的不同。简而言之,此次作业难度不大,主要是通过这些题目来了解Java的编译语法,让我们从上学期的程序设计基础中脱离,深入到本学期的面向对象程序设计中来。

  题目集2有十题,类似于题目集一,但是题量有所减少,最后两题的难度有所提升,最后的一个判断三角形和求下一天在当时也困扰了我挺久。其中求下一天该题在第三次作业中还拓展到了求下n天以及求两天的天数差,难度在逐渐加大。

  题目集3虽然只有四题,但是他的难度是前两个题目集无法比拟的。第一次用了多个类,并且的最后一题是题目集2最后一题的迭代,难度在我看来算是接触Java以来算是最难的一类题目了,写了很久查阅了很多资料也没能在截止时间做到满分。但在我看来,与其写那种基础的题目,不如写这种有难度的题目来的有意义,通过写这种难题能更快的帮助我们掌握语法和逻辑。

 

知识点方面:题目集一让我从c语言成功过渡到Java,虽然没有掌握很多新的知识(印象最深的是在这次题目集中第一次见到了Boolean类型),但是至少明白了Java的编译规则,能够编写出没有语法错误的Java代码,同时复习了上学期所学的基础语法;题目集二是题目集一的进阶版本,但是最后两题的难度明显有所加大,也正是这个题目集发布的期间,段老师给我们介绍了Java的框架,让我认识到了Java各个部分的名称以及大部分代码的意义,让我们知道了不同的方法与类的基本使用方法;题目集三很难但是也真的让我从中学到了非常多的东西(当然这个过程是非常痛苦的),例如多个类的使用,对象数组的使用,private封装的使用等等。

2、设计与分析

判断三角形类型

题目集2第八题具体代码如下:

复制代码
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        float a,b,c;
        a=input.nextFloat();
        b=input.nextFloat();
        c=input.nextFloat();
        if (a >= 1 && a <= 200 && b >= 1 && b <= 200 && c >= 1 && c <= 200){
            if (a + b <= c || a + c <= b || b + c <= a){
                System.out.printf("Not a triangle");
            }
            else if (a == b && b == c){
                System.out.printf("Equilateral triangle");
            }
            else if ((a == b && Math.abs(a * a + b * b - c * c) < 0.0001) || (b == c &&
                    Math.abs(b * b + c * c - a * a) < 0.0001) || (a == c && Math.abs(a * a + c * c - b * b) < 0.0001)){
                System.out.printf("Isosceles right-angled triangle");
            }
            else if (a == b || a == c || b == c){
                System.out.printf("Isosceles triangle");
            }
            else if (((a * a + b * b) == c * c) || ((b * b + c * c) == a * a) || ((a * a + c * c) == b * b)){
                System.out.printf("Right-angled triangle");
            }
            else{
                System.out.printf("General triangle");
            }
        }
        else{
            System.out.printf("Wrong Format");
        }
    }
}
复制代码

 

求下一天

  

 1 import java.util.*;
 2 class Date{
 3 private int year;
 4 private int month;
 5 private int day;
 6 
 7 public Date(int year,int month,int day){
 8 this.year=year;
 9 this.month=month;
10 this.day=day;
11 }
12 
13 
14 public void setDay(int day){
15 this.day = day;
16 }
17 public int getDay(){
18 return day;
19 }
20 public void setMonth(int month){
21 this.month = month;
22 }
23 public int getMonth(){
24 return month;
25 }
26 public void setYear(int month){
27 this.year = year;
28 }
29 public int getYear(){
30 return year;
31 }
32 
33 
34 public static boolean isLeapYear(int year) {
35 boolean isLeapYear = (year % 4 == 0 && year % 100 != 0 )||year % 400 == 0;
36 return isLeapYear;
37 }
38 public static boolean checkInputValidity(int year,int month,int day) {
39 int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
40 if(isLeapYear(year))
41 a[2] = 29;
42 boolean checkInputValidity = (year>=1900 && year<=2000 && month>0 && month<=12 && day<=a[month] && day>0);
43 return checkInputValidity;
44 }
45 public static void getNextDate(int year,int month,int day) {
46 int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
47 int day1 = day;
48 int month1 = month;
49 int year1 = year;
50 if(isLeapYear(year))
51 a[2] = 29;
52 if(checkInputValidity(year,month,day)) {
53 if(month==12) {
54 if(day==a[month]) {
55 year1 = year+1;
56 month1 = 1;
57 day1 = 1;
58 }
59 else{
60 month1 = month;
61 day1 = day +1;
62 }
63 }
64 else {
65 if(day==a[month]) {
66 month1 = month + 1;
67 day1 = 1;
68 }
69 else{
70 month1 = month;
71 day1 = day+1;
72 }
73 }
74 System.out.println("Next day is:"+year1+"-"+month1+"-"+day1);
75 }
76 else
77 System.out.println("Date Format is Wrong");
78 }
79 }
80 
81 public class Main{
82 public static void main(String[] args) {
83 Scanner input=new Scanner(System.in);
84 int year = input.nextInt();
85 int month = input.nextInt();
86 int day = input.nextInt();
87 Date oop=new Date(year , month , day);
88 oop.setYear(year);
89 oop.setMonth(month);
90 oop.setDay(day);
91 oop.getNextDate(oop.getYear(), oop.getMonth(),oop.getDay());
92 }
93 }

 

  题目集三相对于三四题,一二题比较简单,题目集最后两题都是关于日期类的编写,有了前面编写代码的经验,我也对我的算法进行了一定的优化。

定义日期类

  本题吸取了上一次的经验,对我的代码进行了优化,并且涉及到的类的关系也比较简单,以下是本题的类图及代码:

复制代码
1 

 

 
复制代码
复制代码
 1 import java.util.*;
 2 public class Main{
 3     public static void main(String[] args){
 4         Scanner input = new Scanner(System.in);
 5         int year,month,day;
 6         year=input.nextInt();
 7         month=input.nextInt();
 8         day=input.nextInt();
 9         Date date = new Date(year,month,day);
10         if (!date.checkInputValidity()){
11             System.out.printf("Date Format is Wrong");
12             return;
13         }
14         date.getNextDate();
15         if (date.checkInputValidity()){
16             System.out.printf("Next day is:%d-%d-%d",date.getYear(),date.getMonth(),date.getDay());
17         }
18         else{
19             System.out.printf("Date Format is Wrong");
20         }
21     }
22 }
23 class Date{
24     private int year;
25     private int month;
26     private int day;
27     public int[] mon_maxnum = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
28     public Date(){
29 
30     }
31 
32     public Date(int year,int month,int day){
33         this.year = year;
34         this.month = month;
35         this.day = day;
36     }
37     public int getYear(){
38         return this.year;
39     }
40 
41     public void setYear(int year){
42         this.year=year;
43     }
44 
45     public int getMonth(){
46         return this.month;
47     }
48 
49     public void setMonth(int month){
50         this.month=month;
51     }
52 
53     public int getDay(){
54         return this.day;
55     }
56 
57     public void setDay(int day){
58         this.day=day;
59     }
60 
61     public boolean isLeapYear(int year){
62         if ((this.year % 4 == 0 && this.year % 100 != 0) || (year % 400 == 0)){
63             return true;
64         } else{
65             return false;
66         }
67     }
68 
69     public boolean checkInputValidity(){
70         if (isLeapYear(this.year)){
71             mon_maxnum[2] = 29;
72         }
73         if ((this.year >= 1900 && this.year <= 2000) && (this.month >= 1 && this.month <= 12) && (this.day >= 1 && this.day <= mon_maxnum[this.month])){
74             return true;
75         } else{
76             return false;
77         }
78     }
79     public void getNextDate(){
80         if (isLeapYear(this.year)){
81             mon_maxnum[2] = 29;
82         }
83         if (this.day + 1 > mon_maxnum[this.month]){
84             this.day = 1;
85             if (this.month < 12){
86                 this.month++;
87             } else{
88                 this.month = 1;
89                 this.year++;
90             }
91         } else{
92             this.day++;
93         }
94     }
95 }
复制代码

 

日期类设计

  这里现附上本题的类图:

 

复制代码

 

复制代码

 

第三次作业第四题:日期类设计。该题分为两个板块(因为求前n天和求下n天方法类似,我就把他们归于一个板块了),分别是求前n天或者是下n天以及两个日期的天数差。

下面附上我未改良过的初始源码:

 

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         int year = 0;
  7         int month = 0;
  8         int day = 0;
  9 
 10         int choice = input.nextInt();
 11 
 12         if (choice == 1) { // test getNextNDays method
 13             int m = 0;
 14             year = Integer.parseInt(input.next());
 15             month = Integer.parseInt(input.next());
 16             day = Integer.parseInt(input.next());
 17 
 18             DateUtil date = new DateUtil(year, month, day);
 19 
 20             if (!date.checkInputValidity()) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24 
 25             m = input.nextInt();
 26 
 27             if (m < 0) {
 28                 System.out.println("Wrong Format");
 29                 System.exit(0);
 30             }
 31 
 32             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
 33             System.out.println(date.getNextNDays(m).showDate());
 34         } else if (choice == 2) { // test getPreviousNDays method
 35             int n = 0;
 36             year = Integer.parseInt(input.next());
 37             month = Integer.parseInt(input.next());
 38             day = Integer.parseInt(input.next());
 39 
 40             DateUtil date = new DateUtil(year, month, day);
 41 
 42             if (!date.checkInputValidity()) {
 43                 System.out.println("Wrong Format");
 44                 System.exit(0);
 45             }
 46 
 47             n = input.nextInt();
 48 
 49             if (n < 0) {
 50                 System.out.println("Wrong Format");
 51                 System.exit(0);
 52             }
 53 
 54             System.out.print(
 55                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
 56             System.out.println(date.getPreviousNDays(n).showDate());
 57         } else if (choice == 3) {    //test getDaysofDates method
 58             year = Integer.parseInt(input.next());
 59             month = Integer.parseInt(input.next());
 60             day = Integer.parseInt(input.next());
 61 
 62             int anotherYear = Integer.parseInt(input.next());
 63             int anotherMonth = Integer.parseInt(input.next());
 64             int anotherDay = Integer.parseInt(input.next());
 65 
 66             DateUtil fromDate = new DateUtil(year, month, day);
 67             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 68 
 69             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 70                 System.out.println("The days between " + fromDate.showDate() + 
 71                         " and " + toDate.showDate() + " are:"
 72                         + fromDate.getDaysofDates(toDate));
 73             } else {
 74                 System.out.println("Wrong Format");
 75                 System.exit(0);
 76             }
 77         }
 78         else{
 79             System.out.println("Wrong Format");
 80             System.exit(0);
 81         }        
 82     }
 83 
 84     public int month;
 85     public int year;
 86     public int day;
 87 }
 88 
 89 class DateUtil{
 90     private int year;
 91     private int month;
 92     private int day;
 93     
 94     public DateUtil(int year,int month,int day){
 95         this.year = year;
 96         this.month = month;
 97         this.day = day;
 98     }
 99     public DateUtil(Main date) {
100         this(date.year, date.month, date.day);
101     }
102     
103     public void setYear(int year){
104         this.year = year;
105     }
106     public void setMonth(int month){
107         this.month = month;
108     }
109     public void setDay(int day){
110         this.day = day;
111     }
112     public int getYear(){
113         return year;
114     }
115     public int getMonth(){
116         return month;
117     }
118     public int getDay(){
119         return day;
120     }
121     
122     
123      public boolean isLeapYear(int year) {
124         boolean isLeapYear = (year % 4 == 0 && year % 100 != 0 )||year % 400 == 0;
125         return isLeapYear;
126      }
127     
128      public boolean checkInputValidity() {
129         if(this.year>=1820 && this.year<=2020 && this.month>0 && this.month<=12 && this.day<=31 && this.day>0){
130             return true;
131         }else 
132             return false;
133      }
134     
135      public  DateUtil getNextNDays(int m) {
136          int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
137          int day1 = day;
138          int month1 = month;
139          int year1 = year;
140          if(isLeapYear(year))
141              a[2] = 29;
142          if(checkInputValidity()) {
143              if(m > a[month]-day && m>0 ){
144                  m = m+day;
145                  for(;m>a[month1];){
146                      m = m-a[month1];
147                      month1 = month1 + 1;
148                      if(month1==13){
149                          year1++;
150                          month1 = 1;
151                          a[2] = 28;
152                          if(isLeapYear(year1)){
153                              a[2] = 29;
154                          }
155                      }
156                  }
157              }
158              if(m+day <= a[month]) m = m+day;
159              day1 = m;
160          }
161          return new DateUtil(year1,month1,day1);
162     }
163     
164      public  DateUtil getPreviousNDays(int n) {
165          int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
166          int day1 = day;
167          int month1 = month;
168          int year1 = year;
169          if(isLeapYear(year)) {
170              a[2] = 29;
171          }
172          if(checkInputValidity()){
173              if(day-n < 0){
174                  n = n-day;
175                  month1 = month - 1;
176                  for(;n>=a[month1];){
177                      n = n-a[month1];
178                      month1 = month1 - 1;
179                      if(month1 == 0){
180                          year1--;
181                          month1+=12;
182                          a[2] = 28;
183                          if(isLeapYear(year1)){
184                              a[2] = 29;
185                          }
186                      }
187                  }
188              }
189              day1 = a[month1] - n;
190              if(day == n){
191                  month1 = month1-1;
192                  if(month1==0){
193                      month1 = 12;
194                      year1 = year1-1;
195                  }
196                  day1 = a[month1];
197              }
198              else {
199                  day1=a[month1]-n;
200              }
201          }
202          return new DateUtil(year1,month1,day1);
203     }
204     
205     public boolean compareDates(DateUtil toDate){
206             if(this.year>toDate.year) 
207                 return true;
208             else if (this.year<toDate.year)
209                 return false;
210             else if(this.month>toDate.month) 
211                 return true;
212             else if(this.month<toDate.month) 
213                 return false;
214             else if(this.day>toDate.day) 
215                 return true;
216             else 
217                 return false;
218     }
219     
220     public boolean equalTwoDates(DateUtil toDate){
221         if(this.day==toDate.day&&this.year==toDate.year&&this.month==toDate.month){
222             return true;
223         }
224         else  return false;
225     }
226     
227     public String showDate(){
228         return this.year+"-"+this.month+"-"+this.day;
229     }
230     
231     public int getDaysofDates(DateUtil toDate)
232     {
233         if(this.equalTwoDates(toDate)){
234             return 0;
235         }
236         else {
237             int i=1;
238             if(this.year==toDate.year){
239                 if(this.month>toDate.month){
240                     i=(this.month-toDate.month-1)*28+1;
241                 }
242                 else  i=(toDate.month-this.month-1)*28+1;
243             }
244             else {
245                 if(this.year>toDate.year){
246                     i=((this.year-toDate.year-1)*365);
247                 }
248                 else  i=((toDate.year-this.year-1)*365);
249             }
250             
251             if(this.compareDates(toDate)){
252                 boolean flag=true;
253                 while (flag){
254                     if(this.getPreviousNDays(i).equalTwoDates(toDate)){
255                         flag =false;
256                         return i;
257                     }
258                     i++;
259                 }
260             }
261             else {
262                 boolean flag=true;
263                 i = 1;
264                 while (flag){
265                     if(this.getNextNDays(i).equalTwoDates(toDate)){
266                         flag =false;
267                         return i;
268                     }
269                     i++;
270                 }
271             }
272         }
273         return 0;
274     }
275 }

 

虽然该代码实现了题目要求的大部分功能,但是可以看出仍然有些问题:由于int能读取的最大值的限制,当求取下n天时,如果n为int的最大值,那么该程序就会输出奇怪且不正确的结果。在当时我是没有意识到这个问题的,看到了int最大值的测试点后我才考虑到是不是我的int进行了加法运算,导致某个数超出了int的最大值,从而导致程序出现异常。果然,我发现在getnextndays这个方法里面发现了144行的m = m + day;我就明白肯定是这里出现了错误,于是我查询了相关资料,并且向同学请教,将代码进行了修改:

 

复制代码
  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4         Scanner input = new Scanner(System.in);
  5         int year = 0;
  6         int month = 0;
  7         int day = 0;
  8 
  9         int choice = input.nextInt();
 10 
 11         if (choice == 1) { // test getNextNDays method
 12             int m = 0;
 13             year = Integer.parseInt(input.next());
 14             month = Integer.parseInt(input.next());
 15             day = Integer.parseInt(input.next());
 16 
 17             DateUtil date = new DateUtil(year, month, day);
 18 
 19             if (!date.checkInputValidity()) {
 20                 System.out.println("Wrong Format");
 21                 System.exit(0);
 22             }
 23 
 24             m = input.nextInt();
 25 
 26             if (m < 0) {
 27                 System.out.println("Wrong Format");
 28                 System.exit(0);
 29             }
 30 
 31             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
 32             System.out.println(date.getNextNDays(m).showDate());
 33         } else if (choice == 2) { // test getPreviousNDays method
 34             int n = 0;
 35             year = Integer.parseInt(input.next());
 36             month = Integer.parseInt(input.next());
 37             day = Integer.parseInt(input.next());
 38 
 39             DateUtil date = new DateUtil(year, month, day);
 40 
 41             if (!date.checkInputValidity()) {
 42                 System.out.println("Wrong Format");
 43                 System.exit(0);
 44             }
 45 
 46             n = input.nextInt();
 47 
 48             if (n < 0) {
 49                 System.out.println("Wrong Format");
 50                 System.exit(0);
 51             }
 52 
 53             System.out.print(
 54                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
 55             System.out.println(date.getPreviousNDays(n).showDate());
 56         } else if (choice == 3) {    //test getDaysofDates method
 57             year = Integer.parseInt(input.next());
 58             month = Integer.parseInt(input.next());
 59             day = Integer.parseInt(input.next());
 60 
 61             int anotherYear = Integer.parseInt(input.next());
 62             int anotherMonth = Integer.parseInt(input.next());
 63             int anotherDay = Integer.parseInt(input.next());
 64 
 65             DateUtil fromDate = new DateUtil(year, month, day);
 66             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 67 
 68             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 69                 System.out.println("The days between " + fromDate.showDate() +
 70                         " and " + toDate.showDate() + " are:"
 71                         + fromDate.getDaysofDates(toDate));
 72             } else {
 73                 System.out.println("Wrong Format");
 74                 System.exit(0);
 75             }
 76         }
 77         else{
 78             System.out.println("Wrong Format");
 79             System.exit(0);
 80         }
 81     }
 82 }
 83 class DateUtil {
 84     private int year;
 85     private int month;
 86     private int day;
 87     public int[] mon_maxnum = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 88 
 89     public DateUtil() {
 90 
 91     }
 92 
 93     public DateUtil(int year, int month, int day) {
 94         this.year = year;
 95         this.month = month;
 96         this.day = day;
 97     }
 98 
 99     public int getYear() {
100         return this.year;
101     }
102 
103     public void setYear(int year) {
104         this.year = year;
105     }
106 
107     public int getMonth() {
108         return this.month;
109     }
110 
111     public void setMonth(int month) {
112         this.month = month;
113     }
114 
115     public int getDay() {
116         return this.day;
117     }
118 
119     public void setDay(int day) {
120         this.day = day;
121     }
122 
123     public boolean isLeapYear(int year) {
124         if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
125             return true;
126         } else {
127             return false;
128         }
129     }
130 
131     public boolean checkInputValidity() {
132         if (isLeapYear(this.year)) {
133             mon_maxnum[2] = 29;
134         }
135         if ((this.year >= 1820 && this.year <= 2020) && (this.month >= 1 && this.month <= 12) && (this.day >= 1 && this.day <= mon_maxnum[this.month])) {
136             return true;
137         } else {
138             return false;
139         }
140     }
141 
142     public boolean compareDates(DateUtil date) {
143         if (this.year < date.getYear()) {
144             return true;
145         } else if (this.month < date.getMonth()) {
146             return true;
147         } else if (this.day > date.getDay()) {
148             return true;
149         } else {
150             return false;
151         }
152     }
153 
154     public boolean equalTwoDates(DateUtil date) {
155         if ((this.year == date.getYear()) && (this.month == date.getMonth()) && (this.day == date.getDay())) {
156             return true;
157         } else {
158             return false;
159         }
160     }
161 
162     public int getDaysofDates(DateUtil date) {
163         int substract = 0, flag = 0;//i为年份j为月份k为日
164         if (this.year<=date.getYear()) {
165             int i = this.year, j = this.month, k = this.day;//i为年份j为月份k为日
166             for (; i <= date.getYear(); ) {
167                 if (isLeapYear(i)) {
168                     mon_maxnum[2] = 29;
169                 } else {
170                     mon_maxnum[2] = 28;
171                 }
172                 if (i == date.getYear()) {
173                     for (; j <= date.getMonth(); ) {
174                         if ((i == date.getYear()) && (j == date.getMonth())) {
175                             if (isLeapYear(i)) {
176                                 mon_maxnum[2] = 29;
177                             } else {
178                                 mon_maxnum[2] = 28;
179                             }
180                             substract = substract + date.getDay() - k;
181                             break;
182                         } else {
183                             substract = substract + mon_maxnum[j];
184                             j++;
185                         }
186                     }
187                     break;
188                 } else {
189                     if (isLeapYear(i)) {
190                         mon_maxnum[2] = 29;
191                     } else {
192                         mon_maxnum[2] = 28;
193                     }
194                     if (j < 12) {
195                         substract += mon_maxnum[j];
196                         j++;
197                     } else {
198                         substract += 31;
199                         i++;
200                         j = 1;
201                     }
202                 }
203             }
204         } else {
205             int i = date.getYear(), j = date.getMonth(), k = date.getDay();//i为年份j为月份k为日
206             for (; i <= this.year; ) {
207                 if (i == this.year) {
208                     if (isLeapYear(i)) {
209                         mon_maxnum[2] = 29;
210                     } else {
211                         mon_maxnum[2] = 28;
212                     }
213                     for (; j <= this.month; ) {
214                         if (i == this.year && j == this.month) {
215                             substract = substract + this.day - k;
216                             flag = 1;
217                             break;
218                         }
219                         substract = substract + mon_maxnum[j];
220                         j++;
221                     }
222                     flag = 1;
223                 }
224                 if (flag == 1) {
225                     break;
226                 } else {
227                     if (j < 12) {
228                         if (isLeapYear(i)) {
229                             mon_maxnum[2] = 29;
230                         } else {
231                             mon_maxnum[2] = 28;
232                         }
233                         substract += mon_maxnum[j];
234                         j++;
235                     } else {
236                         substract += 31;
237                         i++;
238                         j = 1;
239                     }
240                 }
241             }
242         }
243         return substract;
244     }
245     public String showDate (){
246         return this.year+"-"+this.month+"-"+this.day;
247     }
248     public DateUtil getNextNDays (int n){
249         DateUtil Date = new DateUtil(this.year, this.month, this.day);
250         for (; n > 0; ) {
251             if (isLeapYear(Date.getYear())) {
252                 mon_maxnum[2] = 29;
253             } else {
254                 mon_maxnum[2] = 28;
255             }
256             if (Date.getDay() < mon_maxnum[Date.getMonth()]) {
257                 Date.setDay(Date.getDay() + 1);
258                 n--;
259             } else {
260                 if (Date.getMonth() < 12) {
261                     Date.setMonth(Date.getMonth() + 1);
262                     Date.setDay(1);
263                     n--;
264                 } else {
265                     Date.setMonth(1);
266                     Date.setDay(1);
267                     Date.setYear(Date.getYear() + 1);
268                     n--;
269                 }
270             }
271         }
272         return Date;
273     }
274 
275     public DateUtil getPreviousNDays ( int n){
276         DateUtil Date = new DateUtil(this.year, this.month, this.day);
277         for (; n > 0; ) {
278             if (isLeapYear(Date.getYear())) {
279                 mon_maxnum[2] = 29;
280             } else {
281                 mon_maxnum[2] = 28;
282             }
283             if (Date.getDay() > 1) {
284                 Date.setDay(Date.getDay() - 1);
285                 n--;
286             } else {
287                 if (Date.getMonth() > 1) {
288                     Date.setMonth(Date.getMonth() - 1);
289                     Date.setDay(mon_maxnum[Date.getMonth()]);
290                     n--;
291                 } else {
292                     Date.setYear(Date.getYear() - 1);
293                     Date.setMonth(12);
294                     Date.setDay(31);
295                     n--;
296                 }
297             }
298         }
299         return Date;
300     }
301 }
复制代码

 

 

 

3、踩坑心得

踩到的第一个坑就是判断三角形这题:浮点数的相加会有误差出现,这就导致了判断三角形类型的余弦定理的使用上与我们正常的认知不同,不能利用两个边的平方与第三个边的平方的关系来判定三角形的类型。因为在做题之前忘记了这一点,导致我完成代码之后感觉良好但是有几个测试点无法通过。询问同学之后才知道Java中浮点数的相加会产生误差,但是这个误差非常小,所以我们只要让两边的差小于一个很小的数就等同于两边相等。源码如下:       

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         
 8         Scanner in = new Scanner(System.in);
 9         float a = in.nextFloat();
10         float b = in.nextFloat();
11         float c = in.nextFloat();
12         double wucha = 0.001;
13         if(a<1||b<1||c<1||a>200||b>200||c>200)
14         {
15             System.out.println("Wrong Format");
16             return;
17         }
18         else if(a+b<c+wucha||a+c<b+wucha||b+c<a+wucha)
19         {
20             System.out.println("Not a triangle");
21             return;
22         }
23         else if(a==b||a==c||b==c)
24         {
25             if(a==b&&a==c)
26             {
27                 System.out.println("Equilateral triangle");
28                 return;
29             }
30             else if((a*a+b*b<c*c+wucha&&a*a+b*b>c*c-wucha)||(a*a+c*c<b*b+wucha&&a*a+c*c>b*b-wucha)||(c*c+b*b<a*a+wucha&&c*c+b*b>a*a-wucha))
31             {
32                 System.out.println("Isosceles right-angled triangle");
33                 return;
34             }
35             else
36             {
37                 System.out.println("Isosceles triangle");
38                 return;
39             }
40         }
41         else if((a*a+b*b<c*c+wucha&&a*a+b*b>c*c-wucha)||(a*a+c*c<b*b+wucha&&a*a+c*c>b*b-wucha)||(c*c+b*b<a*a+wucha&&c*c+b*b>a*a-wucha))
42         {
43             System.out.println("Right-angled triangle");
44             return;
45         }
46         else
47         {
48             System.out.println("General triangle");
49             return;
50         }
51     }
52 }

 第二个坑就是第三次作业的第二题:我自己感觉写的很正确,但是提交之后却总是有两个测试点过不去。我用一些常用的例子以及题目给的样例检验都是合格的,所以一直没有发现问题在哪。当我向一些满分的同学询问之后,恰好有一位同学经历过跟我一样的错误,我才知道我错在了求利率这里。 

double monthlyInterestRate = (balance / 1200) * annualInterestRate;
这一步必须要先除后乘,否则会丢失精度从而导致错误。(这个是真的不知道,困扰很久,请教了很多人)
 1 import java.util.*;
 2 class Account{
 3     private int id = 0;
 4     private double balance = 0;
 5     private double annualInterestRate = 0;
 6     
 7     public Account(int id,double balance) {
 8         this.id = id;
 9         this.balance = balance;
10     }
11     
12     public int getId() {
13         return id;
14     }
15     public void setId(int id) {
16         this.id = id;
17     }
18     public double getBalance() {
19         return balance;
20     }
21     public void setBalance(double balance) {
22         this.balance = balance;
23     }
24     public double getAnnualInterestRate() {
25         return annualInterestRate;
26     }
27     public void setAnnualInterestRate(double annualInterestRate) {
28         this.annualInterestRate = annualInterestRate;
29     }
30     public double getMonthlyInterestRate() {
31         double monthlyInterestRate = (balance / 1200) * annualInterestRate;
32         return monthlyInterestRate;
33     }
34     
35     public void withDraw(double withDraw) {
36         if(withDraw > balance || withDraw < 0)
37             System.out.println("WithDraw Amount Wrong");
38         else
39             balance = balance - withDraw;
40     }
41     public void deposit(double deposit) {
42         if(deposit > 20000 || deposit < 0 )
43             System.out.println("Deposit Amount Wrong");
44         else
45             balance = balance + deposit ;
46     }
47 }
48 
49 public class Main{
50     public static void main(String[] args) {
51         Scanner in = new Scanner(System.in);
52         int id = in.nextInt();
53         double balance = in.nextDouble();
54         double annualInterestRate = in.nextDouble();
55         double withDraw = in.nextDouble();
56         double deposit = in.nextDouble();
57         char DateCreated = 2020-07-31;
58         Account account = new Account( id , balance );
59         account.setAnnualInterestRate(annualInterestRate);
60         account.withDraw(withDraw);
61         account.deposit(deposit);
62         System.out.println("The Account'balance:"+String.format("%.2f",account.getBalance()));
63         System.out.println("The Monthly interest:"+String.format("%.2f",account.getMonthlyInterestRate()));
64         System.out.println("The Account'dateCreated:2020-07-31");
65     }
66 }

第三个坑无疑就是求下n天的int最大值的那个测试点了,不写这一题都快忘记了int还有最大值这一说,得控制好对象,争取每个对象都在合理的范围内。(虽然这题的测试点角度很***钻,但我觉得他其实也很有意义,很有意思)因为上文附过源码,所以我在这就不再附一次了。

 

通过这几次作业,写代码的学习我明白代码的学习不只是光看书或者是看网课就能提高自己的,更重要的是与他人的交流,与他人思想的碰撞。只有取他人之长补自己之短,才能够真正意义上的垒高自己,真正的学会Java这门编译语言。路还很长。

4、改进建议

  写题目一定是为了解决实际问题的,而不能盯着测试点看,如果代码仅仅是为了通过几个测试点而编写出来的,那这样的练习可以说是毫无意义。再就是对代码的学习应该要懂得类比,举一反三:例如第三次作业的第三题求下一天,与第三次作业的第四题的求前n天求下n天本质上就是一个类型的题目,如果我们能弄懂求下一天的原理,那么见到求下n天之类的题目时就会有一种熟悉感,编写代码的过程也会变得很轻松,问题就迎刃而解。从这里可以明白,我们对代码的理解不能停留在表面,应可以看的更远,争取做到举一反三。如果我们对写过的题目的理解仅仅停留在表面,那我们每次写迭代的题目都会像是一个新的题目,这无形中扩大了题目的难度,同时也失去了迭代题目最宝贵的意义:引导我们的思想不断深入理解题目的意义。

5、总结

   这是我第一次写关于Java学习的博客总结,同时对Java的学习也是刚刚开始,所以我知道自己身上仍然有很多不足,但是学习一种新的编程语言本来就是一个不简单的过程,看到自己身上的缺陷是正常的,重要的是看到缺陷之后能努力去改正他,尽力去做得更好。同时我也发现,Java的学习不仅仅是需要对基础知识的熟悉,更要细心,同时养成良好的习惯。如果没有良好的编译习惯,那当程序运行异常找错时,是非常非常痛苦的,这一点我深有体会。因为不熟悉debug的操作,然后写代码时又喜欢节省空间,把代码挤在一起,所以出现错误查错的时候非常难受,密密麻麻的代码没有章法揉成一团,这浪费了我非常多的时间。这些应该是我写完pta三次作业最大的收获吧!再就是关于Java的课的看法:虽然我每节课都赶去抢前排,每节课都尽量去认真听,但我却很少能听懂段老师在课上讲解的知识点,就只能在csdn查找相关的文章,在厚厚的Java书里阅读语法,照猫画虎的在eclipse里敲代码,对private,class,public,static等词汇的用法几乎是一窍不通……只能利用课后的慕课来弥补语法知识的匮乏。Java的作业量不算少,然后题目的难度分布正常,前面的题目比较容易上手,后面的题目一般就需要我查阅相关资料来完成。其他的方面我没有什么看法意见,我觉得段老师这种教学模式虽然让我们很累很痛苦,但是从这个学习的过程中是可以真真正正的学习到东西的。总而言之,Java的学习之路才刚开始,即使前路一片茫茫黑暗,但是只要持之以恒,也一定能在这条路上走出属于自己的风采。