第二次博客作业

第二次博客作业

1.   前言

/*总结三次题目集的知识点、题量、难度等情况*/

 

第四次:

第四次的作业,第一题知识点和难点都在正则表达式。正则表达式的练习是筛选水文信息,使用Java中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算。正则表达式的格式需要分析,对正则表达式的源码的分析。

 

 

 

第二题的知识点参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,难度不算高,此题考查的是类的设计,考察了计算的方法,日子算法设计,日历算法和各种情况的考虑情况。类的分析如下:

 

此处难点在于设计各种情况的算法,如出现闰月等特殊情况,以及降低计算的成本算法

 

第三题:图形继承。此题考察了继承关系,难度不算高,难点在于理解继承等概念。编写程序,实现图形类的继承,并定义相应类对象并进行测试。

 

 

总结:这次作业难度主要集中在第一题,题量不算高。难度综合中上等级。

 

第五次

第一题:承接自第四次作业,参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,但是改变了类之间的关系,类之间的聚合度降低了。

 

第二题:编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数,依旧是考察正则表达式,但是这次的输入和检测比上次要更加简单。

 

第三题:合并两个升序排序的整型数组为一个新的升序整型数组并输出。这题难度较低,主要是考察了排序算法。

 

第四题:分别使用插入排序、选择排序及冒泡排序三种算法对整型数据进行排序。如题所示,考察了各种排序算法。

 

总结:这次作业题量适中,难度适中。

 

第六次

第一题:校验键盘输入的 QQ 号是否合格,判定合格的条件如下:

  • 要求必须是 5-15 位;
  • 0 不能开头;
  • 必须都是数字;

此题考查正则表达式。在一行中输入一个字符串。难度一般

 

第二题:对输入的字符串中的字符进行排序并输出。

这一题考查了字符类型的转换,将字符转化为int型,再将其排序

 

第三题:正则表达式训练-验证码校验,接受给定的字符串,判断该字符串是否属于验证码。验证码是由四位数字或者字母(包含大小写)组成的字符串。

这一题考察了正则表达式。

 

第四题:正则表达式训练-学号校验

对软件学院2020级同学学号进行校验,学号共八位,规则如下:

  • 1、2位:入学年份后两位,例如20年
  • 3、4位:学院代码,软件学院代码为20
  • 5位:方向代码,例如1为软件工程,7为物联网
  • 6位:班级序号
  • 7、8位:学号(序号)
  • 此题考查正则表达式。在一行中输入一个字符串。难度一般

 

第五题:掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。

此题考察了类的继承和多态。

 

第六次:编写程序,使用接口及类实现多态性。对类的多态性考查。

 

总结:难度不高,题量稍多。

 

2.   设计与分析:重点对题目的提交源码进行分析

①  题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较

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

 

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

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

 

源码如下:

