第2次Blog作业

前言

1.知识点:考察java的聚合,正则表达式关于字符串这一方向,java的继承与多态,接口,数组的应用,重点在于继承与多态。

2.题量:题目集43道题,题目集45道题,题目集65道题。与前几次习题集相比较,题目量有所下降。

3.难度:在难度上面每次题目集的难题尽量控制到了每次题目集1-2题,例如:题目集47-1,题目集57-4,题目集67-5,同时有一些相对简单的题目,用于帮助我们增长信心。

设计与分析

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

7-2类图

 

 

 

7-4类图

 

 

 

 

两题都是考察聚合这一知识点,只是在设计分析上有所不同。

  1. 从两道题目的类图上看的出题目集4的7-2中year类(存储年份相关信息及方法)聚合于Month类(存储月份相关信息及方法)Month类聚合于Day类,Day类(存储天数相关信息及方法)聚合于DateUtil类相比较于题目集5的7-4中的year类,Month类,day类都聚合于一个DateUtil类,类图没有后者简洁,易懂,设计代码的过程相比较会相对繁琐。
  2. 题目集5的7-4中的几个类都聚合于同一个类,在设计代码时比前者更快更简洁,省去了类year,类month,类day之间的频繁调用。

 

 

 

 

 

下面附上两题的题目和代码

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 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4         Scanner imput = new Scanner(System.in);
  5         int a = imput.nextInt();
  6         if(a!=1&&a!=2&&a!=3&&a!=4) {
  7             System.out.println("Wrong Format");
  8         }
  9         else {
 10             switch(a)
 11             {
 12             case 1:float radius = imput.nextFloat();
 13                     if(radius<=0){
 14                         System.out.println("Wrong Format");
 15                         return;
 16                     }
 17                    Circle one = new Circle();
 18                    one.setRadius(radius);
 19                    System.out.printf("Circle's area:%.2f\n",one.getArea());
 20                    break;
 21             case 2:float width = imput.nextFloat();
 22                    float length = imput.nextFloat();
 23                     if(width<=0||length<=0){
 24                         System.out.println("Wrong Format");
 25                         return;
 26                     }
 27                    Rectangle two = new Rectangle();
 28                    two.setLength(length);
 29                    two.setWidth(width);
 30                    System.out.printf("Rectangle's area:%.2f\n",two.getArea());
 31                    break;
 32             case 3:float radius1 = imput.nextFloat();
 33                     if(radius1<=0){
 34                         System.out.println("Wrong Format");
 35                         return;
 36                     }
 37                    Ball three = new Ball();
 38                    three.setRadius(radius1);
 39                    System.out.printf("Ball's surface area:%.2f\n",three.getArea());
 40                    System.out.printf("Ball's volume:%.2f\n",three.getVolume());
 41                    break;
 42             case 4:float width1 = imput.nextFloat();
 43                    float length1 = imput.nextFloat();
 44                    float height = imput.nextFloat();
 45                     if(width1<=0||length1<=0||height<=0){
 46                         System.out.println("Wrong Format");
 47                         return;
 48                     }
 49                    Box forth  = new Box();
 50                    forth.setHeight(height);
 51                    forth.setLength(length1);
 52                    forth.setWidth(width1);
 53                    System.out.printf("Box's surface area:%.2f\n",forth.getArea());
 54                    System.out.printf("Box's volume:%.2f\n",forth.getVolume());
 55                    break;
 56             }
 57         }
 58     }
 59 }
 60 class Shape {
 61     public double getArea(){
 62        return 0.0;
 63 }
 64     public Shape() {
 65         System.out.println("Constructing Shape");
 66     }
 67 }
 68 
 69 
 70 
 71 class Circle extends Shape{
 72     public float getRadius() {
 73         return radius;
 74     }
 75     public void setRadius(float radius) {
 76         this.radius = radius;
 77     }
 78     private float radius;
 79     public Circle() {
 80         System.out.println("Constructing Circle");
 81     }
 82     public double getArea()
 83     {
 84         return Math.PI*radius*radius;
 85     }
 86     
 87 
 88 }
 89 class Rectangle extends Shape{
 90     private float width;
 91     private float length;
 92     public Rectangle() {
 93         super();
 94         System.out.println("Constructing Rectangle");
 95     }
 96     public double getArea() {
 97         return width*length;
 98     }
 99     public float getWidth() {
100         return width;
101     }
102     public void setWidth(float width) {
103         this.width = width;
104     }
105     public float getLength() {
106         return length;
107     }
108     public void setLength(float length) {
109         this.length = length;
110     }
111     public float getHeight() {
112         // TODO Auto-generated method stub
113         return 0;
114     }
115 }
116 class Ball extends Circle{
117     public Ball() {
118       System.out.println("Constructing Ball");
119 }
120     public double getArea() {
121         double a = super.getArea();
122         return 4*a;
123     }
124     public double getVolume() {
125         double l=super.getRadius();
126         return Math.PI*l*l*l*4/3.0;
127     }
128 }
129 class Box extends Rectangle{
130     private float height;
131     public Box() {
132         System.out.println("Constructing Box");
133     }
134 
135     public float getHeight() {
136         return height;
137     }
138 
139     public void setHeight(float height) {
140         this.height = height;
141     }
142     public double getArea(){
143         return 2*(super.getArea()+super.getHeight()*super.getWidth()+super.getHeight()*super.getLength());
144     }
145     public double getVolume() {
146         return super.getArea()*height;
147     }
148 
149 }

 

 

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

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

 

 

 

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

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

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

 代码如下

  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4         Scanner imput = new Scanner(System.in);
  5         int a = imput.nextInt();
  6         if(a!=1&&a!=2&&a!=3) {
  7             System.out.println("Wrong Format");
  8             System.exit(0);
  9         }
 10         else {
 11         switch(a) {
 12         case 1:int year = imput.nextInt();
 13                int month = imput.nextInt();
 14                int day = imput.nextInt();
 15                int b = imput.nextInt();
 16                DateUtil one = new DateUtil(year, month, day);
 17                if (!one.checkInputValidity()) {
 18                     System.out.println("Wrong Format");
 19                     System.exit(0);
 20                 }
 21                if(b<0) {
 22                    System.out.println("Wrong Format");
 23                     System.exit(0);
 24                }
 25                System.out.println(one.getYear() + "-" + one.getMonth() + "-" + one.getDay() + " next " + b + " days is:"+one.getNextNDays(b).showDate());
 26                break;
 27         case 2:int year1 = imput.nextInt();
 28                int month1 = imput.nextInt();
 29                int day1 = imput.nextInt();
 30                int b1 = imput.nextInt();
 31                DateUtil two = new DateUtil(year1, month1, day1);
 32                if (!two.checkInputValidity()) {
 33                   System.out.println("Wrong Format");
 34                   System.exit(0);
 35                }
 36                if(b1<0) {
 37                   System.out.println("Wrong Format");
 38                   System.exit(0);
 39                }
 40               System.out.println(two.getYear() + "-" + two.getMonth() + "-" + two.getDay() + " previous " + b1 + " days is:"+two.previousNDays(b1).showDate());
 41               break;
 42         case 3:int year2 = imput.nextInt();
 43                int month2 = imput.nextInt();
 44                int day2 = imput.nextInt();
 45                DateUtil three = new DateUtil(year2, month2, day2);
 46                int year3 = imput.nextInt();
 47                int month3 = imput.nextInt();
 48                int day3 = imput.nextInt();
 49                DateUtil forth = new DateUtil(year3, month3, day3);
 50                if(!three.checkInputValidity()&&!forth.checkInputValidity()) {
 51                    System.out.println("Wrong Format");
 52                    System.exit(0);
 53                }
 54                else {
 55                    System.out.println("The days between " + three.showDate() +" and " + forth.showDate() + " are:"+ three.getDaysofDates(forth));
 56                }
 57                break;
 58         }
 59         
 60         
 61     }
 62 
 63     }
 64 }
 65 class DateUtil {
 66       private int year;
 67       private int month;
 68       private int day;
 69       private int [] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
 70       DateUtil(){
 71           
 72       }
 73       public int getYear() {
 74         return year;
 75     }
 76     public void setYear(int year) {
 77         this.year = year;
 78     }
 79     public int getMonth() {
 80         return month;
 81     }
 82     public void setMonth(int month) {
 83         this.month = month;
 84     }
 85     public int getDay() {
 86         return day;
 87     }
 88     public void setDay(int day) {
 89         this.day = day;
 90     }
 91     DateUtil(int y,int m,int d){
 92         this.year = y;
 93         this.month = m;
 94         this.day = d;
 95       }
 96     public int setDayMax(int year, int month) {
 97         if(month==2&&isLeapYear(year)) {
 98             return 29;
 99         }
100         return mon_maxnum[month-1];
101         
102     }
103     public boolean checkInputValidity() {
104         if(year<1820||year>2020||month<1||month>12||day<0||day>31) {
105             return false;
106         }
107         else {
108             return true;
109         }
110         
111         
112     }
113     public DateUtil getNextNDays(int n) {
114         for (int i = 0; i < n; i++) {
115             day++;
116             if (day > setDayMax(year, month)) {
117                 day = 1;
118                 month++;
119                 if (month > 12) {
120                     month = 1;
121                     year++;
122                 }
123             }
124         }
125         return new DateUtil(year, month, day);
126     }
127         
128     public DateUtil previousNDays(int n) {
129         for (int i = 0; i < n; i++) {
130             day--;
131             while (day < 1) {
132                 month--;
133                 if (month < 1) {
134                     month = 12;
135                     year--;
136                 }
137                 day = setDayMax(year, month);
138             }
139         }
140         return new DateUtil(year, month, day);
141     }
142     public boolean compareDates(DateUtil date) {
143         if (year > date.year) {
144             return true;
145         }
146         if (year == date.year) {
147             if (month > date.month) {
148                 return true;
149             }
150             if (month == date.month) {
151                 if (day >= date.day) {
152                     return true;
153             }
154         }
155         }
156         return false;
157     
158         
159     }
160     public boolean equalTwoDates(DateUtil date) {
161         if (year == date.year && month == date.month && day == date.day) {
162             return true;
163         }
164         else {
165             return false;
166         }
167         
168     }
169     int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
170     public int getDaysofDates(DateUtil date) {
171         DateUtil dateUtil1 = this;
172         DateUtil dateUtil2 = date;
173         if (this.compareDates(date)) {
174             dateUtil1 = date;
175             dateUtil2 = this;
176         }
177 
178         int days;
179         int leapYearNum = 0;
180         for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
181             if (isLeapYear(i)) {
182                 leapYearNum++;
183             }
184         }
185         int d1;
186         int d2;
187         days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;
188         d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
189         d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
190         return days - d1 + d2;
191 
192         
193         
194     }
195     public String showDate() {
196          return year + "-" + month + "-" + day;
197     }
198     public boolean isLeapYear(int year)
199     {
200         return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
201     }
202 
203 }

 

 

 

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

