第二次博客作业

(1)前言:

第四次作业:主要考察字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算,面向对象设计根据类图设计程序,类的继承用继承要求编写函数。总共有3题,难度依次减弱,第一题对我来说由于要使用正则函数比较难,第二题内容多,写起来比较繁杂,第三题就是简单的考察对类的继承的运用。

第五次作业:主要考察对上一次作业日期问题的再次改写,使用正则函数解决问题和两简单排序。一共4题,第一题是上一次作业的扩展,第二题难度较大有点没有头绪,后面两题比较简单。

第六次作业:主要考察简单正则函数的使用,用正则函数解决简单问题,还用掌握类的继承、多态性及其使用方法,编写程序,使用接口及类实现多态性,按类图结构

完成代码。 一共6题,前四题比较简单,第五题比较难,第六题还好,学会使用接口就可以了。

(2)设计与分析:

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

7-2 日期问题面向对象设计(聚合一)

设计分析: 类图细节比较多,而且其类与类之间的联系是有DateUtil用Day联系Month,再用Month联系Year,环环相扣,连接紧密,调用之间可以做到相对直接的调用方法去实现一个整体的功能,每个类有有莫大的联系.做题的思路的话,直接从底层类Year开始构造整个框架,依次完善各个类,利用好类与类之间函数的相互调用,可以简化最后的操作,且思路清晰。

源码:

import java.util.Scanner;public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int i = 0;
        DateUtil date;
        DateUtil date1;
        DateUtil date2;
        i=in.nextInt();
        switch(i)
        {
        case 1:
        case 2:
            int day,month,year,n;
            year=in.nextInt();
            month=in.nextInt();
            day=in.nextInt();
            n=in.nextInt();
            date = new DateUtil(day,month,year);
            if(date.checkIntputValidity())
            {
                if(i==1)
                date.getNextDays(n);
                if(i==2)
                date.getPreviousDays(n);
                System.out.println(date.showDate());
            }
            else
                {System.out.println("Wrong Format");}
            break;
        case 3:
            int day1,month1,year1,day2,month2,year2;
            year1=in.nextInt();
            month1=in.nextInt();
            day1=in.nextInt();
            year2=in.nextInt();
            month2=in.nextInt();
            day2=in.nextInt();
            date1 = new DateUtil(day1,month1,year1);
            date2 = new DateUtil(day2,month2,year2);
            if(date1.checkIntputValidity()&&date2.checkIntputValidity())
            {
                int a;
                a=date1.getDaysofDates(date2);
                System.out.println(a);
            }
            else {
                System.out.println("Wrong Format");
            }
            break;
        default:
            System.out.println("Wrong Format");
        }
    }

}

