总结三次题目集的知识点、题量、难度等情况
一、前言
花了几个礼拜写完了这些题目集,让我对正则表达式里面的各种表达方式有了更深的了解,和正则表达式里的各种函数,掌握它们各自的作用,并且java中大部分情况是对字符串的操作,正让则表达式有着非比寻常的作用,我充分理解了正则表达式的重要作用在。在做这些题目的时候让我对正则表达式加深理解,也让我对Java更感兴趣,所以,在接下来的日子里,我会花更多时间在Java上。这三次作业的知识点于我而言,经常在定义方法的时候,不知道如何定义,习惯定义在main函数里,所以经常会在eclipse里头报错,但是如果指定义一个main函数,把其他内容书写进去的话就显得头重脚轻,对于正则表达式的相关内容,自己还摸索的不够透彻,对于相应对字符串的处理还不够清楚,需要再对正则表达式进行学习,对相应知识进行了解和深化,不断端相应知识进行学习。所以一定要能正确定义方法,还有方法的类型。知识点覆盖的较为全面,在写面向过程的程序来说,主要是为了解决某一个存在的问题,而面向对象的程序主要是为了解决某个对象要完成的功能,对我们的思考也有一定挑战,与此同时,在写程序的同时,也要注意数据的各种形式,方法的类型,类的调用等等,对我们的基本功也有一定的考察。这三次的题目总体来说有点难度,题目中大量要采用正则表达式,我没有对这个正则表达式进行预习,所以写程序的时候就会感到很艰难,经常感到无从下手,单从Java框架来说是没问题,但是对于主题算法就比较的困难,正则表达式针对字符串有一些特殊的操作,对于正则表达式的相关内容,自己还摸索的不够透彻,对于相应对字符串的处理还不够清楚,需要再对正则表达式进行学习,对相应知识进行了解和深化,不断端相应知识进行学习,因此在之后的Java学习中要不断地进行提前学习,而不能跟在老师的后面,那样的话就无法真正学到知识,这也就要求我们软件工程师要不断地学习,建立终生学习的学习目标。题量适中,难度也适中,知识点较多,对于中等学生来说较为适合,但是对正则表达式还是有一定困难,许多关于正则表达式的题目无从下手,题目大多为十天期限,对提高写作业效率有较大帮助,在写程序时问题出现较多,常常需要花大量时间来调试,对写程序的效率有较大的影响,常常要两三个小时来完成作业,这也就导致了有时实验报告无法按时提交的情况,但是在写程序的过程中,能让我学到更多知识,无论是上网查询到的知识还是同学告诉的知识,都使我不断地获取新知识,使我受益匪浅,在与他人交流题目的过程,也是增长我与他人交流能力的过程,对于今后的发张也有着至关重要的作用,在想算法的时候,也锻炼了我的思维能力,对我今后对各个事情的思考也有很大帮助,在写程序的同时,因为想不出该怎么下手也常常感到厌烦,但这也磨练了我们的耐心,使我在面对比较大的程序时采用分块的思想,将问题进行模块化,将这些小模块进行一个一个解决,锻炼自己的思维。
二设计与分析:
就7-2 日期问题面向对象设计(聚合一)这个题目而言,其类图如下:

