题目集4~6的总结
一:前言
题目集四
1.题目集四的的知识点
① 使用Java中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算。
② 利用聚合对日期问题进行面向对象设计。
③ 利用继承对图形类问题进行面向对象设计。
④ 其中包含前三集作业中Java基本知识,如Java的for循环结构和类的私有属性等。
2.题目集四的题量
① 水文数据校验及处理
② 日期问题面向对象设计(聚合一)
③ 图形继承
3.题目集四的难度
个人认为难度稍微偏大,每道题都得花不少时间,特别是水文数据检验及处理,繁琐的正则表达式处理,相较于前三集难度存在小跨越,不过对于提高编程能力还是有不错的收益。
题目集五
1.题目集五的的知识点
① 除了前面的for循环跟数组合并之外,新增了对于插入排序、选择排序及冒泡排序这三种排序的练习。
② 对于提取关键字的正则表达式的练习和提取了的关键字的排序。
③ 针对题目集四中聚合问题的更深层次的练习。
2.题目集五的题量
① 找出最长的单词-hebust
② 合并两个有序数组为新的有序数组
③ 对整型数据排序
④ 统计Java程序中关键词的出现次数
⑤ 日期问题面向对象设计(聚合二)
3.题目集五的难度
相对来说难度适中,前几题比较偏基础,后面集体难度还是有些的,特别是统计关键字的算法。相较于整体难度偏大的题目四来说还是会相对给人一点小成就感哈哈哈。
题目集六
1.题目集六的的知识点
① 对于正则表达式的练习。
② 对字符串的排序算法。
③ Java中的继承,接口和多态。
2.题目集六的题量
① 正则表达式训练---QQ号校验,验证码校验,学号校验
② 字符串训练-字符排序
③ 图形继承与多态
④ 实现图形接口及多态性
3.题目集六的难度
难度适中,正则表达式那几题的解题思路相差无几,不过继承,接口和多态那两题还是稍微有点难度的,难度较小的多次训练对于提高知识的掌握性还是有帮助的。
二:设计与分析
①题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较
题目集4(7-2)代码类图如下:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner scanner = new Scanner(System.in); 8 int select=scanner.nextInt(); 9 int year = scanner.nextInt(); 10 int month = scanner.nextInt(); 11 int day = scanner.nextInt(); 12 13 if(select==2) { 14 int n = scanner.nextInt(); 15 if(checkInputValidity(year,month,day)==true){ 16 String a=""; 17 if(n<0) { 18 System.out.print("Wrong Format"); 19 } 20 else { 21 for(int i=0;i<n;i++) { 22 a=lastDate(year,month,day); 23 24 25 String b[]=a.split("\\s+"); 26 year=Integer.parseInt(b[0]); 27 month=Integer.parseInt(b[1]); 28 day=Integer.parseInt(b[2]); 29 30 } 31 32 33 System.out.print(year+"-"+month+"-"+day); 34 35 } 36 37 } 38 else 39 { 40 System.out.print("Wrong Format"); 41 } 42 } 43 else if(select==1) { 44 int n = scanner.nextInt(); 45 if(checkInputValidity(year,month,day)==true){ 46 String a=""; 47 if(n>0) { 48 for(int i=0;i<n;i++) { 49 a=nextDate(year,month,day); 50 51 52 String b[]=a.split("\\s+"); 53 year=Integer.parseInt(b[0]); 54 month=Integer.parseInt(b[1]); 55 day=Integer.parseInt(b[2]); 56 57 } 58 59 60 System.out.print(year+"-"+month+"-"+day); 61 62 } 63 64 65 } 66 else 67 { 68 System.out.print("Wrong Format"); 69 } 70 } 71 else if(select==3) { 72 int year1 = scanner.nextInt(); 73 int month1 = scanner.nextInt(); 74 int day1 = scanner.nextInt(); 75 if(year<year1) { 76 int temp = year;year=year1;year1=temp; 77 temp = month;month=month1;month1=temp; 78 temp = day;day=day1;day1=temp; 79 } 80 else if(year==year1) { 81 if(month==month1) { 82 if(day==day1) { 83 System.out.println(1); 84 } 85 else if(day<day1) { 86 int temp = day;day=day1;day1=temp; 87 } 88 } 89 else if(month<month1) { 90 int temp = month;month=month1;month1=temp; 91 temp = day;day=day1;day1=temp; 92 } 93 } 94 if(checkInputValidity(year,month,day)==true&&checkInputValidity(year1,month1,day1)==true){ 95 int f=0; 96 long n = 0; 97 String a=""; 98 99 while(f==0) { 100 a=lastDate(year,month,day); 101 102 103 String b[]=a.split("\\s+"); 104 year=Integer.parseInt(b[0]); 105 month=Integer.parseInt(b[1]); 106 day=Integer.parseInt(b[2]); 107 if(year1==year&&month1==month&&day1==day) { 108 f=1; 109 } 110 111 n++; 112 } 113 114 115 System.out.print(n); 116 117 118 119 120 } 121 else 122 { 123 System.out.print("Wrong Format"); 124 } 125 } 126 else { 127 System.out.print("Wrong Format"); 128 } 129 130 } 131 public static boolean isLeapYear(int year) { 132 if(year%4==0&&year%100!=0||year%400==0) { 133 return true; 134 } 135 else { 136 return false; 137 } 138 139 140 } 141 public static boolean checkInputValidity(int year,int month,int day) { 142 if(year>=1900&&year<=2050&&month>0&&month<13&&day>0&&day<32) { 143 if(month==1||month==3|month==5||month==7||month==8||month==10||month==12) { 144 if(day>=1&&day<=31) { 145 return true; 146 } 147 else { 148 return false; 149 } 150 } 151 else if(month==4||month==6|month==9||month==11) { 152 if(day>=1&&day<=30) { 153 return true; 154 } 155 else { 156 return false; 157 } 158 } 159 else { 160 if(isLeapYear(year)==true) { 161 if(day>=1&&day<=29) { 162 return true; 163 } 164 else { 165 return false; 166 } 167 } 168 else { 169 if(day>=1&&day<=28) { 170 return true; 171 } 172 else { 173 return false; 174 } 175 } 176 177 } 178 } 179 else { 180 return false; 181 } 182 183 184 } 185 public static String nextDate(int year,int month,int day) { 186 String a=""; 187 if(month==1||month==3|month==5||month==7||month==8||month==10||month==12) 188 { 189 if(day>=0&&day<=30) 190 { 191 day=day+1; 192 a=year+" "+month+" "+day; 193 } 194 else if(day==31) 195 { 196 if(month==12) 197 { 198 year=year+1; 199 month=1; 200 day=1; 201 a=year+" "+month+" "+day; 202 } 203 else 204 { 205 month=month+1; 206 day=1; 207 a=year+" "+month+" "+day; 208 } 209 } 210 211 } 212 else if(month==4||month==6|month==9||month==11) 213 { 214 if(day>=0&&day<=29) 215 { 216 day=day+1; 217 a=year+" "+month+" "+day; 218 } 219 else if(day==30) 220 { 221 month=month+1; 222 day=1; 223 a=year+" "+month+" "+day; 224 } 225 226 } 227 else 228 { 229 if(isLeapYear(year)==true) 230 { 231 if(day>=0&&day<=28) 232 { 233 day=day+1; 234 a=year+" "+month+" "+day; 235 } 236 else if(day==29) 237 { 238 month=month+1; 239 day=1; 240 a=year+" "+month+" "+day; 241 } 242 243 } 244 else 245 { 246 if(day>=0&&day<=27) 247 { 248 day=day+1; 249 a=year+" "+month+" "+day; 250 } 251 else if(day==28) 252 { 253 month=month+1; 254 day=1; 255 a=year+" "+month+" "+day; 256 } 257 258 } 259 } 260 return a; 261 } 262 public static String lastDate(int year,int month,int day) { 263 String a=""; 264 if(month==2||month==4|month==6||month==8||month==9||month==11) 265 { 266 if(day>1) 267 { 268 day=day-1; 269 a=year+" "+month+" "+day; 270 } 271 else 272 { 273 month=month-1; 274 day=31; 275 a=year+" "+month+" "+day; 276 } 277 278 } 279 280 281 else if(month==5||month==7||month==10||month==12) 282 { 283 if(day>1) 284 { 285 day=day-1; 286 a=year+" "+month+" "+day; 287 } 288 else 289 { 290 month=month-1; 291 day=30; 292 a=year+" "+month+" "+day; 293 } 294 295 } 296 else if(month==3) 297 { 298 if(isLeapYear(year)==true) 299 { 300 if(day>1) 301 { 302 day=day-1; 303 a=year+" "+month+" "+day; 304 } 305 else 306 { 307 month=month-1; 308 day=29; 309 a=year+" "+month+" "+day; 310 } 311 } 312 else 313 { 314 if(day>1) 315 { 316 day=day-1; 317 a=year+" "+month+" "+day; 318 } 319 else 320 { 321 month=month-1; 322 day=28; 323 a=year+" "+month+" "+day; 324 } 325 } 326 } 327 else { 328 if(day>1) 329 { 330 day=day-1; 331 a=year+" "+month+" "+day; 332 } 333 else 334 { 335 year = year-1; 336 month=12; 337 day=31; 338 a=year+" "+month+" "+day; 339 } 340 } 341 return a; 342 } 343 }

