题目集1~3的总结
- (1)前言
- (2)设计与分析
- (3)总结
一、前言
题目集01的考察比较基础难度偏低,几十行代码就差不多可以完成,只要C语言的基础较好,题目集01就不会有太大的问题,考察了java的输入输出及对String类的一些简单方法;题目集02的难度一般,题量较少,主要考察的知识点是对Sring类的charAt(i)的使用,如何精准的提取字符串的内容;题目集03的题目难度较高(对于我现在的水平),考察了对类的设计,加强了对类的认识。
二、设计与分析
题目集01:
7-2 串口字符解析
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。

代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = new String(); str = in.nextLine(); boolean isNull=false; int num = 0,num1=0,t=0; for(int i = 0;i<str.length();i++){ if(str.charAt(i)=='1'){ isNull=true; }else if(str.charAt(i)=='0') { t=i; isNull=false; break; } } for(int i=t;i<str.length();i++){ if(str.length()<11||isNull){ System.out.println("null data");//若数据不足11位或者输入数据全1没有起始位,则输出"null data", break; }else if(str.charAt(i)=='0'&&(i+10)<str.length()){ for(int j=i+1;j<=(i+9);j++){ if(str.charAt(j)=='1') { num1++; } } if(num1%2!=0&&str.charAt(10+i)!='1'){ num++; System.out.println(num+":validate error");//若数据结束符和奇偶校验均不合格,输出“validate error”。 }else if(num1%2==0&&str.charAt(10+i)=='1'){ num++; System.out.println(num+":parity check error");//若某个数据奇偶校验错误,则输出“parity check error”。 }else if(num1%2==0&&str.charAt(10+i)!='1'){ num++; System.out.println(num+":validate error");//若某个数据的结束符不为1,则输出“validate error”。 }else if(str.charAt(i)=='0'&&str.charAt(10+i)=='1'&&num1%2!=0){ num++; System.out.println(num+":"+str.subSequence(i+1,i+9)); } num1=0; i=i+10; } } } }
踩坑心得:
- 两组数据,一个为正常值,一个结束符不为1,不要判断完第一组数据的结束符就忘记判断接下来的数据的结束符是否为1。
- 三组数据,含结束符错误数据、奇偶校验错误数据和正常值,两种错误与正确的数据的情况的判断,对于这3种情况的正确输出,考虑要全面,把多种情况列出来分析,再把重复的情况排除,学会调试(debug),用debug不断测试,找出未能正确输出的代码,dubug可以帮助你快速的找出问题,而不是没有方向地修改代码。
- 本题题目表面看上去比较复杂,因为存在一些新的概念,要去读懂题目的意思,弄懂奇偶校验位,不要太着急直接写代码,同时要注意输出格式,如空格,回车等,以免浪费更多时间。
- 注意String的长度,设置访问限制,不要超过字符串的最大长度,这是经常容易出现的语法问题
改进建议:先判断首和尾是否符合要求,可以使用String的subSequence方法提取8位数据。
题目集03:
7-1 用类解一元二次方程式
定义一个代表一元二次方程ax2+bx+c=0的类QuadraticEquation,其属性为三个系数a、b、c(均为私有属性),类中定义的方法参考main方法中的代码。
代码:
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); double a = Double.parseDouble(input.next()); double b = Double.parseDouble(input.next()); double c = Double.parseDouble(input.next()); if(a == 0){ System.out.println("Wrong Format"); System.exit(0); } //create a QuadraticEquation object QuadraticEquation equation = new QuadraticEquation(a, b, c); //get value of b * b - 4 * a * c double discriminant = equation.getDiscriminant(); System.out.println("a=" + equation.getA() + ",b=" + equation.getB() + ",c=" + equation.getC()+":"); if (discriminant < 0) { System.out.println("The equation has no roots."); } else if (discriminant == 0){ System.out.println("The root is " + String.format("%.2f", equation.getRoot1())); } else // (discriminant >= 0){ System.out.println("The roots are " + String.format("%.2f", equation.getRoot1()) + " and " + String.format("%.2f", equation.getRoot2())); } } } class QuadraticEquation{ private double a,b,c; public QuadraticEquation() { super(); // TODO 自动生成的构造函数存根 } //your code public double getRoot2() { // TODO 自动生成的方法存根 return (-b-Math.sqrt(getDiscriminant()))/(2*a); } public double getRoot1() { // TODO 自动生成的方法存根 return (-b+Math.sqrt(getDiscriminant()))/(2*a); } public double getC() { // TODO 自动生成的方法存根 return c; } public double getB() { // TODO 自动生成的方法存根 return b; } public double getA() { // TODO 自动生成的方法存根 return a; } public double getDiscriminant() { // TODO 自动生成的方法存根 return b*b-4*a*c; } public QuadraticEquation(double a, double b, double c) { // TODO 自动生成的构造函数存根 this.a=a; this.b=b; this.c=c; } }
踩坑心得:
- 本题需要注意输出格式:两行和保留两位小数。
- 先判断x2的系数是否为0,再判断Δ的大小,最后再根据题目要求输出。
7-2 日期类设计
参考题目集二中和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:
public boolean checkInputValidity();//检测输入的年、月、日是否合法 public boolean isLeapYear(int year);//判断year是否为闰年 public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期 public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期 public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后) public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等 public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数 public String showDate();//以“year-month-day”格式返回日期值
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
有三种输入方式(以输入的第一个数字划分[1,3]):
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int year = 0; int month = 0; int day = 0; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } m = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); System.out.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } n = input.nextInt(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print( date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); System.out.println(date.getPreviousNDays(n).showDate()); } else if (choice == 3) { //test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else{ System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil { private int year = 0; private int month = 0; private int day = 0; public DateUtil() { super(); // TODO 自动生成的构造函数存根 } public DateUtil(int year, int month, int day) { // TODO 自动生成的构造函数存根 this.day=day; this.month=month; this.year=year; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public boolean checkInputValidity()//检测输入的年、月、日是否合法 { boolean check=true; if(year>2020||year<1820||month>12||month<1||day>31||day<1) { return false; } else { if(isLeapYear(year)) { if(month==2&&day>29) { check=false; } else if((month==4||month==6||month==9||month==11)&&day>30) { check=false; } } else { if(month==2&&day>28) { check=false; } else if((month==4||month==6||month==9||month==11)&&day>30) { check=false; } } } return check; } public boolean isLeapYear(int year){//判断year是否为闰年 boolean isLeapYear=false; if((year % 4 == 0&&year % 100 != 0)||year % 400 ==0){ isLeapYear = true; } else { isLeapYear = false; } return isLeapYear; } public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期 { while(n>0) { if(isLeapYear(year)){ if(month==2){ if(n<=29-day) { day+=n; n=0; } else { month++; n-=30-day; day=1; } } else if(month==1||month==3||month==5||month==7||month==8||month==10) { if(n<=31-day) { day+=n; n=0; } else { month++; n-=32-day; day=1; } } else if(month==12) { if(n<=31-day) { day+=n; n=0; } else { month=1; year++; n-=32-day; day=1; } } else if(month==4||month==6||month==9||month==11) { if(n<=30-day) { day+=n; n=0; } else { n-=31-day; day=1; month++; } } } else { if(month==2){ if(n<=28-day) { day+=n; n=0; } else { month++; n-=29-day; day=1; } } else if(month==1||month==3||month==5||month==7||month==8||month==10) { if(n<=31-day) { day+=n; n=0; } else { month++; n-=32-day; day=1; } } else if(month==12) { if(n<=31-day) { day+=n; n=0; } else { month=1; year++; n-=32-day; day=1; } } else if(month==4||month==6||month==9||month==11) { if(n<=30-day) { day+=n; n=0; } else { n-=31-day; day=1; month++; } } } } DateUtil date = new DateUtil(year, month, day); return date; } public DateUtil getPreviousNDays(int n){//取得year-month-day的前n天日期 while(n>0) { if(isLeapYear(year)) { if(month==1) { if(n>=day) { n-=day; day=31; month=12; year--; } else { day-=n; n=0; } } else if(month==5||month==7||month==10||month==12) { if(n>=day) { n-=day; day=30; month--; } else { day-=n; n=0; } } else if(month==8) { if(n>=day) { n-=day; day=31; month--; } else { day-=n; n=0; } } else if(month==3) { if(n>=day) { n-=day; day=29; month--; } else { day-=n; n=0; } } else if(month==2||month==4||month==6||month==9||month==11) { if(n>=day) { n-=day; day=31; month--; } else { day-=n; n=0; } } } else { if(month==1) { if(n>=day) { n-=day; day=31; month=12; year--; } else { day-=n; n=0; } } else if(month==5||month==7||month==10||month==12) { if(n>=day) { n-=day; day=30; month--; } else { day-=n; n=0; } } else if(month==8) { if(n>=day) { n-=day; day=31; month--; } else { day-=n; n=0; } } else if(month==3) { if(n>=day) { n-=day; day=28; month--; } else { day-=n; n=0; } } else if(month==2||month==4||month==6||month==9||month==11) { if(n>=day) { n-=day; day=31; month--; } else { day-=n; n=0; } } } } DateUtil date = new DateUtil(year, month, day); return date; } public boolean compareDates(DateUtil date){//比较当前日期与date的大小(先后) if(year>date.year) return true; else if(year<date.year) return false; else if(year==date.year&&month>date.month) return true; else if(year==date.year&&month<date.month) return false; else if(year==date.year&&month==date.month&&day>date.day) return true; else if(year==date.year&&month==date.month&&day<date.day) return false; else return false; } public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等 if(year==date.year&&month==date.month&&day==date.day) return true; else return false; } public int getDaysofDates(DateUtil date){//求当前日期与date之间相差的天数 if(equalTwoDates(date)) return 0; else { if(compareDates(date)) { return getDays()-date.getDays(); } else { return date.getDays()-getDays(); } } } public int getDays() { int days = 0; int i=0; month--; if(isLeapYear(year)){ while(month>0){ if(month==1||month==3||month==5||month==7||month==8||month==10){ month--; days+=31; } else if(month==4||month==6||month==9||month==11){ month--; days+=30; } else if(month==2){ month--; days+=29; } } days+=day; } else{ while(month>0){ if(month==1||month==3||month==5||month==7||month==8||month==10){ month--; days+=31; } else if(month==4||month==6||month==9||month==11){ month--; days+=30; } else if(month==2){ month--; days+=28; } } days+=day; } for(i=1;i<year;i++){ if(i%4==0&&i%100!=0||i%400==0) { days+=366;} else days+=365; } return days; } public String showDate(){//以“year-month-day”格式返回日期值 String str=year+"-"+month+"-"+day; return str; } }
踩坑心得:
- 注意边界值的测试,年,月,日。
- 平年和闰年的的区别,可以二月的天数。
- 加天数,月不变化,月变化,年不变化,年变化的情况,考虑全面。
- 判断天数是否小于每一个月的最大天数。
- 选项3可以用两组时间到同一年同一个月同一天的数值再相减,以求出两组时间相差的天数。
改进建议:
- 可以用数组存每个月的天数。(若为闰年,二月多加一天)
- 用循环来实现选项1和2,减少重复代码的出现。
- 可以建三个类实现三个选项的功能。
7-3 日期问题面向对象设计(聚合一)
参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner input = new Scanner(System.in); int year = 0; int month = 0; int day = 0; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } m = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } n = input.nextInt(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.println(date.getPreviousNDays(n).showDate()); } else if (choice == 3) { //test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println(fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else{ System.out.println("Wrong Format"); System.exit(0); } } } class Day{ private Month month=new Month(); private int value; private int [] mon_maxnum= {31,28,31,30,31,30,31,31,30,31,30,31}; public Day() { super(); // TODO 自动生成的构造函数存根 } public Day(int yearValue,int monthValue,int dayValue) { } public Month getMonth() { return month; } public void setMonth(Month month) { this.month = month; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public boolean validate() { if((value>31||value<1)&&(month.getValue()==1||month.getValue()==3||month.getValue()==5||month.getValue()==7||month.getValue()==8||month.getValue()==10||month.getValue()==12)|| (value>30||value<1)&&(month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11) ||(value>29||value<1)&&month.getValue()==2&&month.getYear().isLeapYear() ||(value>28||value<1)&&month.getValue()==2&&!month.getYear().isLeapYear()) return false; else return true; } public void dayIncrement() { if(value<mon_maxnum[month.getValue()-1]&&month.getValue()-1!=1) value++; else if(month.getYear().isLeapYear()&&month.getValue()-1==1&&value<29) { value++; } else if(!month.getYear().isLeapYear()&&month.getValue()-1==1&&value<28) { value++; } else { month.monthIncrement(); resetMin(); } } public void dayReduction() { if(value>1) value--; else { month.monthReduction(); resetMax(); } } public void resetMin() { value=1; } public void resetMax() { if(month.getYear().isLeapYear()&&month.getValue()==2) value=29; else value=mon_maxnum[month.getValue()-1]; } } class Month{ private Year year=new Year(); private int value; public Month() { super(); // TODO 自动生成的构造函数存根 } public Month(int yearValue,int monthValue) { super(); this.value=monthValue; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Year getYear() { return year; } public void setYear(Year year) { this.year = year; } public void monthIncrement() { if(value<12) value++; else { resetMin(); year.yearIncrement(); } } public void monthReduction() { if(value>1) value--; else { resetMax(); year.yearReduction(); } } public void resetMin() { value=1; } public void resetMax() { value=12; } public boolean validate() { if(value>12|value<1) return false; else return true; } } class Year{ public Year() { super(); // TODO 自动生成的构造函数存根 } private int value; public Year(int value) { super(); this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public boolean isLeapYear() { if((value % 4 == 0&&value % 100 != 0)||value % 400 ==0) return true; else return false; } public boolean validate() { if(value>2050||value<1900) return false; else return true; } public void yearIncrement() { value++; } public void yearReduction() { value--; } } class DateUtil{ private Day day = new Day(); public DateUtil() { super(); // TODO 自动生成的构造函数存根 } public DateUtil(int y, int m, int d) { super(); day.setValue(d); day.getMonth().setValue(m); day.getMonth().getYear().setValue(y); } public Day getDay() { return day; } public void setDay(Day day) { this.day = day; } public boolean checkInputValidity() { return (day.getMonth().getYear().validate()&&day.validate()&&day.getMonth().validate()); } public boolean compareDates(DateUtil date) { if(day.getMonth().getYear().getValue()>date.day.getMonth().getYear().getValue()) return true; else if(day.getMonth().getYear().getValue()<date.day.getMonth().getYear().getValue()) return false; else if(date.day.getMonth().getYear().getValue()==day.getMonth().getYear().getValue()&&day.getMonth().getValue()>date.day.getMonth().getValue()) return true; else if(date.day.getMonth().getYear().getValue()==day.getMonth().getYear().getValue()&&day.getMonth().getValue()<date.day.getMonth().getValue()) return false; else if(date.day.getMonth().getYear().getValue()==day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()&&day.getValue()>date.day.getValue()) return true; else if(date.day.getMonth().getYear().getValue()==day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()&&day.getValue()<date.day.getValue()) return false; else return false; } public int getDaysofDates(DateUtil date) { int a=getDays(),b=date.getDays(); if(equalTwoDates(date)) return 0; else { if(compareDates(date)) { return getDays()-date.getDays(); } else { return date.getDays()-getDays(); } } } public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等 if(day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()&&day.getValue()==date.day.getValue()) return true; else return false; } public String showDate() { String str=day.getMonth().getYear().getValue()+"-"+day.getMonth().getValue()+"-"+day.getValue(); return str; } public DateUtil getNextNDays(int n){//取得year-month-day的下n天日期 while(n>0) { day.dayIncrement(); n--; } DateUtil date = new DateUtil(day.getMonth().getYear().getValue(), day.getMonth().getValue(), day.getValue()); return date; } public DateUtil getPreviousNDays(int n) { while(n>0) { day.dayReduction(); n--; } DateUtil date = new DateUtil(day.getMonth().getYear().getValue(), day.getMonth().getValue(), day.getValue()); return date; } public int getDays() { int days = 0; int i=0; int m = day.getMonth().getValue()-1; int d; if(day.getMonth().getYear().isLeapYear()){ while(m>0){ if(m==1||m==3||m==5||m==7||m==8||m==10){ m--; days+=31; } else if(m==4||m==6||m==9||m==11){ m--; days+=30; } else if(m==2){ m--; days+=29; } } days+=day.getValue(); } else{ while(m>0){ if(m==1||m==3||m==5||m==7||m==8||m==10){ m--; days+=31; } else if(m==4||m==6||m==9||m==11){ m--; days+=30; } else if(m==2){ m--; days+=28; } } days+=day.getValue(); } for(i=1899;i<day.getMonth().getYear().getValue();i++){ if(i%4==0&&i%100!=0||i%400==0) { days+=366;} else days+=365; } return days; } }
我的类图:

踩坑心得:
- 分析清楚类图,弄清不同类之间是如何联系的。(“-”代表private,“+‘代表public)
- 注意该题与7-2的年的取值范围不一样。
- 先写classYear ,再写class Month ,然后再写class Day ,最后写class Dateutil。
改进建议:将类与类之间的的组合关系修改为聚合。
7-4 日期问题面向对象设计(聚合二)
参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner input = new Scanner(System.in); int year = 0; int month = 0; int day = 0; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } m = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print( date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:"); System.out.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } n = input.nextInt(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print( date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:"); System.out.println(date.getPreviousNDays(n).showDate()); } else if (choice == 3) { //test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else{ System.out.println("Wrong Format"); System.exit(0); } } } class Day{ private int value; public Day() { super(); // TODO 自动生成的构造函数存根 } public Day(int value) { super(); this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public void dayIncrement() { value++; } public void dayReduction() { value--; } public void resetMin() { value=1; } public void resetMax() { } } class Month{ private int value; public Month() { super(); // TODO 自动生成的构造函数存根 } public Month(int value) { super(); this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public void monthIncrement() { value++; } public void monthReduction() { value--; } public void resetMin() { value=1; } public void resetMax() { value=12; } public boolean validate() { if(value>12||value<1) return false; else return true; } } class Year{ public Year() { super(); // TODO 自动生成的构造函数存根 } private int value; public Year(int value) { super(); this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public boolean isLeapYear() { if((value % 4 == 0&&value % 100 != 0)||value % 400 ==0) return true; else return false; } public boolean validate() { if(value>2020||value<1820) return false; else return true; } public void yearIncrement() { value++; } public void yearReduction() { value--; } } class DateUtil{ private Day day = new Day(); private Year year=new Year(); private Month month=new Month(); private int [] mon_maxnum= {31,28,31,30,31,30,31,31,30,31,30,31}; public DateUtil() { super(); // TODO 自动生成的构造函数存根 } public DateUtil(int y, int m, int d) { super(); year.setValue(y); month.setValue(m); day.setValue(d); } public Day getDay() { return day; } public void setDay(Day day) { this.day = day; } public Year getYear() { return year; } public void setYear(Year year) { this.year = year; } public Month getMonth() { return month; } public void setMonth(Month month) { this.month = month; } public boolean checkInputValidity() {//检测输入的年、月、日是否合法 boolean check=true; if(year.getValue()>2020||year.getValue()<1820||!month.validate()||day.getValue()>31||day.getValue()<1) { return false; } else { if(year.isLeapYear()) { if(month.getValue()==2&&day.getValue()>29) { check=false; } else if((month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11)&&day.getValue()>30) { check=false; } } else { if(month.getValue()==2&&day.getValue()>28) { check=false; } else if((month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11)&&day.getValue()>30) { check=false; } } } return check; } public DateUtil getNextNDays(int n) {//取得year-month-day的下n天日期 while(n>0) { if(year.isLeapYear()){ if(month.getValue()==2){ if(n<=29-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { month.setValue(month.getValue()+1); n-=30-day.getValue(); day.setValue(1); } } else if(month.getValue()==1||month.getValue()==3||month.getValue()==5||month.getValue()==7||month.getValue()==8||month.getValue()==10) { if(n<=31-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { month.setValue(month.getValue()+1); n-=32-day.getValue(); day.setValue(1); } } else if(month.getValue()==12) { if(n<=31-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { month.setValue(1); year.setValue(year.getValue()+1); n-=32-day.getValue(); day.setValue(1); } } else if(month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11) { if(n<=30-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { n-=31-day.getValue(); day.setValue(1); month.setValue(month.getValue()+1); } } } else { if(month.getValue()==2){ if(n<=28-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { month.setValue(month.getValue()+1); n-=29-day.getValue(); day.setValue(1); } } else if(month.getValue()==1||month.getValue()==3||month.getValue()==5||month.getValue()==7||month.getValue()==8||month.getValue()==10) { if(n<=31-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { month.setValue(month.getValue()+1); n-=32-day.getValue(); day.setValue(1); } } else if(month.getValue()==12) { if(n<=31-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { month.setValue(1); year.setValue(year.getValue()+1); n-=32-day.getValue(); day.setValue(1); } } else if(month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11) { if(n<=30-day.getValue()) { day.setValue(day.getValue()+n); n=0; } else { n-=31-day.getValue(); day.setValue(1); month.setValue(month.getValue()+1); } } } } DateUtil date = new DateUtil(year.getValue(), month.getValue(), day.getValue()); return date; } public DateUtil getPreviousNDays(int n) {//取得year-month-day的前n天日期 while(n>0) { if(year.isLeapYear()) { if(month.getValue()==1) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(31); month.setValue(12); year.setValue(year.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==5||month.getValue()==7||month.getValue()==10||month.getValue()==12) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(30); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==8) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(31); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==3) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(29); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==2||month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(31); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } } else { if(month.getValue()==1) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(31); month.setValue(12); year.setValue(year.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==5||month.getValue()==7||month.getValue()==10||month.getValue()==12) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(30); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==8) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(31); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==3) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(28); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } else if(month.getValue()==2||month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11) { if(n>=day.getValue()) { n-=day.getValue(); day.setValue(31); month.setValue(month.getValue()-1); } else { day.setValue(day.getValue()-n); n=0; } } } } DateUtil date = new DateUtil(year.getValue(), month.getValue(), day.getValue()); return date; } public boolean compareDates(DateUtil date) {//比较当前日期与date的大小(先后) if(year.getValue()>date.year.getValue()) return true; else if(year.getValue()<date.year.getValue()) return false; else if(year.getValue()==date.year.getValue()&&month.getValue()>date.month.getValue()) return true; else if(year.getValue()==date.year.getValue()&&month.getValue()<date.month.getValue()) return false; else if(year.getValue()==date.year.getValue()&&month.getValue()==date.month.getValue()&&day.getValue()>date.day.getValue()) return true; else if(year.getValue()==date.year.getValue()&&month.getValue()==date.month.getValue()&&day.getValue()<date.day.getValue()) return false; else return false; } public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等 if(year.getValue()==date.year.getValue()&&month.getValue()==date.month.getValue()&&day.getValue()==date.day.getValue()) return true; else return false; } public int getDaysofDates(DateUtil date){//求当前日期与date之间相差的天数 if(compareDates(date)) return getDays()-date.getDays(); else return date.getDays()-getDays(); } private int getDays() { int days = 0; int i=0; month.setValue(month.getValue()-1); if(year.isLeapYear()){ while(month.getValue()>0){ if(month.getValue()==1||month.getValue()==3||month.getValue()==5||month.getValue()==7||month.getValue()==8||month.getValue()==10){ month.setValue(month.getValue()-1); days+=31; } else if(month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11){ month.setValue(month.getValue()-1); days+=30; } else if(month.getValue()==2){ month.setValue(month.getValue()-1); days+=29; } } days+=day.getValue(); } else{ while(month.getValue()>0){ if(month.getValue()==1||month.getValue()==3||month.getValue()==5||month.getValue()==7||month.getValue()==8||month.getValue()==10){ month.setValue(month.getValue()-1); days+=31; } else if(month.getValue()==4||month.getValue()==6||month.getValue()==9||month.getValue()==11){ month.setValue(month.getValue()-1); days+=30; } else if(month.getValue()==2){ month.setValue(month.getValue()-1); days+=28; } } days+=day.getValue(); } for(i=1;i<year.getValue();i++){ if(i%4==0&&i%100!=0||i%400==0) { days+=366;} else days+=365; } return days; } public String showDate(){//以“year-month-day”格式返回日期值 String str=year.getValue()+"-"+month.getValue()+"-"+day.getValue(); return str; } }
我的类图:

踩坑心得:
- 与题目集037-3不同,以class Dtaeutil为中心,将class Year,class Month,class Day聚合起来。
- 所有类(除主类)与class Dateutil 联系,再与主类联系。
三、总结:
- 做题前看清题目意思和题目的输出格式要求。
- 掌握了一些String类的常用方法,可以做到提取某几位的字面量。
- 对类的设计掌握不是很好,例如题目集03的7-3,题目要求是聚合关系的类图,而我自己写的代码生成的类图却是组合的关系,同时在完成题目集03所用时间较长。
- 题目集02、03的代码几乎没有面向对象的思维,多数是c语言风格的代码,总想着在一个类里面解决问题,可以构建多个类实现单一职责原则,做到低耦合,逻辑性更强,需要加强这方面的思维。
- 题目集03的7-2的代码中,部分代码重复出现,导致代码行数很多,写完代码后需要检查一下,把重复的代码糅合在一起。
- 掌握基本的语法规则,加强面向对象的思维。
- 做完题目集03时的思路不是特别清晰,完成时较吃力,需要重新复盘,厘清思路。

浙公网安备 33010602011771号