class Year
{
    private int year;
    Year(int year)
    {
        this.year = year;
    }
    Year()
    {

    }
    int getYear()
    {
        return year;
    }
    void setYear(int year)
    {
        this.year = year;
    }
    boolean isLeapYear()
    {
        if((year%100!=0&&year%4==0)||(year%400==0))
        {
            return true;
        }
        return false;
    }
    boolean Yeardate()
    {
        if(year<1900||year>2050)
        {
            return false;
        }
        return true;
    }
    void yearIncrement()
    {
        this.year++;
    }
    void yearReduction()
    {
        this.year--;
    }
}
class Month
{
    private int month;
    Year year = new Year();
    Month()
    {

    }
    Month(int year,int month)
    {
        this.month = month;
        this.year.setYear(year);
    }
    int getMonth()
    {
        return month;
    }
    void setMonth(int month)
    {
        this.month = month;
    }
    Year getYear()
    {
        return this.year;
    }
    void setYear(Year year)
    {
        this.year=year;
    }
    void resetMin()
    {
        this.month = 1;
    }
    void resetMax()
    {
        this.month = 12;
    }
    boolean Monthdate()
    {
        if(month>12||month<1||!this.year.Yeardate())
        {
            return false;
        }
        return true;
    }
    void monthIncrement()
    {
        this.month++;
        if(this.month==13)
        {
            this.resetMin();
            this.year.yearIncrement();
        }
    }
    void monthReduction()
    {
        this.month--;
        if(this.month==0)
        {
            this.resetMax();
            this.year.yearReduction();
        }
    }
}
class Day
{
    private int day;
    Month month = new Month();
    private int mon_maxnum[]=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
    Day()
    {

    }
    Day(int year,int month,int day)
    {
        this.month.year.setYear(year);
        this.month.setMonth(month);
        this.day=day;
    }
    int getDay()
    {
        return day;
    }
    void setDay(int day)
    {
        this.day=day;
    }
    Month getMonth()
    {
        return this.month;
    }
    void setMonth(Month month)
    {
        this.month=month;
    }
    void resetMin()
    {
        this.day=1;
    }
    void resetMax()
    {
        if(this.month.year.isLeapYear())
        {
            mon_maxnum[2]=29;
        }
        else
        {
            mon_maxnum[2]=28;
        }
        this.day=mon_maxnum[this.month.getMonth()];
    }
    boolean Daydate()
    {
        if(this.month.year.isLeapYear())
        {
            mon_maxnum[2]=29;
        }
        else
        {
            mon_maxnum[2]=28;
        }
        if(this.month.Monthdate())
        {
        if(day>mon_maxnum[this.month.getMonth()]||day<1)
        {
            return false;
        }
    }
        return true;
    }
    void dayIncrement()
    {
        if(this.month.year.isLeapYear())
        {
            mon_maxnum[2]=29;
        }
        else
        {
            mon_maxnum[2]=28;
        }
        this.day++;
        if(day==(mon_maxnum[this.month.getMonth()]+1))
        {
            this.resetMin();
            this.month.monthIncrement();
        }
    }
    void dayReduction()
    {
        this.day--;
        if(day==0)
        {
            this.month.monthReduction();
            this.resetMax();
        }
    }
}
class DateUtil
{
    Day day = new Day();
    DateUtil()
    {

    }
    DateUtil(int day,int month,int year)
    {
        this.day.setDay(day);
        this.day.month.setMonth(month);
        this.day.month.year.setYear(year);
    }
    Day getDay()
    {
        return this.day;
    }
    void setDay(Day day)
    {
        this.day=day;
    }
    boolean checkIntputValidity()
    {
        if(this.day.month.Monthdate())
        {
        if(this.day.Daydate())
        {
            return true;
        }
        }
        return false;
    }
    boolean compareDate(DateUtil date)
    {
        int i=0,j=0,k=0;
        i=this.day.month.year.getYear()-date.day.month.year.getYear();
        j=this.day.month.getMonth()-date.day.month.getMonth();
        k=this.day.getDay()-date.day.getDay();
        if(i>0)
        {
            return true;
        }
        else if(i==0)
        {
            if(j>0)
            {
                return true;
            }
            else if(j==0)
            {
                if(k>0)
                    return true;
            }
        }
        return false;
    }
    boolean equalTwoDate(DateUtil date)
    {
        int i=0,j=0,k=0;
        i=this.day.month.year.getYear()-date.day.month.year.getYear();
        j=this.day.month.getMonth()-date.day.month.getMonth();
        k=this.day.getDay()-date.day.getDay();
        if(i==0&&j==0&&k==0)
        {
            return true;
        }
        return false;
    }
    String showDate()
    {
        String a,b,c,d="-";
        Integer i,j,k;
        StringBuffer bf =new StringBuffer();
        i=this.day.month.year.getYear();
        a=i.toString();
        bf.append(a);
        bf.append(d);
        j=this.day.month.getMonth();
        b=j.toString();
        bf.append(b);
        bf.append(d);
        k=this.day.getDay();
        c=k.toString();
        bf.append(c);
        return bf.toString();
    }
    DateUtil getNextDays(int n)
    {
        int i=0;
        for(i=0;i<n;i++)
        {
            this.day.dayIncrement();
        }
        return this;
    }
    DateUtil getPreviousDays(int n)
    {
        int i=0;
        for(i=0;i<n;i++)
        {
            this.day.dayReduction();
        }
        return this;
    }
    int getDaysofDates(DateUtil date)
    {
        int i=0;
        if(this.equalTwoDate(date))
        {
            return 0;
        }

     if(this.compareDate(date))
        {
            while(!this.equalTwoDate(date))
            {
                if(date.day.Daydate())
                {
                date.day.dayIncrement();
                i++;
                }
                else
                {
                    System.out.println("Wrong Format");
                    i=0;
                    break;
                }
            }

        }
     else
     {
         while(!this.equalTwoDate(date))
            {
             if(date.day.Daydate())
                {
                this.day.dayIncrement();
                i++;
                }
             else
                {
                    System.out.println("Wrong Format");
                    i=0;
                    break;
                }
            }
     }
     for(int r=0;r<i;r++)
        {
            if(date.day.Daydate())
            {
            date.day.dayReduction();
            }
        }
     return i;
    }
}

