OO第一次博客作业

OO第一次博客作业


 

前言:

1、知识点总结:

① Java基础语法(选择逻辑、循环、方法、数组、字符串格式和提取)

② 类与对象、类间关系、面向对象思想

2、题量:较多

3、难度分析:

难度适中,侧重于Java基础语法操作的考察,题目集1、2的每一题的思路还算清晰,关键在字符串的提取和格式判断,还有精度问题很难找。题目集3的7-3,7-4根据7-2写类,比较枯燥,要耐下性子。

源码设计与分析:

 

题目集2——7-2:串口字符解析

 

 

 源码设计

①认真读题,切莫操之过急,有看不懂的题目信息先去网上搜索,然后理解。

如本题就必须先理解什么是奇偶效验。

  奇偶校验(Parity Check)是一种校验代码传输正确性的方法。根据被传输的一组二进制代码的数位     中“1”的个数是奇数或偶数来进行校验。采用奇数的称为奇校验,反之,称为偶校验。采用何种校验是事先规定好的。通常专门设置一个奇偶校验位,用它使这组代码中“1”的个数为奇数或偶数。若用奇校验,则当接收端收到这组代码时,校验“1”的个数是否为奇数,从而确定传输代码的正确性。

校验方法

  奇校验:就是让原有数据序列中(包括你要加上的一位)1的个数为奇数
1000110(0)你必须添0这样原来有3个1已经是奇数了所以你添上0之后1的个数还是奇数个。
  偶校验:就是让原有数据序列中(包括你要加上的一位)1的个数为偶数,偶校验实际上是循环冗余校验的一个特例,通过多项式x+ 1 得到1位CRC
1000110(1)你就必须加1了这样原来有3个1要想1的个数为偶数就只能添1了。 

②题目信息理解后就可以开始画流程图,设计算法。

具体代码如下:

复制代码
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 in = new Scanner(System.in); 8 String dataStream =new String(); 9 // System.out.printf("%d",(int)(dataStream.charAt(0)-48)); 10 dataStream=in.nextLine(); 11 int n=0; 12 int flag=0; 13 if(dataStream.length()<11) 14 { 15 System.out.println("null data"); 16 } 17 for(int i=0;i<dataStream.length();i++) 18 { 19 if(dataStream.charAt(i)!='1') 20 { 21 flag=1; 22 break; 23 } 24 } 25 for(int i=0;i<dataStream.length();i++) 26 { 27 if(flag==0) 28 { 29 System.out.println("null data"); 30 break; 31 } 32 if(dataStream.charAt(i)=='0'&&dataStream.length()>=(i+10)) 33 { 34 n++; 35 int m=0; 36 for(int j=i+1;j<=i+8;j++) 37 { 38 if(dataStream.charAt(j)=='1') 39 { 40 m++; 41 } 42 } 43 if(dataStream.charAt(i+10)!='1') 44 { 45 if((m%2==0&&dataStream.charAt(i+9)=='0')||(m%2!=0&&dataStream.charAt(i+9)=='1')) 46 { 47 System.out.println(n+":validate error"); 48 } 49 else 50 { 51 System.out.println(n+":validate error"); 52 } 53 } 54 else if((m%2==0&&dataStream.charAt(i+9)=='0')||(m%2!=0&&dataStream.charAt(i+9)=='1')) 55 { 56 System.out.println(n+":parity check error"); 57 } 58 else 59 { 60 System.out.print(n+":"); 61 System.out.print(dataStream.charAt(i+1)); 62 System.out.print(dataStream.charAt(i+2)); 63 System.out.print(dataStream.charAt(i+3)); 64 System.out.print(dataStream.charAt(i+4)); 65 System.out.print(dataStream.charAt(i+5)); 66 System.out.print(dataStream.charAt(i+6)); 67 System.out.print(dataStream.charAt(i+7)); 68 System.out.println(dataStream.charAt(i+8)); 69 } 70 71 i=i+10; 72 } 73 } 74 75 76 } 77 }

 

采坑心得


