首页 > 编程语言 >Java题目集4~6的总结

Java题目集4~6的总结

时间:2023-04-29 16:44:50浏览次数:34  
标签:总结 题目 int value getValue year date Java public

1.前言

第四次作业

主要涉及的知识点有通过查询Java API文档,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。另加上题目一难度较大的类间设计与方法使用。题量中等,但是难度分配不均,7-1菜单计价程序是迭代2次的程序,这对于我一次也没迭代过的人来说确实有点难了,然后其余题目难度适中。

第五次作业

主要涉及的知识点是正则表达式、字符串、类间聚合设计。题量适中,写下来没有题目集四难。第一次接触正则表达式,觉得这个应该蛮难的,所以我看了一些有关正则表达式的网课,然后发现其实懂了它的用法之后也不是很难,就像套公式一样,遵循着一套规则,只要多写几遍差不多就能基本掌握了。对于最后两题的话,考的是聚合关系,是对年月日三个类的聚合,因为之前写过日期类的代码,所以做起来没有那么难,但代码很多地方由于聚合需要修改,还有算法稍微有一点点改变。

第六次作业

主要涉及的知识点是类的设计和类与类的关系处理。题量只有一题,但难度较大,预计代码行数1000+,需用时20h+。刚看到这个题目的时候人都是懵的,因为第四次作业7-1没有完成,所以根本无从下手,也不知道这几个类之间要如何联系起来用。

2.设计与分析

题目集4 7-1

对于7-1的菜单计价程序,我光看到题目的要求和理解题目的意思就花了蛮长时间,测试点也比较多,测试用例一个一个看了好几遍才看懂,然后这个题目又是迭代了两次的版本,我更加不知怎么入手,因此一开始我就感觉到了压力,觉得自己做不出来这道题,就先搁置了它。在完成其他题目后我才回头来仔细思考这道题,发现时间也不够用了,就比较着急的建了题目里的几个类,类里面只有基本的属性和getter,setter方法,至于它们之间有什么具体的关系我是没搞懂,同时看到pta排名发现很多人都没用写完这道题,就有了知难而退和放弃的念头,所以最后一半也没完成。

题目集5 7-5

