题目集4-6题目总结

  1. 前言

第四次题目集正式引入了java中的核心要素——类之间的聚合与继承,多种构造方法与实现功能方法的便捷高效应用。题量为三道大题,需要严密的分析思路和方法步骤以及整体观的结合,考察学生知识应用于设计分析,难度较大。考察的主要知识点包括类的对象属性与访问修饰,类的构造方法,类中方法的调用,类与类之间聚合关系的明确建立,类与类之间继承关系关键字extends的写法,继承父类对象以及方法super的使用规范写法和注意事项,主方法中创建类名以及如何传参和调用类中方法的使用。

 

 

 

 

第五次题目集前三道题目复习了之前学习数组的相关知识,字符串以及整形数组的建立和遍历输出,以某种字符分隔字符串的函数split方法,字符串的长度计算length()方法,以及数组排序的Arrays.sort()函数。此外还有三种典型的对数组内元素大小排序的方法——冒泡排序法,选择排序法,插入排序法。后两道题关于正则表达式和类的使用,考察分析实践能力,类的建立,方法的正确使用与返回值,一系列数据的处理,正则表达式的合理规范使用。题量为五题,难度中等偏上。

 

 

 

 

 

第六次题目集题目则进行了集中的正则表达式写法使用训练和java类中类继承,多态与接口的强化练习,运用正则表达式对输入各种数据格式的检验,ASCLL码对字符串里各个字符进行比较大小排序。考察获取字符串单个字符的函数charAt()方法,对数组中所有元素进行排序的Arrays.sort函数方法,复杂正则表达式的书写,类与类中继承关系的构建extends,实现功能的方法的正确分析,接口中抽象方法的构建,impiements实现接口的使用,类中实现抽象方法的正确步骤以及方法的重写与重载。题量为六题,难度中等偏上。

 

 

  1. 设计与分析

 

(1)题目集47-2)和题目集57-5)两种日期类聚合设计的比较优劣

 

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

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

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

 

 

 

 

(1)题目集47-2)和题目集57-5)两种日期类聚合设计的比较优劣

 

题目集47-2

设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

 

 

 

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

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

 

这道题首先需要建立一个日期类(DateUtil),把年份,月份,天数作为类中的对象,并对这些对象运用private进行封装,同时进行访问与修饰,其次构建无参构造方法与有参构造方法,然后就是求下n天,求前n天,求两个日期之间相差的天数这三种具体方法的实现,

比如

public DateUtil getPreviousNDays(int n)//求前n天

public DateUtil getNextNDays(int n)//求后n天

public int getDaysOfDates(DateUtil date)//求两个日期之间相差的天数

 

求前n天就是day用一个for循环减1减n次,同时注意day<1时,month-1,如果month<1,month重新变为12,year年份减1。

求下n天就是用一个for循环加1加n次,同时注意如果天数大于所在月份的最大天数,天数变为1,月份加1,如果月份大于12,月份变为1,年份加1.

相关代码及注释如下:

 

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int year;
        int month;
        int day;

        int choice=input.nextInt();
        switch(choice)
        {
            case 1:
                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);
                }

                int x=input.nextInt();

                if(x<=0)
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                else
                {
                    System.out.println(date.getNextNDays(x).showDate());
                }
                break;


            case 2:
                year=Integer.parseInt(input.next());
                month=Integer.parseInt(input.next());
                day=Integer.parseInt(input.next());

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

                if(!date1.checkInputValidity(year,month,day))
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }

                int y=input.nextInt();

                if(y<=0)
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                System.out.println(date1.getPreviousNDays(y).showDate());
                
                break;

            case 3:
                year=Integer.parseInt(input.next());
                month=Integer.parseInt(input.next());
                day=Integer.parseInt(input.next());

                int year3=Integer.parseInt(input.next());
                int month3=Integer.parseInt(input.next());
                int day3=Integer.parseInt(input.next());

                DateUtil date2=new DateUtil(year,month,day);
                DateUtil date3=new DateUtil(year3,month3,day3);


                if(date2.checkInputValidity(year,month,day)&&date3.checkInputValidity(year3,month3,day3))
                {
                    System.out.println(date2.getDaysOfDates(date3));
                    System.exit(0);
                }
                else
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                
                break;
            default:
                System.out.println("Wrong Format");
                System.exit(0);
                
                break;


        }
    }

}