/*

 * 设计如下几个类: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 Auto-generated method stub

              Scanner in=new Scanner(System.in);

              int chioce=in.nextInt();

              if(chioce<1) {

                     System.out.print("Wrong Format");

              }

              if(chioce>3) {

                     System.out.print("Wrong Format");

              }

              /*

               *

               * 1 year month day n //测试输入日期的下n天

       2 year month day n //测试输入日期的前n天

       3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数*/

              if(chioce==1) {

                     int year=in.nextInt();

                     int month=in.nextInt();

                     int day=in.nextInt();

                     DateUtil date=new DateUtil(day,month,year);

                     if(date.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     int NNextday=in.nextInt();

                     date=date.getNextNDay(NNextday, date);

                     System.out.print(date.showDate());

                    

              }

              if(chioce==2) {

                     int year=in.nextInt();

                     int month=in.nextInt();

                     int day=in.nextInt();

                     DateUtil date=new DateUtil(day,month,year);

                     if(date.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     int NPreday=in.nextInt();

                     date=date.getPreviousNDay(NPreday, date);

                     System.out.print(date.showDate());

              }

              if(chioce==3) {

                     int year=in.nextInt();

                     int month=in.nextInt();

                     int day=in.nextInt();

                     DateUtil date1=new DateUtil(day,month,year);

                     if(date1.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     int otheryear=in.nextInt();

                     int othermonth=in.nextInt();

                     int otherday=in.nextInt();

                     DateUtil date2=new DateUtil(otherday,othermonth,otheryear);

                     if(date2.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     int sum=date1.getDaysofDates(date2);

                     System.out.print(sum);

              }

       }

 

}

 

class Year{

       private int year;

       public Year() {}

       public Year(int year) {

              this.year=year;

       }

       public int getYear(){

              return year;

       }

       public void setYear(int year) {

              this.year=year;

       }

       public boolean isLeepYear() {

              if(year%400==0) {

                     return true;

              }

              if(year%4==0) {

                     if(year%100!=0) {

                            return true;

                     }

              }

              return false;

       }

       public boolean isLeepYear(int n) {

              if(n%400==0) {

                     return true;

              }

              if(n%4==0) {

                     if(n%100!=0) {

                            return true;

                     }

              }

              return false;

       }

       public boolean validate() {

              if(year<1900) {

                     return false;

              }

              if(year>2050) {

                     return false;

              }

              return true;

       }

       public void yearIncrement() {

              year++;

       }

       public void yearReduction() {

              year--;

       }

}

 

class Month{

       private int month;

       Year year=new Year();

      

       public Month() {}

       public Month(int month) {

              this.month=month;

       }

       public int getMonth() {

              return month;

       }

       public void setMonth(int month) {

              this.month=month;

       }

       public int getYear() {

              return year.getYear();

       }

       public void setYear(int year) {

              this.year.setYear(year);

       }

       public void resetMin() {

              this.month=1;

       }

       public void resetMax() {

              this.month=12;

       }

       public boolean validate() {

              if(month<1) {

                     return false;

              }

              if(month>12) {

                     return false;

              }

              return true;

       }

       public void monthIncrement() {

              month++;

       }

       public void monthReduction() {

              month--;

       }

}

 

class Day{

       private int day;

       public Month month=new Month();

       int[] mon_maxnum=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};

      

       public Day() {}

       public Day(int yearValue,int monthValue,int dayValue) {

              this.day=dayValue;

              month.setMonth(monthValue);

              month.year.setYear(yearValue);

       }

      

       public int getDay() {

              return day;

       }

       public void setDay(int day) {

              this.day=day;

       }

       public int getMonth() {

              return month.getMonth();

       }

       public void setMonth(int month) {

              this.month.setMonth(month);

       }

       public void resetMin() {

              this.day=1;

       }

       public void resetMax() {

              this.day=mon_maxnum[month.getMonth()];

       }

       public boolean validate() {

              if(month.year.isLeepYear()&&month.getMonth()==2) {

                     if(day>29) {return false;}

                     if(day==29) {return true;}

                     if(day<1) {return false;}

              }

              if(day<1) {return false;}

              if(day>mon_maxnum[month.getMonth()]) {return false;}

              return true;

       }

       public void dayIncrement() {

              day++;

       }

       public void dayReduction() {

              day--;

       }

}

 

class DateUtil{

       public Day day=new Day();

      

       public DateUtil() {};

       public DateUtil(int d,int m,int y) {

              this.day.setDay(d);

              this.day.month.setMonth(m);

              this.day.month.setYear(y);

       }

      

       public int getDay() {

              return day.getDay();

       }

       public void setDay(int day) {

              this.day.setDay(day);

       }

      

       public boolean checkInputValidity() {

              if(this.day.validate()==true) {

                     if(this.day.month.validate()==true) {

                            if(this.day.month.year.validate()==true) {

                                   return true;

                            }

                     }

              }

              return false;

       }

      

      

      

       public boolean equalTwoDates(DateUtil date) {

       if(this.day.month.year.getYear()==date.day.month.year.getYear()&&this.day.getMonth()==date.day.getMonth()&&this.day.getDay()==date.day.getDay()) {

                     return true;

              }

              return false;

       }

      

       public boolean equalTwoDates(DateUtil date1,DateUtil date2) {

       if(date1.day.month.year.getYear()==date2.day.month.year.getYear()&&date1.day.getMonth()==date2.day.getMonth()&&date1.day.getDay()==date2.day.getDay()) {

                     return true;

              }

              return false;

       }

       public boolean compareDates(DateUtil date) {

              if(this.day.month.getYear()>date.day.month.getYear()) {

                     return true;

              }else if(this.day.month.getYear()<date.day.month.getYear()) {

                     return false;

              }

              if(this.day.getMonth()>date.day.getMonth()) {

                     return true;

              }else if(this.day.getMonth()<date.day.getMonth()) {

                     return false;

              }

              if(this.day.getDay()>date.getDay()) {

                     return true;

              }else if(this.day.getMonth()<date.day.getMonth()) {

                     return false;

              }

              return false;

             

       }

       /*

        *

        * 1 year month day n //测试输入日期的下n天

2 year month day n //测试输入日期的前n天

3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数*/

       public String showDate() {

              String s=day.month.getYear()+"-"+day.month.getMonth()+"-"+day.getDay();

              return s;

       }

      

      

       public int getMax_day_ofMonth(int year,int month){

              if(day.month.year.isLeepYear(year)) {

                     if(month==2) {

                            return 29;

                     }

              }

              return day.mon_maxnum[month];

       }

      

      

       public DateUtil getNextNDay(int n,DateUtil date) {

             

              for(int i=0;i<n;i++) {

                     date.day.dayIncrement();

                     if(date.day.getDay()>date.getMax_day_ofMonth(date.day.month.getYear(), date.day.getMonth())) {

                            date.day.resetMin();

                            date.day.month.monthIncrement();

                            if(date.day.month.getMonth()>12) {

                                   date.day.month.year.yearIncrement();

                                   date.day.resetMin();

                                   date.day.month.resetMin();

                            }

                     }

              }

             

              return date;

       }

      

       public DateUtil getPreviousNDay(int n,DateUtil date) {

              for(int i=0;i<n;i++) {

                     date.day.dayReduction();

                     if(date.day.getDay()<1) {

                            date.day.month.monthReduction();

                            if(date.day.month.getMonth()<1) {

                                   date.day.month.year.yearReduction();

                                   date.day.month.resetMax();

                                  

                            }

                            date.day.setDay(date.getMax_day_ofMonth(date.day.month.year.getYear(), date.day.getMonth()));

                     }

                    

              }

              return date;

       }

      

       public int getDaysofDates(DateUtil date) {

              int sum=0;

              DateUtil dateUtil1=this;

              DateUtil dateUtil2=date;

             

              if(this.equalTwoDates(this,dateUtil2)==true) {

                     return sum;

              }

             

              //dateUtil1为big的,dateUtil2要small,com大于是true

              if(dateUtil1.compareDates(dateUtil2)==false) {

                     dateUtil1=date;

                     dateUtil2=this;

              }

              while(dateUtil1.equalTwoDates(dateUtil2)==false) {

                     sum++;

                     dateUtil1.day.dayReduction();

                     if(dateUtil1.day.getDay()<1) {

                            dateUtil1.day.month.monthReduction();

                            if(dateUtil1.day.month.getMonth()<1) {

                                   dateUtil1.day.month.year.yearReduction();

                                   dateUtil1.day.month.resetMax();

                                  

                            }

                           dateUtil1.day.setDay(dateUtil1.getMax_day_ofMonth(dateUtil1.day.month.year.getYear(), dateUtil1.day.getMonth()));

                     }

              }

              return sum;

       }

}

 

第五次作业7_5

参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day

 

 

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

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

 

源码如下:

 

import java.util.Scanner;

 

public class Main {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Scanner in=new Scanner(System.in);

              int chioce=in.nextInt();

              if(chioce<1) {

                     System.out.print("Wrong Format");

              }

              if(chioce>3) {

                     System.out.print("Wrong Format");

              }

              if(chioce==1) {

                     int year=in.nextInt();

                     int month=in.nextInt();

                     int day=in.nextInt();

                     DateUtil date=new DateUtil(day,month,year);

                     if(date.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     int NNextday=in.nextInt();

                     String Before=date.showDate();

                     date=date.getNextNDay(NNextday, date);

                     System.out.print(Before+" next "+NNextday+" days is:"+date.showDate());

                    

              }

              if(chioce==2) {

                     int year=in.nextInt();

                     int month=in.nextInt();

                     int day=in.nextInt();

                     DateUtil date=new DateUtil(day,month,year);

                     if(date.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     int NPreday=in.nextInt();

                     String Before=date.showDate();

                     date=date.getPreviousNDay(NPreday, date);

                     System.out.print(Before+" previous "+NPreday+" days is:"+date.showDate());

              }

              if(chioce==3) {

                     int year=in.nextInt();

                     int month=in.nextInt();

                     int day=in.nextInt();

                     DateUtil date1=new DateUtil(day,month,year);

                     if(date1.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     String Before1=date1.showDate();

                     int otheryear=in.nextInt();

                     int othermonth=in.nextInt();

                     int otherday=in.nextInt();

                     DateUtil date2=new DateUtil(otherday,othermonth,otheryear);

                     String Before2=date2.showDate();

                     if(date2.checkInputValidity()==false) {

                            System.out.print("Wrong Format");

                            return;

                     }

                     int sum=date1.getDaysofDates(date2);

                     System.out.print("The days between "+Before1+" and "+Before2+" are:"+sum);

              }

             

       }

      

}

 

class Day{

       private int day;

       public Day() {}

       public Day(int value) {

              this.day=value;

       }

       public int getDay() {

              return day;

       }

       public void setDay(int day) {

              this.day=day;

       }

       public void dayIncrement() {

              day++;

       }

       public void dayReduction() {

              day--;

       }

      

}

 

class Month{

       private int month;

       public Month() {}

       public Month(int month) {

              this.month=month;

       }

       public int getMonth() {

              return month;

       }

       public void setMonth(int month) {

              this.month=month;

       }

       public void resetMin() {

              this.month=1;

       }

       public void resetMax() {

              this.month=12;

       }

       public void monthIncrement() {

              month++;

       }

       public void monthReduction() {

              month--;

       }

       public boolean validate() {

              if(month<1) {

                     return false;

              }

              if(month>12) {

                     return false;

              }

              return true;

       }

}

 

class Year{

       private int year;

       public Year() {}

       public Year(int year) {

              this.year=year;

       }

       public int getYear(){

              return year;

       }

       public void setYear(int year) {

              this.year=year;

       }

       public boolean isLeepYear() {

              if(year%400==0) {

                     return true;

              }

              if(year%4==0) {

                     if(year%100!=0) {

                            return true;

                     }

              }

              return false;

       }

       public boolean validate() {

              if(year<1820) {

                     return false;

              }

              if(year>2020) {

                     return false;

              }

              return true;

       }

       public void yearIncrement() {

              year++;

       }

       public void yearReduction() {

              year--;

       }

}

 

class DateUtil{

       public Year year=new Year();

       public Month month=new Month();

       public Day day=new Day();

       int[] mon_maxnum=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};

      

       public DateUtil() {};

       public DateUtil(int d,int m,int y) {

              this.day.setDay(d);

              this.month.setMonth(m);

              this.year.setYear(y);

       }

      

       public int getYear() {

              return year.getYear();

       }

       public void setYear(int year) {

              this.year.setYear(year);

       }

       public int getMonth() {

              return month.getMonth();

       }

       public void setMonth(int month) {

              this.month.setMonth(month);

       }

       public int getDay() {

              return day.getDay();

       }

       public void setDay(int day) {

              this.day.setDay(day);

       }

      

       public void resetMin() {

              this.day.setDay(1);

       }

       public void resetMax() {

              if(month.getMonth()==2&&year.isLeepYear()==true) {

              this.day.setDay(mon_maxnum[month.getMonth()]+1);

              }else {

                     this.day.setDay(mon_maxnum[month.getMonth()]);

              }

       }

      

       public int getMaxDay() {

              if(month.getMonth()==2&&year.isLeepYear()==true) {

                     return mon_maxnum[month.getMonth()]+1;

              }else {

                     return mon_maxnum[month.getMonth()];

              }

             

       }

      

       public boolean Day_validate() {

             

              if(day.getDay()<1) {return false;}

              if(day.getDay()>getMaxDay()) {return false;}

              return true;

       }

      

       public boolean checkInputValidity() {

              if(Day_validate()==true) {

                     if(this.month.validate()==true) {

                            if(this.year.validate()==true) {

                                   return true;

                            }

                     }

              }

              return false;

       }

      

       public DateUtil getNextNDay(int n,DateUtil date) {

             

              for(int i=0;i<n;i++) {

                     date.day.dayIncrement();

                     if(date.Day_validate()==false) {

                            date.month.monthIncrement();

                            date.resetMin();

                            if(date.month.validate()==false) {

                                   date.year.yearIncrement();

                                   date.month.resetMin();

                                   date.resetMin();

                            }

                     }

              }

             

              return date;

       }

      

      

              public DateUtil getPreviousNDay(int n,DateUtil date) {

              if(n<=1461) {

              for(int i=0;i<n;i++) {

                     date.day.dayReduction();

                     if(date.Day_validate()==false) {

                            date.month.monthReduction();

                            date.resetMax();

                            if(date.month.validate()==false) {

                                   date.month.resetMax();

                                   date.resetMax();

                                   date.year.yearReduction();

                            }

                     }

              }

              return date;}

              else {

                     int year_x=(n/1461)*4;

                     int year_y=date.year.getYear()+year_x;

                     date.year.setYear(year_y);

                     int new_n=n%1461;

                     for(int i=0;i<new_n;i++) {

                            date.day.dayReduction();

                            if(date.Day_validate()==false) {

                                   date.month.monthReduction();

                                   date.resetMax();

                                   if(date.month.validate()==false) {

                                          date.month.resetMax();

                                          date.resetMax();

                                          date.year.yearReduction();

                                   }

                            }

                     }

                     return date;

              }

        }

      

       public boolean compareDates(DateUtil date) {

              if(this.year.getYear()>date.year.getYear()) {

                     return true;

              }else if(this.getYear()<date.getYear()) {

                     return false;

              }

              if(this.getMonth()>date.getMonth()) {

                     return true;

              }else if(this.getMonth()<date.getMonth()) {

                     return false;

              }

              if(this.getDay()>date.getDay()) {

                     return true;

              }else if(this.getMonth()<date.getMonth()) {

                     return false;

              }

              return false;

             

       }

      

       public boolean equalTwoDates(DateUtil date1,DateUtil date2) {

       if(date1.getYear()==date2.getYear()&&date1.getMonth()==date2.getMonth()&&date1.getDay()==date2.getDay()) {

                     return true;

              }

              return false;

       }

      

       public int getDaysofDates(DateUtil date) {

              int sum=0;

              DateUtil dateUtil1=this;

              DateUtil dateUtil2=date;

             

              if(this.equalTwoDates(this,dateUtil2)==true) {

                     return sum;

              }

             

              //dateUtil1为big的,dateUtil2要small,com大于是true

              if(dateUtil1.compareDates(dateUtil2)==false) {

                     dateUtil1=date;

                     dateUtil2=this;

              }

              while(dateUtil1.equalTwoDates(dateUtil1, dateUtil2)==false) {

                     sum++;

                     dateUtil1=dateUtil1.getPreviousNDay(1,dateUtil1);

              }

              return sum;

       }

      

       public String showDate() {

              String s=this.getYear()+"-"+this.getMonth()+"-"+this.getDay();

              return s;

       }

}

 

优劣分析:第二个类的设计要比第一个类更加减少了耦合,类和类之间的练习个更加轻,在之后修改代码的需求时,所需要修改的地方要少。

 

②  题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

题集4  7-3:

 

编写程序,实现图形类的继承,并定义相应类对象并进行测试。

  1. 类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
  2. 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
  3. 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
  4. 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
  5. 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
  6. 注意:
  • 每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
  • 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
  • 输出的数值均保留两位小数

 

源码:

 

import java.util.Scanner;

 

public class Main {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Scanner in=new Scanner(System.in);

              int choose=in.nextInt();

              if(choose<1) {

                     System.out.print("Wrong Format");

              }

              else if(choose>4) {

                     System.out.print("Wrong Format");

              }

              if(choose==1) {

                     double r=in.nextDouble();

                     if(r<0.0) {

                            System.out.print("Wrong Format");

                     }

                     else {

                            Circle circle=new Circle();

                            circle.setRadius(r);

                            System.out.print(String.format("Circle's area:%.2f",circle.getArea()));

                     }

              }else if(choose==2) {

                     double width=in.nextDouble();

                     double length=in.nextDouble();

                     if(width<0.0||length<0.0) {

                            System.out.print("Wrong Format");

                     }

                     else {

                            Rectangle rectangle=new Rectangle();

                            rectangle.setLength(length);

                            rectangle.setWidth(width);

                           System.out.print(String.format("Rectangle's area:%.2f",rectangle.getArea()));

                     }

              }else if(choose==3) {

                     double r2=in.nextDouble();

                     if(r2<0.0) {

                            System.out.print("Wrong Format");

                     }

                     else {

                            Ball ball=new Ball();

                            ball.setRadius(r2);

                            System.out.println(String.format("Ball's surface area:%.2f",ball.getArea()));

                            System.out.print(String.format("Ball's volume:%.2f",ball.getVolume()));

                     }

              }else if(choose==4) {

                     double width2=in.nextDouble();

                     double length2=in.nextDouble();

                     double height=in.nextDouble();

                     if(width2<0.0||length2<0.0||height<0.0) {

                            System.out.println("Wrong Format");

                     }

                     else {

                            Box box=new Box();

                            box.setHeight(height);

                            box.setWidth(width2);

                            box.setLength(length2);

                            System.out.println(String.format("Box's surface area:%.2f",box.getArea()));

                            System.out.print(String.format("Box's volume:%.2f",box.getVolume()));

                     }

              }

       }

 

}

class Shape //定义一个无自身属性,有一个返回值为0.0的求面积方法

{

 

       public Shape()

       {

       System.out.println("Constructing Shape");

       }

       public double getArea()

       {

          return 0.0;

       }

}

class Circle extends Shape//继承自Shape

{

       public Circle()

       {

       System.out.println("Constructing Circle");

       }

       private double radius;//新定义一个半径

      

       public void setRadius(double radius) {// 设置半径

              this.radius = radius;

       }

      

       public double getRadius() {// 获取半径

              return radius;

       }

 

       @Override

       public double getArea() {

              // TODO Auto-generated method stub

              double area=Math.PI*radius*radius;

              return area;

       }     

}

 

class Rectangle extends Shape

{

       public Rectangle()

       {

       System.out.println("Constructing Rectangle");

       }

       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;

       }

 

       @Override

       public double getArea() {

              // TODO Auto-generated method stub

              double area=width*length;

              return area;

       }

      

}