题目集4(7-3)

  • 这道题主要还是考察的类的继承,父类和子类之间的关系,设计几个图形来计算相应的面积,Circle,继承自Shape,Rectangle,继承自Shape,Ball,继承自Circle,并且重写了当中的求面积和体积的方法
  • 类图如下

 

 

 

圈复杂度为14,相对较低

 

 

 

 

    

题目集6(7-5) 同样也考察了继承,但增加了使用多态的条件。getArea()方法为抽象方法,功能为求得图形的面积;validate()方法也为抽象方法,对图形的属 性进行合法性校验;toString()继承自 Object。

类图如下

 

 

 

圈复杂度为14,相比较与习题集4中的7-1又有点下降

 

 

 

 

 

题目集67-6)同样也是考察继承,但添加了使用接口及类实现多态性。

 

类图如下

 

 

 

 

圈复杂度为4,算是很低的圈复杂度了

 

 

 

下面附上3道题的题目和代码

习题集4(7-3)

7-3 图形继承 (15 分)

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

  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

输入格式:

共四种合法输入

  • 1 圆半径
  • 2 矩形宽、长
  • 3 球半径
  • 4 立方体宽、长、高

输出格式:

按照以上需求提示依次输出

输入样例1:

在这里给出一组输入。例如:

1 1.0

