ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PTA题目集总结

2022-04-11 01:01:11  阅读:155  来源: 互联网

标签:总结 题目 int System PTA month year date day


2022年4月9日 倪梓鳌

题目集2 7-2

题目: 串口字符解析

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

 

初看这道题的第一感觉就是有很多陌生的名词,很难去理解,需要自行去百度理解,一旦耐心将题目读完会发现这道题其实不算太难。

由于对java并不是特别了解,所以在使用java字符串的时候,很多关于String类的方法都不会用。还有就是在使用字符串的时候,没有特别的注意字符串的长度,在很多if语句判断的时候会出现超出长度的情况。 在学习了相关知识后很快完成了这道题目。

这道题目只有一个主类Main,没有使用多个类。

代码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         Scanner in=new Scanner(System.in);
 7         String num=in.next();
 8         int count=0;
 9         int amount=0;  // 统有效数据的个数
10         if(num.length()<11)
11         {
12             System.out.println("null data");
13         }
14         else
15         {
16             for(int i=0;i<num.length();i++)
17             {
18                 if(num.charAt(i)=='0')
19                 {
20                     count++;
21                 }
22             }
23             if(count==0)
24             {
25                 System.out.println("null data");
26             }
27             else
28             {
29                 for(int j=0;j<num.length();j++)
30                 {
31                     if(num.charAt(j)=='0')
32                     {
33                         amount++;
34                         int state=0;  //统计1的个数
35                         int number=j;   //截取0的位置
36                         String str=num.substring(number+1,number+9);
37                         //奇校验
38                         for(int k=j;k<=j+8;k++)
39                         {
40                             if(num.charAt(k)=='1')
41                             {
42                                 state++;
43                             }
44                         }
45                         if(state%2==0)  //奇校验为偶时
46                         {
47                             if(num.charAt(j+9)!='1' && num.charAt(j+10)!='1')
48                             {
49                                 System.out.println(amount+":validate error");
50                             }
51                             else if(num.charAt(j+9)!='1')
52                             {
53                                 System.out.println(amount+":parity check error");
54                             }
55                             else if(num.charAt(j+10)!='1')
56                             {
57                                 System.out.println(amount+":validate error");
58                             }
59                             else
60                             {
61                                 System.out.println(amount+":"+str);
62                             }
63                         }
64                         else  //奇校验为奇时
65                         {
66                             if(num.charAt(j+9)!='0' && num.charAt(j+10)!='1')
67                             {
68                                 System.out.println(amount+":validate error");
69                             }
70                             else if(num.charAt(j+9)!='0')
71                             {
72                                 System.out.println(amount+":parity check error");
73                             }
74                             else if(num.charAt(j+10)!='1')
75                             {
76                                 System.out.println(amount+":validate error");
77                             }
78                             else 
79                             {
80                                 System.out.println(amount+":"+str);
81                             }
82                         }
83                         j=j+10;
84                     }
85                 }
86             }
87         }    
88     }
89 }
View Code

 

题目集3 7-1

题目:用类解一元二次方程式 

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

 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 
 7         double a = Double.parseDouble(input.next());
 8         double b = Double.parseDouble(input.next());
 9         double c = Double.parseDouble(input.next());
10         
11         if(a == 0){
12             System.out.println("Wrong Format");
13             System.exit(0);
14         }
15         
16         //create a QuadraticEquation object
17         QuadraticEquation equation = new QuadraticEquation(a, b, c);
18         //get value of b * b - 4 * a * c
19         double discriminant = equation.getDiscriminant();
20         
21         System.out.println("a=" + equation.getA() +
22                 ",b=" + equation.getB() + 
23                 ",c=" + equation.getC()+":");
24 
25         if (discriminant < 0) {
26           System.out.println("The equation has no roots.");
27         }
28         else if (discriminant == 0)
29         {
30           System.out.println("The root is " + 
31                   String.format("%.2f", equation.getRoot1()));
32         }
33         else // (discriminant >= 0)
34         {
35           System.out.println("The roots are " + 
36                   String.format("%.2f", equation.getRoot1()) 
37             + " and " +  String.format("%.2f", equation.getRoot2()));
38         }
39     }
40 }
41 
42 class QuadraticEquation{
43         //your code
44 }
View Code

 