class Ball extends Circle

{

       public Ball()

       {

       System.out.println("Constructing Ball");

       }

 

       @Override

       public double getArea() {

              // TODO Auto-generated method stub

              double area=4.0*super.getArea();

              return area;//方法的重载,super关键字

       }

       public double getVolume()

       {

              double r2=getRadius();

              double Volume=4.0/3.0*r2*r2*r2*Math.PI;

              return Volume;

       }

      

}

 

class Box extends Rectangle

{

       public Box()

       {

       System.out.println("Constructing Box");

       }

       private double height;

       public double getHeight() {

              return height;

       }

       public void setHeight(double height) {

              this.height = height;

       }

       public double getVolume()

       {

              double Volume=height*super.getArea();

              return Volume;

       }

       @Override

       public double getArea() {

              // TODO Auto-generated method stub

              double l=super.getLength();

              double w=super.getWidth();

              double area=2*l*w+2*l*height+2*w*height;

              return area;

       }

      

      

      

}

 

 

题集6   7-5

掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。

源码:

 

import java.util.Scanner;

 

public class Main {

      

       public static void main(String[] args) {

              Scanner in=new Scanner(System.in);

              int C=in.nextInt();

              int R=in.nextInt();

              int T=in.nextInt();

              boolean flag=true;

             

              if(C<0||R<0||T<0) {

                     System.out.print("Wrong Format");

                     return;

              }

              int sum=C+R+T;

              double area1[]=new double[sum];

              double area2[]=new double[sum];

             

              int begin=0;

              int end=0;

             

              for(int i=0;i<C;i++) {

                     double r=in.nextDouble();

                     Circle c=new Circle(r);

                     if(c.validate()==false) {

                            flag=false;

                     }

                     double temp=c.getArea();

                     area1[begin++]=temp;

                     area2[end++]=temp;

              }

             

              for(int i=0;i<R;i++) {

                     double width=in.nextDouble();

                     double length=in.nextDouble();

                     Rectangle r=new Rectangle(width,length);

                     if(r.validate()==false) {

                            flag=false;

                     }

                     double temp=r.getArea();

                     area1[begin++]=temp;

                     area2[end++]=temp;

              }

             

              for(int i=0;i<T;i++) {

                     double r1=in.nextDouble();

                     double r2=in.nextDouble();

                     double r3=in.nextDouble();

                     Triangle t=new Triangle(r1,r2,r3);

                     if(t.validate()==false) {

                            flag=false;

                     }

                     double temp=t.getArea();

                     area1[begin++]=temp;

                     area2[end++]=temp;

              }

             

              if(flag==false) {

                     System.out.print("Wrong Format");

                     return;

              }

             

              System.out.println("Original area:");

              for(int i=0;i<sum;i++) {

                     System.out.printf("%.2f ",area1[i]);

              }

              System.out.print("\n");

              double sumArea=0;

              for(int i=0;i<sum;i++) {

                     sumArea=sumArea+area1[i];

              }

              System.out.printf("Sum of area:");

              System.out.printf("%.2f",sumArea);

              System.out.print("\n");

              //Sorted area:

              System.out.print("Sorted area:");

              int A=area1.length;

              for(int i=0;i<A;i++) {

                     double temp;

                     for(int j=1;j<A;j++) {

                            if(area1[j-1]>area1[j]) {

                                   temp=area1[j];

                                   area1[j]=area1[j-1];

                                   area1[j-1]=temp;

                            }

                     }

              }

              System.out.print("\n");

              for(int i=0;i<A;i++) {

                     System.out.printf("%.2f ",area1[i]);

              }

              System.out.print("\n");

              //Sum of area:

              System.out.printf("Sum of area:");

              System.out.printf("%.2f",sumArea);

       }

      

}

 