分析:
利用聚合的思想,将Year、Month、 Day串联在一起,然后创建一个DateUtil类与Day和主类之间创建联系,Year、Month、 Day类中定义各自的属性与方法,DateUtil类中则调用他们的方法从而实现程序所 要实现的功能,如求下n天、前n天、两个日期之间的天数。
题目集5(7-4)代码类图如下:
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 int select = sc.nextInt(); 7 int year = sc.nextInt(); 8 int month = sc.nextInt(); 9 int day = sc.nextInt(); 10 if(checkInputValidity(year,month,day)==true) { 11 if(select==1) { 12 long num = sc.nextLong(); 13 long n = numOfDays(year,month ,day)+num; 14 15 System.out.println(year+"-"+month+"-"+day+" next "+num+" days is:" + data(n)); 16 17 18 } 19 else if(select==2) { 20 long num = sc.nextLong(); 21 long n = numOfDays(year,month ,day)-num; 22 23 System.out.println(year+"-"+month+"-"+day+" previous "+num+" days is:" + data(n)); 24 25 26 } 27 else if(select==3) { 28 int year1 = sc.nextInt(); 29 int month1 = sc.nextInt(); 30 int day1 = sc.nextInt(); 31 long n = numOfDays(year,month ,day)-numOfDays(year1,month1 ,day1); 32 if(n>=0) { 33 System.out.println("The days between "+year+"-"+month+"-"+day+" and "+year1+"-"+month1+"-"+day1+" are:" + n); 34 } 35 else { 36 n=-n; 37 System.out.println("The days between "+year+"-"+month+"-"+day+" and "+year1+"-"+month1+"-"+day1+" are:" + n); 38 } 39 } 40 else { 41 System.out.println("Wrong Format"); 42 } 43 44 } 45 else System.out.println("Wrong Format"); 46 } 47 48 public static boolean isLeapYear(int year) { 49 if(year%4==0&&year%100!=0||year%400==0) { 50 return true; 51 } 52 else { 53 return false; 54 } 55 56 57 } 58 public static long numOfDays(int year,int month ,int day) { 59 long days=0; 60 int i; 61 int []ld = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; 62 for(i=-100;i<year;i++){ 63 if(isLeapYear(i)==true) { 64 days+=366; 65 } 66 else 67 days+=365; 68 } 69 if(isLeapYear(year)==true) 70 ld[2]=29; 71 for(i = 1;i<month;i++){ 72 days+=ld[i]; 73 } 74 days+=day; 75 return days; 76 } 77 public static String data(long n) { 78 long days=0; 79 int year = 0; 80 int month = 0; 81 int day = 0; 82 int i; 83 int []ld = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; 84 for(i=-100;days<n;i++){ 85 if(isLeapYear(i)==true) { 86 days+=366; 87 } 88 else { 89 days+=365; 90 } 91 year=i; 92 } 93 if(isLeapYear(year)==true) { 94 days-=366; 95 } 96 else { 97 days-=365; 98 } 99 n=n-days; 100 days=0; 101 if(isLeapYear(year)==true) { 102 ld[2]=29; 103 } 104 105 for(i = 1;days<n;i++){ 106 days+=ld[i];month=i; 107 } 108 day =(int) (n+ld[month]-days); 109 110 return year+"-"+month+"-"+day; 111 112 } 113 public static boolean checkInputValidity(int year,int month,int day) { 114 if(year>=1820&&year<=2020&&month>0&&month<13&&day>0&&day<32) { 115 if(month==1||month==3|month==5||month==7||month==8||month==10||month==12) { 116 if(day>=1&&day<=31) { 117 return true; 118 } 119 else { 120 return false; 121 } 122 } 123 else if(month==4||month==6|month==9||month==11) { 124 if(day>=1&&day<=30) { 125 return true; 126 } 127 else { 128 return false; 129 } 130 } 131 else { 132 if(isLeapYear(year)==true) { 133 if(day>=1&&day<=29) { 134 return true; 135 } 136 else { 137 return false; 138 } 139 } 140 else { 141 if(day>=1&&day<=28) { 142 return true; 143 } 144 else { 145 return false; 146 } 147 } 148 149 } 150 } 151 else { 152 return false; 153 } 154 155 156 } 157 158 159 160 }