class DateUtil{
    private int year;
    private int month;
    private int day;
    private int[] a={31,28,31,30,31,30,31,31,30,31,30,31};

    public DateUtil(){

    }//无参构建

    public DateUtil(int year,int month,int day)
    {
        this.year=year;
        this.month=month;
        this.day=day;
    }

    public int  getYear()
    {
        return year;
    }

    public void setYear(int year)
    {
        this.year=year;
    }

    public int getMonth()
    {
        return month;
    }

    public void setMonth(int month)
    {
        this.month=month;
    }

    public int getDay()
    {
        return day;
    }

    public void setDay(int day)
    {
        this.day=day;
    }

    public boolean isLeapYear(int year)
    {
        if((year%4==0&&year%100!=0)||(year%400==0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean checkInputValidity(int year,int month,int day)
    {
        if(isLeapYear(year))
        {
            a[1]=29;
        }
        if((year>=1900&&year<=2050)&&(month>=1&&month<=12)&&(day>=1&&day<=a[month-1]))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public int getDaysOfDates(int year,int month)//月份的对应天数
    {
        int x=a[month-1];
        if(isLeapYear(year)&&month==2)
        {
            x=29;
        }
        return x;
    }


    public DateUtil getPreviousNDays(int n)//求前n天
    {
        int year=this.year;
        int month=this.month;
        int day=this.day;
        for(int i=1;i<=n;i++)
        {
            day--;
            while(day<1)
            {
                month--;
                if(month<1)
                {
                    month=12;
                    year--;
                }
                day+=getDaysOfDates(year,month);
            }

        }

        return new DateUtil(year,month,day);
    }


    public DateUtil getNextNDays(int n)//求后n天
    {
        int year=this.year;
        int month=this.month;
        int day=this.day;

        for(int i=1;i<=n;i++)
        {
            day++;
            if(day>getDaysOfDates(year,month))
            {
                day=1;
                month++;
                if(month>12)
                {
                    month=1;
                    year++;
                }
            }
        }

        return new DateUtil(year,month,day);
    }


    public boolean equalTwoDate(DateUtil date)//比较两个日期是否相等
    {
        if(date!=null)
        {
            if(year==date.year&&month==date.month&&day==date.day)
            {
                return true;
            }
        }

        return false;
    }



    public boolean compareDates(DateUtil date)//后输入的日期是否大于前一个
    {
        if(date!=null)
        {
            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 int getDaysOfDates(DateUtil date)
    {
        DateUtil a=this;
        DateUtil b=date;
        if(this.compareDates(date))
        {
            a=date;
            b=this;
        }

        int m;//记录年天数
        int k=0;//记录月天数
        int h=0;//记录月天数
        int t=0;//记录两个日期之间闰年数
        int q;//记录总天数
        for(int i=a.getYear();i<b.getYear();i++)
        {
            if(isLeapYear(i))
            {
                t++;
            }
        }

        m=365*(b.getYear()-a.getYear())+t;//闰年是366天

        for(int i=a.getMonth()-1;i>=1;i--)
        {
            k+=getDaysOfDates(a.getYear(),i);
        }
        k+=a.getDay();

        for(int i=b.getMonth()-1;i>=1;i--)
        {
            h+=getDaysOfDates(b.getYear(),i);
        }
        h+=b.getDay();


        q=m-k+h;

        return q;


    }


    public String showDate()
    {
        return year+"-"+month+"-"+day;
    }









}

 

 

 

 

题目集5(7-5)

设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

 

 

 

 

 

 

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

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

其实这两道题结构思路相差不大,就是年份取值范围不同,输出更加详细,更具体,体现了面向对象服务的一点特点,其次更重要的一点是对程序运行简便性有了更多的要求,如果存在太多循环和if-else语句,会发生运行超时,需要注意这一点,这次日期类更加严谨,类与类之间的联系有了更缜密的调用,构造方法,添加了一些辅助方法,用来进一步检验输入数据的合理性计算和比较输出。

相关代码及注释如下:

 

 

 

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int year;
        int month;
        int day;

        int choice=input.nextInt();
        switch(choice)
        {
            case 1:
                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);
                }

                int x=input.nextInt();

                if(x<0)
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                else
                {
                    System.out.print(year+"-"+month+"-"+day+" next "+x +" days "+"is:");
                    System.out.print(date.getNextNDays(x).showDate());
                }
                break;


            case 2:
                year=Integer.parseInt(input.next());
                month=Integer.parseInt(input.next());
                day=Integer.parseInt(input.next());

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

                if(!date1.checkInputValidity(year,month,day))
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }

                int y=input.nextInt();

                if(y<0)
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                else
                {
                    System.out.print(year+"-"+month+"-"+day+" previous "+y +" days "+"is:");
                    System.out.print(date1.getPreviousNDays(y).showDate());
                }

                break;

            case 3:
                year=Integer.parseInt(input.next());
                month=Integer.parseInt(input.next());
                day=Integer.parseInt(input.next());

                int year3=Integer.parseInt(input.next());
                int month3=Integer.parseInt(input.next());
                int day3=Integer.parseInt(input.next());

                DateUtil date2=new DateUtil(year,month,day);
                DateUtil date3=new DateUtil(year3,month3,day3);


                if(date2.checkInputValidity(year,month,day)&&date3.checkInputValidity(year3,month3,day3))
                {
                    System.out.print("The days between "+year+"-"+month+"-"+day+" and "+year3+"-"+month3+"-"+day3+" are:");
                    System.out.print(date2.getDaysOfDates(date3));
                    //System.exit(0);
                }
                else
                {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }

                break;
            default:
                System.out.println("Wrong Format");
                System.exit(0);
                break;


        }
    }

}



class DateUtil{
    private int year;
    private int month;
    private int day;
    private int[] a={31,28,31,30,31,30,31,31,30,31,30,31};