类图

 源码

  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         Scanner input = new Scanner(System.in);
  8         int year = 0;
  9         int month = 0;
 10         int day = 0;
 11 
 12         int choice = input.nextInt();
 13 
 14         if (choice == 1) { // test getNextNDays method
 15             int m = 0;
 16             year = Integer.parseInt(input.next());
 17             month = Integer.parseInt(input.next());
 18             day = Integer.parseInt(input.next());
 19 
 20             DateUtil date = new DateUtil(year, month, day);
 21 
 22             if (!date.checkInputValidity()) {
 23                 System.out.println("Wrong Format");
 24                 System.exit(0);
 25             }
 26 
 27             m = input.nextInt();
 28 
 29             if (m < 0) {
 30                 System.out.println("Wrong Format");
 31                 System.exit(0);
 32             }
 33 
 34             System.out.println(date.getNextNDays(m).showDate());
 35         } else if (choice == 2) { // test getPreviousNDays method
 36             int n = 0;
 37             year = Integer.parseInt(input.next());
 38             month = Integer.parseInt(input.next());
 39             day = Integer.parseInt(input.next());
 40 
 41             DateUtil date = new DateUtil(year, month, day);
 42 
 43             if (!date.checkInputValidity()) {
 44                 System.out.println("Wrong Format");
 45                 System.exit(0);
 46             }
 47 
 48             n = input.nextInt();
 49 
 50             if (n < 0) {
 51                 System.out.println("Wrong Format");
 52                 System.exit(0);
 53             }
 54  
 55             System.out.println(date.getPreviousNDays(n).showDate());
 56            
 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(fromDate.getDaysofDates(toDate));
 71             } else {
 72                 System.out.println("Wrong Format");
 73                 System.exit(0);
 74             }
 75         }
 76         else{
 77             System.out.println("Wrong Format");
 78             System.exit(0);
 79         }        
 80     }
 81 
 82 }
 83 
 84 class Year {
 85     private int value;
 86 
 87     public Year() {
 88         
 89     }
 90 
 91     public Year(int value) {
 92         this.value = value;
 93     }
 94 
 95     public int getValue() {
 96         return value;
 97     }
 98 
 99     public void setValue(int value) {
100         this.value = value;
101     }
102 
103     public boolean isLeapYear() {
104         if (this.value%400 == 0|| (this.value%4 == 0 && this.value%100 != 0)) {
105             return true;
106         } else {                
107             return false;
108         }
109     }
110     
111     public boolean validate() {
112         if (this.value > 2050 || this.value < 1900) {
113             return false;
114         } else {
115             return true;
116         }
117     }
118     
119     public void yearIncrement() {
120         this.value = this.value + 1;
121     }
122 
123     public void yearReduction() {
124         this.value = this.value - 1;
125     }
126         
127 }
128 
129 
130 class Month {
131     private int value;
132     private Year year = new Year();
133     
134     public Month() {
135         
136     }
137 
138     public Month(int yearValue, int montnValue) {
139         this.year = new Year(yearValue);
140         this.setValue(montnValue);
141     }
142 
143     public int getValue() {
144         return value;
145     }
146 
147     public void setValue(int value) {
148         this.value = value;
149     }
150 
151     public Year getYear() {
152         return year;
153     }
154 
155     public void setYear(Year year) {
156         this.year = year;
157     }
158     
159     public void resetMin() {
160         this.setValue(1);
161     }
162     
163     public void resetMax() {
164         this.setValue(12);
165     }
166     
167     public boolean validate () {
168         if (this.value > 12 || this.value < 1) {
169             return false;
170         } else {
171             return true;
172         }
173     }
174     
175     public void monthIncrement() {
176         if (this.validate()) {
177            if (this.value == 12) {
178                this.resetMin();
179                this.year.yearIncrement();
180            } else {
181                this.value++;
182            }
183         }
184     }
185     
186     public void monthReduction() {
187         if (this.validate()) {
188             if (this.value == 1) {
189                 this.resetMax();
190                 this.year.yearReduction();
191             } else {
192                 this.value--;
193             }
194         
195         }
196     }
197     
198 }
199 
200 
201 class Day {
202     private int value;
203     private Month month = new Month();
204     private int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
205     
206     public Day() {
207     
208     }
209     
210     public Day(int yearValue, int monthValue, int dayValue) {
211         this.getMonth().getYear().setValue(yearValue);;
212         this.getMonth().setValue(monthValue);
213         this.setValue(dayValue);
214     }
215 
216     public int getValue() {
217         return value;
218     }
219 
220     public void setValue(int value) {
221         this.value = value;
222     }
223 
224     public Month getMonth() {
225         return month;
226     }
227 
228     public void setMonth(Month value) {
229         this.month = value;
230     }
231     
232     public void resetMin() {
233         this.value = 1;
234     }
235     
236     public void resetMax() {
237         int maxNum = this.mon_maxnum[this.month.getValue() - 1];
238         if (this.getMonth().getYear().isLeapYear() && this.month.getValue() == 2) {
239             maxNum = 29;
240         }
241         this.value = maxNum;
242     }
243     
244     public boolean validate() {
245         int maxNum = this.mon_maxnum[this.month.getValue() - 1];
246         if (this.getMonth().getYear().isLeapYear() && this.month.getValue() == 2) {
247             maxNum = 29;
248         }
249         if (this.value > 0 && this.value <= maxNum) {
250             return true;
251         } else {
252             return false;
253         }    
254     }
255     
256     public void dayIncrement() {
257         int maxNum = this.mon_maxnum[this.month.getValue() - 1];
258         if (this.getMonth().getYear().isLeapYear() && this.month.getValue() == 2) {
259             maxNum = 29;
260         }
261         if (this.validate()) {
262             if (this.value == maxNum) {
263                 this.month.monthIncrement();
264                 this.resetMin();
265             } else {
266                 this.value++;
267             }    
268         }
269         
270     }
271     
272     public void dayRuduction() {
273         if (this.validate()) {
274             if (this.value == 1) {
275                 this.month.monthReduction();
276                 this.resetMax();
277             } else {
278                 this.value--;
279             }
280             
281         }
282     }
283     
284 }
285 
286 
287 class DateUtil {
288     private Day day = new Day();
289 
290     public DateUtil() {
291         
292     }
293 
294     public DateUtil(int yearValue, int monthValue, int dayValue) {
295         this.day = new Day(yearValue, monthValue, dayValue );
296     }
297 
298     public Day getDay() {
299         return day;
300     }
301 
302     public void setDay(Day d) {
303         this.day = d;
304     }
305     
306     public boolean checkInputValidity() {
307         if (!this.getDay().getMonth().validate()) return false;
308         if (this.getDay().validate()
309             && this.getDay().getMonth().getYear().validate()) {
310             return true;
311         } else {
312             return false;
313         }
314         
315     }
316     
317     public boolean compareDates(DateUtil date) {
318         if (this.getDay().getMonth().getYear().getValue() 
319                 > date.getDay().getMonth().getYear().getValue()) {
320             return true;
321         } else if (this.getDay().getMonth().getYear().getValue() 
322                 == date.getDay().getMonth().getYear().getValue()
323                 && this.getDay().getMonth().getValue() > date.getDay().getMonth().getValue()) {
324             return true;
325         } else if (this.getDay().getMonth().getYear().getValue() 
326                 == date.getDay().getMonth().getYear().getValue()
327                 && this.getDay().getMonth().getValue() == date.getDay().getMonth().getValue()
328                 && this.getDay().getValue() > date.getDay().getValue()) {
329             return true;
330         } else {
331             return false;
332         }
333         
334     }
335     
336     public boolean equalTwoDates(DateUtil date) {
337         if (this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue()
338             && this.getDay().getMonth().getValue() == date.getDay().getMonth().getValue() 
339             && this.getDay().getValue() == date.getDay().getValue()) {
340             return true;
341         } else {
342             return false;
343         }    
344     }
345     
346     public String showDate() {
347         return String.format("%d-%d-%d",this.getDay().getMonth().getYear().getValue(),
348                 this.getDay().getMonth().getValue(),this.getDay().getValue() );
349     }
350     
351     public DateUtil getNextNDays(int n) {
352         for (int i = 0; i < n; i++) {
353             this.day.dayIncrement();
354         }
355        return this;                  
356     }
357     
358     public DateUtil getPreviousNDays(int n) {
359         for (int i = 0; i < n; i++) {
360             this.day.dayRuduction();
361         }
362        return this;    
363         
364     }
365     
366     public int getDaysofDates(DateUtil date) {
367         int daysBetween=0;
368         int[]mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
369         int yearSum=0;
370         int monthSum1=0;
371         int monthSum2=0;
372         
373         DateUtil dateChange = new DateUtil();
374         
375        
376         if(this.getDay().getMonth().getYear().getValue() 
377                 > date.getDay().getMonth().getYear().getValue()) {
378             dateChange.getDay().getMonth().getYear().setValue(date.getDay().getMonth().getYear().getValue());
379             date.getDay().getMonth().getYear().setValue(this.getDay().getMonth().getYear().getValue());
380             this.getDay().getMonth().getYear().setValue(dateChange.getDay().getMonth().getYear().getValue());    
381         }        
382         for(int i = this.getDay().getMonth().getYear().getValue(); i < date.getDay().getMonth().getYear().getValue(); i++) {
383             dateChange.getDay().getMonth().getYear().setValue(i);
384             if(dateChange.getDay().getMonth().getYear().isLeapYear()) {
385                 yearSum += 366;
386             }
387             else {
388                 yearSum += 365;
389             }    
390         }
391         for(int i = 1; i < this.getDay().getMonth().getValue(); i++) {
392             monthSum1 += mon_maxnum[i];    
393         }
394         if(this.getDay().getMonth().getYear().isLeapYear() 
395                 && this.getDay().getMonth().getValue() > 2) {
396             monthSum1++;
397         }
398         for(int i = 1; i < date.getDay().getMonth().getValue(); i++) {
399             monthSum2 += mon_maxnum[i];    
400         }
401         if(date.getDay().getMonth().getYear().isLeapYear() 
402                 && date.getDay().getMonth().getValue() > 2) {
403             monthSum2++;
404         }
405       
406         daysBetween = yearSum + monthSum2 + date.getDay().getValue() - monthSum1 - this.getDay().getValue();
407         return daysBetween;
408 
409     }
410         
411 
412 }