代码分析:根据source monitor的数据分析,我的代码的圈复杂度很高,应为使用了比较多的if else。但是看题目要求,分类清晰,且板块不多,用if else完成我觉得比较简单清楚,不会复杂化。

 

7-5 日期问题面向对象设计(聚合二)

设计分析: 这道题是从箭头的末端开始,先构造year,day,month类,然后在构建DateUtil类,进而实现日期的聚合关系,主要是由DatUtil类来管理,虽然层次不够清晰,但构建的理念清晰,且可维护性强, 可以实现较方便的实现对方法的添加,以及对类的修改,简而言之就是方便进行改造或者升级,进行迭代。

源码:

import java.util.Scanner;
import java.util.regex.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;
        int choice = input.nextInt();
        if (choice == 1) { // test getNextNDays method
            int num = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil date = new DateUtil(year, month, day);
            if (!date.checkInputValidity(year, month, day)) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            num = input.nextInt();
            if (num < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            System.out.println(date.showDate() + " next " + num + " days is:" + date.getNextNDays(num).showDate());
        } 
        else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity(year, month, day)) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            n = input.nextInt();
            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            System.out.println(date.showDate() + " previous " + n + " days is:" + date.getPreviousNDays(n).showDate());
        } 
        else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());
            DateUtil Date1 = new DateUtil(year, month, day);
            DateUtil Date2 = new DateUtil(anotherYear, anotherMonth, anotherDay);
            if (Date1.checkInputValidity(year, month, day) && Date2.checkInputValidity(year, month, day)) {
                System.out.println("The days between " + Date1.showDate() + " and " + Date2.showDate() + " are:" + Date1.getDaysofDates(Date2));
            } 
            else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
    }

class DateUtil{
    private int year;
    private int month;
    private int day;
    private static Day day1;
    
