第二次博客作业
前言:(1)知识点:
1、 Java语言中类的声明、创建与使用;类的构造方法;成员变量、成员方法的定义与使用方法;
2、 java中引用变量与对象实例之间的关系与区别;
3、 java中方法调用时引用类型参数的传递过程;
4 、 java中图形继承 的使用方法;
5、 java中对象组合的方式与方法;
6、Java中数据排序的实现方式。
7、Java中正则表达式的使用方法
本次作业与之前几次相比,难度更大了一些,题量也有所提升,不过第六次的难度倒不是很大,它的考察目标主要集中在了对正则法则的掌握上面,复杂程度不是很高,我认为第四次作业和第五次作业才是难度比较大的,尤其是第四次作业的第一题和第五次作业的第二题,都需要考察对正则法则的使用。
设计与分析:
①题目集四7-2代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner x=new Scanner(System.in); int year=0,month=0,day=0,a,b; a=x.nextInt();//输入判断类型 year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日 DateUtil c=new DateUtil(year,month,day); if(a==1){//求下n天 b=x.nextInt();//输入n if(!c.checkInputValidity()||b<0){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.println(c.getNextNDays(b).showDate()); } else if(a==2){ b=x.nextInt();//输入n if(!c.checkInputValidity()||b<0){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.println(c.getPreviousNDays(b).showDate()); } else if(a==3){ int y1,m1,d1; y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日 DateUtil d=new DateUtil(y1,m1,d1); if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.println(c.getDaysofDates(d)); } else System.out.println("Wrong Format"); } } class DateUtil{ Day day; //默认构造方法 public DateUtil(){ } //带参构造方法 public DateUtil(int d,int m,int y){ this.day=new Day(d,m,y); } //getter public Day getDay(){ return day; } //setter public void setDay(Day d){ this.day=d; } //效验数据合法性 public boolean checkInputValidity(){ if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate()) return true; else return false; } //比较两个日期大小 public boolean compareDates(DateUtil date) { if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue()) return false; else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue()) return false; else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue()) return false; else return true; } //判定两个日期是否相等 public boolean equalTwoDates(DateUtil date){ if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue()) return true; else return false; } //日期值格式化 public String showDate(){ return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue(); } //计算该年的剩余天数 public int syts(DateUtil d){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int b=0,i; for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){ b=b+a[i]; } b=b+a[d.getDay().getMonth().getValue()]-d.getDay().getValue(); if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2)//闰年 b++; return b; } //求下n天 public DateUtil getNextNDays(int n){ int a1[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int a2[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int y=0,m=0,d=0,b=0; int i,j; int e = 0;//该月的天数 d=n+this.getDay().getValue(); m=this.getDay().getMonth().getValue(); y=this.getDay().getMonth().getYear().getValue(); b = syts(this); if(b > n) { if(this.getDay().getMonth().getYear().isLeapYear()) { e=a2[this.getDay().getMonth().getValue()]; } else e=a1[this.getDay().getMonth().getValue()]; while(d > e) { d=d-e; m=m+1; if(this.getDay().getMonth().getYear().isLeapYear()) { e=a2[m]; } else e=a1[m]; } } else { n=n-b; y=this.getDay().getMonth().getYear().getValue()+1; int c=365;//平年天数 if(new Year(y).isLeapYear()){//闰年天数 c++; } while(n-c>0){//找到年 n=n-c; y++; c=365; if(new Year(y).isLeapYear()) c++; } i=1; if(this.getDay().getMonth().getYear().isLeapYear()) { while(n-a2[i]>0&&i<=12){//找到月 n=n-a2[i]; i++; } } else { while(n-a1[i]>0&&i<=12){//找到月 n=n-a1[i]; i++; } } m=i; d=n;//找到天 } return new DateUtil(y, m, d); } //求前n天 public DateUtil getPreviousNDays(int n){ int a1[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int a2[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int y=0,m=0,d=0,b=0; int i=0,j=0; int e = 0;//该月的天数 d=this.getDay().getValue(); m=this.getDay().getMonth().getValue(); y=this.getDay().getMonth().getYear().getValue(); b =365 - syts(this); if(this.getDay().getMonth().getYear().isLeapYear()) { j = 1; } while(j == 1) { i++; j--; } if(this.getDay().getMonth().getYear().isLeapYear()) { b++; } if(b > n) { if(d > n) { d=d-n; } else { n=n-d; m=m-1; i=m; while(n-a1[i]>0&&i>=0){//找到月 n=n-a1[i]; m--; i--; } d=a1[i]-n;//找到天 if(new Year(y).isLeapYear()&&m==2){ d++; } } } else { n=n-b; y=y-1; int f=365; if(new Year(y).isLeapYear()){ f++; } while(n-f>0){//找到年 n=n-f; y--; f=365; if(new Year(y).isLeapYear()) f++; } i=12; while(n-a1[i]>0&&i>=0){//找到月 n=n-a1[i]; i--; } m=i; d=a1[i]-n;//找到天 if(new Year(f).isLeapYear()&&m==2){ d++; } } return new DateUtil(y, m, d); } //求两个日期之间的天数 public int getDaysofDates(DateUtil date){ DateUtil b1=this; DateUtil b2=date; int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if(this.equalTwoDates(date)){//如果两天的日期相等 return 0; } else if(!this.compareDates(date)){//如果日期大小不对 b1=date; b2=this; } int d2=date.getDay().getValue(); int m2=date.getDay().getMonth().getValue(); int y2=date.getDay().getMonth().getYear().getValue(); int d1=this.getDay().getValue(); int m1=this.getDay().getMonth().getValue(); int y1=this.getDay().getMonth().getYear().getValue(); int i,j = 0,ts=0; for(i=y1+1;i<y2;i++){//两个日期的年数之和 ts=ts+365; if(new Year(i).isLeapYear()) ts++; } if(b1.getDay().getMonth().getYear().isLeapYear()) { j = 1; } while(j == 1) { i++; j--; } if(y1==y2&&m1==m2){//年份相同,月份相同,日不同 ts=d2-d1; } else if(y1==y2&&m1!=m2){//年份相同,月份不同 if(b1.getDay().getMonth().getYear().isLeapYear())//是闰年 a[2]=29; ts=ts+a[m1]-d1;//小日期该月剩余的天数 ts=ts+d2;//大日期的天数 for(j=m1+1;j<=m2-1;j++)//月份天数和 ts+=a[j]; } else if(y1!=y2){//年份不同 ts=ts+a[m1]-d1;//小日期在该月剩余的天数 ts=ts+d2;//大日期在该月已经过的天数 for(j=m1+1;j<=12;j++)//小日期在该年剩余的天数 ts=ts+a[j]; for(j=m2-1;j>0;j--)//大日期在该年已经过的天数 ts=ts+a[j]; if(b1.getDay().getMonth().getYear().isLeapYear()&&m1<=2)//如果小日期该年为闰年且该天在1月或2月 ts++; if(b2.getDay().getMonth().getYear().isLeapYear()&&m2>2)//如果大日期该年为闰年且该天在1月或2月后 ts++; } return ts; } } class Year{ int value; //默认构造方法 public Year(){ } //带参构造方法 public Year(int value){ this.value=value; } //getter public int getValue(){ return value; } //setter public void setValue(int value){ this.value=value; } //判断year是否为闰年 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 true; else return false; } //年份加一 public void yearIncrement(){ value=value+1; } //年份减一 public void yearReduction(){ value=value-1; } } class Month{ int value; Year year; //默认构造方法 public Month(){ } //带参构造方法 public Month(int yearValue,int monthValue){ this.year=new Year(yearValue); this.value=monthValue; } //getter public int getValue(){ return value; } public Year getYear(){ return year; } //setter public void setValue(int value){ this.value=value; } public void setYear(Year year){ this.year=year; } //日期复位(1) public void resetMin(){ value=1; } //月份设置为12 public void resetMax(){ value=12; } //效验数据合法性 public boolean validate(){ if(value>=1&&value<=12) return true; else return false; } //月份加一 public void monthIncrement(){ value=value+1; } //月份减一 public void monthReduction(){ value=value-1; } } class Day{ int value; Month month; int a[]={31,28,31,30,31,30,31,31,30,31,30,31}; //默认构造方法 public Day(){ } //带参构造方法 public Day(int yearValue,int monthValue,int dayValue){ this.month=new Month(yearValue,monthValue); this.value=dayValue; } //getter public int getValue(){ return value; } public Month getMonth(){ return month; } //setter public void setValue(int value){ this.value=value; } public void setMonth(Month value){ this.month=value; } //日期复位(1) public void resetMin(){ value=1; } //日期设为该月最大值 public void resetMax(){ value=a[month.getValue()-1]; } //效验数据合法性 public boolean validate(){ if(this.getMonth().getYear().isLeapYear()) a[1]=29; if(value>=1&&value<=a[month.getValue()-1]) return true; else return false; } //日期加一 public void dayIncrement() { value=value+1; } //日期减一 public void dayReduction() { value=value-1; } }
生成类图:

题目集五7-5代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner x=new Scanner(System.in); int year=0,month=0,day=0,a,b; a=x.nextInt();//输入判断类型 year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日 DateUtil c=new DateUtil(year,month,day); if(a==1){//求下n天 b=x.nextInt();//输入n if(!c.checkInputValidity()||b<0){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.print(year+"-"+month+"-"+day+" next "+b+" days is:"+c.getNextNDays(b).showDate()); } else if(a==2){ b=x.nextInt();//输入n if(!c.checkInputValidity()||b<0){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.print(year+"-"+month+"-"+day+" previous "+b+" days is:"+c.getPreviousNDays(b).showDate()); } else if(a==3){ int y1,m1,d1; y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日 DateUtil d=new DateUtil(y1,m1,d1); if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.print("The days between "+year+"-"+month+"-"+day+" and "+y1+"-"+m1+"-"+d1+" are:"+c.getDaysofDates(d)); } else System.out.println("Wrong Format"); } } class DateUtil{ Day day; Month month; Year year; int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //默认构造方法 public DateUtil(){ } //带参构造方法 public DateUtil(int y,int m,int d){ this.year=new Year(y); this.month=new Month(m); this.day=new Day(d); } //getter public Day getDay(){ return day; } //setter public void setDay(Day d){ this.day=d; } public Year getYear(){ return year; } public void setYear(Year year){ this.year=year; } public Month getMonth(){ return month; } public void setMonth(Month value){ this.month=value; } public void setDayMin() { this.day.value=1; } public void setDatMax() { if(this.year.isLeapYear()) a[2]=29; this.day.value=a[this.month.value]; } //效验数据合法性 public boolean checkInputValidity(){ if(this.year.isLeapYear()) a[2]=29; if(this.getYear().validate()&&this.getMonth().validate()&&this.day.value<=a[this.month.value]) return true; else return false; } //比较两个日期大小 public boolean compareDates(DateUtil date) { if(date.getYear().getValue()<this.getYear().getValue()) return false; else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()<this.getMonth().getValue()) return false; else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()==this.getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue()) return false; else return true; } //判定两个日期是否相等 public boolean equalTwoDates(DateUtil date){ if(this.getDay().getValue()==date.getDay().getValue()&&this.getMonth().getValue()==date.getMonth().getValue()&& this.getYear().getValue()==date.getYear().getValue()) return true; else return false; } //日期值格式化 public String showDate(){ return this.getYear().getValue()+"-"+this.getMonth().getValue()+"-"+this.getDay().getValue(); } //计算该年的剩余天数 public int syts(DateUtil d){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int b=0,i; for(i=d.getMonth().getValue()+1;i<=12;i++){ b=b+a[i]; } b=b+a[d.getMonth().getValue()]-d.getDay().getValue(); if(d.getYear().isLeapYear()&&d.getMonth().getValue()<=2)//闰年 b++; return b; } //求下n天 public DateUtil getNextNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //int a2[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int day=this.day.getValue(); int month=this.month.getValue(); int year=this.year.getValue(); if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } for (int i = 0; i < n; i++) { day++; if (day > a[month]) { day = 1; month++; if (month > 12) { month = 1; year++; if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } else { a[2]=28; } } } } return new DateUtil(year, month, day); } //求前n天 public DateUtil getPreviousNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //int a2[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int day=this.day.getValue(); int month=this.month.getValue(); int year=this.year.getValue(); if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } for (int i = 0; i < n; i++) { day--; if (day < 1) { month--; day = a[month]; if (month < 1) { month = 12; day = a[month]; year--; if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } else { a[2]=28; } } } } return new DateUtil(year, month, day); } //求两个日期之间的天数 public int getDaysofDates(DateUtil date){ DateUtil b1=this; DateUtil b2=date; if(this.equalTwoDates(date)){//如果两天的日期相等 return 0; } else if(!this.compareDates(date)){//如果日期大小不对 b1=date; b2=this; } int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j,ts=0; for(i=b1.year.getValue()+1;i<b2.year.getValue();i++){//两个日期的年数之和 ts=ts+365; if(new Year(i).isLeapYear()) ts++; } if(b1.year.getValue()==b2.year.getValue()&&b1.month.getValue()==b2.month.getValue()){//年份相同,月份相同,日不同 ts=b2.getDay().getValue()-b1.getDay().getValue(); } else if(b1.year.getValue()==b2.year.getValue()&&b1.month.getValue()!=b2.month.getValue()){//年份相同,月份不同 if(b1.year.isLeapYear())//是闰年 a[2]=29; ts=ts+a[b1.month.getValue()]-b1.getDay().getValue();//小日期该月剩余的天数 ts=ts+b2.getDay().getValue();//大日期的天数 for(j=b1.month.getValue()+1;j<=b2.month.getValue()-1;j++)//月份天数和 ts+=a[j]; } else if(b1.year.getValue()!=b2.year.getValue()){//年份不同 ts=ts+a[b1.month.getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数 ts=ts+b2.getDay().getValue();//大日期在该月已经过的天数 for(j=b1.month.getValue()+1;j<=12;j++)//小日期在该年剩余的天数 ts=ts+a[j]; for(j=b2.month.getValue()-1;j>0;j--)//大日期在该年已经过的天数 ts=ts+a[j]; if(b1.year.isLeapYear()&&b1.month.getValue()<=2)//如果小日期该年为闰年且该天在1月或2月 ts++; if(b2.year.isLeapYear()&&b2.month.getValue()>2)//如果大日期该年为闰年且该天在1月或2月后 ts++; } return ts; } } class Year{ int value; //默认构造方法 public Year(){ } //带参构造方法 public Year(int value){ this.value=value; } //getter public int getValue(){ return value; } //setter public void setValue(int value){ this.value=value; } //判断year是否为闰年 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 true; else return false; } //年份加一 public void yearIncrement(){ value=value+1; } //年份减一 public void yearReduction(){ value=value-1; } } class Month{ int value; //Year year; //默认构造方法 public Month(){ } public Month(int value){ this.value=value; } //getter public int getValue(){ return value; } //setter public void setValue(int value){ this.value=value; } //日期复位(1) public void resetMin(){ value=1; } //月份设置为12 public void resetMax(){ value=12; } //效验数据合法性 public boolean validate(){ if(value>=1&&value<=12) return true; else return false; } //月份加一 public void monthIncrement(){ value=value+1; } //月份减一 public void monthReduction(){ value=value-1; } } class Day{ int value; //默认构造方法 public Day(){ } public Day(int value){ this.value=value; } //getter public int getValue(){ return value; } //setter public void setValue(int value){ this.value=value; } //日期复位(1) //日期加一 public void dayIncrement() { value=value+1; } //日期减一 public void dayReduction() { value=value-1; } }
生成类图:

优劣比较:大家可以看到,第一种方法是把各个类的方法打乱分布,把天的一些方法放到了日期里面,把月的方法放到了天里面,把年的方法放到了月里面,但是第二种方法就没有这么复杂,它是按照各种类来构造方法的,在调用各个方法时就没有那么麻烦了。总体来看,两种方法是差不多的,只是类里面构造的方法不同。
②题目集四7-3代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner x=new Scanner(System.in); int year=0,month=0,day=0,a,b; a=x.nextInt();//输入判断类型 year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日 DateUtil c=new DateUtil(year,month,day); if(a==1){//求下n天 b=x.nextInt();//输入n if(!c.checkInputValidity()||b<0){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.print(year+"-"+month+"-"+day+" next "+b+" days is:"+c.getNextNDays(b).showDate()); } else if(a==2){ b=x.nextInt();//输入n if(!c.checkInputValidity()||b<0){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.print(year+"-"+month+"-"+day+" previous "+b+" days is:"+c.getPreviousNDays(b).showDate()); } else if(a==3){ int y1,m1,d1; y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日 DateUtil d=new DateUtil(y1,m1,d1); if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法 System.out.println("Wrong Format"); System.exit(0); } else System.out.print("The days between "+year+"-"+month+"-"+day+" and "+y1+"-"+m1+"-"+d1+" are:"+c.getDaysofDates(d)); } else System.out.println("Wrong Format"); } } class DateUtil{ Day day; Month month; Year year; int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //默认构造方法 public DateUtil(){ } //带参构造方法 public DateUtil(int y,int m,int d){ this.year=new Year(y); this.month=new Month(m); this.day=new Day(d); } //getter public Day getDay(){ return day; } //setter public void setDay(Day d){ this.day=d; } public Year getYear(){ return year; } public void setYear(Year year){ this.year=year; } public Month getMonth(){ return month; } public void setMonth(Month value){ this.month=value; } public void setDayMin() { this.day.value=1; } public void setDatMax() { if(this.year.isLeapYear()) a[2]=29; this.day.value=a[this.month.value]; } //效验数据合法性 public boolean checkInputValidity(){ if(this.year.isLeapYear()) a[2]=29; if(this.getYear().validate()&&this.getMonth().validate()&&this.day.value<=a[this.month.value]) return true; else return false; } //比较两个日期大小 public boolean compareDates(DateUtil date) { if(date.getYear().getValue()<this.getYear().getValue()) return false; else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()<this.getMonth().getValue()) return false; else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()==this.getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue()) return false; else return true; } //判定两个日期是否相等 public boolean equalTwoDates(DateUtil date){ if(this.getDay().getValue()==date.getDay().getValue()&&this.getMonth().getValue()==date.getMonth().getValue()&& this.getYear().getValue()==date.getYear().getValue()) return true; else return false; } //日期值格式化 public String showDate(){ return this.getYear().getValue()+"-"+this.getMonth().getValue()+"-"+this.getDay().getValue(); } //计算该年的剩余天数 public int syts(DateUtil d){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int b=0,i; for(i=d.getMonth().getValue()+1;i<=12;i++){ b=b+a[i]; } b=b+a[d.getMonth().getValue()]-d.getDay().getValue(); if(d.getYear().isLeapYear()&&d.getMonth().getValue()<=2)//闰年 b++; return b; } //求下n天 public DateUtil getNextNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //int a2[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int day=this.day.getValue(); int month=this.month.getValue(); int year=this.year.getValue(); if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } for (int i = 0; i < n; i++) { day++; if (day > a[month]) { day = 1; month++; if (month > 12) { month = 1; year++; if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } else { a[2]=28; } } } } return new DateUtil(year, month, day); } //求前n天 public DateUtil getPreviousNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //int a2[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int day=this.day.getValue(); int month=this.month.getValue(); int year=this.year.getValue(); if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } for (int i = 0; i < n; i++) { day--; if (day < 1) { month--; day = a[month]; if (month < 1) { month = 12; day = a[month]; year--; if((year%4==0&&year%100!=0)||year%400==0) { a[2]=29; } else { a[2]=28; } } } } return new DateUtil(year, month, day); } //求两个日期之间的天数 public int getDaysofDates(DateUtil date){ DateUtil b1=this; DateUtil b2=date; if(this.equalTwoDates(date)){//如果两天的日期相等 return 0; } else if(!this.compareDates(date)){//如果日期大小不对 b1=date; b2=this; } int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j,ts=0; for(i=b1.year.getValue()+1;i<b2.year.getValue();i++){//两个日期的年数之和 ts=ts+365; if(new Year(i).isLeapYear()) ts++; } if(b1.year.getValue()==b2.year.getValue()&&b1.month.getValue()==b2.month.getValue()){//年份相同,月份相同,日不同 ts=b2.getDay().getValue()-b1.getDay().getValue(); } else if(b1.year.getValue()==b2.year.getValue()&&b1.month.getValue()!=b2.month.getValue()){//年份相同,月份不同 if(b1.year.isLeapYear())//是闰年 a[2]=29; ts=ts+a[b1.month.getValue()]-b1.getDay().getValue();//小日期该月剩余的天数 ts=ts+b2.getDay().getValue();//大日期的天数 for(j=b1.month.getValue()+1;j<=b2.month.getValue()-1;j++)//月份天数和 ts+=a[j]; } else if(b1.year.getValue()!=b2.year.getValue()){//年份不同 ts=ts+a[b1.month.getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数 ts=ts+b2.getDay().getValue();//大日期在该月已经过的天数 for(j=b1.month.getValue()+1;j<=12;j++)//小日期在该年剩余的天数 ts=ts+a[j]; for(j=b2.month.getValue()-1;j>0;j--)//大日期在该年已经过的天数 ts=ts+a[j]; if(b1.year.isLeapYear()&&b1.month.getValue()<=2)//如果小日期该年为闰年且该天在1月或2月 ts++; if(b2.year.isLeapYear()&&b2.month.getValue()>2)//如果大日期该年为闰年且该天在1月或2月后 ts++; } return ts; } } class Year{ int value; //默认构造方法 public Year(){ } //带参构造方法 public Year(int value){ this.value=value; } //getter public int getValue(){ return value; } //setter public void setValue(int value){ this.value=value; } //判断year是否为闰年 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 true; else return false; } //年份加一 public void yearIncrement(){ value=value+1; } //年份减一 public void yearReduction(){ value=value-1; } } class Month{ int value; //Year year; //默认构造方法 public Month(){ } public Month(int value){ this.value=value; } //getter public int getValue(){ return value; } //setter public void setValue(int value){ this.value=value; } //日期复位(1) public void resetMin(){ value=1; } //月份设置为12 public void resetMax(){ value=12; } //效验数据合法性 public boolean validate(){ if(value>=1&&value<=12) return true; else return false; } //月份加一 public void monthIncrement(){ value=value+1; } //月份减一 public void monthReduction(){ value=value-1; } } class Day{ int value; //默认构造方法 public Day(){ } public Day(int value){ this.value=value; } //getter public int getValue(){ return value; } //setter public void setValue(int value){ this.value=value; } //日期复位(1) //日期加一 public void dayIncrement() { value=value+1; } //日期减一 public void dayReduction() { value=value-1; } }
生成类图:

题目集六7-5代码:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in=new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); if(a<0||b<0||c<0) { System.out.println("Wrong Format"); System.exit(0); } int i=0,j=0,k=1; double sum = 0; double area[]= new double [a+b+c]; for(i=0;i<a;i++) { double radius = in.nextDouble(); Circle y = new Circle(); y.Circle(radius); area[i]=y.getArea(); if(y.validate()) { } else k=0; } for(i=0;i<b;i++) { double width = in.nextDouble(); double length = in.nextDouble(); Rectangle r = new Rectangle(); r.Rectangle(width, length); area[a+i]=r.getArea(); if(r.validate()) { } else k=0; } for(i=0;i<c;i++) { double side1 = in.nextDouble(); double side2 = in.nextDouble(); double side3 = in.nextDouble(); Triangle t = new Triangle(); t.Triangle(side1, side2, side3); area[a+b+i]=t.getArea(); if(t.validate()) { } else k=0; } if(a==0&&b==0&&c==0) { k=1; } if (k == 1 && (a >= 0 && b >= 0 && c >= 0)) { System.out.println("Original area:"); for (i = 0; i < a; i++) { System.out.print(String.format("%.2f", area[i]) + " "); } for (i = a; i < a + b; i++) { System.out.print(String.format("%.2f", area[i]) + " "); } for (i = a + b; i < a + b + c; i++) { System.out.print(String.format("%.2f", area[i]) + " "); } System.out.println(); for (i = 0; i < a + b + c; i++) { sum += area[i]; } System.out.println("Sum of area:" + String.format("%.2f", sum)); Arrays.sort(area); System.out.println("Sorted area:"); for (i = 0; i < a + b + c; i++) { System.out.print(String.format("%.2f", area[i]) + " "); } System.out.println(); sum =0; for (i = 0; i < a + b + c; i++) { sum += area[i]; } System.out.println("Sum of area:" + String.format("%.2f", sum)); } else { System.out.println("Wrong Format"); } } } abstract class Shape{ Shape(){ } abstract public double getArea(); abstract public boolean validate(); } class Circle extends Shape//继承自Shape { Shape shape; public double getArea(){ double s; s=Math.PI*radius*radius; return s; } public Circle(){ } double radius; public void Circle(double radius) { this.radius=radius; } public boolean validate(){ if(radius>0) return true; else return false; } } class Rectangle extends Shape { Shape shape; public Rectangle() { } private double width; private double length; public void Rectangle(double width,double length) { this.width=width; this.length=length; } @Override public boolean validate(){ if(width>0&&length>0) return true; else return false; } public double getArea() { // TODO Auto-generated method stub double s = width*length; return s; } } class Triangle extends Shape { Shape shape; Triangle(){ } double side1; double side2; double side3; @Override public boolean validate() { if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) { return false; } if(side1-side2>=side3||side1-side3>=side2||side2-side3>=side1) { return false; } return true; } public void Triangle(double side1,double side2,double side3) { this.side1=side1; this.side2=side2; this.side3=side3; } @Override public double getArea(){ double p=(side1+side2+side3)/2; double s=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); return s; } }
生成类图:

题目集六7-6代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in=new Scanner(System.in); double radius = in.nextDouble(); double width = in.nextDouble(); double length = in.nextDouble(); if(radius<=0||width<=0||length<=0) { System.out.println("Wrong Format"); System.exit(0); } GetArea c = new Circle(radius); GetArea r = new Rectangle(width,length); System.out.println(String.format("%.2f", c.getArea())); System.out.println(String.format("%.2f", r.getArea())); } } interface GetArea{ public double getArea(); } class Circle implements GetArea { public Circle(double radius) { this.radius = radius; } private double radius;//新定义一个半径 public void setRadius(double radius) {// 设置半径 this.radius = radius; } public double getRadius() {// 获取半径 return radius; } public double getArea() { // TODO Auto-generated method stub double S = Math.PI*radius*radius; return S;//重写父类的方法 } } class Rectangle implements GetArea { public Rectangle(double width,double length) { this.width = width; this.length = length; } private double width; private double length; public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getArea() { // TODO Auto-generated method stub double s = width*length; return s; } }
生成类图:

三种代码的思路比较:
第一种代码是考察对于继承的掌握情况,算是一道比较简单的题目,涉及的知识点也不是太多,只需要对于继承有基本的理解即可。第二种代码则是涉及到了继承与多态,需要在继承父类的情况下,对子类作出不同的改变,难度算是中等。第三种代码则是涉及到了继承、封装与多态,一开始我对于这道题目还是有点没搞清楚的,这也导致我错了很多次,这道题需要注意的地方就是在定义接口这里,在定义了之后,你还需要在子类里面给它写好方法。
③对三次题目集中用到的正则表达式技术的分析总结
说实话,在这三次题目集涉及到正则法则的题目中,我只有第六次题目集是可以完全做出来的,在前两次题目集中涉及到的正则法则题目对于我来说难度可能有点偏大,即使是参考了网上其他人做的代码之后,我也不能说自己可以完全掌握了这些题目,还是有部分代码是不能理解的。
涉及到的知识点:
正则的匹配规则、正则的运算符。
④题目集5(7-4)中Java集合框架应用的分析总结
代码:
import java.util.regex.Pattern; import java.util.Scanner; import java.util.Map; import java.util.HashMap; import java.util.regex.Matcher; import java.util.TreeMap; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 String[] KEYWORDS1 = { "abstract","assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "false", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null" }; String[] KEYWORDS2 = { "package", "private", "protected", "public","true", "return", "strictfp", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws","transient", "try", "void", "volatile", "while" }; String kg,exit="exit"; int i,n1,n2,flag=0; Map map1=new TreeMap(); Map map2=new TreeMap(); Scanner x = new Scanner(System.in); StringBuilder a = new StringBuilder(); kg = x.nextLine(); while( !kg.equals(exit)) { a.append(kg.replaceAll("//.*", " ").replaceAll("\".*\"", " "));//去掉"//"后和的内容以及双引号里的内容 kg = x.nextLine(); flag=1; } String b = a.toString().replaceAll("/\\*\\s*.*\\s*\\*/", " ");//去掉"/* */"里的内容,放入字符串b中 if(flag==0) { System.out.println("Wrong Format"); } // 循环找每个关键词出现的次数 for(i=0;i< KEYWORDS1.length;i++) { Pattern pattern1 = Pattern.compile("\\b"+KEYWORDS1[i]+"\\b");//创建关键词的正则表达式 Matcher matcher1 = pattern1.matcher(b);//字符串与关键词匹配 if(i<20) { Pattern pattern2 = Pattern.compile("\\b"+KEYWORDS2[i]+"\\b"); Matcher matcher2 = pattern2.matcher(b); n2=0; while(matcher2.find()) {//找到该关键词的话,记录该关键词的次数 n2++; } if(n2!=0){//把次数不是0的关键词替换为次数 map2.put(KEYWORDS2[i], n2); } } n1=0; while(matcher1.find()) {//找到该关键词的话,记录该关键词的次数 n1++; } if(i == KEYWORDS1.length-1&&n1>0) { n1=n1-1; } if(n1!=0){//把次数不是0的关键词替换为次数 map1.put(KEYWORDS1[i], n1); } } String mapa= String.valueOf(map1);//把map转化为字符串mapa String mapb=mapa.replace("{","").replace("}","").replace("[", "").replace("]", "");//把map1里的"{""}"去掉存入字符串map2 String[] mapc=mapb.split(", ");//把mapb根据", "分开,存入字符串数组mapv String map3= String.valueOf(map2); String map4=map3.replace("{","").replace("}","").replace("[", "").replace("]", "");//把map1里的"{""}"去掉存入字符串map2 String[] map5=map4.split(", "); //循环输出 for (i=0;i< mapc.length;i++){ String[] mapd=mapc[i].split("=");//把每个字符串map3根据"="分开,存入字符串数组map4 System.out.println(mapd[1]+"\t"+mapd[0]); } for (i=0;i< map5.length-1;i++){ String[] map6=map5[i].split("=");//把每个字符串map3根据"="分开,存入字符串数组map4 System.out.println(map6[1]+"\t"+map6[0]); } String[] map6=map5[map5.length-1].split("="); System.out.print(map6[1]+"\t"+map6[0]); } }
这道题我没构造其他类,所以就不放类图了吧,这道题的难点在于对于代码中关键字符的匹配并且还需要统计出现次数进行排序,不知道为什么,我的代码一直没有通过测试,我自己测试是都过了例子的,这道题是比较麻烦的,它需要对关键词出现次数进行排序,我是先转换成树图进行存取,然后再将里面的东西转换回字符串,使用split函数进行标志位分段输出。
踩坑心得
这次题目集出现的问题还是蛮多的,没有解决的问题大多集中在第四次题目集和第五次题目集当中。
题目集四7-1

大家可以看到,这三个测试点是我没有通过的地方,现在我大概有了一些思路,它需要我们去进行每列的数据匹配,对于不同的情况给出不同的输出,我对于每列的数据判断出了问题,不能实现这个功能。
题目集五7-4

大家可以看到,最后两个测试点我是没有成功过去的,对于这两个测试点我有一点猜想,在eclipse里我进行案例输入是没有出现问题的,但是在提交代码的时候出现了问题,可能是正常值与我预期的不符,我感觉是空值判断上出的问题。
改进建议
我认为在题目集四中的7-2代码可以更加精简,在三个计算前n天,后n天时可以采用相似的方法去计算,可以单独做出一个父类,这样在之后的要求改动中也可以减少一些不必要的步骤。
总结
虽然做了这么多次作业,但是我对于正则表达式的运用还是停留在表面,只能使用一些基础的匹配运算,在这个方面上我还需要加强掌握,另外在学习了封装、继承和多态这三个面向对象的基本特征之后,我对于Java与c的不同又有了更深的理解,另外多使用继承确实可以减少很多麻烦,我还需要多多学习这些东西。
浙公网安备 33010602011771号