分析

这一题是日期问题面向对象设计,类与类之间是聚合的关系,通过类图可以知道这几个类是通过把一个类放在另一个类里当成属性,从而使该类可以引用属性的方法,而且是一类聚合一个类,并不是日期聚合年月日三个类。其实这题就是要学会如何使用getter/setter方法去一层层调用不同类中的属性与方法。其中大部分的方法只需要按上一次实验的代码为参照稍加修改并对各个属性的访问方法进行修改。计算两个日期相差的天数算法在之前的日期设计类题目里就有写过了,大同小异,基本没什么大的变化。然后计算前n天和后n天的算法我是跟以前的不同了,因为我按照之前的算法结果总会出现问题,在询问大佬之后觉得他这个算法挺简单易懂,而且不容易错就学习了他的算法。计算前n天的话,就是循环n次一天一天减回去,虽然费时但不容易错,在减的时候要注意日期边界值,比如1号减一天之后day改为前一个月的最大值,month--,如果该月为1月,那么还要year--,同时注意闰年和平年的二月总天数,这一点如果不考虑将会相差好多天。计算后n天的话算法一样,就是循环n次一天一天加上去,加的时候也需注意边界值,比如31号加一天之后day改为1,month++,如果该月为12月,则还要year++,一样要考虑闰年和平年。

