PTA题目集4~6总结

打印 上一主题 下一主题

主题 933|帖子 933|积分 2799

一前言

  题目集四主要考察的是对LocalDate,ArrayList,HashSet等Java自带类的使用
  题目集五主要考察的是对正则表达式的使用,以及对其题目集三的时间题目的进行类结构的改变
  题目集六只有一道题,主要是对题目集四的第一题进行加大难度
  总的来说这几次的题目量比前面几次都要少,但是题目难度开始加深。
二设计与分析

   由于部分题目过于简单,所以这里主要是对题目集四的7-1,题目集五的7-5,7-6,题目集六的7-1进行分析
   题目集四7-1:当时见到这道题时认为难度较大且复杂,所以在这次题目集时放弃了写这道题,但由于题目集六的7-1题目是在此基础上的加深所以主要在后面分析
   题目集五7-5:此题目主要是我们将前面题目集三的7-3的进行如下类图的类结构更改

所以我们主要是对类图进行分析,我们发现各个类之间是聚合的关系,每一个类与类之间聚合,所以我们按照此类图设计类时应该由year到month到day到DateUtil之间一个个写。最后的题目要求的主要功能则在DateUtil这里实现,因为这里是最后一个聚合。
代码如下:
   
[code]  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             System.out.println(date.getNextNDays(m).showDate()); 31         } else if (choice == 2) { // test getPreviousNDays method 32             int n = 0; 33             year = Integer.parseInt(input.next()); 34             month = Integer.parseInt(input.next()); 35             day = Integer.parseInt(input.next()); 36  37             DateUtil date = new DateUtil(year, month, day); 38  39             if (!date.checkInputValidity()) { 40                 System.out.println("Wrong Format"); 41                 System.exit(0); 42             } 43  44             n = input.nextInt(); 45  46             if (n < 0) { 47                 System.out.println("Wrong Format"); 48                 System.exit(0); 49             } 50             System.out.println(date.getPreviousNDays(n).showDate()); 51         } else if (choice == 3) {    //test getDaysofDates method 52             year = Integer.parseInt(input.next()); 53             month = Integer.parseInt(input.next()); 54             day = Integer.parseInt(input.next()); 55  56             int anotherYear = Integer.parseInt(input.next()); 57             int anotherMonth = Integer.parseInt(input.next()); 58             int anotherDay = Integer.parseInt(input.next()); 59  60             DateUtil fromDate = new DateUtil(year, month, day); 61             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 62  63             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 64                 System.out.println(fromDate.getDaysofDates(toDate)); 65             } else { 66                 System.out.println("Wrong Format"); 67                 System.exit(0); 68             } 69         } 70         else{ 71             System.out.println("Wrong Format"); 72             System.exit(0); 73         } 74     } 75 public static class Day { 76     int value; 77     int mon_maxnum[]= new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};//数组储存各月份天数 78     Month month = new Month(); 79     Day(){ 80  81     } 82     Day(int yearValue,int monthValue,int dayValue){ 83         value = dayValue; 84         month = new Month( yearValue, monthValue); 85     } 86  87     public void setValue(int value) { 88         this.value = value; 89     } 90  91     public int getValue() { 92         return value; 93     } 94  95     public Month getMonth() { 96         return month; 97     } 98  99     public void setMonth(Month month) {100         this.month = month;101     }102     public void resetMin(){103         value = 1;104     }105     public void resetMax(){106           if(month.year.isLeapYear()){107             mon_maxnum[2]=29;108         }else{109               mon_maxnum[2]=28;110           }111         value = mon_maxnum[month.value];112     }113     boolean validate(){114         if(value>mon_maxnum[month.value]||value12||value2050||valuedate.day.month.year.value){238             return true;239         }else if(this.day.month.year.value==date.day.month.year.value){240             if(this.day.month.value>date.day.month.value){241                 return true;242             }else if(this.day.month.value==date.day.month.value){243                 if(this.day.value>date.day.value){244                     return true;245                 }else{246                     return false;247                 }248             }249         }else if(this.day.month.year.value

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

滴水恩情

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表