OO题目集4~6作业总结

OO题目集4~6作业总结

1、前言

针对PTA题目集4~6作业,有如下分析:

题目集4知识点、难度、题量:

7-1:水文数据校验及处理:熟悉Java的正则表达式,使用正则表达式对测量日期进行校验;需要弄懂CheckData类、DealData类的结构等。

7-2:日期问题面向对象设计(聚合一):使用聚合对日期问题进行设计,实现求下n天、求前n天、求两个日期相差的天数三个功能要求,需要对聚合有足够的了解,才可以调用属性和对象。

7-3:图形继承:考察继承的知识点,需要熟悉父类与子类之间的关系,懂得如何调用父类的属性以及方法,知晓this、super等关键字的用法。

题目集4的难度:对于水文数据校验及处理,我感觉比较难,对于正则表达式掌握的还不够全面,题目集4主要是考察正则表达式以及对数据的处理。正则表达式的难度还是比较大的,特别是对于数据繁琐的时候,使用正则表达式有时还需要先将数据分割,再进行正则表达式的匹配。对于日期问题面向对象设计(聚合一),难度而言感觉主要是在聚合方面,对于聚合理解的还不够透彻,导致编码过程困难重重。对于图形继承,相比于7-1、7-2比较简单,了解继承的特点,使用extends进行继承属性,并重写方法。

 

题目集5知识点、难度、题量:

7-1:找出最长的单词-hebust:考察的知识点为分割字符串,形成字符串数组,再通过判断字符串数组中的字符串长度对最长单词进行输出。

7-2:合并两个有序数组为新的有序数组:将两个整型数组合并并升序输出。

7-3:对整型数据排序:对数据进行排序,考察三种排序方法,分别是插入排序、选择排序、冒泡排序。

7-4:统计Java程序中关键字的出现次数:接口的使用,关键字的判定,HashMap等结构的使用。

7-5:日期问题面向对象设计(聚合二):使用聚合对日期问题进行设计,实现求下n天、求前n天、求两个日期相差的天数三个功能要求,需要对聚合有足够的了解,才可以调用属性和对象。

题目集5的难度:对于7-1的题目,考察一个分割方法spilt的使用,之后进行长度检测,比较简单;对于7-2,合并有序数组,在之前的题目集1~3也写过,其实这题有很多解法,所以我也就偷懒直接用Arrays.sort的方法进行排序,相对简单;对于7-3,三种排序方法,也相对简单;对于7-4,感觉难度一下子就上来了,需要使用HashMap等结构,并且需要判定代码种的关键字,且注释不进行判别,难度很大;对于7-5,有了题目集4的聚合学习,对于聚合也有了一定的了解,这道题难度适中。

 

题目集6知识点、难度、题量:

7-1:正则表达式训练-QQ号校验:考察正则表达式

7-2:字符串训练-字符排序:考察ASCII值、字符排序

7-3:正则表达式训练-验证码校验:考察正则表达式

7-4:正则表达式训练-学号校验:考察正则表达式

7-5:图形继承与多态:考察ArrayList、继承和多态

7-6:实现图形接口及多态性:考察接口和多态性

题目集6的难度:题目整体比较简单,需要掌握正则表达式和ASCII值,7-5的题目考察ArrayList,有一定的难度,其他题目的难度都还好,简单或者适中。

2、设计与分析

由于篇幅限制,因此使用比较有代表性的题目代码进行分析。

①题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较

题目集4(7-2)代码如下:

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

类图如下:

 

 

 题目集5(7-4)代码如下:

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

类图如下:

 

 

题目集4的聚合一,感觉聚合不彻底,聚合代表的是整体与个体之间的关系,而这道题目的类图,并没有将day、month、year聚合在dateutil类中。

题目集5的聚合二,聚合完全,将day、month和year都聚合在了dateutil类中,通过main类调用dateutil实现功能。

②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