这道题偏简单,之前做个类似的,需要改进的是需要新建一个类去实现二元一次方程组的运算。

需要注意的是对一些特殊情况的处理,例如

1:b=0,c=0的时候就不能去套用公式计算;

2:a=0的时候非二次方程;

3:只有一个根的情况。

代码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO 自动生成的方法存根
 7     Scanner input = new Scanner(System.in);
 8 
 9      double a = Double.parseDouble(input.next());
10      double b = Double.parseDouble(input.next());
11      double c = Double.parseDouble(input.next());
12             
13      if(a == 0){
14          System.out.println("Wrong Format");
15          System.exit(0);
16      }
17             
18             //create a QuadraticEquation object
19      QuadraticEquation equation = new QuadraticEquation(a, b, c);
20 //            //get value of b * b - 4 * a * c
21      double discriminant = equation.getDiscriminant();
22             
23             System.out.println("a=" + equation.getA() +
24                     ",b=" + equation.getB() + 
25                     ",c=" + equation.getC()+":");
26 
27             if (discriminant < 0) {
28               System.out.println("The equation has no roots.");
29             }
30             else if (discriminant == 0)
31             {
32               System.out.println("The root is " + 
33                       String.format("%.2f", equation.getRoot1()));
34             }
35             else // (discriminant >= 0)
36             {
37               System.out.println("The roots are " + 
38                       String.format("%.2f", equation.getRoot1()) 
39                 + " and " +  String.format("%.2f", equation.getRoot2()));
40             }
41     }
42 }
43 
44 class QuadraticEquation{
45     
46     double A=0;
47     double B=0;
48     double C=0;
49     
50     public QuadraticEquation(double A, double B, double C) {
51         this.A=A;
52         this.B=B;
53         this.C=C;
54     }
55     
56     double getA(){
57         return A;
58     }
59     
60     double getB(){
61         return B;
62     }
63     
64     double getC(){
65         return C;
66     }
67     
68     double getDiscriminant(){
69         double result=0;
70         result=B*B-4*A*C;
71         return result;
72     }
73     
74     double getRoot1(){
75         double Root1=0;
76         if((B==0) && (C==0))
77         {
78             Root1=0;
79             return Root1;
80         }
81         else
82         {
83             Root1=(-B+Math.sqrt(B*B-4*A*C))/2*A;
84             return Root1;
85         }
86     }
87     
88     double getRoot2(){
89         double Root2=0;
90         Root2=(-B-Math.sqrt(B*B-4*A*C))/2*A;
91         return Root2;
92     }
93 }
View Code

 

题目集3 7-2

题目:日期类设计

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

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

 

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

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

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

程序主方法如下:

 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 }

 

这道题最大的难点在于日期的计算,因为一年中每个月的最大天数并不完全相同,所以在计算日期的时候需要分情况去讨论。

1-下n天:

在求下n天的时候为了方便计算,我将day赋值为1,再把减少的天数加到n上,如果n的值较大时,可以一年一年的去减天数,从而使n的值趋于一个偏小的值。这样会在循环的时候减小循环的次数。当n<366时,开始一个月一个月的缩小n的范围,我的方法是用一个大循环一个月一个月的加,下一个月是大月n就减31,小月-30,2月减28或29,12月的时候要注意年份的变化,day始终不变为1,当n小到无法进入下一个月的时候,day加上n即可。由两个循环完成整个算法。

while(n>366)
            {
                if(isLeapYear(i))
                {
                    n=n-366;
                }
                else
                {
                    n=n-365;
                }
                i++;
            }
            year=i;
            int j=day;
            n=n+(j-1);
            day=1;

 

2-前n天:

在做这个功能的时候我原本想故技重施将day赋值为1去计算,但实行的功能的过程中,发现并没有想象中的那么方便,再经过思考后我决定将day赋值为31去计算,方法与求下n天的大致类似。关键部分在于月份和年份的变化,例如1月和三月的时候。上面已经详细介绍过了,就不多赘述了。