abstract class Shape{

       public abstract double getArea();

       public abstract boolean validate();

}

 

class Circle extends Shape{

       private double r;

      

       public Circle(double r) {

              this.r=r;

       }

      

       public void setR(double r) {

              this.r=r;

       }

      

       @Override

       public double getArea() {

              // TODO Auto-generated method stub

              return Math.PI*r*r;

       }

 

       @Override

       public boolean validate() {

              // TODO Auto-generated method stub

              double R=this.r;

              if(R<=0) {

              return false;}

              return true;

       }

      

}

 

class Rectangle extends Shape{

       private double width;

       private double length;

      

       public Rectangle() {

             

       }

      

       public Rectangle(double width,double length) {

              this.width=width;

              this.length=length;

       }

      

       public void setRectangle(double width,double length) {

              this.width=width;

              this.length=length;

       }

       @Override

       public double getArea() {

              // TODO Auto-generated method stub

              return width*length;

       }

 

       @Override

       public boolean validate() {

              // TODO Auto-generated method stub

              if(width<0)

              return false;

              if(length<0) {

                     return false;

              }

              return true;

       }

      

}

 

class Triangle extends Shape{

       private double r1;

       private double r2;

       private double r3;

      

       public Triangle(double r1,double r2,double r3) {

              this.r1 = r1;

              this.r2 = r2;

              this.r3 = r3;

       }

      

 