由上面的类图可知,一共有四个类,分别为Day、Dateutil、Year、Month这四个类,分别对日期进行不同的操作,我认为这一题的知识点来说,知识点覆盖的较为全面,刚学java时将其与c语言进行对比起来学,刚开始进行对过程的程序过渡到面向对象的程序,给了我们熟悉的感觉,而面向对象的程序主要是为了解决某个对象要完成的功能,在写程序的同时,也要注意数据的各种形式,方法的类型,类的调用等等,对我们的细心也有一定的考察。
题代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int year = 0;
int month = 0;
int day = 0;
int a = sc.nextInt();
switch(a) {
case 1:
int b = 0;
year = Integer.parseInt(sc.next());
month = Integer.parseInt(sc.next());
day = Integer.parseInt(sc.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checkscValidity()) {
System.out.println("Wrong Format");
break;
}
b = sc.nextInt();
if (b < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue()
+ "-" + date.getDay().getValue() + " next " + b + " days is:");
System.out.println(date.getNextNDays(b).showDate());
break;
case 2:
int n = 0;
year = Integer.parseInt(sc.next());
month = Integer.parseInt(sc.next());
day = Integer.parseInt(sc.next());
DateUtil date1 = new DateUtil(year, month, day);
if (!date1.checkscValidity()) {
System.out.println("Wrong Format");
break;
}
n = sc.nextInt();
if (n < 0) {
System.out.println("Wrong Format");
break;
}
System.out.print(date1.getDay().getMonth().getYear().getValue() + "-" + date1.getDay().getMonth().getValue()
+ "-" + date1.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date1.getPreviousNDays(n).showDate());
break;
case 3:
year = Integer.parseInt(sc.next());
month = Integer.parseInt(sc.next());
day = Integer.parseInt(sc.next());
int anotherYear = Integer.parseInt(sc.next());
int anotherMonth = Integer.parseInt(sc.next());
int anotherDay = Integer.parseInt(sc.next());
DateUtil fromDate = new DateUtil(year, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
if (fromDate.checkscValidity() && toDate.checkscValidity()) {
System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
break;
}
break;
default:
System.out.println("Wrong Format");
break;
}
}
}
}
class DateUtil{
private Day day=new Day();
DateUtil(){
System.out.println("无参");
}
DateUtil(int x,int y,int z){
this.getDay().getMonth().getYear().setValue(x);
this.getDay().getMonth().setValue(y);
this.getDay().setValue(z);
}
public Day getDay() {
return day;
}
public void setDay(Day x) {
this.day = x;
}
public boolean checkscValidity() {
if(getDay().getMonth().getYear().validate()==true&&getDay().getMonth().validate()==true&&getDay().validate()==true) {
if(getDay().getMonth().getValue()==4||getDay().getMonth().getValue()==6||getDay().getMonth().getValue()==9||getDay().getMonth().getValue()==11) {
if(getDay().getValue()>31)
return false;
else
return true;
} else if(getDay().getMonth().getValue()==2) {
if(getDay().getMonth().getYear().isLeapYear()) {
if(getDay().getValue()>29)
return false;
else
return true;
}
else
{
if(getDay().getValue()>28)
return false;
else
return true;
}
}
else
return true;
}
else
return false;
}
public boolean compareDates(DateUtil date) {
if(getDay().getMonth().getYear().getValue()>date.getDay().getMonth().getYear().getValue())
return true;
else if(getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue()) {
if(getDay().getMonth().getValue()>date.getDay().getMonth().getValue())
return true;
else if(getDay().getMonth().getValue()==date.getDay().getMonth().getValue()) {
if(getDay().getValue()>date.getDay().getValue())
return true;
else if(getDay().getValue()==date.getDay().getValue())
return true;
else
return false;
} else
return false;
}
else
return false;
}
public boolean equalTwoDates(DateUtil date) {
if(getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue()&&getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&&getDay().getValue()==date.getDay().getValue())
return true;
else
return false;
}
public String showDate() {
int o=getDay().getMonth().getYear().getValue(),p=getDay().getMonth().getValue(),q=getDay().getValue();
String s=o+"-"+p+"-"+q;
return s;
}
public DateUtil getNextNDays(int n) {
int[] mon_maxnum= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int nd=day.getValue();
int nm=day.getMonth().getValue();
int ny=day.getMonth().getYear().getValue();
int as=0;
if(getDay().getMonth().getYear().isLeapYear())
mon_maxnum[2]=29;
else
mon_maxnum[2]=28;
if(n>mon_maxnum[nd]-nd) {
as=as+mon_maxnum[nd]-nd;
while(as<n&&n-as>31){
nm++;
if(nm==13) {
nm=1;
ny++;
}
getDay().getMonth().getYear().setValue(ny);
if(getDay().getMonth().getYear().isLeapYear())
mon_maxnum[2]=29;
else
mon_maxnum[2]=28;
as=as+mon_maxnum[nm];
}
nd++;
nd=n-as;
}
else
nd=nd+n;
DateUtil x=new DateUtil(ny,nm,nd);
return x;
}
public DateUtil getPreviousNDays(int n) {
int[] w= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int nd=getDay().getValue();
int nm=getDay().getMonth().getValue();
int ny=getDay().getMonth().getYear().getValue();
int as=0;
if(getDay().getMonth().getYear().isLeapYear())
w[2]=29;
else
w[2]=28;
if(n>nd) {
as=as+nd;
while(as<n&&n-as>31){
nm--;
if(nm==0) {
nm=12;
ny--;
}
getDay().getMonth().getYear().setValue(ny);
if(getDay().getMonth().getYear().isLeapYear())
w[2]=29;
else
w[2]=28;
as=as+w[nm];
}
nm--;
nd=w[nm]-(n-as);
}
else
nd=nd-n;
DateUtil x=new DateUtil(ny,nm,nd);
return x;
}
public int getDaysofDates(DateUtil date) {
int yearOne,yearTwo,monthOne,monthTwo,dayOne,dayTwo;
if(equalTwoDates(date)==false) {
if(compareDates(date)==true) {
yearOne=date.getDay().getMonth().getYear().getValue();
monthOne=date.getDay().getMonth().getValue();
dayOne=date.getDay().getValue();
yearTwo=getDay().getMonth().getYear().getValue();
monthTwo=getDay().getMonth().getValue();
dayTwo=getDay().getValue();
} else{
yearOne=getDay().getMonth().getYear().getValue();
monthOne=getDay().getMonth().getValue();
dayOne=getDay().getValue();
yearTwo=date.getDay().getMonth().getYear().getValue();
monthTwo=date.getDay().getMonth().getValue();
dayTwo=date.getDay().getValue();
}
int[] str={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,summ=0;
for(i=yearOne+1;i<yearTwo;i++) {
getDay().getMonth().getYear().setValue(i);
if(getDay().getMonth().getYear().isLeapYear())
summ=summ+366;
else
summ=summ+365;
}
getDay().getMonth().getYear().setValue(yearOne);
if(getDay().getMonth().getYear().isLeapYear())
str[2]=29;
if(yearOne!=yearTwo) {
summ=summ+str[monthOne]-dayOne;
monthOne++;
while(monthOne!=13) {
summ=summ+str[monthOne];
monthOne++;
}
getDay().getMonth().getYear().setValue(yearTwo);
if(getDay().getMonth().getYear().isLeapYear())
str[2]=29;
int j=1;
summ=summ+dayTwo;
while(j!=monthTwo) {
summ=summ+str[j];
j++;
}
}
else {
getDay().getMonth().getYear().setValue(yearOne);
if(getDay().getMonth().getYear().isLeapYear())
str[2]=29;
if(monthOne!=monthTwo) {
summ=summ+dayTwo+str[monthOne]-dayOne;
monthOne++;
while(monthOne!=monthTwo) {
summ=summ+str[monthOne];
monthOne++;
}
}
else
summ=summ+dayTwo-dayOne;
}
return summ;
}
else
return 0;
}
}
class Day{
private int value=0;
private Month month=new Month();
Day(){
}
Day(int yearValue,int monthValue,int dayValue) {
this.month.getYear().setValue(yearValue);
this.value=dayValue;
this.month.setValue(monthValue);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public void resetMin() {
setValue(1);
}
public void resetMax() {
setValue(31);
}
public boolean validate() {
if(getValue()>=1&&getValue()<=31){
return true;
}
else
return false;
}
public void dayIncrement() {
setValue(getValue()+1);
}
public void dayReduction() {
setValue(getValue()-1);
}
}
class Month{
private int value=0;
private Year year=new Year();
Month(){
}
Month(int yearValue,int monthValue) {
this.year.setValue(yearValue);
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 resetMin() {
setValue(1);
}
public void resetMax() {
setValue(12);
}
public boolean validate() {
if(getValue()>=1&&getValue()<=12) {
return true;
}
else
return false;
}
public void monthIncrement() {
setValue(getValue()+1);
}
public void monthReduction() {
setValue(getValue()-1);
}
}
class Year{
private int value;
Year(){
}
Year(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isLeapYear() {
if(getValue()%400==0||(getValue()%4==0&&getValue()%100!=0))
return true;
else
return false;
}
public boolean validate() {
if(getValue()>=1820&&getValue()<=2020) {
return true;
}
else
return false;
}
public void yearIncrement() {
setValue(getValue()+1);
}
public void yearReduction() {
setValue(getValue()-1);
}
}
7-5 日期问题面向对象设计(聚合二)分析,类图如下:

代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;
int xuanze = sr.nextInt();
if (xuanze == 1) { // test getNextNDays method
int x = 0;
year = Integer.parseInt(sr.next());
month = Integer.parseInt(sr.next());
day = Integer.parseInt(sr.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checksrValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
x = sr.nextInt();
if (x < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue()
+ "-" + date.getDay().getValue() + " next " + x + " days is:");
System.out.println(date.getNextNDays(x).showDate());
} else if (xuanze == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(sr.next());
month = Integer.parseInt(sr.next());
day = Integer.parseInt(sr.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checksrValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
n = sr.nextInt();
if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue()
+ "-" + date.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (xuanze == 3) { // test getDaysofDates method
year = Integer.parseInt(sr.next());
month = Integer.parseInt(sr.next());
day = Integer.parseInt(sr.next());
int anotherYear = Integer.parseInt(sr.next());
int anotherMonth = Integer.parseInt(sr.next());
int anotherDay = Integer.parseInt(sr.next());
DateUtil fromDate = new DateUtil(year, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
if (fromDate.checksrValidity() && toDate.checksrValidity()) {
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 Year{
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Year() {//无参构造
}
public Year(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<1820||value>2020)
return false;
else
return true;
}
public void yearIncrement() {//year++
value++;
}
public void yearReduction() {//year--
value--;
}
}
class Month {
private int value;
private Year year;
public Month() {//无参构造
}
public Month(int yearValue,int monthValue) {//有参构造
this.value=monthValue;
Year year = new Year(yearValue);
this.setYear(year);
}
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 resetMin() {//month复位为1
this.value = 1;
}
public void resetMax() {//month复位为12
this.value = 12;
}
public void monthIncrement() {//month++
value++;
}
public void monthReduction() {//month--
value--;
}
public boolean validate() {//检验合法性
if(value<1||value>12)
return false;
else
return true;
}
}
class Day{
private int value;
private Month month;
private int[] mon_maxnum= {31,28,31,30,31,30,31,31,30,31,30,31};
public int[] getMon_maxnum() {
return mon_maxnum;
}
public void setMon_maxnum(int i) {
this.mon_maxnum[1] = i;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public Day() {
}
public Day(int yearValue,int monthValue,int dayValue) {
this.value=dayValue;
Month month = new Month(yearValue,monthValue);
this.setMonth(month);
if(getMonth().getYear().isLeapYear())
mon_maxnum[1]=29;
}
public void Resetmin() {//复位日期为1
this.value=1;
}
public void Resetmax() {
this.value=mon_maxnum[getMonth().getValue()-1];
}
public boolean validate() {
if(this.value<1||this.value>mon_maxnum[getMonth().getValue()-1])
return false;
else
return true;
}
public void dayIncrement() {//day--
this.value--;
}
public void dayReduction() {//day++
this.value++;
}
}
class DateUtil {
private Day day;
public Day getDay() {
return day;
}
public void setDay(Day day) {
this.day = day;
}
public DateUtil() {
}
public DateUtil(int y,int m,int d) {
Day day =new Day(y,m,d);
this.setDay(day);
}
public boolean checksrValidity() {
if(!day.validate()||!day.getMonth().validate()||!day.getMonth().getYear().validate())
return false;
else
return true;
}
public boolean equalTwoDates(DateUtil date) {
if(this.day==date.day)
return true;
else
return false;
}
public boolean compareDates(DateUtil date) {
if(date.getDay().getMonth().getYear().getValue()>getDay().getMonth().getYear().getValue()||(date.getDay().getMonth().getYear()==this.getDay().getMonth().getYear()&&date.getDay().getMonth().getValue()>this.getDay().getMonth().getValue())||(date.getDay().getMonth()==this.getDay().getMonth()&&date.getDay().getValue()>this.getDay().getValue()))
return true;
else
return false;
}
public String showDate(){
return String.valueOf(this.getDay().getMonth().getYear().getValue())+"-"+String.valueOf(this.getDay().getMonth().getValue())+"-"+String.valueOf(this.getDay().getValue());
}
public DateUtil getNextNDays(int n) {
DateUtil data = new DateUtil(this.getDay().getMonth().getYear().getValue(),this.getDay().getMonth().getValue(),this.getDay().getValue());
DateUtil help = new DateUtil(this.getDay().getMonth().getYear().getValue()+1,this.getDay().getMonth().getValue(),this.getDay().getValue());
while(n>365) {
if(data.getDay().getMonth().getValue()<=2)
if(data.getDay().getMonth().getYear().isLeapYear())
n-=366;
else
n-=365;
else
if(help.getDay().getMonth().getYear().isLeapYear())
n-=366;
else
n-=365;
data.getDay().getMonth().getYear().yearIncrement();
help.getDay().getMonth().getYear().yearIncrement();
}
if(data.getDay().getMonth().getYear().isLeapYear()&&data.getDay().getMonth().getValue()<=2&&data.getDay().getMonth().getYear().getValue()!=this.getDay().getMonth().getYear().getValue())
n++;
while(n>30) {
if(data.getDay().getMonth().getYear().isLeapYear()) {
data.getDay().setMon_maxnum(29);
n-=data.getDay().getMon_maxnum()[data.getDay().getMonth().getValue()-1];
}
else {
data.getDay().setMon_maxnum(28);
n-=data.getDay().getMon_maxnum()[data.getDay().getMonth().getValue()-1];
}
data.getDay().getMonth().monthIncrement();
if(data.getDay().getMonth().getValue()>12) {
data.getDay().getMonth().setValue(1);
data.getDay().getMonth().getYear().yearIncrement();
}
}
if(data.getDay().getMonth().getYear().isLeapYear())
data.getDay().setMon_maxnum(29);
else
data.getDay().setMon_maxnum(28);
if(data.getDay().getValue()+n>data.getDay().getMon_maxnum()[data.getDay().getMonth().getValue()-1]) {
data.day.setValue((data.getDay().getValue()+n)-data.getDay().getMon_maxnum()[data.getDay().getMonth().getValue()-1]);
data.getDay().getMonth().monthIncrement();
}else
data.getDay().setValue(data.getDay().getValue()+n);
if(data.getDay().getMonth().getValue()>12) {
data.getDay().getMonth().setValue(1);
data.getDay().getMonth().getYear().yearIncrement();
}
return data;
}
public DateUtil getPreviousNDays(int n) {
DateUtil data = new DateUtil(this.getDay().getMonth().getYear().getValue(),this.getDay().getMonth().getValue(),this.getDay().getValue());
DateUtil help = new DateUtil(this.getDay().getMonth().getYear().getValue(),this.getDay().getMonth().getValue(),this.getDay().getValue());
while(n>365) {
help.getDay().getMonth().getYear().yearReduction();
if((data.getDay().getMonth().getYear().isLeapYear()&&data.getDay().getMonth().getValue()>2)||(help.getDay().getMonth().getYear().isLeapYear()&&data.getDay().getMonth().getValue()<=2))
n-=366;
else
n-=365;
data.getDay().getMonth().getYear().yearReduction();
}
while(n>30) {
if(data.getDay().getMonth().getValue()==1) {
data.getDay().getMonth().setValue(12);
data.getDay().getMonth().getYear().yearReduction();
n-=data.getDay().getMon_maxnum()[11];
}else {
if(data.getDay().getMonth().getYear().isLeapYear())
data.getDay().setMon_maxnum(29);
else
data.getDay().setMon_maxnum(28);
n-=data.getDay().getMon_maxnum()[data.getDay().getMonth().getValue()-2];
data.getDay().getMonth().monthReduction();
}
}
if(data.getDay().getMonth().getYear().isLeapYear())
data.getDay().setMon_maxnum(29);
else
data.getDay().setMon_maxnum(28);
if(data.getDay().getValue()-n<1) {
if(data.getDay().getMonth().getValue()==1) {
data.getDay().getMonth().setValue(12);
data.getDay().getMonth().getYear().yearReduction();
data.getDay().setValue(data.getDay().getMon_maxnum()[data.getDay().getMonth().getValue()-1]+(data.getDay().getValue()-n));
}else {
data.getDay().setValue(data.getDay().getMon_maxnum()[data.getDay().getMonth().getValue()-2]+(data.getDay().getValue()-n));
data.getDay().getMonth().monthReduction();
}
}else
data.getDay().setValue(data.getDay().getValue()-n);
if(data.getDay().getMonth().getValue()<1) {
data.getDay().getMonth().setValue(12);
data.getDay().getMonth().getYear().yearReduction();
}
return data;
}
public int getDaysofDates(DateUtil date) {
int y=0;
if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue()||(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())||(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())) {
DateUtil help = new DateUtil(this.getDay().getMonth().getYear().getValue(),this.getDay().getMonth().getValue(),this.getDay().getValue());
this.day=date.day;
date.day=help.day;
}
for(;this.getDay().getMonth().getYear().getValue()<date.getDay().getMonth().getYear().getValue()-1;this.getDay().getMonth().getYear().yearIncrement()) {
if(this.getDay().getMonth().getYear().isLeapYear())
y+=366;
else
y+=365;
}
for(;this.getDay().getMonth().getValue()<=12;this.getDay().getMonth().monthIncrement()) {
if(this.getDay().getMonth().getYear().isLeapYear())
this.getDay().setMon_maxnum(29);
else
this.getDay().setMon_maxnum(28);
y+=this.getDay().getMon_maxnum()[this.getDay().getMonth().getValue()-1];
}
this.getDay().getMonth().setValue(1);
this.getDay().getMonth().getYear().yearIncrement();
if(this.getDay().getMonth().getYear().isLeapYear())
this.getDay().setMon_maxnum(29);
for(;this.getDay().getMonth().getValue()<date.getDay().getMonth().getValue()-1;this.getDay().getMonth().monthIncrement())
y+=this.getDay().getMon_maxnum()[this.getDay().getMonth().getValue()-1];
y+=(date.getDay().getValue()+this.getDay().getMon_maxnum()[this.getDay().getMonth().getValue()-1]-this.getDay().getValue());
return y;
}
}
②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)分析如下:类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积。
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
Ball ball = new Ball();
Box box = new Box();
int n = input.nextInt();
if(n==1) {
double r=input.nextDouble();
if(r<0) {
System.out.println("Wrong Format");
}
else {
circle.getArea(r);
}
}
else if(n==2) {
double a=input.nextDouble();
double b=input.nextDouble();
if(a<0||b<0) {
System.out.println("Wrong Format");
}
else {
rectangle.getArea(a, b);
}
}
else if(n==3) {
double r=input.nextDouble();
if(r<0) {
System.out.println("Wrong Format");
}
else {
ball.getArea(r);
ball.getVolume(r);
}
}
else if(n==4) {
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
if(a<0||b<0||c<0) {
System.out.println("Wrong Format");
}
else {
box.getArea(a, b, c);
box.getVolume(a, b, c);
}
}
else {
System.out.println("Wrong Format");
}
}
}
class Circle extends Shape{
public double getArea(double r) {
System.out.println("Constructing Shape");
System.out.println("Constructing Circle");
double s=(double) (r*r*Math.PI);
System.out.print("Circle's area:"+String.format("%.2f", s));
return 0;
}
}
class Shape {
public double getArea() {
return 0.0;
}
}
class Rectangle extends Shape{
public double getArea(double a,double b) {
System.out.println("Constructing Shape");
System.out.println("Constructing Rectangle");
double s=(double) (a*b);
System.out.println("Rectangle's area:"+String.format("%.2f", s));
return 0;
}
}
class Ball extends Circle{
public double getArea(double r) {
System.out.println("Constructing Shape");
System.out.println("Constructing Circle");
System.out.println("Constructing Ball");
double s=(double) (r*r*Math.PI*4);
System.out.println("Ball's surface area:"+String.format("%.2f", s));
return 0;
}
public double getVolume(double r) {
double s=(double) (r*r*r*Math.PI*4/3);
System.out.println("Ball's volume:"+String.format("%.2f", s));
return 0;
}
}
class Box extends Rectangle{
public double getArea(double a,double b,double c) {
System.out.println("Constructing Shape");
System.out.println("Constructing Rectangle");
System.out.println("Constructing Box");
double s=(double) (a*b*2+a*c*2+b*c*2);
System.out.println("Box's surface area:"+String.format("%.2f", s));
return 0;
}
public double getVolume(double a,double b,double c) {
double v=(double) (a*b*c);
System.out.println("Box's volume:"+String.format("%.2f", v));
return 0;
}
}
7-5 图形继承与多态
(1)Shape 类、Rectangle 类、Circle 类以及 Triangle 类设计尽量做到独立、可复用; (2)排序方法可自行选择,冒泡排序、插入排序、选择排序等;
代码如下:
import java.util.Scanner;
class Shape{
Shape(){
System.out.println("Constructing Shape");
}
public double getArea() {
return 0.0;
}
}
class Circle extends Shape{
private double rd;
Circle() {
System.out.println("Constructing Circle");
}
public double getArea() {
return rd*rd*Math.PI ;
}
public void setRadius(double radius) {
this.rd = rd;
}
public double getRadius() {
return rd;
}
}
class Rectangle extends Shape{
private double a,b;
Rectangle() {
System.out.println("Constructing Rectangle");
}
public double getArea() {
return a*b;
}
public void setWidth(double width) {
this.a = width;
}
public void setLength(double length) {
this.b = length;
}
public double getWidth() {
return a;
}
public double getLength() {
return b;
}
}
class Ball extends Circle{
Ball(){
System.out.println("Constructing Ball");
}
@Override
public double getArea() {
return super.getArea()*4;
}
public double getVolume() {
return getArea()/3*getRadius();
}
}
class Box extends Rectangle{
private double height;
Box(){
System.out.println("Constructing Box");
}
@Override
public double getArea() {
return 2*(getWidth()*getLength()+getWidth()*getHeight()+getLength()*getHeight());
}
public double getVolume() {
return super.getArea()*height;
}
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return height;
}
}
public class Main {
private static Scanner type;
public static void main(String[] args) {
type = new Scanner(System.in
);
int a=type.nextInt();
if(a>4||a<1) {
System.out.println("Wrong Format");
return;
}
if(a==1) {
double b=type.nextDouble();
if(b<0) {
System.out.println("Wrong Format");
return;
}
Circle c=new Circle();
c.setRadius(b);
System.out.println("Circle's area:"+String.format("%.2f",c.getArea()));
}
if(a==2) {
double b=type.nextDouble();
double c=type.nextDouble();
if(b<0||c<0) {
System.out.println("Wrong Format");
return;
}
Rectangle d=new Rectangle();
d.setWidth(b);
d.setLength(c);
System.out.println("Rectangle's area:"+String.format("%.2f",d.getArea()));
}
if(a==3) {
double b=type.nextDouble();
if(b<0) {
System.out.println("Wrong Format");
return;
}
Ball c=new Ball();
c.setRadius(b);
System.out.println("Ball's surface area:"+String.format("%.2f",c.getArea()));
System.out.println("Ball's volume:"+String.format("%.2f",c.getVolume()));
}
if(a==4) {
double b=type.nextDouble();
double c=type.nextDouble();
double d=type.nextDouble();
if(b<0||c<0||d<0) {
System.out.println("Wrong Format");
return;
}
Box e=new Box();
e.setHeight(b);
e.setLength(c);
e.setWidth(d);
System.out.println("Box's surface area:"+String.format("%.2f",e.getArea()));
System.out.println("Box's volume:"+String.format("%.2f",e.getVolume()));
}
}
}7-6 实现图形接口及多态性分析如下:
GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)
类图

③对三次题目集中用到的正则表达式技术的分析总结
题目集4中的7-1运用正则表达式去校验水文数据是否合法,主要是用正则表达式去匹配数据的正确性。题目集5中的的7-4是用正则表达式去统计Java程序中关键词的出现次数,这里对注释的删除可以运用正则表达式去匹配并删除。而关键字的匹配可以将其存入一个数组中,这题目得用到集合的思想与方法。题目集6中的7-1,7-3,7-4都运用了正则表达式去匹配,
④题目集5(7-4)中Java集合框架应用的分析总结
题目集5中7-4的题目运用了正则表达式,并且运用了集合的思想。
踩坑心得:
比如题目6中的7-5;我没有输出排序后的各个图形面积。

这里圆的半径要大于0,但是自己很蠢的的写了>=0;导致找了半天,测了半天。
改进建议:
写题目的时候要多注意题目要我们做什么,不要一拿到题目就开始写,得先思考,再入手题目。
总结:
经过几个礼拜的努力,终于把这几次题目集写完了,于我而言,这三次题目集的难度比前几次的难度更高,因为这三次的题目集都涉及到了新的知识,如继承、多态、接口以及集合的使用,还有正则表达式的练习,特别是正则表达式。这些题目集让我受益良多,不但让我巩固了前面学的知识,而且有的就是正则表达式的使用也熟练了许多,这都是这三次题目集带给我们的帮助。我需要进一步改进的还是思维上的转变,现在慢慢适应想问题想的很全面就开始动手去敲代码,课程,作业的话其实这几次的题目集的难度有很大提高,但是我还是会尽力去完成作业。
浙公网安备 33010602011771号