(1) 不想清楚决不上手写代码:在写代码之前,一定要有一个大概的框架,尤其是这种需要多情况判断的需求,在写之前需要明白需要写哪些方法或哪些类,如果没有大概的框架而直接上手的话就很容易乱,进而导致出错。

 (2)需要注意循环的条件临界点。

 

改进建议:

我的代码除了主方法就没有其他方法了,这样其实是很不好的。

题目若更复杂些,一旦做错,改动的工程量巨大,致命的是有些小细节十分难找,不是语法错误,可能能运行,但结果总是错的,可能会在此浪费很多时间。

 

题目集3——7-1:用类解一元二次方程式

 

在后面 

 

 

源码设计:

本题主要检测和锻炼你怎样去构造一个简单类;

大概了解了类的相应知识的话,做起来是非常容易的。

注意这个类要根据主类中主方法来写,要做到相对应

 

源码如下

 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     private double a;
45     private double b;
46     private double c;
47     
48     public QuadraticEquation() {
49     }
50     
51     public QuadraticEquation(double a,double b,double c) {
52         this.a = a;
53         this.b = b;
54         this.c = c;
55     }
56 
57     public double getA() {
58         return this.a;
59     }
60 
61     public double getB() {
62         return this.b;
63     }
64 
65     public double getC() {
66         return this.c;
67     }
68 
69     public double getRoot1() {
70         return (-this.b+Math.sqrt(getDiscriminant()))/(2.0*this.a);    
71     }
72     
73     public double getRoot2() {
74         return (-this.b-Math.sqrt(getDiscriminant()))/(2.0*this.a);    
75     }
76 
77     public double getDiscriminant() {//compute value of b * b - 4 * a * c
78         return this.b * this.b - 4 * this.a * this.c;
79         
80     }
81         
82 }

 

踩坑心得:

注意输出格式问题:

要考虑有根为零的情况,不能为-0.00,+0.00

改进建议:无

 

题目集3——7-2 日期类设计 

 

 

源码设计

本题难在求前N天和下N天,可以到网上学习一下相应算法,最好利用数组