题目集5 7-6

类图

 

源码

  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         Scanner input = new Scanner(System.in);
  8         int year = 0;
  9         int month = 0;
 10         int day = 0;
 11 
 12         int choice = input.nextInt();
 13 
 14         if (choice == 1) { // test getNextNDays method
 15             int m = 0;
 16             year = Integer.parseInt(input.next());
 17             month = Integer.parseInt(input.next());
 18             day = Integer.parseInt(input.next());
 19 
 20             DateUtil date = new DateUtil(year, month, day);
 21 
 22             if (!date.checkInputValidity()) {
 23                 System.out.println("Wrong Format");
 24                 System.exit(0);
 25             }
 26 
 27             m = input.nextInt();
 28 
 29             if (m < 0) {
 30                 System.out.println("Wrong Format");
 31                 System.exit(0);
 32             }
 33             System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" 
 34             + date.getDay().getValue() + " next " + m + " days is:");
 35             System.out.println(date.getNextNDays(m).showDate());
 36         } else if (choice == 2) { // test getPreviousNDays method
 37             int n = 0;
 38             year = Integer.parseInt(input.next());
 39             month = Integer.parseInt(input.next());
 40             day = Integer.parseInt(input.next());
 41 
 42             DateUtil date = new DateUtil(year, month, day);
 43 
 44             if (!date.checkInputValidity()) {
 45                 System.out.println("Wrong Format");
 46                 System.exit(0);
 47             }
 48 
 49             n = input.nextInt();
 50 
 51             if (n < 0) {
 52                 System.out.println("Wrong Format");
 53                 System.exit(0);
 54             }
 55  
 56             System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() 
 57                     + "-" + date.getDay().getValue() + " previous " + n + " days is:");                
 58             System.out.println(date.getPreviousNDays(n).showDate());
 59         } else if (choice == 3) {    //test getDaysofDates method
 60             year = Integer.parseInt(input.next());
 61             month = Integer.parseInt(input.next());
 62             day = Integer.parseInt(input.next());
 63 
 64             int anotherYear = Integer.parseInt(input.next());
 65             int anotherMonth = Integer.parseInt(input.next());
 66             int anotherDay = Integer.parseInt(input.next());
 67 
 68             DateUtil fromDate = new DateUtil(year, month, day);
 69             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 70 
 71             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 72                 System.out.println("The days between " + fromDate.showDate() + 
 73                         " and " + toDate.showDate() + " are:"
 74                         + fromDate.getDaysofDates(toDate));
 75             } else {
 76                 System.out.println("Wrong Format");
 77                 System.exit(0);
 78             }
 79         }
 80         else{
 81             System.out.println("Wrong Format");
 82             System.exit(0);
 83         } 
 84     }   
 85 }  
 86 
 87 
 88 
 89 class Year {
 90     private int value;
 91 
 92     public Year() {
 93         
 94     }
 95 
 96     public Year(int value) {
 97         this.value = value;
 98     }
 99 
100     public int getValue() {
101         return value;
102     }
103 
104     public void setValue(int value) {
105         this.value = value;
106     }
107 
108     public boolean isLeapYear() {
109         if (this.value%400 == 0|| (this.value%4 == 0 && this.value%100 != 0)) {
110             return true;
111         } else {                
112             return false;
113         }
114     }
115     
116     public boolean validate() {
117         if (this.value > 2020 || this.value < 1820) {
118             return false;
119         } else {
120             return true;
121         }
122     }
123     
124     public void yearIncrement() {
125         this.value = this.value + 1;
126     }
127 
128     public void yearReduction() {
129         this.value = this.value - 1;
130     }
131         
132 }
133 
134 
135 class Month {
136     private int value;
137     
138     public Month() {
139         
140     }
141 
142     public Month(int value) {
143         this.value = value;
144     }
145 
146     public int getValue() {
147         return value;
148     }
149 
150     public void setValue(int value) {
151         this.value = value;
152     }
153     
154     public void resetMin() {
155         this.setValue(1);
156     }
157     
158     public void resetMax() {
159         this.setValue(12);
160     }
161     
162     public boolean validate () {
163         if (this.value > 12 || this.value < 1) {
164             return false;
165         } else {
166             return true;
167         }
168     }
169     
170     public void monthIncrement() {
171         if (this.validate()) {
172            if (this.value == 12) {
173                this.resetMin();
174            } else {
175                this.value++;
176            }
177         }
178     }
179     
180     public void monthReduction() {
181         if (this.validate()) {
182             if (this.value == 1) {
183                 this.resetMax();
184             } else {
185                 this.value--;
186             }
187         
188         }
189     }
190     
191 }
192 
193 
194 class Day {
195     private int value;
196     
197     public Day() {
198     
199     }
200     
201     public Day(int value) {
202         this.value = value;
203     }
204 
205     public int getValue() {
206         return value;
207     }
208 
209     public void setValue(int value) {
210         this.value = value;
211     }
212 
213     public void dayIncrement() {
214         this.value++;
215     }
216     
217     public void dayRuduction() {    
218         this.value--;    
219     }
220     
221 }
222 
223 
224 class DateUtil {
225     private Year year = new Year();
226     private Month month = new Month();
227     private Day day = new Day();
228     private int[] mon_maxnum = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
229     
230     public DateUtil() {
231         
232     }
233 
234     public DateUtil(int y, int m, int d) {
235         this.year.setValue(y);
236         this.month.setValue(m);
237         this.day.setValue(d);
238     }
239 
240     public Day getDay() {
241         return day;
242     }
243 
244     public void setDay(Day day) {
245         this.day = day;
246     }
247     
248     public Year getYear() {
249         return year;
250     }
251 
252     public void setYear(Year year) {
253         this.year = year;
254     }
255 
256     public Month getMonth() {
257         return month;
258     }
259 
260     public void setMonth(Month month) {
261         this.month = month;
262     }
263 
264     public void setDayMin() {
265         this.day.setValue(1);
266     }
267     
268     public void setDayMax() {
269         int maxNum = this.mon_maxnum[this.month.getValue() - 1];
270         if (this.year.isLeapYear() && this.month.getValue() == 2) {
271             maxNum = 29;
272         }
273         this.day.setValue(maxNum);
274     }
275     
276     public boolean checkInputValidity() {
277         if (!this.month.validate()) return false;
278         int maxNum = this.mon_maxnum[this.month.getValue() - 1];
279         if (this.year.isLeapYear() && this.month.getValue() == 2) {
280             maxNum = 29;
281         }
282         if (this.day.getValue() > 0 && this.day.getValue() <= maxNum
283             && this.year.validate()) {
284             return true;
285         } else {
286             return false;
287         }
288         
289     }
290     
291     public boolean compareDates(DateUtil date) {
292         if (this.year.getValue() 
293                 > date.year.getValue()) {
294             return true;
295         } else if (this.year.getValue() 
296                 == date.year.getValue()
297                 && this.month.getValue() > date.month.getValue()) {
298             return true;
299         } else if (this.year.getValue() 
300                 == date.year.getValue()
301                 && this.month.getValue() == date.month.getValue()
302                 && this.day.getValue() > date.day.getValue()) {
303             return true;
304         } else {
305             return false;
306         }
307         
308     }
309     
310     public boolean equalTwoDates(DateUtil date) {
311         if (this.year.getValue() == date.year.getValue()
312             && this.month.getValue() == date.month.getValue() 
313             && this.day.getValue() == date.day.getValue()) {
314             return true;
315         } else {
316             return false;
317         }    
318     }
319     
320     public String showDate() {
321         return String.format("%d-%d-%d",this.year.getValue(),
322                 this.month.getValue(),this.day.getValue() );
323     }
324     
325     public DateUtil getNextNDays(int n) {
326         for (int i = 0; i < n; i++) {
327             int maxNum = this.mon_maxnum[this.month.getValue() - 1];
328             if (this.year.isLeapYear() && this.month.getValue() == 2) {
329                 maxNum = 29;
330             }
331             if (this.day.getValue() == maxNum) {
332                 if (this.month.getValue() == 12) {
333                     this.year.yearIncrement();
334                     this.month.resetMin();    
335                 } else {
336                     this.month.monthIncrement();
337                 }
338                 this.setDayMin();
339             } else {
340                 this.day.dayIncrement();
341             }    
342         }
343        return this;                  
344     }
345     
346     public DateUtil getPreviousNDays(int n) {
347         for (int i = 0; i < n; i++) {
348             int maxNum = this.mon_maxnum[this.month.getValue() - 1];
349             if (this.year.isLeapYear() && this.month.getValue() == 2) {
350                 maxNum = 29;
351             }
352             if (this.day.getValue() == 1) {
353                 if (this.month.getValue() == 1) {
354                     this.year.yearReduction();
355                     this.month.resetMax();    
356                 } else {
357                     this.month.monthReduction();
358                 }
359                 this.setDayMax();
360             } else {
361                 this.day.dayRuduction();
362             }    
363         }
364        return this;                  
365     }
366     
367     public int getDaysofDates(DateUtil date) {
368         int daysBetween=0;
369         int yearSum=0;
370         int monthSum1=0;
371         int monthSum2=0;
372         
373         DateUtil dateChange = new DateUtil();
374         
375        
376         if(this.year.getValue() 
377                 > date.year.getValue()) {
378             dateChange.year.setValue(date.year.getValue());
379             date.year.setValue(this.year.getValue());
380             this.year.setValue(dateChange.year.getValue());    
381         }        
382         for(int i = this.year.getValue(); i < date.year.getValue(); i++) {
383             dateChange.year.setValue(i);
384             if(dateChange.year.isLeapYear()) {
385                 yearSum += 366;
386             }
387             else {
388                 yearSum += 365;
389             }    
390         }
391         for(int i = 1; i < this.month.getValue(); i++) {
392             monthSum1 += mon_maxnum[i - 1];    
393         }
394         if(this.year.isLeapYear() 
395                 && this.month.getValue() > 2) {
396             monthSum1++;
397         }
398         for(int i = 1; i < date.month.getValue(); i++) {
399             monthSum2 += mon_maxnum[i - 1];    
400         }
401         if(date.year.isLeapYear() 
402                 && date.month.getValue() > 2) {
403             monthSum2++;
404         }
405       
406         daysBetween = yearSum + monthSum2 + date.day.getValue() - monthSum1 - this.day.getValue();
407         return Math.abs(daysBetween);
408 
409     }
410         
411 
412 }