输出样例1:

在这里给出相应的输出。例如:

Constructing Shape

Constructing Circle

Circle's area:3.14

输入样例2:

在这里给出一组输入。例如:

4 3.6 2.1 0.01211

输出样例2:

在这里给出相应的输出。例如:

Constructing Shape

Constructing Rectangle

Constructing Box

Box's surface area:15.26

Box's volume:0.09

输入样例3:

在这里给出一组输入。例如:

2 -2.3 5.110

输出样例2:

在这里给出相应的输出。例如:

Wrong Format

代码如下

 

 

  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4         Scanner imput = new Scanner(System.in);
  5         int a = imput.nextInt();
  6         if(a!=1&&a!=2&&a!=3&&a!=4) {
  7             System.out.println("Wrong Format");
  8         }
  9         else {
 10             switch(a)
 11             {
 12             case 1:float radius = imput.nextFloat();
 13                     if(radius<=0){
 14                         System.out.println("Wrong Format");
 15                         return;
 16                     }
 17                    Circle one = new Circle();
 18                    one.setRadius(radius);
 19                    System.out.printf("Circle's area:%.2f\n",one.getArea());
 20                    break;
 21             case 2:float width = imput.nextFloat();
 22                    float length = imput.nextFloat();
 23                     if(width<=0||length<=0){
 24                         System.out.println("Wrong Format");
 25                         return;
 26                     }
 27                    Rectangle two = new Rectangle();
 28                    two.setLength(length);
 29                    two.setWidth(width);
 30                    System.out.printf("Rectangle's area:%.2f\n",two.getArea());
 31                    break;
 32             case 3:float radius1 = imput.nextFloat();
 33                     if(radius1<=0){
 34                         System.out.println("Wrong Format");
 35                         return;
 36                     }
 37                    Ball three = new Ball();
 38                    three.setRadius(radius1);
 39                    System.out.printf("Ball's surface area:%.2f\n",three.getArea());
 40                    System.out.printf("Ball's volume:%.2f\n",three.getVolume());
 41                    break;
 42             case 4:float width1 = imput.nextFloat();
 43                    float length1 = imput.nextFloat();
 44                    float height = imput.nextFloat();
 45                     if(width1<=0||length1<=0||height<=0){
 46                         System.out.println("Wrong Format");
 47                         return;
 48                     }
 49                    Box forth  = new Box();
 50                    forth.setHeight(height);
 51                    forth.setLength(length1);
 52                    forth.setWidth(width1);
 53                    System.out.printf("Box's surface area:%.2f\n",forth.getArea());
 54                    System.out.printf("Box's volume:%.2f\n",forth.getVolume());
 55                    break;
 56             }
 57         }
 58     }
 59 }
 60 class Shape {
 61     public double getArea(){
 62        return 0.0;
 63 }
 64     public Shape() {
 65         System.out.println("Constructing Shape");
 66     }
 67 }
 68 
 69 
 70 
 71 class Circle extends Shape{
 72     public float getRadius() {
 73         return radius;
 74     }
 75     public void setRadius(float radius) {
 76         this.radius = radius;
 77     }
 78     private float radius;
 79     public Circle() {
 80         System.out.println("Constructing Circle");
 81     }
 82     public double getArea()
 83     {
 84         return Math.PI*radius*radius;
 85     }
 86     
 87 
 88 }
 89 class Rectangle extends Shape{
 90     private float width;
 91     private float length;
 92     public Rectangle() {
 93         super();
 94         System.out.println("Constructing Rectangle");
 95     }
 96     public double getArea() {
 97         return width*length;
 98     }
 99     public float getWidth() {
100         return width;
101     }
102     public void setWidth(float width) {
103         this.width = width;
104     }
105     public float getLength() {
106         return length;
107     }
108     public void setLength(float length) {
109         this.length = length;
110     }
111     public float getHeight() {
112         // TODO Auto-generated method stub
113         return 0;
114     }
115 }
116 class Ball extends Circle{
117     public Ball() {
118       System.out.println("Constructing Ball");
119 }
120     public double getArea() {
121         double a = super.getArea();
122         return 4*a;
123     }
124     public double getVolume() {
125         double l=super.getRadius();
126         return Math.PI*l*l*l*4/3.0;
127     }
128 }
129 class Box extends Rectangle{
130     private float height;
131     public Box() {
132         System.out.println("Constructing Box");
133     }
134 
135     public float getHeight() {
136         return height;
137     }
138 
139     public void setHeight(float height) {
140         this.height = height;
141     }
142     public double getArea(){
143         return 2*(super.getArea()+super.getHeight()*super.getWidth()+super.getHeight()*super.getLength());
144     }
145     public double getVolume() {
146         return super.getArea()*height;
147     }
148 
149 }

 

 

 