if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
                n=n+(31-day);
                day=31;
            }

 

3-求两个日期相差的天数:

在做这个功能的时候我改了一次算法,第一次做的时候我想一天一天的去减直到到达所给天数为止,但步骤过于繁琐且非常容易出现逻辑错误,于是我尝试了另一个算法,因为题目合法日期为year∈[1820,2020]的一月一日。所以我把两个日期到1820年1月1日的天数求出来,在取两个天数的差值即为所求。

 

代码:

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         // TODO 自动生成的方法存根
  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.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
 35             System.out.println(date.getNextNDays(m).showDate());
 36          
 37             
 38         } else if (choice == 2) { // test getPreviousNDays method
 39             int n = 0;
 40             year = Integer.parseInt(input.next());
 41             month = Integer.parseInt(input.next());
 42             day = Integer.parseInt(input.next());
 43 
 44             DateUtil date = new DateUtil(year, month, day);
 45 
 46             if (!date.checkInputValidity()) {
 47                 System.out.println("Wrong Format");
 48                 System.exit(0);
 49             }
 50 
 51             n = input.nextInt();
 52 
 53             if (n < 0) {
 54                 System.out.println("Wrong Format");
 55                 System.exit(0);
 56             }
 57 
 58             System.out.print(
 59                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
 60             System.out.println(date.getPreviousNDays(n).showDate());
 61         } else if (choice == 3) {    //test getDaysofDates method
 62             year = Integer.parseInt(input.next());
 63             month = Integer.parseInt(input.next());
 64             day = Integer.parseInt(input.next());
 65 
 66             int anotherYear = Integer.parseInt(input.next());
 67             int anotherMonth = Integer.parseInt(input.next());
 68             int anotherDay = Integer.parseInt(input.next());
 69 
 70             DateUtil fromDate = new DateUtil(year, month, day);
 71             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 72 
 73             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 74                 System.out.println("The days between " + fromDate.showDate() + 
 75                         " and " + toDate.showDate() + " are:"
 76                         + fromDate.getDaysofDates(toDate));
 77             } else {
 78                 System.out.println("Wrong Format");
 79                 System.exit(0);
 80             }
 81         }
 82         else{
 83             System.out.println("Wrong Format");
 84             System.exit(0);
 85         }        
 86     }
 87 }
 88 
 89 class DateUtil{
 90     int year;
 91     int month;
 92     int day;
 93     
 94     public DateUtil(int A, int B, int C){
 95         this.year=A;
 96         this.month=B;
 97         this.day=C;
 98     }
 99     
100     int getYear() {
101         return year;
102     }
103     
104     int getMonth() {
105         return month;
106     }
107     
108     int getDay(){
109         return day;
110     }
111     
112     //判断是否为闰年
113     public boolean isLeapYear(int year)
114     {
115         if((year%4==0&&year%100!=0)||(year%4==0&&year%400==0))
116         {
117             return true;
118         }
119         else
120         {
121             return false;
122         }
123     }
124     
125     //判断是否为有效输入
126         public boolean checkInputValidity()
127         {
128             if(year>=1820&&year<=2020)
129             {
130                 if(month>=1&&month<=12)
131                 {
132                     if(isLeapYear(year))
133                     {
134                         if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day>0&&day<=31)||(month==2&&(day>0&&day<=29))||(month==4||month==6||month==9||month==11)&&(day>0&&day<=30))
135                         {
136                             return true;
137                         }
138                         else
139                         {
140                             return false;
141                         }
142                     }
143                     else
144                     {
145                         if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day>0&&day<=31)||(month==2&&(day>0&&day<=28))||(month==4||month==6||month==9||month==11)&&(day>0&&day<=30))
146                         {
147                             return true;
148                         }
149                         else
150                         {
151                             return false;
152                         }
153                     }
154                 }
155                 else
156                 {
157                     return false;
158                 }
159             }
160             else
161             {
162                 return false;
163             }
164         }
165         
166         //往后推
167         public DateUtil getNextNDays(int n) {
168             int i=year;
169             while(n>366)
170             {
171                 if(isLeapYear(i))
172                 {
173                     n=n-366;
174                 }
175                 else
176                 {
177                     n=n-365;
178                 }
179                 i++;
180             }
181             year=i;
182             int j=day;
183             n=n+(j-1);
184             day=1;
185             while(n>0)
186             {
187                 if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10) && (day+1>31)) {
188                     month=month+1;
189                     n=n-1;
190                     day=1;
191                 }
192                 else if((month==12) && (day+1>31)) {
193                     year=year+1;
194                     month=1;
195                     day=1;
196                     n=n-1;
197                 }
198                 else if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day+1<=31)) {
199                     day=day+1;
200                     n--;
201                 }
202                 else if((month==2) && (isLeapYear(year)) && (day+1>29)) {
203                     month=month+1;
204                     day=1;
205                     n=n-1;
206                 }
207                 else if((month==2) && (!isLeapYear(year)) && (day+1>28)) {
208                     month=month+1;
209                     day=1;
210                     n--;
211                 }
212                 else if((month==2) && (isLeapYear(year)) && (day+1<=29)) {
213                     day=day+1;
214                     n--;
215                 }
216                 else if((month==2) && (!isLeapYear(year)) && (day+1<=28)) {
217                     day=day+1;
218                     n--;
219                 }
220                 else if((month==4 || month==6 || month==9 || month==11) && (day+1>30)) {
221                     month=month+1;
222                     day=1;
223                     n=n-1;
224                 }
225                 else if((month==4 || month==6 || month==9 || month==11) && (day+1<=30)) {
226                     day=day+1;
227                     n--;
228                 }
229             }
230             
231             DateUtil nData=new DateUtil(year,month,day);
232             return nData;
233         }
234         
235     //往前推
236         public DateUtil getPreviousNDays(int n) {
237             if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
238                 n=n+(31-day);
239                 day=31;
240             }
241             else if((month==2) && (isLeapYear(year))) {
242                 n=n+(29-day);
243                 day=29;
244             }
245             else if((month==2) && (!isLeapYear(year))) {
246                 n=n+(28-day);
247                 day=28;
248             }
249             else if(month==4 || month==6 || month==9 || month==11) {
250                 n=n+(30-day);
251                 day=30;
252             }
253             while(true) {
254                 if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day-n>0)) {
255                         day=day-n;
256                         break;
257                 }
258                 else if((month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day-n<=0))
259                 {
260                     n=n-31;
261                     month=month-1;
262                     if(month==7) {
263                         day=31;
264                     }
265                     else if((month==2) && (isLeapYear(year))) {
266                         day=29;
267                     }
268                     else if((month==2) && (!isLeapYear(year))) {
269                         day=28;
270                     }
271                     else if(month==4 || month==6 || month==9 || month==11)
272                     {
273                         day=30;
274                     }
275                 }
276                 else if((month==1) && (day-n<=0)) {
277                     year=year-1;
278                     month=12;
279                     day=31;
280                     n=n-31;
281                 }
282                 else if((month==2) && (isLeapYear(year)) && (29-n<=0)) {
283                     n=n-29;
284                     month=month-1;
285                     day=31;
286                 }
287                 else if((month==2) && (isLeapYear(year)) && (29-n>0)) {
288                     day=day-n;
289                     break;
290                 }
291                 else if((month==2) && (!isLeapYear(year)) && (29-n<=0)) {
292                     n=n-28;
293                     month=month-1;
294                     day=31;
295                 }
296                 else if((month==2) && (!isLeapYear(year)) && (28-n>0)) {
297                     day=day-n;
298                     break;
299                 }
300                 else if((month==4 || month==6 || month==9 || month==11) && (30-n<=0)) {
301                     month=month-1;
302                     day=31;
303                     n=n-30;
304                 }
305                 else if((month==4 || month==6 || month==9 || month==11) && (30-n>0)) {
306                     day=day-n;
307                     break;
308                 }
309             }
310             DateUtil nData=new DateUtil(year,month,day);
311             return nData;
312         }
313         
314         //求当前日期与date之间相差的天数
315         public int getDaysofDates(DateUtil date) {
316             int i=year-1;
317             int j=date.year-1;
318             int numDay=0;
319             int otherYear=date.year;
320             int otherMonth=date.month;
321             int otherDay=date.day;
322             int numDay1=0;
323             int numDay2=0;
324             if(equalTwoDates(date))
325             {
326                 numDay=0;
327                 return numDay;
328             }
329             else
330             {
331                 while(i>=1820)
332                 {
333                     if(isLeapYear(i))
334                     {
335                         numDay1=numDay1+366;
336                     }
337                     else
338                     {
339                         numDay1=numDay1+365;
340                     }
341                     i--;
342                 }
343                 for(int p=1;p<month;p++)
344                 {
345                     if(p==1 || p==3 || p==5 || p==7 || p==8 || p==10 || p==12)
346                     {
347                         numDay1=numDay1+31;
348                     }
349                     else if((p==2) && (isLeapYear(year)))
350                     {
351                         numDay1=numDay1+29;
352                     }
353                     else if((p==2) && (!isLeapYear(year)))
354                     {
355                         numDay1=numDay1+28;
356                     }
357                     else if(p==4 || p==6 || p==9 || p==11)
358                     {
359                         numDay1=numDay1+30;
360                     }
361                 }
362                 numDay1=numDay1+day;
363                 while(j>=1820)
364                 {
365                     if(isLeapYear(j))
366                     {
367                         numDay2=numDay2+366;
368                     }
369                     else
370                     {
371                         numDay2=numDay2+365;
372                     }
373                     j--;
374                 }
375                 for(int p=1;p<date.month;p++)
376                 {
377                     if(p==1 || p==3 || p==5 || p==7 || p==8 || p==10 || p==12)
378                     {
379                         numDay2=numDay2+31;
380                     }
381                     else if((p==2) && (isLeapYear(date.year)))
382                     {
383                         numDay2=numDay2+29;
384                     }
385                     else if((p==2) && (!isLeapYear(date.year)))
386                     {
387                         numDay2=numDay2+28;
388                     }
389                     else if(p==4 || p==6 || p==9 || p==11)
390                     {
391                         numDay2=numDay2+30;
392                     }
393                 }
394                 numDay2=numDay2+date.day;
395                 if(numDay1>numDay2)
396                 {
397                     numDay=numDay1-numDay2;
398                 }
399                 else
400                 {
401                     numDay=numDay2-numDay1;
402                 }
403             }
404             return numDay;    
405         }
406         
407         //判断两日期是否相等
408         public boolean equalTwoDates(DateUtil date) {
409             if((year==date.year) && (month==date.month) && (day==date.day))
410             {
411                 return true;
412             }
413             else
414             {
415                 return false;
416             }
417             
418         }
419         
420         //比较日期大小
421         public boolean compareDates(DateUtil date) {
422             if(year>date.year) {
423                 return true;
424             }
425             else if(year==date.year)
426             {
427                 if(month>date.month)
428                 {
429                     return true;
430                 }
431                 else if(month<date.month)
432                 {
433                     return false;
434                 }
435                 else if(month==date.month)
436                 {
437                     if(day>date.day)
438                     {
439                         return true;
440                     }
441                     else
442                     {
443                         return false;
444                     }
445                 }
446             }
447             else if(year<date.year)
448             {
449                 return false;
450             }
451             return false;
452         }
453         
454     //返回结果日期
455     public String showDate() {
456         String Data1=year+"";
457         String Data2=month+"";
458         String Data3=day+"";
459         String Data=Data1+"-"+Data2+"-"+Data3;
460         return Data;
461     }
462 }
View Code

 