    public DateUtil() {
    }
    public DateUtil(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
     public void setYear(int year) {
            this.year = year;
        }
        public void setMonth(int month) {
            this.month = month;
        }
        public void setDay(int day) {
            this.day = day;
        }
        public int getYear() {
            return year;
        }
        public int getMonth() {
            return month;
        }
        public int getDay() {
            return day;
        }
    public Day getDay1() {
        return DateUtil.day1;
    }
    public void setDay(Day d) {
        DateUtil.day1 = d;
    }
    public static boolean checkInputValidity(int year,int month,int day) {
        if(year < 1820 || year > 2020) {
            return false;
        }
        else {
            if(month < 1 || month > 12) {
                return false;
            }
            else {
                if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                    if(day > 31 || day < 1) {
                        return false;
                    }
                    else {
                        return true;
                    }
                }
                else if(month == 4 || month == 6 || month == 9 || month == 11 ) {
                    if(day > 30 || day <0) {
                        return false;
                    }
                    else {
                        return true;
                    }
                }
                else {
                    if(Year.isLeapYear(year) == true) {
                        if(day > 29 || day < 0) {
                            return false;
                        }
                        else {
                            return true;
                        }
                    }
                    else {
                        if(day > 28 || day < 0) {
                            return false;
                        }
                        else {
                            return true;
                        }
                    }
                }
            }
        }
    
    }
    public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后)
    {
        if (this.year > date.year) return true;
        if (this.year == date.year) {
            if (this.month > date.month) return true;
            if (this.month == date.month) {
                if (this.day >= date.day) return true;
            }
        }
        return false;
    }
    public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
    {
        if (date != null) {
            if (year == date.year && month == date.month && day == date.day) {
                return true;
            }
        }
        return false;
    }
    public String showDate()//以“year-month-day”格式返回日期值
    {
        return year + "-" + month + "-" + day;
    }
    private int getDayOfMonth(int year, int month) {
        int days = Day.mon_maxnum[month - 1];
        if (month == 2 && Year.isLeapYear(year)) {
            days = 29;
        }
        return days;
    }
    public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
    {
        
        int year = this.year;
        int month = this.month;
        int day = this.day;
        for (int i = 0; i < n; i++) {
            day++;
            if (day > getDayOfMonth(year, month)) {
                day = 1;
                month++;
                if (month > 12) {
                    month = 1;
                    year++;
                }
            }
        }
        return new DateUtil(year, month, day);
    }
    public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
    {
        int year = this.year;
        int month = this.month;
        int day = this.day;
        for (int i = 0; i < n; i++) {
            day--;
            while (day < 1) {
                month--;
                if (month < 1) {
                    month = 12;
                    year--;
                }
                day += getDayOfMonth(year, month);
            }
        }
        return new DateUtil(year, month, day);
    }
    private static final int[] monthdate = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数
    {
        DateUtil dateUtil1 = this; //
        DateUtil dateUtil2 = date; //
        if (this.compareDates(date)) {
            dateUtil1 = date;
            dateUtil2 = this;
        }
        int days;
        int leapYearNum = 0;
        for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
            if (Year.isLeapYear(i)) {
                leapYearNum++;
            }
        }
        days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;
        int d1 = monthdate[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && Year.isLeapYear(dateUtil1.getYear()) ? 1 : 0);
        int d2 = monthdate[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && Year.isLeapYear(dateUtil2.getYear()) ? 1 : 0);
        return days - d1 + d2;
    }
}
class Day{
    private static int value;
    private static Month month;
    static int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
    private Month year;
    public Day() {
    }
    public Day(Month yearvalue,Month monthvalue,int dayvalue) {
        this.value = dayvalue;
        this.month = monthvalue;
        this.year = yearvalue;
    }
    public void setValue(int value) {
        Day.value = value;
    }
    public static int getValue() {
        return Day.value;
    } 
    public void setMonth(Month month) {
        Day.month = month;
    }
    public Month getMonth() {
        return Day.month;
    }
    public void resetmin() {
        value = 1; 
    }
    public void resetmax() {
        value = mon_maxnum[month.getValue()-1];
    }
    public boolean validate() {
        if(Year.isLeapYear(Year.value))
            mon_maxnum[1]++;
        if(this.value>=1&&this.value<=mon_maxnum[this.month.getValue()-1])
            return true;
        else  
            return false;    
    }
     public void yearIncrement() {
         value++;
     }
     public void yearReduction() {
         value--;
     }
}
class Month{
    private static int value;
    private static Year year;
    public Month(Year yearvalue,int monthvalue) {
        this.value = monthvalue;
        this.year = yearvalue;
    }
    public Month() {
    }
    public void setValue(int value) {
        Month.value = value;
    }
    
    public static int getValue() {
        return Month.value;
    }
    
    public void setYear(Year year) {
        Month.year = year;
    }
    
    public Year getYear() {
        return Month.year;
    }
    
    public void resetmin() {
        value = 1; 
    }
    
    public void resetmax() {
        value = 12;
    }
    
    public boolean validate(int value) {
        if(value > 12 || value < 1) {
            return false;
        }
        else
            return true;
    }
    
     public void yearIncrement() {
         value++;
     }
     
     public void yearReduction() {
         value--;
     }
    
}

class Year{
    static int value;
    
    public Year(int value) {
        this.value = value;
    }
    public Year() {
    }
    public void setValue(int value) {
        Year.value = value;
    }
    
    public static int getValue() {
        return Year.value;
    }
    