分析

这一题是上一题的升级版,类与类之间聚合的关系更强了,大大降低了耦合度。通过DateUtil聚合了年月日三个类,将它们都作为属性来调用它们的方法。这题算法跟上题大同小异,但是因为方法的访问权限发生了改变,计算前n天和后n天的算法需要修改。这也让我知道类的关系一旦发生改变,很多算法都需要修改。

题目集6 7-1

类图

类图并没有设计完整。

 

源码

这一题并没有完成,这里就不展示源码了。

分析

因为之前的菜单计价程序就没有写完,然后这题又是迭代,增加了很多的异常情况,所以这一题完成起来很困难,在尝试了蛮长时间后,发现自己搞不懂类之间的关系与主方法如何写,同时由于畏难情绪总是把这题放着不想写。最终也是没有完成这题。

3.踩坑心得

第四次作业

这次印象最深的坑是检查重复数据和去掉重复的数据。因为如果不用哈希表单纯暴力用for循环的话,有多个for循环嵌套,会导致程序运行太慢或超时,同时哈希表不会存入重复数据。在使用hashset和linkedhashset之后这两题都得到了解决。

踩坑截图

 

 

改正后

 

 

第五次作业

这次作业踩的坑有正则表达式的坑和日期设计类的算法和访问权限的使用,日期那两题在前面已经分析就不再讲了。7-1中一开始我的正则表达式没写开始结束的符号^和$,导致读取字符的时候会多读取,使得结果错误。后面才发现要用^和$来结束一段字符的读取。