题目集4(7-3)代码如下:

  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 number = input.nextInt();
  9         if(number == 1) {
 10             double radius = input.nextDouble();
 11             if(radius < 0) {
 12                 System.out.println("Wrong Format");
 13             }else {
 14                 Circle circle = new Circle();
 15                 circle.setRadius(radius);
 16                 System.out.println("Circle's area:" + String.format("%.2f", circle.getArea()));
 17             }    
 18         }else if(number == 2) {
 19             double width = input.nextDouble();
 20             double length = input.nextDouble();
 21             if(width < 0||length < 0) {
 22                 System.out.println("Wrong Format");
 23             }else {
 24                 Rectangle rectangle = new Rectangle();
 25                 rectangle.setWidth(width);
 26                 rectangle.setLength(length);
 27                 System.out.println("Rectangle's area:" + String.format("%.2f", rectangle.getArea()));
 28             }
 29         }else if(number == 3) {
 30             double radius = input.nextDouble();
 31             if(radius < 0) {
 32                 System.out.println("Wrong Format");
 33             }else {
 34                 Ball ball = new Ball();
 35                 ball.setRadius(radius);
 36                 System.out.println("Ball's surface area:" + String.format("%.2f", ball.getArea()) + "\nBall's volume:" + String.format("%.2f", ball.getVolume()));
 37             }
 38         }else if(number == 4) {
 39             double width = input.nextDouble();
 40             double length = input.nextDouble();
 41             double height = input.nextDouble();
 42             if(width < 0||length < 0||height < 0) {
 43                 System.out.println("Wrong Format");
 44             }else {
 45                 Box box = new Box();
 46                 box.setWidth(width);
 47                 box.setLength(length);
 48                 box.setHeight(height);
 49                 System.out.println("Box's surface area:" + String.format("%.2f", box.getArea()) + "\nBox's volume:" + String.format("%.2f", box.getVolume()));
 50             }
 51         }else {
 52             System.out.println("Wrong Format");
 53         }
 54     }
 55 }
 56 
 57  class Shape {
 58     public Shape() {
 59         System.out.println("Constructing Shape");
 60     }
 61 
 62     public double getArea() {
 63         return 0.0;
 64     }
 65 }
 66 
 67  class Circle extends Shape {
 68     private double radius;
 69     public Circle() {
 70         System.out.println("Constructing Circle");
 71     }
 72     public double getRadius() {
 73         return radius;
 74     }
 75     public void setRadius(double radius) {
 76         this.radius = radius;
 77     }
 78     @Override
 79     public double getArea() {
 80         return Math.PI * Math.pow(radius, 2);
 81     }
 82 }
 83 
 84  class Rectangle extends Shape {
 85     private double width;
 86     private double length;
 87     public Rectangle() {
 88         System.out.println("Constructing Rectangle");
 89     }
 90     public double getWidth() {
 91         return width;
 92     }
 93     public void setWidth(double width) {
 94         this.width = width;
 95     }
 96     public double getLength() {
 97         return length;
 98     }
 99     public void setLength(double length) {
100         this.length = length;
101     }
102     @Override
103     public double getArea() {
104         return width * length;
105     }
106 }
107 
108  class Ball extends Circle {
109     private double radius;
110     public Ball() {
111         System.out.println("Constructing Ball");
112     }
113     public double getRadius() {
114         return radius;
115     }
116     public void setRadius(double radius) {
117         this.radius = radius;
118     }
119     @Override
120     public double getArea() {
121         return 4 * Math.PI * Math.pow(radius, 2);
122     }
123     public double getVolume() {
124         return (4 / 3.0) * Math.PI * Math.pow(radius, 3);
125     }
126 }
127 
128  class Box extends Rectangle {
129     private double width;
130     private double length;
131     private double height;
132     public Box() {
133         System.out.println("Constructing Box");
134     }
135     public double getWidth() {
136         return width;
137     }
138     public void setWidth(double width) {
139         this.width = width;
140     }
141     public double getLength() {
142         return length;
143     }
144     public void setLength(double length) {
145         this.length = length;
146     }
147     public double getHeight() {
148         return height;
149     }
150     public void setHeight(double height) {
151         this.height = height;
152     }
153     @Override
154     public double getArea() {
155         return 2 * (width * length + width * height + length * height);
156     }
157     public double getVolume() {
158         return width * length * height;
159     }
160 }

