java三次作业题目集4、5、6分析

1)前言

题目集4

主要使用到java语言的输入输出语句,if-else语句,类的继承,正则表达式的运用,字符串的识别与切割,运用简单的数学知识完成计算,例如计算圆的面积矩形的面积、球的面积等。总共3题,题目难度很大

题目集5

主要使用到java语言的简单的输入输出语句,if-else语句,字符串数组、整型数组的定义,数组的合并,java的定义和使及其继承,this语句的使用、使用Pattern以及Matcher方法对字符串进行匹配、正则表达式的运用,字符串的识别与切割,字符串的切割与识别等。总共5题,题目难度第4题较难

题目集6

主要使用到java语言的简单的输入输出语句,if-else语句,类的定义和使用,正则表达式的使用,this语句的使用、使用Pattern以及Matcher方法对字符串进行匹配。需要对类的创建和使用有一个清晰的认识以及熟练使用正则表达式。以及自学正则表达式的自学、抽象类的使用等。总共3题,题目难度较为简单

2)设计与分析

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

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

 

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

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

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
  • 源码:
    import java.util.Scanner;
    
    class Year{
        int value;
            public Year(){
        }
            public Year(int value){
            this.value=value;
        }
           public int getValue(){
            return value;
        }
           public void setValue(int value){
            this.value=value;
        }
            public  boolean isLeapYear(){
            if((value%4==0&&value%100!=0)||value%400==0)
                return true;
            elsereturn false;
        }
            public boolean validate(){
            if(value<=2050&&value>=1900)
                return true;
            elsereturn false;
        }
            public void yearIncrement(){
            value=value+1;
        }
            public void yearReduction(){
            value=value-1;
        }
    }
    class Month{
        int value;
        Year year;
           public Month(){
        }
            public Month(int yearValue,int monthValue){
            this.year=new Year(yearValue);
            this.value=monthValue;
        }
            public int getValue(){
            return value;
        }
        public Year getYear(){
            return year;
        }
            public void setValue(int value){
            this.value=value;
        }
        public void setYear(Year year){
            this.year=year;
        }
           public void resetMin(){
            value=1;
        }
        public void resetMax(){
            value=12;
        }
           public boolean validate(){
            if(value>=1&&value<=12)
                return true;
            elsereturn false;
        }
            public void dayIncrement(){
            value=value+1;
        }
            public void dayReduction(){
            value=value-1;
        }
    }
    class Day{
        int value;
        Month month;
        int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
            public Day(){
        }
          public Day(int yearValue,int monthValue,int dayValue){
            this.month=new Month(yearValue,monthValue);
            this.value=dayValue;
        }
            public int getValue(){
            return value;
        }
        public Month getMonth(){
            return month;
        }
            public void setValue(int value){
            this.value=value;
        }
        public void setMonth(Month value){
            this.month=value;
        }
            public void resetMin(){
            value=1;
        }
            public void resetMax(){
            value=a[month.getValue()-1];
        }
            public boolean validate(){
            if(this.getMonth().getYear().isLeapYear())
                a[1]=29;
            if(value>=1&&value<=a[month.getValue()-1])
                return true;
            elsereturn false;
        }
           public void dayIncrement() {
            value=value+1;
        }
           public void dayReduction() {
            value=value-1;
        }
    }
    class DateUtil{
        Day day;
           public DateUtil(){
        }
            public DateUtil(int d,int m,int y){
            this.day=new Day(d,m,y);
        }
            public Day getDay(){
            return day;
        }
        
        public void setDay(Day d){
            this.day=d;
        }
    
            public boolean checkInputValidity(){
            if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate())
                return true;
            elsereturn false;
        }
           public boolean compareDates(DateUtil date) {
            if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())
                return false;
            else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())
                return false;
            else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
                return false;
            elsereturn true;
        }
            public boolean equalTwoDates(DateUtil date){
            if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
                return true;
            elsereturn false;
        }
            public String showDate(){
            return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();
        }
        
        public int syts(DateUtil d){
            int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
            int b=0,i;
            for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){
                b=b+a[i];
            }
            b=b+a[d.getDay().getMonth().getValue()]-d.getDay().getValue();
            if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2)            b++;
            return b;
        }
    
        public DateUtil getNextNDays(int n){
            int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
            int y=0,m=0,d=0;
            int i,j;
            int b=syts(this);
            if(b>n){
                y=this.getDay().getMonth().getYear().getValue();
                if(this.getDay().getMonth().getYear().isLeapYear())
                    a[2]=29;
                }
                int e=a[this.getDay().getMonth().getValue()];
                e=e-this.getDay().getValue();
                if(e>=n){
                    m=this.getDay().getMonth().getValue();
                    d=n+this.getDay().getValue();
                }
                else{
                    n=n-e;
                    m=this.getDay().getMonth().getValue()+1;
                    i=m;
                    while(n-a[i]>0&&i<=12){
                        n=n-a[i];
                        m++;
                        i++;
                    }
                    d=n;
                }
            }
            else{
                n=n-b;
                y=this.getDay().getMonth().getYear().getValue()+1;
                int c=365;
                if(new Year(y).isLeapYear()){
                    c++;
                }
                while(n-c>0){
                    n=n-c;
                    y++;
                    c=365;
                    if(new Year(y).isLeapYear())
                        c++;
                }
                i=1;
                while(n-a[i]>0&&i<=12){
                    n=n-a[i];
                    i++;
                }
                m=i;
                d=n;
            }
            return new DateUtil(y, m, d);
        }
        public DateUtil getPreviousNDays(int n){
            int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
            int y=0,m=0,d=0;
            int i,b;
            b=365-syts(this);
            if(this.getDay().getMonth().getYear().isLeapYear()){
               b++;
            }
            if (b>n){
                y=this.getDay().getMonth().getYear().getValue();
                int e=this.getDay().getValue();
                if(e>n){
                    m=this.getDay().getMonth().getValue();
                    d=e-n;
                }
                else{
                    n=n-e;
                    m=this.getDay().getMonth().getValue()-1;
                    i=m;
                    while(n-a[i]>0&&i>=0){//
                        n=n-a[i];
                        m--;
                        i--;
                    }
                    d=a[i]-n;
                    if(new Year(y).isLeapYear()&&m==2){
                        d++;
                    }
                }
            }
            else{
                n=n-b;
                y=this.getDay().getMonth().getYear().getValue()-1;
                int f=365;
                if(new Year(y).isLeapYear()){
                    f++;
                }
                while(n-f>0){
                    n=n-f;
                    y--;
                    f=365;
                    if(new Year(y).isLeapYear())
                        f++;
                }
                i=12;
                while(n-a[i]>0&&i>=0){//
                    n=n-a[i];
                    i--;
                }
                m=i;
                d=a[i]-n;//if(new Year(f).isLeapYear()&&m==2){
                    d++;
                }
            }
            return new DateUtil(y, m, d);
        }
      public int getDaysofDates(DateUtil date){
            DateUtil b1=this;
            DateUtil b2=date;
            if(this.equalTwoDates(date)){
                return 0;
            }
            else if(!this.compareDates(date)){
                b1=date;
                b2=this;
            }
            int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
            int i,j,ts=0;
            for(i=b1.getDay().getMonth().getYear().getValue()+1;i<b2.getDay().getMonth().getYear().getValue();i++){/
                ts=ts+365;
                if(new Year(i).isLeapYear())
                    ts++;
            }
            if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()==b2.getDay().getMonth().getValue()){//年份相同,月份相同,日不同
                ts=b2.getDay().getValue()-b1.getDay().getValue();
            }
            else if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()!=b2.getDay().getMonth().getValue()){//年份相同,月份不同if(b1.getDay().getMonth().getYear().isLeapYear())
                    a[2]=29;
                ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();
                ts=ts+b2.getDay().getValue();
                    ts+=a[j];
            }
            else if(b1.getDay().getMonth().getYear().getValue()!=b2.getDay().getMonth().getYear().getValue()){
                ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();
                ts=ts+b2.getDay().getValue();
                for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++)
                    ts=ts+a[j];
                for(j=b2.getDay().getMonth().getValue()-1;j>0;j--)
                    ts=ts+a[j];
                if(b1.getDay().getMonth().getYear().isLeapYear()&&b1.getDay().getMonth().getValue()<=2)
                    ts++;
                if(b2.getDay().getMonth().getYear().isLeapYear()&&b2.getDay().getMonth().getValue()>2)
                    ts++;
            }
            return ts;
        }
    }
    //主类public class Main {
        public static void main(String[] args) {
            Scanner x=new Scanner(System.in);
            int year=0,month=0,day=0,a,b;
            a=x.nextInt();//输入判断类型
            year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日
            DateUtil c=new DateUtil(year,month,day);
            if(a==1){
                b=x.nextInt();
                if(!c.checkInputValidity()||b<0){
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                else
                    System.out.println(c.getNextNDays(b).showDate());
            }
            else if(a==2){
                b=x.nextInt();
                if(!c.checkInputValidity()||b<0){
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                else
                    System.out.println(c.getPreviousNDays(b).showDate());
            }
            else if(a==3){
                int y1,m1,d1;
                y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();
                DateUtil d=new DateUtil(y1,m1,d1);
                if(!c.checkInputValidity()||!d.checkInputValidity()){
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
                else
                    System.out.println(c.getDaysofDates(d));
            }
            else
                System.out.println("Wrong Format");
    
        }
    }

    相关类图:

  •  

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

    参考题目7-3的要求,设计如下几个类:DateUtilYearMonthDay,其中年、月、日的取值范围依然为:year[1820,2020] ,month[1,12] ,day[1,31] , 设计类图如下:

     

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

    求下n

    求前n

    求两个日期相差的天数

    注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

    输入格式:

    有三种输入方式(以输入的第一个数字划分[1,3]):

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

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

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

    源码:

    import java.util.Scanner;
    
    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 m = 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()) {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
    
                m = input.nextInt();
    
                if (m < 0) {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
    
                System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
                System.out.println(date.getNextNDays(m).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()) {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
    
                n = input.nextInt();
    
                if (n < 0) {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
    
                System.out.print(
                        date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
                System.out.println(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 fromDate = new DateUtil(year, month, day);
                DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
    
                if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                    System.out.println("The days between " + fromDate.showDate() +
                            " and " + toDate.showDate() + " are:"
                            + fromDate.getDaysofDates(toDate));
                } else {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
            }
            else{
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
    }
    
    class DateUtil {
        private int year;
        private int month;
        private int day;
    
        public DateUtil(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    
        public DateUtil(){}
    
        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;
        }
    
        private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
        private int getDayOfMonth(int year, int month) {
            int days = DAY_OF_MONTH[month - 1];
            if (month == 2 && isLeapYear(year)) {
                days = 29;
            }
            return days;
        }
    
        public boolean checkInputValidity()//检测输入的年、月、日是否合法
        {
            if (year < 1820 || year > 2020) return false;
            if (month < 1 || month > 12) return false;
    //        int _day = this.getDayOfMonth(year, month);
    //        return day <= _day;
            return day >= 1 && day <= 31;
        }
    
        public boolean isLeapYear(int year)//判断year是否为闰年
        {
            return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
        }
    
    
        public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
        {
            int year = this.year;
            int month = this.month;
            int day = this.day;
    //        day = Math.min(day, this.getDayOfMonth(year, month));
            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);
        }
    
        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;
        }
        
        private static final int[] mon = {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 (isLeapYear(i)) {
                    leapYearNum++;
                }
            }
    
            days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;
    
            int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
            int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
            return days - d1 + d2;
        }
        
        public String showDate()//以“year-month-day”格式返回日期值
        {
            return year + "-" + month + "-" + day;
        }
    }

    相应类图:

  • 二者聚合类比较:

    相比题目集47-2),题目集57-4)由主类直接调用继承各年、月、日的类,聚合度更集中,调用更直接简便和有效,而题目集47-2)中,是依次调用继承关系,相比聚合二灵活度更低不易修改和查错。

    应尽量去使用聚合而即题目集57-4)那样的结构,使得程序更易读和修改

    7-3 图形继承 (15 )

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

    Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积

    Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积

    Rectangle,继承自Shape,有两个私有实型属性widthlength,重写父类继承来的求面积方法,求矩形的面积

    Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积

    Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积

    注意:

    每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名

    每个类属性均为私有,且必须有gettersetter方法(可用Eclipse自动生成)

    输出的数值均保留两位小数

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

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

    源码:

    import java.util.*;
    import static java.lang.System.exit;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Main {
          public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              int numC=input.nextInt();
              int numR=input.nextInt();
              int numT=input.nextInt();
              int i,f,k=0;
              boolean t1=true,t2=true,t3=true;
              double sum=0;
              if (numC<0||numT<0||numR< 0) {
                    System.out.println("Wrong Format");
                    exit(0);
                }
              double[] area=new double [numC+numR+numT];
              Circle[] s1=new Circle[numC];
              Rectangle[] s2=new Rectangle[numR];
              Triangle[] s3=new Triangle[numT];
              for(i=0;i<numC;i++) {
                  double r=input.nextDouble();
                  s1[i]=new Circle(r);
                  area[k++]=s1[i].getArea();
                  if(r<=0){
                        t1=false;
                    }
              }
              withDraw(k);
              
              for(i=0;i<numR;i++){
                    double w=input.nextDouble();
                    double l=input.nextDouble();
                    s2[i]=new Rectangle(w,l);
                    area[k++]=s2[i].getArea();
                    if(!s2[i].isRectangle()){
                        t2=false;
                    }
              }
              withDraw(k);
              for(i=0;i<numT;i++){
                    double a1=input.nextDouble();
                    double a2=input.nextDouble();
                    double a3=input.nextDouble();
                    s3[i]=new Triangle(a1,a2,a3);
                    area[k++]=s3[i].getArea();
                    if(!s3[i].isTriangle()){
                        t3=false;
                    }
              }
              withDraw(1);
              if(t2==false||t3==false||t1==false){
                    System.out.println("Wrong Format");
                    exit(0);
              }
              System.out.println("Original area:");
              for(i=0;i<numC+numR+numT;i++){
                  System.out.printf("%.2f ",area[i]);
                  sum+=area[i];
              }
              withDraw(i);
              System.out.printf("\n");
              System.out.printf("Sum of area:");
              System.out.printf("%.2f\n",sum);
              System.out.println("Sorted area:");
              Arrays.sort(area);
              for(i=0;i<numC+numR+numT;i++){
                    System.out.printf("%.2f ",area[i]);
              }
              System.out.printf("\n");
              System.out.printf("Sum of area:%.2f",sum);
          }
          public static int withDraw(int i) {
                int k=i+1;
                return k;
            }
    }
    abstract class Shape {
        public Shape(){
            
        }
        public abstract double getArea();
    }
    class Circle extends Shape {
        double r;
        public Circle() {
            
        }
        public Circle(double r) {
            super();
            this.r = r;
        }
        @Override
        public double getArea() {
            return r*r*Math.PI ;
        }
    }
    class Rectangle extends Shape {
        double l;
        double w;
        public Rectangle() {
            
        }
        public Rectangle(double w, double l) {
            super();
            this.w=w;
            this.l=l;
        }
        public boolean isRectangle() {
            if(w> 0 && l> 0) {
                return true;
            }
            else
                return false;
        }
        @Override
        public double getArea() {
            return w*l;
        }
    }
    class Triangle extends Shape {
        double a = 0;
        double b = 0;
        double c = 0;
        public Triangle(){
            
        }
        public Triangle(double a, double b, double c) {
            super();
            this.a = a;
            this.b = b;
            this.c = c;
        }
        
        public boolean isTriangle() {
            /*double[] sides = new double[3];
            sides[0] = a;
            sides[1] = b;
            sides[2] = c;
            Arrays.sort(sides);
            return sides[0] + sides[1] > sides[2];*/
            if(a+b>c&&a+c>b&&b+c>a&&a>0&&b>0&&c>0) {
                return true;
            }
            else
                return false;
        }
        @Override
        public double getArea() {
            double p=(a+b+c)/2;
            double m;
            m=Math.sqrt((p - a) * (p - b) *(p - c)*p);
            return m;
        }
    }

    相应类图:

  •  

  • 7-5 图形继承与多态 (50 )

    输入格式:

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

    输出格式:

    如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format

    如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):

    各个图形的面积;

    所有图形的面积总和;

    排序后的各个图形面积;

    再次所有图形的面积总和。

    源码:

    import java.util.*;
    import static java.lang.System.exit;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Main {
          public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              int numC=input.nextInt();
              int numR=input.nextInt();
              int numT=input.nextInt();
              int i,f,k=0;
              boolean t1=true,t2=true,t3=true;
              double sum=0;
              if (numC<0||numT<0||numR< 0) {
                    System.out.println("Wrong Format");
                    exit(0);
                }
              double[] area=new double [numC+numR+numT];
              Circle[] s1=new Circle[numC];
              Rectangle[] s2=new Rectangle[numR];
              Triangle[] s3=new Triangle[numT];
              for(i=0;i<numC;i++) {
                  double r=input.nextDouble();
                  s1[i]=new Circle(r);
                  area[k++]=s1[i].getArea();
                  if(r<=0){
                        t1=false;
                    }
              }
              withDraw(k);
              
              for(i=0;i<numR;i++){
                    double w=input.nextDouble();
                    double l=input.nextDouble();
                    s2[i]=new Rectangle(w,l);
                    area[k++]=s2[i].getArea();
                    if(!s2[i].isRectangle()){
                        t2=false;
                    }
              }
              withDraw(k);
              for(i=0;i<numT;i++){
                    double a1=input.nextDouble();
                    double a2=input.nextDouble();
                    double a3=input.nextDouble();
                    s3[i]=new Triangle(a1,a2,a3);
                    area[k++]=s3[i].getArea();
                    if(!s3[i].isTriangle()){
                        t3=false;
                    }
              }
              withDraw(1);
              if(t2==false||t3==false||t1==false){
                    System.out.println("Wrong Format");
                    exit(0);
              }
              System.out.println("Original area:");
              for(i=0;i<numC+numR+numT;i++){
                  System.out.printf("%.2f ",area[i]);
                  sum+=area[i];
              }
              withDraw(i);
              System.out.printf("\n");
              System.out.printf("Sum of area:");
              System.out.printf("%.2f\n",sum);
              System.out.println("Sorted area:");
              Arrays.sort(area);
              for(i=0;i<numC+numR+numT;i++){
                    System.out.printf("%.2f ",area[i]);
              }
              System.out.printf("\n");
              System.out.printf("Sum of area:%.2f",sum);
          }
          public static int withDraw(int i) {
                int k=i+1;
                return k;
            }
    }
    abstract class Shape {
        public Shape(){
            
        }
        public abstract double getArea();
    }
    class Circle extends Shape {
        double r;
        public Circle() {
            
        }
        public Circle(double r) {
            super();
            this.r = r;
        }
        @Override
        public double getArea() {
            return r*r*Math.PI ;
        }
    }
    class Rectangle extends Shape {
        double l;
        double w;
        public Rectangle() {
            
        }
        public Rectangle(double w, double l) {
            super();
            this.w=w;
            this.l=l;
        }
        public boolean isRectangle() {
            if(w> 0 && l> 0) {
                return true;
            }
            else
                return false;
        }
        @Override
        public double getArea() {
            return w*l;
        }
    }
    class Triangle extends Shape {
        double a = 0;
        double b = 0;
        double c = 0;
        public Triangle(){
            
        }
        public Triangle(double a, double b, double c) {
            super();
            this.a = a;
            this.b = b;
            this.c = c;
        }
        
        public boolean isTriangle() {
            /*double[] sides = new double[3];
            sides[0] = a;
            sides[1] = b;
            sides[2] = c;
            Arrays.sort(sides);
            return sides[0] + sides[1] > sides[2];*/
            if(a+b>c&&a+c>b&&b+c>a&&a>0&&b>0&&c>0) {
                return true;
            }
            else
                return false;
        }
        @Override
        public double getArea() {
            double p=(a+b+c)/2;
            double m;
            m=Math.sqrt((p - a) * (p - b) *(p - c)*p);
            return m;
        }
    }

     

    相应类图:

  •  

    • 7-6 实现图形接口及多态性 (30 )

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

       

      其中:

      GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;

      CircleRectangle分别为圆类及矩形类,分别实现GetArea接口

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

      输入格式:

      从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。

      输出格式:

      如果输入的圆的半径值及矩形的宽、长的值非法(0),则输出Wrong Format

      如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。

      源码:

      import java.util.*;
      import static java.lang.System.exit;
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;
      public class Main {
            public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                
                int i,f,k=0;
                
                Circle s1=new Circle();
                Rectangle s2=new Rectangle();
                    double r=input.nextDouble();
                    s1=new Circle(r);
                    if(r<=0){
                        System.out.println("Wrong Format");
                          exit(0);
                      }
                
                withDraw(k);
                
                      double w=input.nextDouble();
                      double l=input.nextDouble();
                      s2=new Rectangle(w,l);
                      if(!s2.isRectangle()){
                          System.out.println("Wrong Format");
                          exit(0);
                      
                }
                withDraw(k);
                withDraw(1);
                
                System.out.printf("%.2f\n",s1.getArea());
                System.out.printf("%.2f\n",s2.getArea());
                
                withDraw(3);
                
            }
            public static int withDraw(int i) {
                  int k=i+1;
                  return k;
              }
      }
      abstract class Shape {
          public Shape(){
              
          }
          public abstract double getArea();
      }
      class Circle extends Shape {
          double r;
          public Circle() {
              
          }
          public Circle(double r) {
              super();
              this.r = r;
          }
          @Override
          public double getArea() {
              return r*r*Math.PI ;
          }
      }
      class Rectangle extends Shape {
          double l;
          double w;
          public Rectangle() {
              
          }
          public Rectangle(double w, double l) {
              super();
              this.w=w;
              this.l=l;
          }
          public boolean isRectangle() {
              if(w> 0 && l> 0) {
                  return true;
              }
              else
                  return false;
          }
          @Override
          public double getArea() {
              return w*l;
          }
      }
      class Triangle extends Shape {
          double a = 0;
          double b = 0;
          double c = 0;
          public Triangle(){
              
          }
          public Triangle(double a, double b, double c) {
              super();
              this.a = a;
              this.b = b;
              this.c = c;
          }
          
          public boolean isTriangle() {
              /*double[] sides = new double[3];
              sides[0] = a;
              sides[1] = b;
              sides[2] = c;
              Arrays.sort(sides);
              return sides[0] + sides[1] > sides[2];*/
              if(a+b>c&&a+c>b&&b+c>a&&a>0&&b>0&&c>0) {
                  return true;
              }
              else
                  return false;
          }
          @Override
          public double getArea() {
              double p=(a+b+c)/2;
              double m;
              m=Math.sqrt((p - a) * (p - b) *(p - c)*p);
              return m;
          }
      }
      class A{
          void fn()
          {
              System.out.println("call A.fn");
          }
          void pn()
          {
              fn();
          }
      }
      class B extends A{
      
          void fn()
          {
              System.out.println("call B.fn");
          }    
      }

      相应类图:

      对于三次题目集中用到的正则表达式总结:

    • 自己对于正则表达式的掌握和学习还停留在初级阶段,要多去网上学习和与别人交流。

       要熟练运用replaceAll、split、Pattern.matches等静态方法的运用,对于正则表达式还需要多加练习,需要对各个表示的方式烂熟于心。

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

      本题需要对一大串代码进行分割和识别统计,首先需要把所有关键字放在字符串数组里以方便进行比对,然后使用replaceAll替换掉注释""中的字符,再使用split根据正则表达式切割字符串,并把切割好的字符串储存在数组里,最后依次使用equals比对关键词并进行统计。

      需要细心考虑所有识别的情况,不能有遗漏,需要十分熟练地掌握和使用正则表达式

       

      (3)踩坑心得

       对于继承类的子类构造时忘记使用super()语句进行父类构造,导致报错;

       在进行循环内月份天数累加时忘记对当前年份进行判断是否为闰年,然后改变2月的最大天数,造成计算结构总是对不上,而且调试很多次也找不到问题所在。

      7-5题里,忘记考虑对跨年到前一年的情况,导致测试点结果错误。

      在创建对象数组时,对其中每个对象进行传参构造时都应该重新new,否则编译器会出现空对象的报错。

      静态方法只能调用静态方法,而且不依赖于变量,所以在设计方法时要考虑清楚修饰词,不要轻易加static,依情况而定。

      4)改进建议

      题目集5(7-4)中可以不用一行一行输入再判断,这样判断不了跨行注释/*   */的情况,使得一个测试点没过,可以整体输入再进行切割判断。

    • 可以不用频繁使用if-else,换用switch会更简单高效且易读。应该熟悉运用新的结构和语法,去夯实自己对新知识的学习,在运用中熟练和加强。对于一些繁琐的输出,可以直接放在方法里进行调用。继续深入学习正则表达式

      5)总结

    • 老师的教学和实践相结合的教学方法能督促学生进行自主学习和夯实基础,每个阶段都有相应的练习发布并且查重,使学生得到自主锻炼。对于java一些基本的框架有一些基本了解,对于正则表达式有了更深入的理解,学到了继承类的使用、抽象类的使用,还有数组使用一些静态方法等进行方便的操作,对于程序设计的一些思想有更深入的理解,计算机的计算方法跟普通人类的思考方式有所不同,我们应多加以练习实践,才能更好地掌握编程语言!

posted @ 2021-05-02 12:42  曙光c  阅读(86)  评论(0)    收藏  举报