踩坑截图

 

改正后

4.改进建议

(1)题目集要早点写,正所谓“笨鸟先飞",我的能力不足但可以通过时间来补,一定要每天拿出一定的时间来写pta。

(2)写代码的时候要记得写注释,不然一大行代码在那里自己都可能看不懂,这对于自己检查错误的时候十分不利,要注意代码的格式,使代码规范,才方便阅读。

(3)要学会分析类与类之间的关系,先画类图再写代码。这样思路才清晰,不会头脑一片混乱,写到哪里算哪里。

5.总结

通过这三次的题目集,我学会了正则表达式的基本使用方法,接触了哈希表和treemap,同时加强了对聚合关系的理解,知道了关系不同时属性和方法的访问权限也不一样,因此类图的设计十分重要。相比前三次题目集,感觉难度一下就上来了,尤其是菜单计价程序的迭代让我十分痛苦,第一次感觉到无从下手和无奈,这也让我深刻反思自己的不足,我没有吃苦和迎难而上的精神,遇到难题就有恐惧心理,不敢多看一眼,内心里就认定自己完成不了,同时我的Java知识也不够牢固,有的知识点需要临时翻书或者网上查找才知道,我的知识面还是太窄了。总之,这次最大的问题就是我知难而退随大流,看到有的同学没有写完菜单的题就也放松了自己,态度不够端正,没有尽力完成题目,这种心理是万万不能取的。因此,在之后的题目集中,我要改正自己的做题态度,放下畏难情绪,如果不懂不会就询问同学,不要自己一个人痛苦又不寻找解决办法然后不了了之,这是对自己的不负责任,而且也不能起到练习的作用。老师既然布置了题目,那就说明这题应该是在我们的能力范围内的,我必须加强自己的心理素质和端正态度,尽力完成每一道题目,即使可能拿不了满分也要认真的去做好每一道题。

 

 

 