类图如下:

 

 

 题目集6(7-5、7-6)代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5 
  6     public static void main(String[] args) {
  7         Scanner input = new Scanner(System.in);
  8         int a = input.nextInt();
  9         int b = input.nextInt();
 10         int c = input.nextInt();
 11         int flag = 0;
 12         ArrayList<Double> aear = new ArrayList<Double>();
 13         if(a < 0||b < 0||c < 0) {
 14             System.out.println("Wrong Format");
 15         }else {
 16             Circle[] circle = new Circle[a];
 17             Rectangle[] rectangle = new Rectangle[b];
 18             Triangle[] triangle = new Triangle[c];
 19             for(int i = 0;i < a;i++) {
 20                 circle[i] = new Circle();
 21                 double radius = input.nextDouble();
 22                 circle[i].setRadius(radius);
 23                 aear.add(circle[i].getArea());
 24                 if(Data(circle[i]) == 0) {
 25                     flag = 1;
 26                 }
 27             }
 28             for(int i = 0;i < b;i++) {
 29                 rectangle[i] = new Rectangle();
 30                 double width = input.nextDouble();
 31                 double length = input.nextDouble();
 32                 rectangle[i].setLength(length);
 33                 rectangle[i].setWidth(width);
 34                 aear.add(rectangle[i].getArea());
 35                 if(Data(rectangle[i]) == 0) {
 36                     flag = 1;
 37                 }
 38             }
 39             for(int i = 0;i < c;i++) {
 40                 triangle[i] = new Triangle();
 41                 double side1 = input.nextDouble();
 42                 double side2 = input.nextDouble();
 43                 double side3 = input.nextDouble();
 44                 triangle[i].setSide1(side1);
 45                 triangle[i].setSide2(side2);
 46                 triangle[i].setSide3(side3);
 47                 aear.add(triangle[i].getArea());
 48                 if(Data(triangle[i]) == 0) {
 49                     flag = 1;
 50                 }
 51             }
 52             if(flag==1) {
 53                 System.out.println("Wrong Format");
 54             }else {
 55                 double sumaear = 0;
 56                 for(int i = 0;i < a+b+c;i++) {
 57                     sumaear = sumaear + (double) aear.get(i);
 58                 }
 59                 System.out.println("Original area:");
 60                 for(int i = 0;i < a+b+c;i++) {
 61                     System.out.printf("%.2f ",aear.get(i));
 62                 }
 63                 System.out.println();
 64                 System.out.printf("Sum of area:%.2f",sumaear);
 65                 System.out.println();
 66                 System.out.println("Sorted area:");
 67                 aear.sort(null);
 68                 for(int i = 0;i < a+b+c;i++) {
 69                     System.out.printf("%.2f ",aear.get(i));
 70                 }
 71                 System.out.println();
 72                 System.out.printf("Sum of area:%.2f",sumaear);
 73             }
 74         }
 75     }    
 76     
 77     public static  int Data(Object o) {
 78         if(o instanceof Shape)
 79         {
 80             if(((Shape) o).validate() == 0)
 81                 return 0;
 82             else
 83                 return 1;
 84         }
 85         return 0;
 86     }
 87 }
 88 
 89 class Shape{
 90     
 91     public double getArea() {
 92         return 0;
 93     }
 94     public int validate() {
 95         return 0;
 96     }
 97     public String toString() {
 98         String str = null;
 99         return str;
100     } 
101 }
102 
103 class Circle extends Shape{
104     private double radius;
105     public void setRadius(double radius) {
106         this.radius = radius;
107     }
108     
109     public double getArea() {
110         return Math.PI * radius * radius;
111     }
112     public int validate() {
113         if(radius > 0)
114             return 1;
115         else
116             return 0;
117     }
118 }
119 
120 class Rectangle extends Shape{
121     private double width;
122     private double length;
123     
124     public void setWidth(double width) {
125         this.width = width;
126     }
127     public void setLength(double lengh) {
128         this.length = lengh;
129     }
130     
131     public double getArea() {
132         return length * width;
133     }
134     public int validate() {
135         if(length > 0&&width > 0)
136             return 1;
137         else
138             return 0;
139     }
140 }
141 
142 class Triangle extends Shape{
143     private double side1;
144     private double side2;
145     private double side3;
146 
147     public void setSide1(double side1) {
148         this.side1 = side1;
149     }
150     public void setSide2(double side2) {
151         this.side2 = side2;
152     }
153     public void setSide3(double side3) {
154         this.side3 = side3;
155     }
156     
157     public double getArea() {
158         double p = (side1 + side2 + side3) / 2;
159         return Math.pow(p * (p - side1) * (p - side2) * (p - side3), 0.5);
160     }
161     public int validate() {
162         if(side1 + side2 > side3&&side1 + side3 > side2&&side3 + side2 > side1)
163             return 1;
164         else
165             return 0;
166     }
167 }
 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         double radius = input.nextDouble();
 9         double width = input.nextDouble();