题目集6(7-5)

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

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

2021-OO第06次作业-5指导书V1.0.pdf

输入格式:

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

输出格式:

  1. 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format。
  2. 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
  • 各个图形的面积;
  • 所有图形的面积总和;
  • 排序后的各个图形面积;
  • 再次所有图形的面积总和。

输入样例1:

在这里给出一组输入。例如:

1 1 1 2.3 3.2 3.2 6.5 3.2 4.2

输出样例1:

在这里给出相应的输出。例如:

Original area:

16.62 10.24 5.68

Sum of area:32.54

Sorted area:

5.68 10.24 16.62

Sum of area:32.54

输入样例2:

在这里给出一组输入。例如:

0 2 2 2.3 2.5 56.4 86.5 64.3 85.6 74.6544 3.2 6.1 4.5

输出样例2:

在这里给出相应的输出。例如:

Original area:

5.75 4878.60 2325.19 7.00

Sum of area:7216.54

Sorted area:

5.75 7.00 2325.19 4878.60

Sum of area:7216.54

输入样例3:

在这里给出一组输入。例如:

0 0 1 3 3 6

输出样例3:

在这里给出相应的输出。例如:

Wrong Format

代码如下

 

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
     Scanner imput = new Scanner(System.in);
     int a = imput.nextInt();
     int b = imput.nextInt();
     int c = imput.nextInt();
     if(a<0||b<0||c<0) {
         System.out.println("Wrong Format");
         return;
     }
     Circle []d=new Circle[a];
     Rectangle []e=new Rectangle[b];
     Triangle []f=new Triangle[c];
     double sum1 = 0;
     double sum2 = 0;
     double sum3 = 0;
    double[] area=new double [a+b+c];
     for(int i = 0;i<a;i++) {
         double radius = imput.nextDouble();
         if(radius<=0){
             System.out.println("Wrong Format");
             return;
         }
         d[i] = new Circle(radius);
         sum1 = sum1 +d[i].getArea();
         area[i]= d[i].getArea();
     }
     for(int i = 0;i<b;i++) {
         double w =imput.nextDouble();
         double h = imput.nextDouble();
         if(w<=0||h<=0){
             System.out.println("Wrong Format");
             return;
         }
         e[i] = new Rectangle(w,h);
         sum2 = sum2 + e[i].getArea();
         area[a+i]= d[i].getArea();
     }
     for(int i = 0;i<c;i++) {
         double b1 =imput.nextDouble();
         double b2 = imput.nextDouble();
         double b3 = imput.nextDouble();
         if(b1<=0||b2<=0||b3<=0){
             System.out.println("Wrong Format");
             return;
         }
         if(!(b1-b2<b3&&b1-b3<b2&&b2-b3<b1&&b2-b1<b3&&b3-b1<b2&&b3-b2<b1)) {
             System.out.println("Wrong Format");
             return;
         }
         f[i] = new Triangle(b1,b2,b3);
         sum3 = sum3 + f[i].getArea();
         area[a+b+i]= d[i].getArea();
     }
     System.out.println("Original area:");
     for(int i = 0;i<a;i++) {
         System.out.printf("%.2f ", d[i].getArea());
     }
     for(int i = 0;i<b;i++) {
         System.out.printf("%.2f ", e[i].getArea());
     }
     for(int i = 0;i<c;i++) {
         System.out.printf("%.2f ", f[i].getArea());
     }
     System.out.println();
     System.out.printf("Sum of area:");
     System.out.printf("%.2f\n",sum1+sum2+sum3);
     System.out.println("Sorted area:");
     Arrays.sort(area);
     for(int i=0;i<a+b+c;i++){
         System.out.printf("%.2f ",area[i]);
     }

     System.out.println();
     System.out.printf("Sum of area:");
     System.out.printf("%.2f\n",sum1+sum2+sum3);
}
}