    public static boolean isLeapYear(int year)//判断year是否为闰年
    {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
    
    public boolean validate(int value) {
        if(value > 2020 || value < 1820) {
            return false;
        }
        else
            return true;
    }
    
     public void yearIncrement() {
         value++;
     }
     
     public void yearReduction() {
         value--;
     }
    
}

代码分析:用了比较多的 if else 圈复杂度比较高,月份可以用数组储存对应天数,还可以用switch case,降低复杂度,使代码简单清晰。

总结比较:第一种优点层次清晰,类与类之间层层交替,关系紧密。但类与类之间关系过于紧密,若程序出现bug或者需要举行修改导致大部分代码受到波及,所以不易更改。而第二种day,month,year类之间没有如何关系,符合面向对象编程理念,便于修改和增添内容,但类与类之间的关系太过浅薄, 编写过程实现功能时调用以及计算都没有日期类聚合1中好用。

 

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

7-3 图形继承 

设计分析:这题比较简单,层次很清楚,只要按要求,构建父类后创建子类,继续创建子类继承其即可,其关系为父类Shape,还有三个子类Circle,Rectangle,Ball,Box。Circle和Rectangle继承Shape,而Ball继承Circle,Box继承Rectangle。主要考察对图形继承的了解和使用,调用构造函数时实际调用的情况。在Ball类中没有创建自己的属性,为了在调用构造方法时,可以用super实现调用父类构造方法来更改属性。

源码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        if(n==1){
                double R;
                R=input.nextDouble();
                if(R>0){
                    Circle circle = new Circle();
                    circle.setRadius(R);
                    System.out.println(String.format("Circle's area:%.2f",circle.getArea()));
                }
                else
                System.out.println("Wrong Format");
        }else if(n==2){
            double L,W;
            W=input.nextDouble();
            L=input.nextDouble();
            if(W>0 && L>0){
                Rectangle rectangle = new Rectangle();
                rectangle.setWidth(W);
                rectangle.setLength(L);
                System.out.println(String.format("Rectangle's area:%.2f",rectangle.getArea()));
            }
            else{
                System.out.println("Wrong Format");
            }
            
        }else if(n==3){
                double R;
                R=input.nextDouble();
                if(R>0){
                    Ball ball = new Ball();
                    ball.setRadius(R);
                    System.out.println(String.format("Ball's surface area:%.2f",ball.getArea()));
                    System.out.println(String.format("Ball's volume:%.2f",ball.getVolume()));
                }
                else
                System.out.println("Wrong Format");
        }else if(n==4){
            double L,W,H;
            W=input.nextDouble();
            L=input.nextDouble();
            H=input.nextDouble();
             if(W>0 && L>0 && H>0){               
                    Box box = new Box();
                    box.setWidth(W);
                    box.setLength(L);
                    box.setHeight(H);
                    System.out.println(String.format("Box's surface area:%.2f",box.getArea()));
                    System.out.println(String.format("Box's volume:%.2f",box.getVolume()));
             }
             else
                System.out.println("Wrong Format");
        }
        else 
          System.out.println("Wrong Format");   
    }
}    
class Shape{
    public Shape() 
    {
       System.out.println("Constructing Shape");
    }
    public double getArea()
    {
       return 0.0;
    }
}

class Circle extends Shape{
    public Circle(){
       System.out.println("Constructing Circle");
    }
    private double radius;
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    public double getArea() {
        return Math.PI*radius*radius;
    }
}
    
class Rectangle extends Shape{
    public Rectangle() {
       System.out.println("Constructing Rectangle");
    }
    private double width;
    private double length;
    public void setWidth(double width) {
        this.width = width;
    }
    public double getWidth() {
        return width;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public double getLength() {
        return length;
    }
    public double getArea() {
        return width*length;
    }    
}
    
class Ball extends Circle{
    public Ball(){
       System.out.println("Constructing Ball");
    }
    public double getArea() {
        return 4.0*super.getArea();
    }
    public double getVolume() {
        double r=getRadius();
        return 4.0/3.0*r*r*r*Math.PI;
    }
    
}
class Box extends Rectangle{
    public Box() {
       System.out.println("Constructing Box");
    }
    private double height;
    public void setHeight(double height) {
        this.height = height;
    }
      public double getHeight() {
        return height;
    }
    public double getArea() {
        double w=getWidth();
        double l=getLength();
        return 2*(w*l+w*height+l*height);
    }
    public double getVolume(){
        return height*super.getArea();
    }
}

代码分析:这题层次清晰,按要求编写即可,无其他改善地方。

 

7-5 图形继承与多态

设计分析:这次的题目是图形继承与多态,是上次图形类再更进,难度不大,就是内容有点多,主要考察我们对多态的使用,同时也是按要求编写即可,利用多态解决问题,为我们节省时间,避免重复多次编写,编译出高质量代码。