       @Override

       public double getArea() {

              // TODO Auto-generated method stub

              double area=0;

             

              double s1;

        s1=(r1+r2+r3)/2;

        area=Math.sqrt(s1*(s1-r1)*(s1-r2)*(s1-r3));

       

              return area;

       }

 

       @Override

       public boolean validate() {

              // TODO Auto-generated method stub

              double rr1=r1;double rr2=r2;double rr3=r3;

              double[] rrr=new double[3];

              rrr[0]=rr1;rrr[1]=rr2;rrr[2]=rr3;

             

             

              for(int i=1;i<3;i++) {

                     double temp;

                     if(rrr[i]<rrr[i-1]) {

                            temp=rrr[i];

                            rrr[i]=rrr[i-1];

                            rrr[i-1]=temp;

                     }

              }

             

              if(rrr[0]+rrr[1]>rrr[2]) {

                     return true;

              }

             

              return false;

       }

      

}

 

7-6

编写程序,使用接口及类实现多态性,类图结构如下所示:

 

 

源码:

 

 

import java.util.Scanner;

 

public class Main {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Scanner in=new Scanner(System.in);

              double r=in.nextDouble();

              double width=in.nextDouble();

              double length=in.nextDouble();

             

              Circle C=new Circle(r);

              Rectangle R=new Rectangle(width,length);

             