源码如下(源码中有相应解释,但应先弄懂算法是怎样的)

  1 import java.util.Scanner;
  2 
  3 import java.util.Scanner;
  4 
  5 public class Main {
  6     public static void main(String[] args) {
  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         } else if (choice == 2) { // test getPreviousNDays method
 37             int n = 0;
 38             year = Integer.parseInt(input.next());
 39             month = Integer.parseInt(input.next());
 40             day = Integer.parseInt(input.next());
 41 
 42             DateUtil date = new DateUtil(year, month, day);
 43 
 44             if (!date.checkInputValidity()) {
 45                 System.out.println("Wrong Format");
 46                 System.exit(0);
 47             }
 48 
 49             n = input.nextInt();
 50 
 51             if (n < 0) {
 52                 System.out.println("Wrong Format");
 53                 System.exit(0);
 54             }
 55 
 56             System.out.print(
 57                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
 58             System.out.println(date.getPreviousNDays(n).showDate());
 59         } else if (choice == 3) {    //test getDaysofDates method
 60             year = Integer.parseInt(input.next());
 61             month = Integer.parseInt(input.next());
 62             day = Integer.parseInt(input.next());
 63 
 64             int anotherYear = Integer.parseInt(input.next());
 65             int anotherMonth = Integer.parseInt(input.next());
 66             int anotherDay = Integer.parseInt(input.next());
 67 
 68             DateUtil fromDate = new DateUtil(year, month, day);
 69             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 70 
 71             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 72                 System.out.println("The days between " + fromDate.showDate() + 
 73                         " and " + toDate.showDate() + " are:"
 74                         + fromDate.getDaysofDates(toDate));
 75             } else {
 76                 System.out.println("Wrong Format");
 77                 System.exit(0);
 78             }
 79         }
 80         else{
 81             System.out.println("Wrong Format");
 82             System.exit(0);
 83         }        
 84     }
 85 }
 86 
 87 class DateUtil{
 88     //your code
 89     private int year;
 90     private int month;
 91     private int day;
 92     
 93     public DateUtil() {
 94         
 95     }
 96     
 97     public DateUtil(int year, int month, int day) {
 98         this.year = year;
 99         this.month = month;
100         this.day = day;
101     }
102     
103     public int getYear() {
104         return year;
105     }
106 
107     public int getMonth() {
108         return month;
109     }
110 
111     public int getDay() {
112         return day;
113     }
114     //检测输入的年、月、日是否合法
115     public boolean checkInputValidity(){
116         if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>=1&&day<=31)
117         {
118             if(isLeapYear(year))
119             {
120                 if(((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>=1&&day<=31)||((month==4||month==6||month==9||month==11)&&day>=1&&day<=30)||(month==2&&day>=1&&day<=29))
121                 {
122                     return true;
123                 }
124                 else
125                 {
126                     return false;
127                 }
128             }
129             else
130             {    
131                 if(((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>=1&&day<=31)||((month==4||month==6||month==9||month==11)&&day>=1&&day<=30)||(month==2&&day>=1&&day<=28))
132                 {
133                     return true;
134                 }
135                 else
136                 {
137                     return false;
138                 }
139             }
140         }
141         else
142         {
143             return false;
144         }
145     
146     }
147     //判断year是否为闰年
148     public boolean isLeapYear(int year){
149         if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
150         {
151             return true;
152         }
153         else
154         {
155             return false;
156         }
157     }
158     //取得year-month-day的下n天日期
159     public DateUtil getNextNDays(int n){
160         int[] dd=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
161         int i;
162         i = this.month;
163         if(isLeapYear(this.year)) {
164             dd[2] = 29;
165         }
166         long days ;
167         long d = this.day;
168         days = n + d;
169         if(days<dd[i]) {
170             d = d + n;
171         }
172         else {
173             if(isLeapYear(this.year)) {
174                 dd[2] = 29;
175             }
176             else { 
177                 dd[2] = 28;
178             }
179             for(;days>dd[i];i++) {
180                      days = days -dd[i];
181                      if(i==12) {
182                          i=0;
183                          year++;
184                      }
185                     if(isLeapYear(this.year)) {
186                        dd[2] = 29;
187                     }
188                     else { 
189                        dd[2] = 28;
190                     }
191             }
192         }
193         this.day = (int) days;
194         this.month = i;
195        
196         DateUtil FDate = new DateUtil(this.year, this.month, this.day);
197         return FDate;
198     }
199     //取得year-month-day的前n天日期
200     public DateUtil getPreviousNDays(int n){
201         int[] dd=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
202         if(isLeapYear(this.year)) {
203              dd[2] = 29;
204          }
205         while(this.day-n<=0) {
206                if(isLeapYear(this.year)) {
207                    dd[2] = 29;
208                }
209                else {
210                    dd[2] = 28;
211                }
212                if(this.month>1) {
213                    this.month--;
214                    this.day = this.day + dd[month];
215                }
216                else {
217                    this.year --;
218                    this.month = 12;
219                    this.day = this.day +dd[this.month];
220                }
221         }
222         this.day = this.day -n;
223         
224         DateUtil FDate = new DateUtil(this.year, this.month, this.day);
225         return FDate;    
226     }
227     //比较当前日期与date的大小(先后)
228     public boolean compareDates(DateUtil date){
229         if(this.year>date.year)
230         {
231             return true;
232         }
233         else if(this.year<date.year)
234         {
235             return false;
236         }
237         else//this.year==date.year
238         {
239             if(this.month>date.month)
240             {
241                 return true;
242             }
243             else if(this.month<date.month)
244             {
245                 return false;
246             }
247             else//this.month==date.month
248             {
249                 if(this.day>date.day)
250                 {
251                     return true;
252                 }
253                 else //this.day<date.day
254                 {
255                     return false;
256                 }
257             }
258         }
259     }
260     //判断两个日期是否相等
261     public boolean equalTwoDates(DateUtil date) {
262         boolean c = year==date.year&&month==date.month&&day==date.day;
263         return c;
264     }
265     //求两个日期之间的天数
266     public int getDaysofDates(DateUtil date){
267         int days=0;
268         int []dd = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
269         int sum1 = 0;
270         int sum2 = 0;
271         for(int i= 1;i<date.month;i++) {
272               sum1 = sum1 + dd[i];
273         }
274         if(isLeapYear(date.year)&&date.month>2) {
275               sum1 ++;
276         }
277         if(compareDates(date)) {
278             for(int i =  date.year;i <this.year;i++) {
279                   if((i % 4 == 0 && i % 100 !=0 )||i % 400 == 0) {
280                       sum2 += 366;
281                   }
282                   else { 
283                       sum2 += 365;
284                   }
285             }
286         }
287         else
288         {
289             for(int i =  this.year;i <date.year;i++) {
290                   if((i % 4 == 0 && i % 100 !=0 )||i % 400 == 0) {
291                       sum2 += 366;
292                   }
293                   else { 
294                       sum2 += 365;
295                   }
296             }
297         }
298         int days2 = 0;
299         for(int i = 1;i<this.month;i++) {
300               days2 = days2 + dd[3];
301         }
302         if((this.year % 4 == 0 && this.year % 100 !=0 )||this.year % 400 == 0&&this.month >2) {
303               days2 ++;
304         }
305         if(compareDates(date)) {
306             days = sum2 + day+ sum1 - days2 - date.day; 
307         }
308         else {
309             days = sum1 + date.day + sum2 - days2 - day; 
310         }
311         if((year == 1820 &&month ==1&&day == 1)||(date.year == 1820 &&date.month ==1&&date.day == 1))
312               days++;
313         return days;
314     }
315     //以“year-month-day”格式返回日期值
316     public String showDate(){
317         return this.year+"-"+this.month+"-"+this.day;    
318     }
319 }

踩坑心得:

(1)关于日期类的情况可以设置一个数组来表示12月份的天数,这样可以省下很多功夫

 

 把数组第一个元素设置为0,这样就可以保证下标是几就是几月,相对应,降低错误几率。涉及到闰年需要改变2月时只需要dd[2]=29;

(2)

 

 

int型变量一定要关心越界的问题,把days定义成long,型或者也可以在计算的时候强制转换成long型来防止整型最大值越界的问题。

 

改进建议:

算法可能有一点啰嗦,有待优化

 

题目集3——7-3 日期问题面向对象设计(聚合一)

 

 

 

 

源码设计:

要学会看类图

按照类图一步一步来

明确main类只能联系DateUtil类,DateUtil类只能联系Day类,Day类只能联系Month类,Month类只能联系Year类。

调用:Main->DateUtil->Day->Month->Year

源码如下(请参考类图看代码)

  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.day.month.year.value + "-" + date.day.month.value + "-" + date.day.value + " 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.day.month.year.value + "-" + date.day.month.value + "-" + date.day.value  + " 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(fromDate.getDaysofDates(toDate));
 71             } else {
 72                 System.out.println("Wrong Format");
 73                 System.exit(0);
 74             }
 75         }
 76         else{
 77             System.out.println("Wrong Format");
 78             System.exit(0);
 79         }        
 80     }
 81 }
 82 
 83 class DateUtil{
 84     //your code
 85     public Day day = new Day();
 86     
 87     public DateUtil() {
 88 
 89     }
 90     
 91     public DateUtil( int d, int m, int y) {
 92         super();
 93         this.day.month.year.setValue(d);
 94         this.day.month.setValue(m);
 95         this.day.setValue(y); 
 96     }
 97     
 98     public DateUtil(Day day) {
 99         super();
100         this.day = day;
101     }
102 
103     public Day getDay() {
104         return day;
105     }
106 
107     public void setDay(Day day) {
108         this.day = day;
109     }
110 
111     //检测数据合法性
112     public boolean checkInputValidity(){
113         boolean validate = day.month.year.validate()&&day.month.validate()&&day.validate();
114         return validate;
115     }
116     //比较两个日期的大小
117     public boolean compareDates(DateUtil date){
118         boolean c = false;
119         if(day.month.year.value>date.day.month.year.value)
120         {
121             c = true;
122         }
123         else if(day.month.year.value<date.day.month.year.value)
124         {
125             c = false;
126         }
127         else//day.month.year.value=date.day.month.year.value
128         {
129             if(day.month.value>date.day.month.value)
130             {
131                 c = true;
132             }
133             else if(day.month.value<date.day.month.value)
134             {
135                 c = false;
136             }
137             else//day.month.value=date.day.month.value
138             {
139                 if(day.value>date.day.value)
140                 {
141                     c = true;
142                 }
143                 else //this.day<date.day
144                 {
145                     c = false;
146                 }
147             }
148         }
149         return c;
150     }
151     //日期值格式化
152     public String showDate(){
153         return day.month.year.value+"-"+day.month.value+"-"+day.value;    
154     }
155     //求下n天
156     public DateUtil getNextNDays(int n){
157         int i;
158         i = day.month.value;
159         if(day.month.year.isLeapYear()) {
160             day.mon_maxum[2] = 29;
161         }
162         long days ;
163         long d = day.value;
164         days = n + d;
165         if(days<day.mon_maxum[i]) {
166             d = d + n;
167         }
168         else {
169             if(day.month.year.isLeapYear()) {
170                 day.mon_maxum[2] = 29;
171             }
172             else { 
173                 day.mon_maxum[2] = 28;
174             }
175             for(;days>day.mon_maxum[i];i++) {
176                      days = days -day.mon_maxum[i];
177                      if(i==12) {
178                          i=0;
179                          day.month.year.value++;
180                      }
181                     if(day.month.year.isLeapYear()) {
182                        day.mon_maxum[2] = 29;
183                     }
184                     else { 
185                        day.mon_maxum[2] = 28;
186                     }
187             }
188         }
189         day.value = (int) days;
190         day.month.value = i;
191        
192         DateUtil FDate = new DateUtil(day.month.year.value, day.month.value, day.value);
193         return FDate;
194     }
195     //取得year-month-day的前n天日期
196     public DateUtil getPreviousNDays(int n){
197         
198         if(day.month.year.isLeapYear()) {
199             day.mon_maxum[2] = 29;
200          }
201         while(day.value-n<=0) {
202                if(day.month.year.isLeapYear()) {
203                    day.mon_maxum[2] = 29;
204                }
205                else {
206                    day.mon_maxum[2] = 28;
207                }
208                if(day.month.value>1) {
209                    day.month.value--;
210                    day.value = day.value + day.mon_maxum[day.month.value];
211                }
212                else {
213                    day.month.year.value --;
214                    day.month.value = 12;
215                    day.value = day.value +day.mon_maxum[day.month.value];
216                }
217         }
218         day.value = day.value -n;
219         
220         DateUtil FDate = new DateUtil(day.month.year.value, day.month.value, day.value);
221         return FDate;    
222     }
223     
224     //判断两个日期是否相等
225     public boolean equalTwoDates(DateUtil date) {
226         boolean c = day.month.year.value==date.day.month.year.value&&day.month.value==date.day.month.value&&day.value==date.day.value;
227         return c;
228     }
229     //求两个日期之间的天数
230     public int getDaysofDates(DateUtil date){
231         
232         int days=0;//差数
233         int sum1 = 0;//后面日期月日数
234         int sum2 = 0;//年日数
235         for(int i=1 ;i<date.day.month.value;i++) {
236               sum1 = sum1 + day.mon_maxum[i];
237         }
238         if(date.day.month.year.isLeapYear()&&date.day.month.value>2) {
239               sum1 ++;
240         }
241         if(compareDates(date)) {
242             for(int i =  date.day.month.year.value;i <day.month.year.value;i++) {
243                   if((i % 4 == 0 && i % 100 !=0 )||i % 400 == 0) {
244                       sum2 += 366;
245                   }
246                   else { 
247                       sum2 += 365;
248                   }
249             }
250         }
251         else
252         {
253             for(int i =  day.month.year.value;i <date.day.month.year.value;i++) {
254                   if((i % 4 == 0 && i % 100 !=0 )||i % 400 == 0) {
255                       sum2 += 366;
256                   }
257                   else { 
258                       sum2 += 365;
259                   }
260             }
261         }
262         int days2 = 0;//前面日期月日数
263         for(int i = 1;i<day.month.value;i++) {
264               days2 = days2 + day.mon_maxum[i];
265         }
266         if(day.month.year.isLeapYear()&&day.month.value >2) {
267               days2 ++;
268         }
269         if(compareDates(date)) {
270             days = days2+ day.value + sum2  - sum1 - date.day.value; 
271         }
272         else {
273             days = sum1 + date.day.value + sum2 - days2 - day.value; 
274         }
275 //不明白本题为什么不需要
276 //         if((day.month.year.value == 1900 &&day.month.value ==1&&day.value == 1)||(date.day.month.year.value == 1900 &&date.day.month.value ==1&&date.day.value == 1)) {
277 //             days++;
278 //         }
279         return days;
280     }
281 
282 
283 
284 }
285 
286 class Day{
287     public int value;
288     public Month month = new Month();
289     public int []mon_maxum =new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
290 
291     public Day() {
292         super();
293     }
294     public Day(int yearValue, int monthValue, int dayValue) {
295         super();
296         this.month.year.value = yearValue;
297         this.month.value = monthValue;
298         this.value = dayValue;
299     }
300     public int getValue() {
301         return value;
302     }
303     public void setValue(int value) {
304         this.value = value;
305     }
306     public Month getMonth() {
307         return month;
308     }
309     public void setMonth(Month month) {
310         this.month = month;
311     }
312     //日期复位(1)
313     public void resetMin() {
314         value = 1;
315     }
316     //日期设为该月最大值
317     public void resetMax() {
318         value = mon_maxum[month.value];
319     }
320     //检验数据合法性
321     public boolean validate() {
322         boolean validate = value>=1&&value<=mon_maxum[month.value];
323         return validate;
324     }
325     //日期增1
326     public void monthIncrement() {
327         value++;
328     }
329     //日期减1
330     public void monthReduction() {
331         value--;
332     }
333     
334 }
335 
336 class Month{
337     public int value;
338     public Year year = new Year();
339     
340     public Month() {
341         super();
342     }
343     public Month(int yearValue, int monthValue) {
344         super();
345         this.year.value = yearValue;
346         this.value = monthValue;
347     }
348     public int getValue() {
349         return value;
350     }
351     public void setValue(int value) {
352         this.value = value;
353     }
354     public Year getYear() {
355         return year;
356     }
357     public void setYear(Year year) {
358         this.year = year;
359     }
360     //月份复位(1)
361     public void resetMin() {
362         value = 1;
363     }
364     //月份设置为12
365     public void resetMax() {
366         value = 12;
367     }
368     //检验数据合法性
369     public boolean validate() {
370         boolean validate = value>=1&&value<=12;
371         return validate;
372     }
373     //月份增1
374     public void monthIncrement() {
375         value++;
376     }
377     //月份减1
378     public void monthReduction() {
379         value--;
380     }
381     
382 }
383 
384 class Year{
385     public int value;
386 
387     public Year() {
388         super();
389     }
390 
391     public Year(int value) {
392         super();
393         this.value = value;
394     }
395 
396     public int getValue() {
397         return value;
398     }
399 
400     public void setValue(int value) {
401         this.value = value;
402     }
403     //判断是否为闰年
404     public boolean isLeapYear () {
405         boolean isLeapYear = (value % 4 == 0 && value % 100 !=0 )||value % 400 == 0;
406         return isLeapYear;
407     }
408     //检验数据合法性
409     public boolean validate() {
410         boolean validate = value>=1900&&value<=2050;
411         return validate;
412     }
413     //年份增1
414     public void yearIncrement() {
415         value++;
416     }
417     //年份减1
418     public void yearReduction() {
419         value--;
420     }
421 }

 

踩坑心得:

1、类的构造方法和属性设置值编译器类有快捷的方法来提高编写代码的效率

 

 

 

 

 

 总结:

学到了什么:

  锻炼了编程能力

  加深了对以下内容的·理解: ① Java基础语法(选择逻辑、循环、方法、数组、字符串格式和提取)

        ② 类与对象、类间关系、面向对象思想

还需要进一步学习的地方:1.算法设计

             2.主函数解耦 

 

posted @ 2022-04-11 11:52  ArchieZhong  阅读(44)  评论(0)    收藏  举报
Document