10         double length = input.nextDouble();
11         Circle circle = new Circle();
12         Rectangle rectangle = new Rectangle();
13         if(radius <= 0 || width <= 0||length <= 0) {
14             System.out.println("Wrong Format");
15         }else {
16             circle.setRadius(radius);
17             rectangle.setWidth(width);
18             rectangle.setLength(length);
19             System.out.println(String.format("%.2f", circle.getArea()));
20             System.out.println(String.format("%.2f", rectangle.getArea()));
21         }
22     }
23 
24 }
25  interface GetArea {
26     public double getArea();
27 }
28  class Circle implements GetArea{
29     private double radius;
30     public Circle() {
31         
32     }
33     public Circle(double radius) {
34         this.radius = radius;
35     }
36     public double getRadius() {
37         return radius;
38     }
39 
40     public void setRadius(double radius) {
41         this.radius = radius;
42     }
43 
44     @Override
45     public double getArea() {
46         // TODO Auto-generated method stub
47         double area = Math.PI * Math.pow(radius, 2);
48         return area;
49     }
50 
51 }
52  class Rectangle implements GetArea{
53     private double width;
54     private double length;
55     public Rectangle() {
56         
57     }
58     public Rectangle(double width,double length) {
59         this.width = width;
60         this.length = length;
61     }
62     public double getWidth() {
63         return width;
64     }
65     public void setWidth(double width) {
66         this.width = width;
67     }
68     public double getLength() {
69         return length;
70     }
71     public void setLength(double length) {
72         this.length = length;
73     }
74     @Override
75     public double getArea() {
76         // TODO Auto-generated method stub
77         double area = getLength() * getWidth();
78         return area;
79     }
80 
81 }

类图如下:

 

 

 

 

 

 

题目集4(7-3)中circle类和rectangle类继承shape类,重写shape类的getArea()方法,ball类继承circle类,重写getArea()方法并定义求定义求体积方法。box类继承rectangle类的属性和getArea()方法,并定义新的属性height,重写父类的getArea()方法,并定义求立方体体积方法。各个子类封装自己的属性,属性无法被继承,只能重新定义。

题目集6(7-5)中circle类、rectangle类和triangle类继承同一个父类shape类,父类未定义属性,各个子类封装自己的属性,属性无法被继承,只能重新定义。

题目集6(7-6)中使用接口GetArea,定义方法getArea(),通过implements关键字实现接口,circle类和rectangle类重写getArea()方法。再circle类和rectangle类中定义它们自己的私有属性。

③对三次题目集中用到的正则表达式技术的分析总结

三次题目集多次用到正则表达式,我都对网络上的资料进行了参考(https://www.cnblogs.com/l-y-h/p/10907646.html)(https://www.runoob.com/regexp/regexp-tutorial.html)。

正则表达式(Regular Expression)是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符")。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。正则表达式是繁琐的,但它是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感。只要认真阅读本教程,加上应用的时候进行一定的参考,掌握正则表达式不是问题。许多程序设计语言都支持利用正则表达式进行字符串操作。

④题目集5(7-4)中Java集合框架应用的分析总结

本题我使用的是TreeMap,TreeMap 是一个有序的key-value集合,它是通过红黑树实现的。TreeMap 继承于AbstractMap,所以它是一个Map,即一个key-value集合。TreeMap 实现了NavigableMap接口,意味着它支持一系列的导航方法。比如返回有序的key集合。TreeMap 实现了Cloneable接口,意味着它能被克隆。TreeMap 实现了java.io.Serializable接口,意味着它支持序列化。TreeMap基于红黑树(Red-Black tree)实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。TreeMap的基本操作 containsKey、get、put 和 remove 的时间复杂度是 log(n) 。另外,TreeMap是非同步的。 它的iterator 方法返回的迭代器是fail-fastl的。

3、踩坑心得

pta的测试点不清楚,再编译器上面能够输出相应的结果,可是在pta上无法通过测试点,且也不知道是哪一部份不能通过测试点,很难进行修改。

 

 

 

 

 

 

4、改进建议

 在进行继承时,将共有的属性统一放在父类当中,可以提高代码的复用性;日期聚合需要将year、month、day都聚合到同一个类中,再通过main方法进行调用。

5、总结

通过这三次题目集的作业,让我掌握了更多的正则表达式知识,对聚合的了解也变得更加深,也懂得了继承的优劣以及多态性的好处。使用继承和多态,大大提高代码的复用性,减少代码规模,减低编码的繁琐程度,对于接口也有了一个简单的了解。这三次作业之后,还需要对正则表达式做更深一步的学习,对于类与类之间的关系也需要好好学习,依赖、组合、聚合、继承都需要好好了解。关于课程方面,我感觉没什么问题,就是pta方面,感觉题目的难易程度不是逐步上升的,而是忽高忽低,有时候做起来很顺手,有时候要花很多时间钻研,所以希望题目的难度可以逐步提升。

posted @ 2021-04-24 15:01  Imauselessp!  阅读(87)  评论(0)    收藏  举报