题目集4-6总结性blog
1、 前言
这三次作业的知识点还是蛮多的,题目集四主要知识点有:校验数据,正则表达式的运用,Java聚合,Java继承;题目集五:字符串匹配,合并数组,数据排序,进一步聚合;题目集六的新知识比较少,主要有:正则表达式的运用,字符排序,Java继承和多态;题目量是依次递增,但难道逐渐减小,经典先苦后甜;最难的就是水文数据校验,因为对正则表达式的运用不够熟练,记得当时做了好久,最后也没有得满分;下面分析一些比较有价值题目;
2、 设计与分析
(1) 先看题目集四的7-2和题目集五的7-5日期类聚和设计的优劣比较。
题目详情:
参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] 。
1、求下n天
2、求前n天
3、求两个日期相差的天数
输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):
1 year month day n //测试输入日期的下n天
2 year month day n //测试输入日期的前n天
3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
当输入有误时,输出格式如下: Wrong Format
当第一个数字为1且输入均有效,输出格式如下:
year-month-day
当第一个数字为2且输入均有效,输出格式如下:
year-month-day
当第一个数字为3且输入均有效,输出格式如下:
天数值
题目集四的7-2类图:

题目集四的7-2复杂度:

题目集五的7-5类图:

题目集五的7-5复杂度:

由两个类图可以看出第二题比第一题要简洁,因为它是由DateUtil类派生出year、month、day类,而第一题是分步继承的,这就会导致这个程序的耦合度较高,这种程序的质量就没有第二种程序好,第二种不仅复杂度低,而且它易于修改相对第一种来说。总体来说,第一种代码容易写出来,但是质量不太好,也不易于修改,第二种比较难写,但是质量好且易于修改。
(2) 题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)。
题目集4(7-3)详情:
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
注意:
每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
输出的数值均保留两位小数
主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format
输入格式:
共四种合法输入
1 圆半径
2 矩形宽、长
3 球半径
4 立方体宽、长、高
输出格式:
按照以上需求提示依次输出
题目集4(7-3)类图:

题目集4(7-3)代码:
1 import java.util.Scanner; 2 import java.text.DecimalFormat; 3 public class Main { 4 Scanner input=new Scanner(System.in); 5 DecimalFormat df2 = new DecimalFormat("####0.00"); 6 public Main() { 7 // TODO 自动生成的构造函数存根 8 } 9 public void show1(double r) { 10 11 if(r>=0) { 12 Circle yuan=new Circle(); 13 yuan.setR(r); 14 System.out.println("Constructing Shape"); 15 System.out.println("Constructing Circle"); 16 System.out.print("Circle's area:"+df2.format(yuan.getArea())); 17 } 18 else 19 System.out.print("Wrong Format"); 20 } 21 public void show2(double length,double width) { 22 if(length>=0&&width>=0){ 23 Rectangle juxing=new Rectangle(); 24 juxing.setLength(length); 25 juxing.setWidth(width); 26 System.out.println("Constructing Shape"); 27 System.out.println("Constructing Rectangle"); 28 System.out.print("Rectangle's area:"+df2.format(juxing.getArea())); 29 } 30 else 31 System.out.print("Wrong Format"); 32 } 33 public void show3(double r) { 34 Ball qiu=new Ball(); 35 if(r>=0) { 36 qiu.setR(r); 37 System.out.println("Constructing Shape"); 38 System.out.println("Constructing Circle"); 39 System.out.println("Constructing Ball"); 40 System.out.println("Ball's surface area:"+df2.format(qiu.getArea())); 41 System.out.print("Ball's volume:"+df2.format(qiu.getVolume())); 42 } 43 else 44 System.out.print("Wrong Format"); 45 } 46 public void show4(double width,double length,double height) { 47 if(height>=0&&length>=0&&width>=0) { 48 Box lft=new Box(); 49 lft.setHeight(height); 50 lft.setLength(length); 51 lft.setWidth(width); 52 System.out.println("Constructing Shape"); 53 System.out.println("Constructing Rectangle"); 54 System.out.println("Constructing Box"); 55 System.out.println("Box's surface area:"+df2.format(lft.getArea())); 56 System.out.print("Box's volume:"+df2.format(lft.getVolume())); 57 } 58 else 59 System.out.print("Wrong Format"); 60 } 61 public static void main(String[] args) { 62 // TODO 自动生成的方法存根 63 Scanner input=new Scanner(System.in); 64 int cz=input.nextInt(); 65 if(cz>=1&&cz<=4) { 66 switch(cz){ 67 case 1: double r=input.nextDouble(); 68 Main test1=new Main(); 69 test1.show1(r); 70 break; 71 case 2: double length=input.nextDouble(); 72 double width=input.nextDouble(); 73 Main test2=new Main(); 74 test2.show2(length,width); 75 break; 76 case 3: double r1=input.nextDouble(); 77 Main test3=new Main(); 78 test3.show3(r1); 79 break; 80 case 4: double width1=input.nextDouble(); 81 double length1=input.nextDouble(); 82 double height=input.nextDouble(); 83 Main test4=new Main(); 84 test4.show4(width1,length1,height); 85 break; 86 } 87 } 88 else 89 System.out.print("Wrong Format"); 90 } 91 92 } 93 class Shape{ 94 public double getArea() { 95 return 0.0; 96 } 97 } 98 class Circle extends Shape{ 99 private double r; 100 public double getArea() { 101 return Math.PI*r*r; 102 } 103 public double getR() { 104 return r; 105 } 106 public void setR(double r) { 107 this.r=r; 108 } 109 } 110 class Rectangle extends Shape{ 111 private double length; 112 private double width; 113 public double getArea() { 114 return length*width; 115 } 116 public double getLength() { 117 return length; 118 } 119 public void setLength(double length) { 120 this.length=length; 121 } 122 public double getWidth() { 123 return width; 124 } 125 public void setWidth(double width) { 126 this.width=width; 127 } 128 } 129 class Ball extends Circle{ 130 public double getArea() { 131 return 4*Math.PI*super.getR()*super.getR(); 132 } 133 public double getVolume() { 134 return (4*Math.PI*super.getR()*super.getR()*super.getR())/3.0; 135 } 136 } 137 class Box extends Rectangle{ 138 private double height; 139 public double getArea() { 140 return (super.getLength()*super.getWidth()*2+super.getLength()*height*2+super.getWidth()*height*2); 141 } 142 public double getVolume() { 143 return super.getLength()*super.getWidth()*height; 144 } 145 public double getHeight() { 146 return height; 147 } 148 public void setHeight(double height) { 149 this.height=height; 150 } 151 }
题目集6(7-5)详情:
掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。
输入格式:
从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。
输出格式:
如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format。
如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
各个图形的面积;
所有图形的面积总和;
排序后的各个图形面积;
再次所有图形的面积总和。
题目集6(7-5)类图:

题目集6(7-5)代码:
1 import java.util.Scanner; 2 import java.text.DecimalFormat; 3 public class Main { 4 public Main() { 5 // TODO 自动生成的构造函数存根 6 } 7 public static void insertionSort(double[] a) { 8 DecimalFormat df2 = new DecimalFormat("####0.00"); 9 for(int i=1;i<a.length;i++) { 10 for(int j=i-1;j>=0;j--) { 11 if(a[j+1]<a[j]) { 12 double temp=a[j+1]; 13 a[j+1]=a[j]; 14 a[j]=temp; 15 } 16 } 17 } 18 for(int i=0;i<a.length;i++) 19 System.out.print(df2.format(a[i])+" "); 20 } 21 public static void main(String[] args) { 22 Scanner input=new Scanner(System.in); 23 DecimalFormat df2 = new DecimalFormat("####0.00"); 24 int num1=input.nextInt(); 25 int num2=input.nextInt(); 26 int num3=input.nextInt(); 27 if(num1<0||num2<0||num3<0) { 28 System.out.println("Wrong Format"); 29 System.exit(0); 30 } 31 int all=num1+num2+num3; 32 Circle circle=new Circle(); 33 Rectangle rectangle=new Rectangle(); 34 Triangle triangle=new Triangle(); 35 double[] data1=new double[all]; 36 double[] data21=new double[all]; 37 double[] data22=new double[all]; 38 double[] data31=new double[all]; 39 double[] data32=new double[all]; 40 double[] data33=new double[all]; 41 double[] mj1=new double[num1]; 42 double[] mj2=new double[num2]; 43 double[] mj3=new double[num3]; 44 double s1=0; 45 double s2=0; 46 double s3=0; 47 for(int i=0;i<num1;i++) { 48 data1[i]=input.nextDouble(); 49 if(data1[i]<=0) { 50 System.out.println("Wrong Format"); 51 System.exit(0); 52 } 53 } 54 for(int i=0;i<num2;i++) { 55 data21[i]=input.nextDouble(); 56 data22[i]=input.nextDouble(); 57 if(data21[i]<0||data22[i]<0) { 58 System.out.println("Wrong Format"); 59 System.exit(0); 60 } 61 } 62 for(int i=0;i<num3;i++) { 63 data31[i]=input.nextDouble(); 64 data32[i]=input.nextDouble(); 65 data33[i]=input.nextDouble(); 66 if(data31[i]<0||data32[i]<0||data33[i]<0||data31[i]+data32[i]<=data33[i]||data31[i]+data33[i]<=data32[i]||data33[i]+data32[i]<=data31[i]) { 67 System.out.println("Wrong Format"); 68 System.exit(0); 69 } 70 } 71 System.out.println("Original area:"); 72 for(int i=0;i<num1;i++) { 73 circle.setR(data1[i]); 74 circle.show(); 75 mj1[i]=circle.getArea(); 76 s1=s1+mj1[i]; 77 } 78 for(int i=0;i<num2;i++) { 79 rectangle.setLength(data21[i]); 80 rectangle.setWidth(data22[i]); 81 rectangle.show(); 82 mj2[i]=rectangle.getArea(); 83 s2=s2+mj2[i]; 84 } 85 for(int i=0;i<num3;i++) { 86 triangle.setSide1(data31[i]); 87 triangle.setSide2(data32[i]); 88 triangle.setSide3(data33[i]); 89 triangle.show(); 90 mj3[i]=triangle.getArea(); 91 s3=s3+mj3[i]; 92 } 93 System.out.println("\n"+"Sum of area:"+df2.format(s1+s2+s3)); 94 System.out.println("Sorted area:"); 95 double[] a=new double[num1+num2+num3]; 96 for(int i=0;i<num1;i++) { 97 a[i]=mj1[i]; 98 } 99 for(int i=num1;i<num2+num1;i++) { 100 a[i]=mj2[i-num1]; 101 } 102 for(int i=num2+num1;i<num3+num2+num1;i++) { 103 a[i]=mj3[i-num2-num1]; 104 } 105 insertionSort(a); 106 System.out.println("\n"+"Sum of area:"+df2.format(s1+s2+s3)); 107 } 108 } 109 class Shape{ 110 DecimalFormat df2 = new DecimalFormat("####0.00"); 111 public double getArea() { 112 return 0.0; 113 } 114 public boolean validdate() { 115 return true; 116 } 117 public String toString() { 118 return null; 119 } 120 } 121 class Circle extends Shape{ 122 private double r; 123 public double getArea() { 124 return Math.PI*r*r; 125 } 126 public void show() { 127 System.out.print(df2.format(this.getArea())+" "); 128 } 129 public double getR() { 130 return r; 131 } 132 public void setR(double r) { 133 this.r=r; 134 } 135 } 136 class Rectangle extends Shape{ 137 private double length; 138 private double width; 139 public double getArea() { 140 return length*width; 141 } 142 public void show() { 143 System.out.print(df2.format(this.getArea())+" "); 144 } 145 public double getLength() { 146 return length; 147 } 148 public void setLength(double length) { 149 this.length=length; 150 } 151 public double getWidth() { 152 return width; 153 } 154 public void setWidth(double width) { 155 this.width=width; 156 } 157 } 158 class Triangle extends Shape{ 159 private double side1; 160 private double side2; 161 private double side3; 162 public double getArea() { 163 double mj=Math.sqrt(((side1+side2+side3)/2)*(((side1+side2+side3)/2)-side1)*(((side1+side2+side3)/2)-side2)*(((side1+side2+side3)/2)-side3)); 164 return mj; 165 } 166 public void show() { 167 System.out.print(df2.format(this.getArea())+" "); 168 } 169 public double getSide1() { 170 return side1; 171 } 172 public void setSide1(double side1) { 173 this.side1=side1; 174 } 175 public double getSide2() { 176 return side2; 177 } 178 public void setSide2(double side2) { 179 this.side2=side2; 180 } 181 public double getSide3() { 182 return side3; 183 } 184 public void setSide3(double side3) { 185 this.side3=side3; 186 } 187 }
题目集6(7-6)详情:
GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)
输入格式:
从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。
输出格式:
如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出Wrong Format
如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数
题目集6(7-6)类图:

题目集6(7-6)代码:
1 import java.text.DecimalFormat; 2 import java.util.Scanner; 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 public class Main { 6 public Main() { 7 8 } 9 public static void main(String[] args) { 10 Scanner input=new Scanner(System.in); 11 DecimalFormat df2 = new DecimalFormat("####0.00"); 12 double r=input.nextDouble(); 13 double length=input.nextDouble(); 14 double width=input.nextDouble(); 15 if(r<=0||length<=0||width<=0) { 16 System.out.println("Wrong Format"); 17 System.exit(0); 18 } 19 GetArea getArea=new Circle(r); 20 System.out.println(df2.format(getArea.getArea())); 21 getArea=new Rectangle(length,width); 22 System.out.println(df2.format(getArea.getArea())); 23 } 24 } 25 interface GetArea{ 26 public double getArea(); 27 } 28 class Circle implements GetArea{ 29 private double r; 30 public Circle(double r) { 31 this.r=r; 32 } 33 public double getArea() { 34 return Math.PI*r*r; 35 } 36 public double getR() { 37 return r; 38 } 39 public void setR(double r) { 40 this.r=r; 41 } 42 } 43 class Rectangle implements GetArea{ 44 public Rectangle(double length,double width) { 45 this.length=length; 46 this.width=width; 47 } 48 private double length; 49 private double width; 50 public double getArea() { 51 return length*width; 52 } 53 public double getLength() { 54 return length; 55 } 56 public void setLength(double length) { 57 this.length=length; 58 } 59 public double getWidth() { 60 return width; 61 } 62 public void setWidth(double width) { 63 this.width=width; 64 } 65 }
通过观察类图和代码可以看到第一题是Rectangle类和Circle类继承Shape类,然后Rectangle类和Circle类分别派生出Box类和Ball类,第二题是三个类:Rectangle、Tritangle、Circle直接继承Shape类,第三题出现了新东西:接口,然后在Rectangle类和Circle类中实现接口GetArea。可以看出来题目是越来越简单的。代码是越来越简洁的,从多继承,到单继承,再到接口;
(3 )对三次题目集中用到的正则表达式技术的分析总结
1、 检验QQ号:"^[1-9][0-9]{4,14}$"这是第一位数不为0,5到15位数字;
2、 检验验证码:"^[A-Za-z0-9]{4}$"四位数字或者字母(包含大小写)组成的字
符串;
3、检验学号:"^2020+((1[1-7])|61|(7[1-3])|(8[1-2]))+(0[1-9]|[1-3][0-9]|40)$"
1、2位:入学年份后两位,例如20年
3、4位:学院代码,软件学院代码为20
5位、6位:方向代码、班级序号,例如1为软件工程,7为物联网(11-17、61、71-73、81-82)
7、8位:学号(序号)(1-40)
4、split函数:通过split(“x”)以x来分割一个字符串;
(4)题目集5(7-4)中Java集合框架应用的分析总结
先看题目:
编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:
Java中共有53个关键字(自行百度)
从键盘输入一段源码,统计这段源码中出现的关键字的数量
注释中出现的关键字不用统计
字符串中出现的关键字不用统计
统计出的关键字及数量按照关键字升序进行排序输出
未输入源码则认为输入非法
输入格式:
输入Java源码字符串,可以一行或多行,以exit行作为结束标志
输出格式:
当未输入源码时,程序输出Wrong Format
当没有统计数据时,输出为空
当有统计数据时,关键字按照升序排列,每行输出一个关键字及数量,格式为数量\t关键字
这道题目我用的是map接口,Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;这就相当于是一种函数集合,我是把要查找的关键字作为key,关键字出现的次数作为value,先把关键字排好存进一个数组gjz[];输出关键字个数的时候只需要输出map.get(gjz[i]),这就非常方便了。不用不知道,一用吓一跳。当然在使用的时候也出现了一些问题,关键字是String类型,而出现的次数是整形值,所以在创建map对象的时候用Map<String, String> map = new HashMap<String,String>()不太好,当时我一直有问题,后面改成了Map map = new TreeMap()就好了;SourceMonitor的生成报表内容如下:

3、 踩坑心得
这次踩到的最大的一个坑是输入的问题,不能把输入放进方法体里面,否则会产生难以理解的问题;就像这样:
{
Scanner input=new Scanner(System.in);
int cz=input.nextInt();
if(cz>=1&&cz<=4) {
switch(cz){
case 1:
Main test1=new Main();
test1.show1();
break;
}
public void show1() {
double r=input.nextDouble();
if(r>=0) {
Circle yuan=new Circle();
yuan.setR(r);
System.out.println("Constructing Shape");
System.out.println("Constructing Circle");
System.out.print("Circle's area:"+df2.format(yuan.getArea()));
}
else
System.out.print("Wrong Format");
}
然后这样输入就会产生:

无法终止程序。
但是这样输入就可以输出:

但是pta里面就是按第一种输入,所以刚开始写的时候,在pta里输出一直是空,得不到分,浪费了好多时间。
4、 改进建议
还是要改进一些pta这个Java编译器的设置,好多时候会出现问题,造成学生时间的浪费。
5、 总结
这三次题目集我也是收获颇多,1、正则表达式的运用;2、类的继承、聚合等都有了更深入的了解;3、Java抽象类的使用;4、Java集合框架,map的使用,了解了键值对;5、Java接口的使用。但是集合框架、抽象类、和接口都只是初步了解,运用还不熟练,这些内容需要更进一步的学习。
浙公网安备 33010602011771号