标签:总结,题目,int,value,getValue,year,date,Java,public
From: https://www.cnblogs.com/oct-ly/p/17360153.html

相关文章

  • Java的Object类
    Object类Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来的。但是并不需要这样写:publicclassEmployeeextendsObject如果没有明确地指出父类,Object就被认为是这个类的父类。所有的数组类型,不管是对象数组还是基本类型的数组都扩展了Object类。Employe......
  • Codeforces Round 854 补题总结
    CodeforcesRound854补题总结前言昨天做这套题很不顺,今天补完题大概总结一下。总结RecentActions按题意模拟即可,考虑到前\(n\)个数一定始终在后\(m\)个数的前面,所以说当当前队列中如果没有出现\(x\)而在第\(i\)轮放进了\(x\),那么当前在队首的编号小于\(n\)的数......
  • Java原码、反码、补码
    二进制的最高位是符号位:0表示正数,1表示负数(0->01->-)正数的原码、反码、补码都是一样(三码合一)负数的反码=它的原码符号位不变,其它位取反(0->1,1->0)负数的补码=它的反码+1,负数的反码=负数的补码-10的反码、补码都是0java没有无符号数,换言之,java中的数都是有符号的在......
  • Java的Object类
    Object类Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来的。但是并不需要这样写:publicclassEmployeeextendsObject如果没有明确地指出父类,Object就被认为是这个类的父类。所有的数组类型,不管是对象数组还是基本类型的数组都扩展了Object类。Employ......
  • oo第二次博客总结
      目录1.前言2.设计与分析3.踩坑心得4.改进建议5.总结一:前言题目集四:1,菜单计价程序-32,有重复的数据3,去掉重复的数据4.单词的统计与排序5.面向对象编程(封装性)6.GPS测绘中度分秒转换7.判断两个日期的先后,计算间隔天数、周数.题目集五:1.正则表达式训练-QQ号校......
  • apollo启动报错java.lang.NoClassDefFoundError PemObjectGenerator
    启动apollo时,报错。java.lang.NoClassDefFoundError:org/bouncycastle/util/io/pem/PemObjectGenerator很明显类没找到,根据经验判断,很可能是maven依赖问题。解决方案apollo版本1.1.4。目前相关的是:org.bouncycastle:bcpkix-jdk15on:jar:1.55org.bouncycastle:bcprov-jdk15on:jar......
  • pta第二部分总结oop训练集05-06
    (1)前言训练集05:(题量适中,难度适中)7-5:多个类的互相调用对于日期类的整体优化,聚合初体验。7-6:对7-5的优化,加强聚合训练。训练集06:(题量少,难度大)7-1:多需求的实现。(完成的不好,编程能力还不够)(2)设计与分析7-5:类图: 源码:importjava.util.Scanner;publicclassMain{......
  • 集训总结
    集训总结前言“吹散记忆的蒲公英,散落碧空;回望过往的风景,尤存风味。”离开初中生活,总会幻想回到过去,回到以前的老师同学身边,羞怯而带有稚气。结束了本蒟蒻的第一次NOIP,第一次自己在外地参加集训。内心却也充满无法言表的激动。而今总结半个月的回忆,内心充满了话,却不知从何开......
  • 题目集4-6
     第二次blog作业1.前言作为一个java和面向对象的初学者,在写题目这方面确实是处处碰壁,但学习最重要的是坚持,希望我能够坚持下去。对于这三次的题目集,就难度而言是比较难的,难在一些测试点需要经过深层次的思考,还要通过大量的测试测试出来,必须承认的是考到的一些知识在题目中都有......
  • IPv6地址总结
    一、IPv6特点地址空间更大,地址长度128位,更便于路由汇总;无需NAT;保留单播,组播,新加入的任意播取代广播;二、IPv6地址分类1.单播地址:和IPv4一样单播地址:除FF00::/8之外的全部IPv6地址可以在启用IPv6接口下自动生成(1)链路本地地址(Link-LocalAddress)链路本地地址是自动生成的,链路本地地......