    public DateUtil(){

    }//无参构建

    public DateUtil(int year,int month,int day)
    {
        this.year=year;
        this.month=month;
        this.day=day;
    }

    public int  getYear()
    {
        return year;
    }

    public void setYear(int year)
    {
        this.year=year;
    }

    public int getMonth()
    {
        return month;
    }

    public void setMonth(int month)
    {
        this.month=month;
    }

    public int getDay()
    {
        return day;
    }

    public void setDay(int day)
    {
        this.day=day;
    }

    public boolean isLeapYear(int year)
    {
        if((year%4==0&&year%100!=0)||(year%400==0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean checkInputValidity(int year,int month,int day)
    {
        if(isLeapYear(year))
        {
            a[1]=29;
        }
        if((year>=1820&&year<=2020)&&(month>=1&&month<=12)&&(day>=1&&day<=a[month-1]))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public int getDaysOfDates(int year,int month)//月份的对应天数
    {
        int x=a[month-1];
        if(isLeapYear(year)&&month==2)
        {
            x=29;
        }
        return x;
    }


    public DateUtil getPreviousNDays(int n)//求前n天
    {
        int year=this.year;
        int month=this.month;
        int day=this.day;
        for(int i=1;i<=n;i++)
        {
            day--;
            while(day<1)
            {
                month--;
                if(month<1)
                {
                    month=12;
                    year--;
                }
                day+=getDaysOfDates(year,month);
            }

        }
        return new DateUtil(year,month,day);
    }


    public DateUtil getNextNDays(int n)//求后n天
    {
        int year=this.year;
        int month=this.month;
        int day=this.day;
        for(int i=1;i<=n;i++)
        {
            day++;
            if(day>getDaysOfDates(year,month))
            {
                day=1;
                month++;
            }
            if(month>12)
            {
                month=1;
                year++;
            }
        }
        return new DateUtil(year,month,day);
    }


    /*public boolean equalTwoDate(DateUtil date)//比较两个日期是否相等
    {
        if(date!=null)
        {
            if(year==date.year&&month==date.month&&day==date.day)
            {
                return true;
            }
        }

        return false;
    }*/



    public boolean compareDates(DateUtil date)//后输入的日期是否大于前一个
    {
        if(date!=null)
        {
            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 int getDaysOfDates(DateUtil date)
    {
        DateUtil a=this;
        DateUtil b=date;
        if(this.compareDates(date))
        {
            a=date;
            b=this;
        }

        int m;//记录年天数
        int k=0;//记录月天数
        int h=0;//记录月天数
        int t=0;//记录两个日期之间闰年数
        int q;//记录总天数
        for(int i=a.getYear();i<b.getYear();i++)
        {
            if(isLeapYear(i))
            {
                t++;
            }
        }

        m=365*(b.getYear()-a.getYear())+t;//闰年是366天

        for(int i=a.getMonth()-1;i>=1;i--)
        {
            k+=getDaysOfDates(a.getYear(),i);
        }
        k+=a.getDay();

        for(int i=b.getMonth()-1;i>=1;i--)
        {
            h+=getDaysOfDates(b.getYear(),i);
        }
        h+=b.getDay();


        q=m-k+h;

        return q;


    }
    public String showDate()
    {
        return year+"-"+month+"-"+day;
    }









}

 

  

 

 

(1)题目集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自动生成)
  • 输出的数值均保留两位小数

主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;

假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format

这道题开始引导学者接触java中比较灵活的继承关系实例,首先创建一个父类Shape,它只有一个求面积的getArea方法,继承它的子类都可以重写这个求面积的方法用来返回相应面积。Circle类继承Shape类,有一个私有属性对象半径Radius,用private修饰即可,同时用get,set方法对半径进行访问与修饰,无参构建的时候显示题目要求的信息,然后重写父类Shape中getArea方法,用数学公式4*Math.PI*Radius*Radius即可。

其次类Rectangle继承类Shape,有两个private(私有属性)Width和Length,用get,set方法对半径进行访问与修饰,无参构建的时候显示题目要求的信息,然后重写父类Shape中getArea方法,用数学公式Width*Length即可。

Ball继承类Circle,继承对象半径,super调用,重写getArea方法,用4*super.getArea即可,还有一个求体积的getVolume方法,用相应的数学公式即可。

Box继承类Rectangle,有一个私有实型对象Height,用get,set方法对半径进行访问与修饰,无参构建的时候显示题目要求的信息,然后重写父类Rectangle中getArea方法,用数学公式height * super.getArea()即可,还有一个求表面积的getArea方法,重写调用,比如

 

double a=getLength();
double b=getWidth();
return 2*(a*b+a*height+b*height);

 

  

 

 

 

 即可

主方法main中需要分情况1,2,3,4来确定使用哪个类,然后对输入的数据进行合理性检验,其次构建使用类,传入数值,计算相关面积与体积,然后显示输出即可。

相关代码及注释如下:

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int y=input.nextInt();
        switch(y)
        {
            case 1:
                double Radius = input.nextDouble();
                if(Radius<=0.0)
                {
                    System.out.print("Wrong Format");
                }
                else
                {
                    Circle circle = new Circle();
                    circle.setRadius(Radius);
                    System.out.printf("Circle's area:%.2f\n",circle.getArea());
                }
                break;

            case 2:
                double width=input.nextDouble();
                double length=input.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.printf("Rectangle's area:%.2f\n",rectangle.getArea());
                }
                break;

            case 3:
                double R=input.nextDouble();
                if(R<=0.0)
                {
                    System.out.print("Wrong Format");
                }
                else
                {
                    Ball ball=new Ball();
                    ball.setRadius(R);
                    System.out.printf("Ball's surface area:%.2f\n",ball.getArea());
                    System.out.printf("Ball's volume:%.2f\n",ball.getVolume());
                }
                break;

            case 4:
                double x=input.nextDouble();
                double z=input.nextDouble();
                double m=input.nextDouble();
                if(x<=0.0||z<=0.0||m<=0.0)
                {
                    System.out.print("Wrong Format");
                }
                else
                {
                    Box box=new Box();
                    box.setWidth(x);
                    box.setLength(z);
                    box.setHeight(m);
                    System.out.printf("Box's surface area:%.2f\n",box.getArea());
                    System.out.printf("Box's volume:%.2f\n",box.getVolume());
                }
                break;

            default:
                System.out.print("Wrong Format");
        }


    }
}


class Shape{
    public Shape()
    {
        System.out.println("Constructing Shape");
    }
    public  double getArea()
    {
        return 0.0;
    }//求图形面积
}


class Circle extends Shape{
    private double radius;

    public Circle()
    {
        System.out.println("Constructing Circle");
    }

    public double getRadius()
    {
        return radius;
    }

    public void setRadius(double radius)
    {
        this.radius=radius;
    }

    public double getArea()
    {
        return Math.PI * radius * radius;//求圆面积
    }


}

class Rectangle extends Shape{
    private double width;
    private double length;

    public Rectangle()
    {
        System.out.println("Constructing Rectangle");
    }

    public double getWidth()
    {
        return width;
    }

    public void setWidth(double width)
    {
        this.width=width;
    }

    public double getLength()
    {
        return length;
    }

    public void setLength(double length)
    {
        this.length=length;
    }

    public double getArea()
    {
        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 x=getRadius();//继承父类,访问半径
        return 4.0/3.0 * x *x *x *Math.PI;//求球体积
    }
}

class Box extends Rectangle{
    private double height;
    public Box()
    {
        System.out.println("Constructing Box");
    }

    public double getHeight()
    {
        return height;
    }

    public void setHeight(double height)
    {
        this.height=height;
    }

    public double getVolume()
    {
        return height * super.getArea();
    }

    public double getArea()
    {
        double a=getLength();
        double b=getWidth();
        return 2*(a*b+a*height+b*height);
    }
}

 

  

 

 

 

 

题目集6(7-6)

掌握类的继承、多态性及其使用方法。从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。

这个题目在前面题目的基础之上添加了继承接口的要求,需要继承接口中部分抽象方法的实现,还有多态性的应用,还有输入输出也更复杂,各个类需要构建多少个,完全由输入决定,还有输出各个图形的面积,面积总和,进行简单的排序再次输出。接口书写比如

 

abstract class Shape{
    public abstract double getArea();//获得面积
public abstract boolean validate();//检验数据是否合理

 

  

重要的是如何添加这个方法,应用@Override,求面积方法与上次一样,合理性检验,Radius需要大于0,Width需要大于0,Length需要大于0,还有一个新类Triange,有三条边a,b,c,get,set访问修饰,getArea重写需要用到海伦公式,比如这样

 

public double getArea()
    {
        double d=(a+b+c)/2;
        return Math.sqrt(d*(d-a)*(d-b)*(d-c));
    }

 

  

对于三角形,三条边都需要大于0,并且任意两边大于第三边,可以这样写

 

public boolean validate()
    {
        if(a<=0||b<0||c<0)
        {
            return false;
        }
        double[] arr=new double[3];
        arr[0]=a;
        arr[1]=b;
        arr[2]=c;
        Arrays.sort(arr);
        return arr[0]+arr[1]>arr[2];
    }
}

 

  

main中的话需要用循环构建各个类,每次传入数值,调用求面积方法,得到的面积存入一个数组进行排序,然后输出,其中有调用合理性方法,如果返回假,可以令k,m,n=1,然后if判断输出错误,可以更加严谨。

相关代码及注释如下:

 

import java.util.Arrays;
import java.util.Scanner;

abstract class Shape{
    public abstract double getArea();
    public abstract boolean validate();
    //public abstract String  toString();
}

class Circle extends Shape
{
    private double Radius;
    public Circle()
    {

    }
    public Circle(double Radius)
    {
        super();
        this.Radius=Radius;
    }

    public double getRadius()
    {
        return Radius;
    }

    public void setRadius(double Radius)
    {
        this.Radius=Radius;
    }

    @Override
    //实现getArea方法

    public double getArea()
    {
        return Math.PI*Radius*Radius;
    }
    @Override

    public boolean validate()
    {
        return Radius>0;
    }

    /*public String toString()
    {

    }*/
}

class Rectangle extends Shape
{

    private double Width;
    private double Length;

    public Rectangle()
    {

    }


    public Rectangle(double Width,double Length)
    {
        this.Length=Length;
        this.Width=Width;


    }

    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()
    {
        return Width*Length;
    }

    @Override

    public boolean  validate()
    {
        return Width>0&&Length>0;
    }



}

class Triangle extends Shape
{
    private double a;
    private double b;
    private double c;

    public Triangle()
    {

    }

    public Triangle(double a,double b,double c)
    {
        super();
        this.a=a;
        this.b=b;
        this.c=c;
    }

    public double getA()
    {
        return a;
    }

    public void setA(double a)
    {
        this.a=a;
    }

    public double getB()
    {
        return b;
    }

    public void setB(double b)
    {
        this.b=b;
    }


    public double getC()
    {
        return c;
    }

    public void setC(double c)
    {
        this.c=c;
    }

    @Override


    public double getArea()
    {
        double d=(a+b+c)/2;
        return Math.sqrt(d*(d-a)*(d-b)*(d-c));
    }

    @Override

    public boolean validate()
    {
        if(a<=0||b<0||c<0)
        {
            return false;
        }
        double[] arr=new double[3];
        arr[0]=a;
        arr[1]=b;
        arr[2]=c;
        Arrays.sort(arr);
        return arr[0]+arr[1]>arr[2];
    }
}

public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int x,y,z;
        int t=0,k=0,m=0,n=0;
        x=input.nextInt();
        y=input.nextInt();
        z=input.nextInt();
        if(x<0||y<0||z<0)
        {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        double sum=0;
        Circle[] circle=new Circle[x];
        Rectangle[] rectangle=new Rectangle[y];
        Triangle[] triangle=new Triangle[z];
        double[] d=new double[x+y+z];

        for(int i=0;i<x;i++)
        {
            double radius=input.nextDouble();
            circle[i]=new Circle(radius);
            d[t++]=circle[i].getArea();
            if(!circle[i].validate())
            {
                k=1;
            }
        }


        for(int i=0;i<y;i++)
        {
            double width=input.nextDouble();
            double length=input.nextDouble();
            rectangle[i]=new Rectangle(width,length);
            d[t++]=rectangle[i].getArea();
            if(!rectangle[i].validate())
            {
                m=1;
            }
        }


        for(int i=0;i<z;i++)
        {
            double a=input.nextDouble();
            double b=input.nextDouble();
            double c=input.nextDouble();
            triangle[i]=new Triangle(a,b,c);
            d[t++]=triangle[i].getArea();
            if(!triangle[i].validate())
            {
                n=1;
            }
        }

        if(k==1||m==1||n==1)
        {
            System.out.println("Wrong Format");
            System.exit(0);
        }

        System.out.println("Original area:");
        for(int i=0;i<x+y+z;i++)
        {
            System.out.printf("%.2f ",d[i]);
            sum+=d[i];
        }
        System.out.print("\n");
        System.out.print("Sum of area:");
        System.out.printf("%.2f\n",sum);
        System.out.println("Sorted area:");
        Arrays.sort(d);
        for(int i=0;i<x+y+z;i++)
        {
            System.out.printf("%.2f ",d[i]);
        }

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

        System.out.print("Sum of area:");
        System.out.printf("%.2f",sum);

    }
}

 

  

题目集6(7-6)

  • GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
  • Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
  • 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)

这个题目只有两个类,主要实现接口继承及多态性的一些实例应用,接口只有一个getArea,里面有一个抽象方法getArea(求面积),其他两个类需要使用这个方法,返回面积值,main中调用,按照要求输出并保留两位小数,这两个类与前面题目的写法相同,main输入需要判断合理性,不合理就输出错误,合理就构造类并传入数据,调用方法计算面积并输出即可。

相关代码及注释如下:

 

import java.util.Scanner;
import java.text.DecimalFormat;

//定义了GetArea接口
interface getArea {
    double getArea(); // 定义抽象方法getArea()
}

class Circle implements getArea
{
    private double Radius;

    public Circle()
    {

    }

    public Circle(double Radius)
    {
        this.Radius=Radius;
    }

    public double getRadius()
    {
        return Radius;
    }

    public void setRadius(double Radius)
    {
        this.Radius=Radius;
    }

    //实现getArea方法

    public double getArea()
    {
        return Math.PI*Radius*Radius;
    }

}


 class Rectangle implements getArea
{
    private double Width;
    private double Length;

    public Rectangle()
    {

    }


    public Rectangle(double Width,double Length)
    {
        this.Length=Length;
        this.Width=Width;


    }

    public double getWidth()
    {
        return Width;
    }