class Circle  extends Shape{
     private double radius;
     public Circle(double radius) {
         this.radius = radius;
     }

    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 w;
    private double h;
    public Rectangle(double w,double h) {
        this.w = w;
        this.h = h;
    }
    public double getW() {
        return w;
    }
    public void setW(double w) {
        this.w = w;
    }
    public double getH() {
        return h;
    }
    public void setH(double h) {
        this.h = h;
    }
    public double getArea() {
        return w*h;
    }
}

 class Triangle extends Shape{
   private double b1;
   private double b2;
   private double b3;
   public Triangle(double b1,double b2,double b3) {
       this.b1 = b1;
       this.b2 = b2;
       this.b3 = b3;  
   }
public double getB1() {
    return b1;
}
public void setB1(double b1) {
    this.b1 = b1;
}
public double getB2() {
    return b2;
}
public void setB2(double b2) {
    this.b2 = b2;
}
public double getB3() {
    return b3;
}
public void setB3(double b3) {
    this.b3 = b3;
}
 public double getArea() {
     double p = (b1 + b2 + b3) / 2;
     return Math.sqrt(p * (p - b1) * (p - b2) * (p - b3));
 }
}

abstract class Shape {
    public abstract double getArea();
}

 

 

题目集6(7-6)

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

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

 