 源码:

import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner input =new Scanner(System.in);
        int a,b,c;
        a=input.nextInt();
        b=input.nextInt();
        c=input.nextInt();
        if(a<0 ||b<0 ||c<0){
            System.out.print("Wrong Format");
        }
        else {
            Shape[] shapes = new Shape[a + b + c];
            double[] areas = new double[a + b + c];
            double sumArea = 0;
            for (int i = 0; i < a; i++) {
                double radius = input.nextDouble();
                Circle circle = new Circle(radius);
                if (circle.vaildate()) {
                    shapes[i] = circle;
                    areas[i] = circle.getArea();
                    sumArea += areas[i];
                } 
                else {
                    System.out.print("Wrong Format");
                    System.exit(0);
                }
            }
            for (int i = a; i < a + b; i++) {
                double width = input.nextDouble();
                double length = input.nextDouble();
                Rectangle rectangle = new Rectangle(width,length);
                if (rectangle.vaildate()) {
                    shapes[i] = rectangle;
                    areas[i] = rectangle.getArea();
                    sumArea += areas[i];
                } else {
                    System.out.print("Wrong Format");
                    System.exit(0);
                }
            }
            for (int i = a + b; i < a + b + c; i++) {
                double side1 = input.nextDouble();
                double side2 = input.nextDouble();
                double side3 = input.nextDouble();
                Triangle triangle = new Triangle(side1,side2,side3);
                if(triangle.vaildate()){
                    shapes[i] = triangle;
                    areas[i] = triangle.getArea();
                    sumArea += areas[i];
                } else {
                    System.out.print("Wrong Format");
                    System.exit(0);
                }
            }
                System.out.println("Original area:");
                for (int i = 0; i < a+b+c; i++) {
                    System.out.print(String.format("%.2f", areas[i]));
                    if(i == a+b+c)
                        break;
                    System.out.print(" ");
                }
                System.out.println();
                System.out.println("Sum of area:" + String.format("%.2f",sumArea));
                System.out.println("Sorted area:");
                Arrays.sort(areas);
                for (int i = 0; i < a+b+c; i++) {
                    System.out.print(String.format("%.2f", areas[i]));
                    if(i == a+b+c)
                        break;
                    System.out.print(" ");
                }
                System.out.println();
                System.out.println("Sum of area:" + String.format("%.2f",sumArea));
            }
    }
}
abstract class Shape{
    public abstract double getArea();
    public abstract boolean validate();
    public abstract String toString();
}
class Circle extends Shape{
    private double radius;
    public Circle(double radius){
        this.radius=radius;
    }
    public double getArea(){
        return Math.PI*radius*radius;
    }
    public boolean vaildate(){
        if(radius>0){
            return true;
        }
        else
            return false;
    }
    public String toString(){
        return null;
    }
    @Override
    public boolean validate() {
        // TODO Auto-generated method stub
        return false;
    }
}
class Rectangle extends Shape{
    private double width;
    private double length;
    public Rectangle(double width,double length){
        this.width=width;
        this.length=length;
    }
    public double getArea(){
        return width*length;
    }
    public boolean vaildate(){
        if(width>0 && length>0){
            return true;
        }
        else
            return false;
    }
    public String toString(){
        return null;
    }
    @Override
    public boolean validate() {
        // TODO Auto-generated method stub
        return false;
    }
}
class Triangle extends Shape{
    private double side1;
    private double side2;
    private double side3;
    public Triangle(double side1,double side2,double side3){
        this.side1=side1;
        this.side2=side2;
        this.side3=side3;
    }
    public double getArea(){
        double p=(side1+side2+side3)/2;
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
    }
    public boolean vaildate(){
        if(side1 > 0 && side2 >0 && side3 >0 && (side1 + side2) > side3 && (side1 + side3) > side2 && (side2 + side3) > side1 && Math.abs(side1 - side2) < side3 && Math.abs(side1 - side2) < side3 && Math.abs(side2 - side3) < side1){
            return true;
        }
        else
            return false;
    }
    public String toString(){
        return null;
    }
    @Override
    public boolean validate() {
        // TODO Auto-generated method stub
        return false;
    }
}

 

代码分析:该代码创建了多个类,将计算天数等多种函数放进该类里面,使用了多态,比前面的代码更简洁,更好,符合创建对象思想。

 

7-6 实现图形接口及多态性 

设计:按类图要求设计,创建类和接口,GetArea为一个接口,只有一个的抽象方法,Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口,

这次主要是从不同方向了解到了面向对象的设计思维,对接口的理解为构造整体框架后实现方法。

源码:

import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        double radius,width,length,area1,area2;
        radius=input.nextDouble();
        width=input.nextDouble();
        length=input.nextDouble();
        if(radius>0 && width>0 && length>0){
            GetArea ymj =new Circle(radius);
            GetArea jxmj =new Recttangle(width,length);
            area1=ymj.getArea();
            area2=jxmj.getArea();
            System.out.printf("%.2f\n",area1);
            System.out.printf("%.2f\n",area2);
        }
        else{
            System.out.print("Wrong Format");
        }
    }
}
class Circle implements GetArea{
    private double radius;
    public double getRadius(){
        return radius;
    }
    public void setRadius(double radius){
        this.radius=radius;
    }
    public Circle(double radius){
        this.radius=radius;
    }
    public Circle(){
    }
    public double getArea(){
        return Math.PI*radius*radius;
    }
}
class Recttangle implements GetArea{
    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(){
        this.length=length;
    }
    public Recttangle(double width,double length){
        this.width=width;
        this.length=length;
    }
    public Recttangle(){
    }
    public double getArea(){
        return width*length;
    }
}
interface GetArea{
    public abstract double getArea();
}

代码分析:这些代码,都有很清晰的要求,按要求创建对象,类,接口即可,无需增加其他内容,所有无改进地方。

 

总结:主要是对接口以及父类的理解,接口是完全抽象的,可以用于构建整体框架,但难以实现规划未来需要添加的方法,但父类的话可以实现,可以理解为接口是完全抽象的类的“父类”,封装主要是为了将抽象的物质具体话,可以用各个类去丰富该封装的对象,具象话该对象,这同样也是面向对象的思维方式。对于多态的话,他也是面向对象必不可少的一部分,多态的实现使得类与类之间可以相互转换,这同样也是面向对象的思维方式,可以简化抽象的物质或者问题。

 

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

除了第六次作业的正则函数比较简单,书本后面有提到,其他的使用正则函数题目都比较难。会有一种毫无头绪的感觉。正则函数简单的来说就是先求出比较式的正则表达式再进行匹配判断是否符合要求。

 

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

题目集5(7-4)使用的是聚合,这也是当前面向对象编程方式最认可的一种形式,这种编程方式,是现在大家普遍认可且优秀的编程方式,这种编程方式也是以后大家在公司进行编写的一种要求,主要是可以让类与类之间减少交互甚至没有交互,可以增加改代码的维护性以及升级,增加功能时的简便性,使得每个板块独立,这样做使得单个板块的复用性变得很高,在修改时可以做到直接治根治本的效果,对其他的类的影响可以降到最低,使得该程序不会受到致命的bug(若遇到bug且类与类之间交互太多,可以使得多方程序崩溃),进而使得该程序的可维护性也得到增强,每个板块做每个板块的事情完全符合单一职责的原则.这种集合框架可维护性强,且相对于非聚合关系的代码来说更加安全,可以增加代码的独立性和稳定性.

(3)采坑心得:

题目集6的7-2:

一直过不了,但检测时又没有问题,经过多次排查,才发现是输入字符串数组设小的,设到100测试点才通过。

题目集6的7-5:

代码编写完成后,一直答案全部错误,经过测试发现没有任何输出,后面用编译器编译才发现没有加   // TODO Auto-generated method stub,经过编译器修改才通过。

题目集5的7-4:

正则表达式不完全,原代码存在问题:正则表达式无法全部列举出所有情况或正则表达式运算符的优先级导致与实际想法不符,改进代码后才通过。

(4)改进建议:

在题目集4中可以减少if else的使用,感觉我的代码比较繁琐,应该将不同情况进行更清楚简洁的分类,对于月份对应的天数,不用一个个都写出来,而是采用数组的形式输入。题目集5的7-2也是如此。对于题目集5的7-3,我应该好好去学一下正则表达式的使用,参考正确的代码,考虑测试点中提示的多种情况,将代码编写正确。对于代码编写最好先在脑中建立一个结构再编写,要不然就很乱,也很容易出错,编写中也最好追求简单的清楚的方法。对于继承多态类的语法还不太熟悉还需要多加练习。

(5)总结:

通过这三次题目集的训练,使我对面向对象编程有了一定的理解,前两次让我更加熟悉java基础语法的使用,比如正则函数,创建类,多态,接口使用等。还有学会面对对象思想用java思想编程而不是以前的c语言,分对象写代码而不是分段。但对于面对对象思想和正则表达式我应该进一步的学习及研究。通过一次次的刷题,逐步增强了自己的编程能力的自学能力,增强了自己的信心。

 

posted @ 2021-11-13 14:34  riany  阅读(75)  评论(0)    收藏  举报