    public void setWidth(double Width)
    {
        this.Width=Width;
    }

    public double getLength()
    {
        return Length;
    }

    public void setLength(double Length)
    {
        this.Length=Length;
    }

    public double getArea()
    {
        return Width*Length;
    }
}


public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        double radius=input.nextDouble();
        double width=input.nextDouble();
        double length=input.nextDouble();
        if(radius<=0||width<=0||length<=0)
        {
            System.out.println("Wrong Format");
        }
        else
        {
            Circle circle=new Circle(radius);
            Rectangle rectangle=new Rectangle(width,length);
            System.out.printf("%.2f\n",circle.getArea());
            System.out.printf("%.2f\n",rectangle.getArea());
        }
    }
}

 

  

 

 

 

 

 

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

题目集六前四道题目对正则表达式进行熟系与强化的训练,正则表达式主要是对特殊排列或者特定的一类字符串的检验,QQ号检验中QQ号必须是5-15位的数字且第一位不能为0,学习相关的正则表达式之后,对部分规定字符串的判断书写了解之后就可以用【1-9】【0-9】{4-14},第一位数字是1-9,之后的数字是0-9,出现4-14次。运用match匹配。验证码检验,验证码是四位数字或字母(不区分大小写),使用\w即可,java中写\\w{4},然后match匹配。学号检验有比较多的情况,比较复杂,2020固定,然后有11,12,13,14,15,16,17,61,71,72,81,82,后两位01-40。了解教科书知识写法后,可以巧妙使用【0-3】【1-9】|40即可。

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

 

 

 

 

 

 