其中:

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

输入格式:

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

输出格式:

  • 如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出Wrong Format
  • 如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。

输入样例1:

在这里给出一组输入。例如:

2 3.6 2.45

输出样例1:

在这里给出相应的输出。例如:

12.57

8.82

输入样例2:

在这里给出一组输入。例如:

9 0.5 -7.03

输出样例2:

在这里给出相应的输出。例如:

Wrong Format

代码如下

 

 1 import java.util.Scanner;
 2 public class Main {
 3 
 4 public static void main(String[] args) {
 5        Scanner imput = new Scanner(System.in);
 6        float radius = imput.nextFloat();
 7        float width = imput.nextFloat();
 8        float length = imput.nextFloat();
 9        if(radius<=0||width<=0||length<=0){
10            System.out.println("Wrong Format");
11        }
12        else {
13            GetArea getarea = new Circel(radius);
14            print(getarea);
15            GetArea getarea1 = new Rectangle(width,length);
16            print(getarea1);
17        }
18    }
19 
20 private static void print(GetArea getarea) {
21     System.out.println(String.format("%.2f", getarea.getArea()));
22     
23 }
24 }
25     class Circel implements GetArea {
26        private double radius;
27 
28     public double getRadius() {
29         return radius;
30     }
31 
32     public void setRadius(double radius) {
33         this.radius = radius;
34     }
35     public Circel() {
36         
37     }
38     public Circel(double radius) {
39         this.radius = radius;
40     }
41 
42     @Override
43     public double getArea() {
44         // TODO Auto-generated method stub
45         return Math.PI*radius*radius;
46         
47     }
48     
49 }
50 class Rectangle implements GetArea{
51    private double width;
52    private double length;
53 public double getWidth() {
54     return width;
55 }
56 public void setWidth(double width) {
57     this.width = width;
58 }
59 public double getLength() {
60     return length;
61 }
62 public void setLength(double length) {
63     this.length = length;
64 }
65  public Rectangle() {
66      
67  }
68  public Rectangle(double width,double length) {
69      this.width = width;
70      this.length = length;
71  }
72 @Override
73 public double getArea() {
74     // TODO Auto-generated method stub
75     return width*length;
76     
77 }
78 }
79 interface GetArea {
80        double getArea();
81 }

 

 

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