题目集3 7-3

题目:日期问题面向对象设计(聚合一)

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

 

这题最大难度在于设计多个类,由于之前做题的时候都只创建了一个类,所以在做这道题的时候略显困难,因此在做题前我特地看了MOOC上的视频,还参考的SCDN的部分知识完成了代码,此外需要注意的是输入和输出的格式与上题相比略有不同。还有就是类与类之间的耦合关系。

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         // TODO 自动生成的方法存根
  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             
 35             System.out.println(date.getNextNDays(m).showDate());
 36          
 37             
 38         } else if (choice == 2) { // test getPreviousNDays method
 39             int n = 0;
 40             year = Integer.parseInt(input.next());
 41             month = Integer.parseInt(input.next());
 42             day = Integer.parseInt(input.next());
 43 
 44             DateUtil date = new DateUtil(year, month, day);
 45 
 46             if (!date.checkInputValidity()) {
 47                 System.out.println("Wrong Format");
 48                 System.exit(0);
 49             }
 50 
 51             n = input.nextInt();
 52 
 53             if (n < 0) {
 54                 System.out.println("Wrong Format");
 55                 System.exit(0);
 56             }
 57 
 58         
 59             System.out.println(date.getPreviousNDays(n).showDate());
 60         } else if (choice == 3) {    //test getDaysofDates method
 61             year = Integer.parseInt(input.next());
 62             month = Integer.parseInt(input.next());
 63             day = Integer.parseInt(input.next());
 64 
 65             int anotherYear = Integer.parseInt(input.next());
 66             int anotherMonth = Integer.parseInt(input.next());
 67             int anotherDay = Integer.parseInt(input.next());
 68 
 69             DateUtil fromDate = new DateUtil(year, month, day);
 70             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 71 
 72             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 73                 System.out.println(fromDate.getDaysofDates(toDate));
 74             } else {
 75                 System.out.println("Wrong Format");
 76                 System.exit(0);
 77             }
 78         }
 79         else{
 80             System.out.println("Wrong Format");
 81             System.exit(0);
 82         }        
 83     }
 84 }
 85 
 86 class DateUtil{
 87     int year;
 88     int month;
 89     int day;
 90     
 91     public DateUtil(int A, int B, int C){
 92         this.year=A;
 93         this.month=B;
 94         this.day=C;
 95     }
 96     
 97     int getYear() {
 98         return year;
 99     }
100     
101     int getMonth() {
102         return month;
103     }
104     
105     int getDay(){
106         return day;
107     }
108     
109     //判断是否为闰年
110     public boolean isLeapYear(int year)
111     {
112         if((year%4==0&&year%100!=0)||(year%4==0&&year%400==0))
113         {
114             return true;
115         }
116         else
117         {
118             return false;
119         }
120     }
121     
122     //判断是否为有效输入
123         public boolean checkInputValidity()
124         {
125             if(year>=1900&&year<=2050)
126             {
127                 if(month>=1&&month<=12)
128                 {
129                     if(isLeapYear(year))
130                     {
131                         if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day>0&&day<=31)||(month==2&&(day>0&&day<=29))||(month==4||month==6||month==9||month==11)&&(day>0&&day<=30))
132                         {
133                             return true;
134                         }
135                         else
136                         {
137                             return false;
138                         }
139                     }
140                     else
141                     {
142                         if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day>0&&day<=31)||(month==2&&(day>0&&day<=28))||(month==4||month==6||month==9||month==11)&&(day>0&&day<=30))
143                         {
144                             return true;
145                         }
146                         else
147                         {
148                             return false;
149                         }
150                     }
151                 }
152                 else
153                 {
154                     return false;
155                 }
156             }
157             else
158             {
159                 return false;
160             }
161         }
162         
163         //往后推
164         public DateUtil getNextNDays(int n) {
165             int i=year;
166             while(n>366)
167             {
168                 if(isLeapYear(i))
169                 {
170                     n=n-366;
171                 }
172                 else
173                 {
174                     n=n-365;
175                 }
176                 i++;
177             }
178             year=i;
179             int j=day;
180             n=n+(j-1);
181             day=1;
182             while(n>0)
183             {
184                 if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10) && (day+1>31)) {
185                     month=month+1;
186                     n=n-1;
187                     day=1;
188                 }
189                 else if((month==12) && (day+1>31)) {
190                     year=year+1;
191                     month=1;
192                     day=1;
193                     n=n-1;
194                 }
195                 else if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day+1<=31)) {
196                     day=day+1;
197                     n--;
198                 }
199                 else if((month==2) && (isLeapYear(year)) && (day+1>29)) {
200                     month=month+1;
201                     day=1;
202                     n=n-1;
203                 }
204                 else if((month==2) && (!isLeapYear(year)) && (day+1>28)) {
205                     month=month+1;
206                     day=1;
207                     n--;
208                 }
209                 else if((month==2) && (isLeapYear(year)) && (day+1<=29)) {
210                     day=day+1;
211                     n--;
212                 }
213                 else if((month==2) && (!isLeapYear(year)) && (day+1<=28)) {
214                     day=day+1;
215                     n--;
216                 }
217                 else if((month==4 || month==6 || month==9 || month==11) && (day+1>30)) {
218                     month=month+1;
219                     day=1;
220                     n=n-1;
221                 }
222                 else if((month==4 || month==6 || month==9 || month==11) && (day+1<=30)) {
223                     day=day+1;
224                     n--;
225                 }
226             }
227             
228             DateUtil nData=new DateUtil(year,month,day);
229             return nData;
230         }
231         
232     //往前推
233         public DateUtil getPreviousNDays(int n) {
234             if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
235                 n=n+(31-day);
236                 day=31;
237             }
238             else if((month==2) && (isLeapYear(year))) {
239                 n=n+(29-day);
240                 day=29;
241             }
242             else if((month==2) && (!isLeapYear(year))) {
243                 n=n+(28-day);
244                 day=28;
245             }
246             else if(month==4 || month==6 || month==9 || month==11) {
247                 n=n+(30-day);
248                 day=30;
249             }
250             while(true) {
251                 if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day-n>0)) {
252                         day=day-n;
253                         break;
254                 }
255                 else if((month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day-n<=0))
256                 {
257                     n=n-31;
258                     month=month-1;
259                     if(month==7) {
260                         day=31;
261                     }
262                     else if((month==2) && (isLeapYear(year))) {
263                         day=29;
264                     }
265                     else if((month==2) && (!isLeapYear(year))) {
266                         day=28;
267                     }
268                     else if(month==4 || month==6 || month==9 || month==11)
269                     {
270                         day=30;
271                     }
272                 }
273                 else if((month==1) && (day-n<=0)) {
274                     year=year-1;
275                     month=12;
276                     day=31;
277                     n=n-31;
278                 }
279                 else if((month==2) && (isLeapYear(year)) && (29-n<=0)) {
280                     n=n-29;
281                     month=month-1;
282                     day=31;
283                 }
284                 else if((month==2) && (isLeapYear(year)) && (29-n>0)) {
285                     day=day-n;
286                     break;
287                 }
288                 else if((month==2) && (!isLeapYear(year)) && (29-n<=0)) {
289                     n=n-28;
290                     month=month-1;
291                     day=31;
292                 }
293                 else if((month==2) && (!isLeapYear(year)) && (28-n>0)) {
294                     day=day-n;
295                     break;
296                 }
297                 else if((month==4 || month==6 || month==9 || month==11) && (30-n<=0)) {
298                     month=month-1;
299                     day=31;
300                     n=n-30;
301                 }
302                 else if((month==4 || month==6 || month==9 || month==11) && (30-n>0)) {
303                     day=day-n;
304                     break;
305                 }
306             }
307             DateUtil nData=new DateUtil(year,month,day);
308             return nData;
309         }
310         
311         //求当前日期与date之间相差的天数
312         public int getDaysofDates(DateUtil date) {
313             int i=year-1;
314             int j=date.year-1;
315             int numDay=0;
316             int otherYear=date.year;
317             int otherMonth=date.month;
318             int otherDay=date.day;
319             int numDay1=0;
320             int numDay2=0;
321             if(equalTwoDates(date))
322             {
323                 numDay=0;
324                 return numDay;
325             }
326             else
327             {
328                 while(i>=1820)
329                 {
330                     if(isLeapYear(i))
331                     {
332                         numDay1=numDay1+366;
333                     }
334                     else
335                     {
336                         numDay1=numDay1+365;
337                     }
338                     i--;
339                 }
340                 for(int p=1;p<month;p++)
341                 {
342                     if(p==1 || p==3 || p==5 || p==7 || p==8 || p==10 || p==12)
343                     {
344                         numDay1=numDay1+31;
345                     }
346                     else if((p==2) && (isLeapYear(year)))
347                     {
348                         numDay1=numDay1+29;
349                     }
350                     else if((p==2) && (!isLeapYear(year)))
351                     {
352                         numDay1=numDay1+28;
353                     }
354                     else if(p==4 || p==6 || p==9 || p==11)
355                     {
356                         numDay1=numDay1+30;
357                     }
358                 }
359                 numDay1=numDay1+day;
360                 while(j>=1820)
361                 {
362                     if(isLeapYear(j))
363                     {
364                         numDay2=numDay2+366;
365                     }
366                     else
367                     {
368                         numDay2=numDay2+365;
369                     }
370                     j--;
371                 }
372                 for(int p=1;p<date.month;p++)
373                 {
374                     if(p==1 || p==3 || p==5 || p==7 || p==8 || p==10 || p==12)
375                     {
376                         numDay2=numDay2+31;
377                     }
378                     else if((p==2) && (isLeapYear(date.year)))
379                     {
380                         numDay2=numDay2+29;
381                     }
382                     else if((p==2) && (!isLeapYear(date.year)))
383                     {
384                         numDay2=numDay2+28;
385                     }
386                     else if(p==4 || p==6 || p==9 || p==11)
387                     {
388                         numDay2=numDay2+30;
389                     }
390                 }
391                 numDay2=numDay2+date.day;
392                 if(numDay1>numDay2)
393                 {
394                     numDay=numDay1-numDay2;
395                 }
396                 else
397                 {
398                     numDay=numDay2-numDay1;
399                 }
400             }
401             return numDay;    
402         }
403         
404         //判断两日期是否相等
405         public boolean equalTwoDates(DateUtil date) {
406             if((year==date.year) && (month==date.month) && (day==date.day))
407             {
408                 return true;
409             }
410             else
411             {
412                 return false;
413             }
414             
415         }
416         
417         //比较日期大小
418         public boolean compareDates(DateUtil date) {
419             if(year>date.year) {
420                 return true;
421             }
422             else if(year==date.year)
423             {
424                 if(month>date.month)
425                 {
426                     return true;
427                 }
428                 else if(month<date.month)
429                 {
430                     return false;
431                 }
432                 else if(month==date.month)
433                 {
434                     if(day>date.day)
435                     {
436                         return true;
437                     }
438                     else
439                     {
440                         return false;
441                     }
442                 }
443             }
444             else if(year<date.year)
445             {
446                 return false;
447             }
448             return false;
449         }
450         
451     //返回结果日期
452     public String showDate() {
453         String Data1=year+"";
454         String Data2=month+"";
455         String Data3=day+"";
456         String Data=Data1+"-"+Data2+"-"+Data3;
457         return Data;
458     }
459 }
View Code

 

踩坑心得

        这两次实验对我来说最大的提升在于对于某些复杂问题算法逻辑,很多题目我都推到重做了一遍,我能感受到第二次做的时候思路明显清晰了许多,所以我认为,在之后做题时候不应该拿到题目就莽做,而是先审题厘清思路,否则写出来的程序就会向我之前那样看起来不美观,非常凌乱。此外我一直是按照之前学习C语言的方法来写java的程序,没有特别注意程序之间的耦合关系,所以在之后写程序的时候我会尽量注意去设计类,做到一个类之做一类事,降低程序的耦合度。

 

标签:总结,题目,int,System,PTA,month,year,date,day
来源: https://www.cnblogs.com/nt1876605253/p/16123751.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有