分析:
与上一题存在些许类似,不过在架构上进行了改动,Year、Month、 Day类在这题中则是直接与DateUtil关联,属性、方法与功能实现与上题一样。
两种聚合的比较:
都在一定程度上实现了面向对象的编程思想,不过对比第一题,第二题中所用的聚合,能更好的维护代码的可重用性,Year、Month、Day类相对独立。
②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)
题目集4(7-3)代码类图如下:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int n = input.nextInt(); 7 if(n == 1) { 8 double radius = input.nextDouble(); 9 if(radius < 0) { 10 System.out.println("Wrong Format"); 11 System.exit(0); 12 } 13 Circle circle = new Circle(); 14 circle.setRadius(radius); 15 System.out.println(String.format("Circle's area:"+"%.2f", circle.getArea())); 16 } 17 else if(n == 2) { 18 double width = input.nextDouble(); 19 double length = input.nextDouble(); 20 if(width < 0 || length < 0) { 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 Rectangle rectangle = new Rectangle(); 25 rectangle.setWidth(width); 26 rectangle.setLength(length); 27 System.out.println(String.format("Rectangle's area:"+"%.2f", rectangle.getArea())); 28 } 29 else if(n == 3) { 30 double radius = input.nextDouble(); 31 if(radius < 0 ) { 32 System.out.println("Wrong Format"); 33 System.exit(0); 34 } 35 Ball ball = new Ball(); 36 ball.setRadius(radius); 37 System.out.println(String.format("Ball's surface area:"+"%.2f", ball.getArea())); 38 System.out.println(String.format("Ball's volume:"+"%.2f", ball.getVolume())); 39 } 40 else if(n == 4) { 41 double length = input.nextDouble(); 42 double width = input.nextDouble(); 43 double height = input.nextDouble(); 44 if(width < 0 || length < 0 || height < 0) { 45 System.out.println("Wrong Format"); 46 System.exit(0); 47 } 48 Box box = new Box(); 49 box.setLength(length); 50 box.setWidth(width); 51 box.setHeight(height); 52 System.out.println("Box's surface area:"+String.format("%.2f", box.getArea())); 53 System.out.println("Box's volume:"+String.format("%.2f", box.getVolume())); 54 } 55 else { 56 System.out.println("Wrong Format"); 57 } 58 59 } 60 61 62 } 63 class Shape { 64 public double getArea() { 65 return 0.0; 66 } 67 public Shape() { 68 System.out.println("Constructing Shape"); 69 } 70 71 } 72 class Circle extends Shape{ 73 private double radius; 74 public Circle() { 75 System.out.println("Constructing Circle"); 76 } 77 78 public double getRadius() { 79 return radius; 80 } 81 82 public void setRadius(double radius) { 83 this.radius = radius; 84 } 85 86 public double getArea() { 87 return Math.PI*radius*radius; 88 } 89 90 } 91 class Rectangle extends Shape{ 92 private double width; 93 private double length; 94 public Rectangle() { 95 System.out.println("Constructing Rectangle"); 96 } 97 public double getWidth() { 98 return width; 99 } 100 public void setWidth(double width) { 101 this.width = width; 102 } 103 public double getLength() { 104 return length; 105 } 106 public void setLength(double length) { 107 this.length = length; 108 } 109 public double getArea() { 110 return width*length; 111 } 112 113 } 114 115 class Ball extends Circle{ 116 public Ball() { 117 System.out.println("Constructing Ball"); 118 } 119 public Ball(double radius) { 120 super(); 121 } 122 public double getArea() { 123 return 4*Math.PI*getRadius()*getRadius(); 124 } 125 126 public double getVolume() { 127 return 4/3.0*Math.PI*getRadius()*getRadius()*getRadius(); 128 } 129 130 } 131 class Box extends Rectangle{ 132 public Box() { 133 System.out.println("Constructing Box"); 134 } 135 private double height; 136 public double getHeight() { 137 return height; 138 } 139 public void setHeight(double height) { 140 this.height = height; 141 } 142 public double getVolume() { 143 return getWidth()*getLength()*height; 144 } 145 @Override 146 public double getArea() { 147 return 2*getWidth()*getLength()+2*getWidth()*height+2*getLength()*height; 148 } 149 150 151 }