              if(C.validate()==false) {

                     System.out.print("Wrong Format");

                     return;

              }

              if(R.validate()==false) {

                     System.out.print("Wrong Format");

                     return;

              }

             

              double temp1=C.getArear();

              double temp2=R.getArear();

              System.out.printf("%.2f",temp1);

              System.out.print("\n");

              System.out.printf("%.2f",temp2);

       }

 

}

 

abstract class GetArea{

       abstract public double getArear();

}

 

class Circle extends GetArea{

       private double r;

      

       public Circle(double r) {

              this.r=r;

       }

      

       public void setR(double r) {

              this.r=r;

       }

       @Override

       public double getArear() {

              // TODO Auto-generated method stub

              return Math.PI*r*r;

       }

       public boolean validate() {

              // TODO Auto-generated method stub

              double R=this.r;

              if(R<=0) {

              return false;}

              return true;

       }

}

 

class Rectangle extends GetArea{

       private double width;

       private double length;

      

       public Rectangle() {

             

       }

      

       public Rectangle(double width,double length) {

              this.width=width;

              this.length=length;

       }

      

       public void setRectangle(double width,double length) {

              this.width=width;

              this.length=length;

       }

       @Override

       public double getArear() {

              // TODO Auto-generated method stub

              return width*length;

       }