3.采坑心得

题目集4(7-2)for(int i=a.getYear();i<b.getYear();i++)

        {

            if(isLeapYear(i))

            {

                t++;

            }

        }

之前我计算年份之间差的天数,两个年份之间相减数乘以365,然后计算天数有点不够,原因是有部分年份是闰年,366天,所以需要判断,把多出来的一天累加即可。

if(y<=0)

                {

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

                    System.exit(0);

                }

之前写的时候忘记System.exit(0),结果程序没停住导致逻辑性错误。

 

 

 

 

 

 

题目集6(7-5)

 

public double getArea()

 

    {

 

        double d=(a+b+c)/2;

 

        return Math.sqrt(d*(d-a)*(d-b)*(d-c));

 

    }

 

之前没有写海伦公式,天真的使用勾股,中值定理,越写越不通,查找相关资料后才明白怎么写,这里只知道三条边,只能用这个公式。

 

double[] arr=new double[3];

 

        arr[0]=a;

 

        arr[1]=b;

 

        arr[2]=c;

 

        Arrays.sort(arr);

 

        return arr[0]+arr[1]>arr[2];

 

这里把三条边数值存入数组,进行排序,然后进行返回。

 

 

 

 

题目集67-6

 System.out.printf("%.2f\n",circle.getArea());

            System.out.printf("%.2f\n",rectangle.getArea());

 