分析:
- 类Shape,无属性,有一个返回0.0的求图形面积的公有方法
public double getArea();//求图形面积 - 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
- 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
- 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法
public double getVolume();//求球体积 - 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法
public double getVolume();//求立方体体积
这道题主要运用了继承的思想,从子类从父类中继承属性,重写方法,最后通过Main类中的调用实现所需功能。
题目集6(7-5)代码类图如下:
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.Collections; 4 import java.util.List; 5 import java.util.Scanner; 6 7 public class Main { 8 public static void main(String[] args) { 9 10 ArrayList<Shape> s = new ArrayList<Shape>(); 11 12 Scanner sc = new Scanner(System.in); 13 int n1=sc.nextInt(); 14 int n2=sc.nextInt(); 15 int n3=sc.nextInt(); 16 int flag = 0; 17 18 19 if(n1>=0 && n2>=0 && n3>=0){ 20 21 for(int i=0;i<n1;i++) { 22 double z=sc.nextDouble(); 23 Circle c = new Circle(); 24 c.setRadius(z); 25 if(c.validate()==false){ 26 System.out.println("Wrong Format"); 27 flag = 1; 28 break; 29 } 30 s.add(c); 31 } 32 33 for(int i=0;i<n2;i++) { 34 double x=sc.nextDouble(); 35 double y=sc.nextDouble(); 36 Rectangle r = new Rectangle(); 37 r.setLength(x); 38 r.setWidth(y); 39 if(r.validate()==false) { 40 System.out.println("Wrong Format"); 41 flag = 1; 42 break; 43 } 44 s.add(r); 45 } 46 47 for(int i=0;i<n3;i++) { 48 49 double o=sc.nextDouble(); 50 double p=sc.nextDouble(); 51 double q=sc.nextDouble(); 52 53 Triangle t = new Triangle(); 54 t.setSide1(o); 55 t.setSide2(p); 56 t.setSide3(q); 57 if(t.validate()==false) { 58 System.out.println("Wrong Format"); 59 flag = 1; 60 break; 61 } 62 s.add(t); 63 } 64 65 if(flag==0) { 66 System.out.println("Original area:"); 67 for(int i=0;i<s.size();i++) { 68 System.out.printf("%.2f ",s.get(i).getArea()); 69 } 70 71 System.out.printf("\nSum of area:%.2f",GetArea.getall(s)); 72 73 System.out.println("\nSorted area:"); 74 75 for(int i = 0; i < s.size() -1; i++) { 76 for(int j = 0; j < s.size() - i - 1; j++) { 77 if(s.get(j).getArea() > s.get(j+1).getArea()) { 78 Shape e = s.get(j); 79 Shape f = s.get(j+1); 80 s.remove(j); 81 s.remove(j); 82 s.add(j,f); 83 s.add(j+1,e); 84 } 85 } 86 } 87 for(int i = 0; i < s.size(); i++) { 88 System.out.printf("%.2f ",s.get(i).getArea()); 89 } 90 System.out.println(); 91 System.out.printf("Sum of area:%.2f",GetArea.getall(s)); 92 } 93 94 }else { 95 System.out.println("Wrong Format"); 96 } 97 } 98 } 99 100 abstract class Shape { 101 102 public double getArea() { 103 return 0; 104 } 105 public boolean validate() { 106 return true; 107 } 108 public String toString() { 109 return null; 110 } 111 112 } 113 class Circle extends Shape{ 114 private double radius; 115 public Circle() { 116 117 } 118 119 public Circle(double radius) { 120 this.radius = radius; 121 } 122 123 public double getRadius() { 124 return radius; 125 } 126 127 public void setRadius(double radius) { 128 this.radius = radius; 129 } 130 131 public double getArea(){ 132 133 134 /*double area= Math.PI*getRadius()*getRadius(); 135 BigDecimal bg = new BigDecimal(area); 136 double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 137 return f1;*/ 138 139 double area= Math.PI*getRadius()*getRadius(); 140 return area; 141 142 } 143 public boolean validate() { 144 if(radius>0) { 145 return true; 146 }else { 147 return false; 148 } 149 150 } 151 public String toString() { 152 return getArea()+" "; 153 154 } 155 156 } 157 class Rectangle extends Shape { 158 private double width; 159 private double length; 160 public Rectangle() { 161 162 } 163 public Rectangle(double width, double length) {//有参构造方法初始化 164 this.width = width; 165 this.length = length; 166 } 167 168 public double getWidth() { 169 return width; 170 } 171 public void setWidth(double width) { 172 this.width = width; 173 } 174 public double getLength() { 175 return length; 176 } 177 public void setLength(double length) { 178 this.length = length; 179 } 180 181 public double getArea(){ //计算面积并显示.showArea方法的重写 182 double area = getWidth()*getLength(); 183 return area; 184 } 185 public boolean validate() { 186 if(width>0&&length>0) { 187 return true ; 188 }else { 189 return false; 190 } 191 } 192 193 public String toString() { 194 return getArea()+" "; 195 } 196 197 } 198 class Triangle extends Shape { 199 private double side1; 200 private double side2; 201 private double side3; 202 203 public Triangle(){ 204 205 } 206 207 public Triangle(double side1,double side2,double side3) { 208 this.side1 = side1; 209 this.side2 = side2; 210 this.side3 = side3; 211 } 212 213 public double getSide1() { 214 return side1; 215 } 216 public void setSide1(double side1) { 217 this.side1 = side1; 218 } 219 public double getSide2() { 220 return side2; 221 } 222 public void setSide2(double side2) { 223 this.side2 = side2; 224 } 225 public double getSide3() { 226 return side3; 227 } 228 public void setSide3(double side3) { 229 this.side3 = side3; 230 } 231 232 public double getArea() { 233 double s = (side1+side2+side3)*1.0/2; 234 double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); 235 return area; 236 } 237 public boolean validate() {//对图形的属性进行合法性校验 238 if((side1>=0)&&(side2>=0)&&(side3>=0)&&(side1+side2>side3)&&(side1+side3>side2)&&(side2+side3>side1)){ 239 return true; 240 }else 241 return false; 242 } 243 244 public String toString() { 245 return getArea()+" "; 246 } 247 248 } 249 250 class GetArea { 251 public static double getall(ArrayList<Shape> s) { 252 double x = 0; 253 for(int i=0;i<s.size();i++) { 254 x+=s.get(i).getArea(); 255 } 256 return x; 257 } 258 }

分析:
这道题运用了继承和多态,Shape 为抽象类,Circle、Rectangle 及 Triangle 为实体类,getArea()方法为抽象方法,功能为求得图形的面积;validate()方法也为抽象方法,对图形的属 性进行合法性校验; toString()继承自 Object,功能为输出图形的面积信息。
题目集6(7-6)代码类图如下:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 double radius = sc.nextDouble(); 7 double width = sc.nextDouble(); 8 double length = sc.nextDouble(); 9 if(radius<=0||width<=0||length<=0) { 10 System.out.println("Wrong Format"); 11 } 12 else { 13 GetArea circle = new Circle(); 14 GetArea rectangle = new Rectangle(); 15 circle.setRadius(radius); 16 rectangle.setWidth(width); 17 rectangle.setLength(length); 18 System.out.println(String.format("%.2f", circle.getArea())); 19 System.out.println(String.format("%.2f",rectangle.getArea())); 20 } 21 } 22 23 } 24 25 interface GetArea { 26 27 double getArea(); 28 29 void setRadius(double radius); 30 31 void setWidth(double width); 32 33 void setLength(double length); 34 35 } 36 37 class Circle implements GetArea { 38 private double radius; 39 40 public double getRadius() { 41 return radius; 42 } 43 44 public void setRadius(double radius) { 45 this.radius = radius; 46 } 47 @Override 48 public double getArea() { 49 // TODO Auto-generated method stub 50 return Math.PI*radius*radius; 51 } 52 53 @Override 54 public void setWidth(double width) { 55 // TODO Auto-generated method stub 56 57 } 58 59 @Override 60 public void setLength(double length) { 61 // TODO Auto-generated method stub 62 63 } 64 65 66 } 67 68 class Rectangle implements GetArea { 69 private double width; 70 private double length; 71 public double getWidth() { 72 return width; 73 } 74 public void setWidth(double width) { 75 this.width = width; 76 } 77 public double getLength() { 78 return length; 79 } 80 public void setLength(double length) { 81 this.length = length; 82 } 83 @Override 84 public double getArea() { 85 // TODO Auto-generated method stub 86 return width*length; 87 } 88 @Override 89 public void setRadius(double radius) { 90 // TODO Auto-generated method stub 91 92 } 93 94 95 }

分析:
本题运用了接口,GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口,从而实现所需功能。
三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等):
针对上面三题,得出总结,①封装能增强安全性和简化编程,使用者不必了解具体的实现细节,而只是要通过外部接口,一特定的访问权限来使用类的成员;②继承能更好的实现代码的复用性,③多态是指 相同的事物,调用其相同的方法,参数也相同时,但表现的行为却不同,能更好的增强程序的可扩展性及可维护性;④接口是一种特殊形式的抽象类,在Java语言中抽象类表示的是一种继承关系,一个类 只能继承继承一个抽象类,而一个类却可以实现多个接口。
③对三次题目集中用到的正则表达式技术的分析总结
这三次题目集中多次运用到正则表达式,可见正则表达式的重要性和实用性。
下面是这三次题目集中所用到的部分正则表达式:
string.matches("^[1-9][0-9]{0,3}/(1[0-2]|[1-9])/([1-2][0-9]|[1-9]|30) ([01]?[02468]|2[024]):00$")(水文数据校验及处理部分正则表达式)
String rule = "[1-9][0-9]{4,14}";(QQ验证)
String rule = "[a-zA-Z0-9] ";(验证码检验)
String rule = "2020(11-17|61|71-73|81|82)[01-40]{8}";(学号检验)
可以看出,正则表达式有自己独特的语法结构,对于其表达式我们也应该熟练的掌握,在需要的时候能自己编制一组正则表达式。
④题目集5(7-4)中Java集合框架应用的分析总结
题目集5(7-4)代码类图如下:
1 import java.util.Scanner; 2 3 public class Main { 4 final static String[] KEYWORDS = { //53个关键字 5 "abstract", "assert", "boolean", "break", "byte", 6 "case", "catch", "char", "class", "const", 7 "continue", "default", "do", "double", "else", 8 "enum", "extends","false", "final", "finally", "float", 9 "for", "goto", "if", "implements", "import", 10 "instanceof", "int", "interface", "long", "native", 11 "new", "null","package", "private", "protected", "public", 12 "return", "short", "static","strictfp", "super", 13 "switch", "synchronized", "this", "throw", "throws", 14 "transient", "true","try", "void", "volatile", "while" 15 }; 16 static int[] key=new int[53]; 17 public static void main(String[] args){ 18 Main.check(); 19 printf(); 20 } 21 public static void check(){ 22 Scanner scan=new Scanner(System.in); 23 String word=""; 24 String str=scan.nextLine(); 25 if(str.equals("exit")) 26 { 27 System.out.println("Wrong Format"); 28 System.exit(0); 29 } 30 while(!str.equals("exit")){ 31 str=str.replaceAll("\\/\\/.*$", " "); 32 word+=" "+str; 33 str=scan.nextLine(); 34 } 35 scan.close(); 36 word=word.replaceAll("(\\\\\")"," "); 37 word=word.replaceAll("\\/\\*.*\\*\\/", " "); 38 word=word.replaceAll("(\").*?(\")", " "); 39 String [] array = word.split("[^A-Za-z0-9_=]"); 40 for(String words : array){ 41 for(int i=0;i<53;i++){ 42 if(words.equals(KEYWORDS[i])) 43 key[i]++; 44 } 45 } 46 } 47 public static void printf(){ 48 for(int i=0;i<53;i++){ 49 if(key[i]!=0) 50 System.out.println(key[i]+"\t"+KEYWORDS[i]); 51 } 52 53 } 54 55 }
这道题偏向算法题的类型,只有一个Main类,所以不需要类图。
分析:
统计Java程序中关键词的出现次数 ,首先将关键字存进数组中,然后利用正则表法式对输入的程序进行筛查,将筛查出来的关键字排序,这道题进行了排序算法与正则表达式的考察。
三.采坑心得
1.对于正则表达式的运用一定要严谨,合格的正则表达式应该面面俱到,同时也得考虑效率问题。
比如String rule = "2020(11-17|61|71-73|81|82)[01-40]{8}";(学号检验)这个表达式就存在问题
2.对于封装、继承、接口、多态的用法得多去了解,刚开始运用的时候不了解他们其中的区别,特别是多态,相对于偏概念性,比较空洞。
3.做水文数据检测与处理那个题目的时候,刚开始没想到运用正则表达式去判定,从而走了很远的弯路,到后面也是写不下去,后面问了同学,才利用正则表达式去解决,合理利用正则表达式很多时候能更 方便准确。
四.改进意见
1.题目难度相对来说,上升的有点不合理,我觉得题目难度应该逐渐加大,而不是像题目集四大体偏难,而题目集六大体上偏容易一些,这样的话不仅对于知识的掌握相对较困难,也会打击学生的自信。
2.对于有些题目来说,类名、方法题目已经规定好了,所以大家的代码重复率会大大增加,那查重的作用就会大打折扣,我觉得还是可以适当不规定那么多,尽量让学生发挥创作空间。
五.总结
通过这三次题目集,让我对于Java的了解与运用上了很大一个台阶,也慢慢学会了对于正则表达式、还有一些面向对象编程思想(这要是这四种:封装、继承、接口、多态)的原理与应用。收获还是比较 明显的,还有通过这次总结,对于类图的绘制与意义都有了大体上的认知。

浙公网安备 33010602011771号