       public boolean validate() {

              // TODO Auto-generated method stub

              if(width<=0)

              return false;

              if(length<=0) {

                     return false;

              }

              return true;

       }

}

 

 

比较:

由近到远地了解了继承等多种性质。

 

 

 

对三次题目集中用到的正则表达式技术的分析总结

正则表达式:正则表达式(Regular Expression)是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符")。

正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。

正则表达式是繁琐的,但它是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感。只要认真阅读本教程,加上应用的时候进行一定的参考,掌握正则表达式不是问题。

许多程序设计语言都支持利用正则表达式进行字符串操作。

 

从匹配单个字符到多个字符,再到字符串,深入浅出地了解了正则表达式,也学会了对字符串进行加工。

题目集5(7-4)中Java集合框架应用的分析总结

源码:

 

import java.util.*;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Main {

      

         

           public static void main(String[] args) {

               Scanner in=new Scanner(System.in);

               String important="abstract  assert  boolean  break  byte  case  catch  char  class  const  continue  default  do " +

                       " double  else  enum  extends  final  finally  float  for  goto  if  implements  import  instanceof  int  " +

                       "interface  long  native  new  package  private  protected  public  return  strictfp  short  static  super  " +

                       "switch  synchronized  this  throw  throws  transient  try  void  volatile  while  true  false  null";

               String []end=important.split("  ");//因为粘贴下来的关键字之间有两个空格,所以切开来预处理,再放入映射容器中    //除了下面2种排序方法,还有很多方法完成排序    /*     一:对end数组使用sort,在关键字放入映射之前就完成排序,使用LinkedMap,该容器可以按插入顺序排序     二:在新建map对象时,使用自定义比较器构建对象,(HashMap,LinkedMap不支持)     如new TreeMap<String,Integer>(String::compareTo) 这里使用了方法引用     或者 new TreeMap<String,Integer>((u,v)->{return u.compareTo(v);});//使用了Lamba表达式     .......*/

               Map<String,Integer> mp=new TreeMap<String,Integer>();

               for(String e:end)  mp.put(e,0);

 

               String s=null;

               int flag=0;//检查是否有关键字出现过

               StringBuilder t=new StringBuilder();//用来完成字符串拼接

 

               t=SB();

               //替换/**/

               s=t.toString();

              

               Pattern p=Pattern.compile("/\\*(.*)?\\*/");

               Matcher m=p.matcher(s);

               while(m.find()){

                   s=s.replace(m.group()," ");

                   m=p.matcher(s);

               }

               //替换""字符串

               p=Pattern.compile("\"(.*?)\"");

               m=p.matcher(s);

               while(m.find()){

                   s=s.replace(m.group()," ");

                   m=p.matcher(s);

               }

 

               if(s.length()==0){ System.out.print("Wrong Format");System.exit(0);}

               s=s.replaceAll("\\p{P}"," ");//替换掉所有的标点符号,$,_可以保留,但此题可以不考虑

 

               //否则获得处理后的字符串

               String[] temp=s.split("\\s+");

               for(String e:temp)

                   if(mp.containsKey(e)) { mp.put(e,mp.get(e)+1);flag=1; }//找到关键字,取出对应的值+1

              

               //对输出情况进行分类

               if(flag==0) System.exit(0);//有代码,但是没有关键字

              

               List<Map.Entry<String, Integer>> list= new ArrayList<Map.Entry<String, Integer>>

               ((Collection<? extends Map.Entry<String, Integer> >) mp.entrySet());

               list.sort((o1, o2) -> o2.getKey().compareTo(o1.getKey()));

              

               PrintX(list);

           }

           public static StringBuilder SB() {

                     Scanner in=new Scanner(System.in);

                     StringBuilder t=new StringBuilder();

                     String s=null;

                     while(!(s=in.nextLine().trim()).equals("exit")){

                   //有第一种注释符号,跳出

                   if(s.matches(".*//.*")) continue;//当然这里并不严谨,毕竟//注释可以放在有效代码后面

                   t.append(s+" ");//将该字符串与下一行字符串隔开

               }

                     return t;

              }

          

           public static void PrintX(List<Map.Entry<String, Integer>> list) {

                     for(int i=list.size()-1;i>=0;i--){

                   if(list.get(i).getValue()==0) continue;

                   System.out.printf("%d\t",list.get(i).getValue());

                   System.out.println(list.get(i).getKey());

               }

              }

       }

 

 

 

3.   采坑心得

对于正则表达式题目:主要是对于寻找目标字符串,对正则表达式设计,还有对字符串的处理动作。

 

对于日期类题目:主要是设计算法时对特殊情况是不足的

 

对于继承类题目:主要是对继承本身的理解不够。

 

4.   改进建议

多学习正则表达式的设计,多学习对字符串的处理,多学习对代码健壮性的设计

 

5.   总结

对于近期的学习,逐步进入了深水区,对代码源码的分析还有对算法的设计,面向对象的总结,面向对象的思考。

posted @ 2021-11-13 21:18  紀氿  阅读(48)  评论(0)    收藏  举报