正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等,可以大大的降低圈复杂度。

在习题集6当中,前4道题都是考察字符串,其中有3道题目我利用到了正则表达式

7-1的验证QQ号

 

7-3的验证码验证

 

7-4的学号验证

 

java中正则表达式的一些常用符号和知识

{n}

是非负整数。正好匹配 n 次。例如,"o{2}"与"Bob"中的"o"不匹配,但与"food"中的两个"o"匹配。

{n,}

是非负整数。至少匹配 次。例如,"o{2,}"不匹配"Bob"中的"o",而匹配"foooood"中的所有 o。"o{1,}"等效于"o+"。"o{0,}"等效于"o*"。

{n,m}

m 和 n 是非负整数,其中 n <= m。匹配至少 n 次,至多 m 次。例如,"o{1,3}"匹配"fooooood"中的头三个 o。'o{0,1}' 等效于 'o?'。注意:您不能将空格插入逗号和数字之间。

x|y

匹配 x 或 y。例如,'z|food' 匹配"z"或"food"。'(z|f)ood' 匹配"zood"或"food"。

 

[xyz]

字符集。匹配包含的任一字符。例如,"[abc]"匹配"plain"中的"a"。

 

[^xyz]

反向字符集。匹配未包含的任何字符。例如,"[^abc]"匹配"plain"中"p","l","i","n"。

 

[a-z]

字符范围。匹配指定范围内的任何字符。例如,"[a-z]"匹配"a"到"z"范围内的任何小写字母。

 

[^a-z]

反向范围字符。匹配不在指定的范围内的任何字符。例如,"[^a-z]"匹配任何不在"a"到"z"范围内的任何字符。

 

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

该题要求题目必须使用List、Set或Map中一种或多种List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口

Set下有HashSet,LinkedHashSet,TreeSet

List下有ArrayList,Vector,LinkedList

Map下有Hashtable,LinkedHashMap,HashMap,TreeMap

Collection接口下还有个Queue接口,有PriorityQueue类

List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口

Set下有HashSet,LinkedHashSet,TreeSet

List下有ArrayList,Vector,LinkedList

Map下有Hashtable,LinkedHashMap,HashMap,TreeMap

Collection接口下还有个Queue接口,有PriorityQueue类

(3)采坑心得:3)采坑心得:

题目集4:在7-3中对于getArea()方法的重写没有使用super关键字,导致一直无法正确运行。

 

 

后面加上super,使用super一般用于使用父类的方法或属性。

 

题目集5:在7-5这道日期题上在求天数的时候太过于繁琐,后在网络上借鉴了一种方法

利用AB:C,A成立则B,否则C。省去了大量的判断语句。

题目集67-4中验证学号判断是否为1-40开始是

 

这样看似可用但实际上是1-49的范围,要想1-40就要添加或的条件如下

 

(4)改进建议

  1. 命名规范,我还是喜欢将各种值写成A,B,C,这对其他人读我的代码会有些困难,需要改变。
  2. 降低圈复杂度,尽量减少if-else语句的大量套用,多使用switch语句。
  3. 多添加类,使代码修改更加方便。

5、总结

     与之前相比我在该课程花费的时间已经大大增加,但似乎还不够,我会继续增加时间。

     在正则表达式这方面我只会一点最基础的,并且需要查资料,所掌握不多。

     增加的这些小题目是非常有必要的,往往最简单的可能是最致命的。

     大量的继承题目,在继承这方面掌握的还算好。

posted @ 2021-05-02 23:15  把你印在脑子里  阅读(121)  评论(0)    收藏  举报