之前写的时候错误的使用System.out.println,结果无法输出调用circlerectangle求面积返回的值,查看书籍了解javaprintfc语言差不多,输出特定位数,然后换成上面这个格式即可。

 

 

 

 

 

  1. 改进建议

 

题目集6(7-4)中也可以不局限与2020年级,可以覆盖整个学校,个人学号也不一定在40以内,可以扩大范围,增加判断与检验。

 

题目集6(7-5)可以在添加几个类,比如圆柱体,圆锥体等等,可以再加一些继承关系,加一些接口,使用一些求母线,斜线,质量,密度的抽象方法调用。

 

  1. 总结

通过这三次题目集的认识训练,我终于接触和摸清了java中神奇的三大特点——继承,多态,类。了解了类的一般无参构建方法以及特殊的有参构建方法,和具体在什么情况下使用,继承关系的正确使用,继承对象及方法的操作,接口抽象方法的调用,多态使用的方便与多性能特点,一系列算法的认识,面对复杂问题时设计的算天数等等的简便巧妙方法,虽然知道自己还有很多不足,但我仍然会继续学习,向其他优秀者学习,争取弥补自己的一些缺点,认清问题所在,不断的完善自己。谢谢!

 

 

 

 

posted @ 2021-04-29 09:22  风大zy  阅读(136)  评论(0)    收藏  举报