题目集4-6的总结性Blog
作者:互联网
一、前言
- 这三次作业涉及的知识点有排序、字符串查找、正则表达式,不同类的聚合设计,继承、接口及多态的应用。
- 正则表达式相关题目有难有易,其中搜索Java关键字一题比较繁琐,类的继承、多态性使用方法以及接口的应用,有了之前作业的基础,逐步抽象,进一步体现多态与继承,。
- 三次题量总体适中。
二、设计与分析
-
题目集4(7-2)
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 year = 0; 7 int month = 0; 8 int day = 0; 9 10 int choice = input.nextInt(); 11 12 DateUtil date = null; 13 14 switch (choice) { 15 case 1: 16 int n = 0; 17 year = Integer.parseInt(input.next()); 18 month = Integer.parseInt(input.next()); 19 day = Integer.parseInt(input.next()); 20 21 date = new DateUtil(year, month, day); 22 23 if (!date.checkInputValidity()) { 24 System.out.println("Wrong Format"); 25 System.exit(0); 26 } 27 28 n = input.nextInt(); 29 30 if (n < 0) { 31 System.out.println("Wrong Format"); 32 System.exit(0); 33 } 34 35 System.out.println(year+"-"+month+"-"+day+" next "+n+" days is:"+date.getNextNDays(n).showDate()); 36 break; 37 case 2: 38 int m = 0; 39 year = Integer.parseInt(input.next()); 40 month = Integer.parseInt(input.next()); 41 day = Integer.parseInt(input.next()); 42 43 date = new DateUtil(year, month, day); 44 45 if (!date.checkInputValidity()) { 46 System.out.println("Wrong Format"); 47 System.exit(0); 48 } 49 50 m = input.nextInt(); 51 52 if (m < 0) { 53 System.out.println("Wrong Format"); 54 System.exit(0); 55 } 56 57 System.out.println(year+"-"+month+"-"+day+" previous "+m+" days is:"+date.getPreviousNDays(m).showDate()); //怎么一会is一会儿are啊 58 break; 59 case 3: 60 year = Integer.parseInt(input.next()); 61 month = Integer.parseInt(input.next()); 62 day = Integer.parseInt(input.next()); 63 64 int anotherYear = Integer.parseInt(input.next()); 65 int anotherMonth = Integer.parseInt(input.next()); 66 int anotherDay = Integer.parseInt(input.next()); 67 68 DateUtil fromDate = new DateUtil(year, month, day); 69 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 70 71 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 72 System.out.println("The days between "+year+"-"+month+"-"+day+" and "+anotherYear+"-"+anotherMonth+"-"+anotherDay+" are:"+fromDate.getDaysofDates(toDate)); 73 } else { 74 System.out.println("Wrong Format"); 75 System.exit(0); 76 } 77 break; 78 default: 79 System.out.println("Wrong Format"); 80 System.exit(0); 81 } 82 input.close(); 83 } 84 } 85 86 class Year { 87 private int value; 88 89 public Year() { 90 91 } 92 93 public Year(int value) { 94 this.value = value; 95 } 96 97 public int getValue() { 98 return value; 99 } 100 101 public void setValue(int value) { 102 this.value = value; 103 } 104 105 public boolean isLeapYear() { 106 return value % 4 == 0 && value % 100 != 0 || value % 400 == 0; 107 } 108 109 public boolean validate() { 110 boolean rtn = true; 111 112 if (value < 1820 || value > 2020) { 113 rtn = false; 114 } 115 return rtn; 116 } 117 118 public void yearIncrement() { 119 value++; 120 } 121 122 public void yearReduction() { 123 value--; 124 } 125 } 126 127 class Month { 128 private int value; 129 private Year year; 130 131 public Month() { 132 year = new Year(); 133 } 134 135 public Month(int yearValue, int monthValue) { 136 this.value = monthValue; 137 year = new Year(yearValue); 138 } 139 140 public int getValue() { 141 return value; 142 } 143 144 public void setValue(int value) { 145 this.value = value; 146 } 147 148 public Year getYear() { 149 return this.year; 150 } 151 152 public void setYear(Year year) { 153 this.year = year; 154 } 155 156 public void resetMin() { 157 value = 1; 158 } 159 160 public void resetMax() { 161 value = 12; 162 } 163 164 public boolean validate() { 165 boolean rtn = true; 166 167 if (value < 1 || value > 12) { 168 rtn = false; 169 } 170 171 return rtn; 172 } 173 174 public void monthIncrement() { 175 value++; 176 if (value > 12) { 177 value = 1; 178 year.yearIncrement(); 179 } 180 } 181 182 public void monthReduction() { 183 value--; 184 if (value <= 0) { 185 value = 12; 186 year.yearReduction(); 187 } 188 } 189 } 190 191 class Day { 192 private int value; 193 private Month month; 194 195 private static final int mon_maxnum[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 196 197 public Day() { 198 month = new Month(); 199 } 200 201 public Day(int yearValue, int monthValue, int dayValue) { 202 this.value = dayValue; 203 this.month = new Month(yearValue, monthValue); 204 } 205 206 public int getValue() { 207 return value; 208 } 209 210 public void setValue(int value) { 211 this.value = value; 212 } 213 214 public Month getMonth() { 215 return month; 216 } 217 218 public void setMonth(Month value) { 219 this.month = value; 220 } 221 222 public void resetMin() { 223 value = 1; 224 } 225 226 public void resetMax() { 227 if (this.getMonth().getValue() == 2 && this.getMonth().getYear().isLeapYear()) { 228 value = mon_maxnum[this.getMonth().getValue()] + 1; 229 } else { 230 value = mon_maxnum[this.getMonth().getValue()]; 231 } 232 } 233 234 public boolean validate() { 235 boolean rtn = true; 236 if (value < 1) { 237 rtn = false; 238 } else if (this.getMonth().getValue() == 2 && this.getMonth().getYear().isLeapYear()) { 239 if (value > mon_maxnum[this.getMonth().getValue()] + 1) 240 rtn = false; 241 } else { 242 if (value > mon_maxnum[this.getMonth().getValue()]) 243 rtn = false; 244 } 245 return rtn; 246 } 247 248 public void dayIncrement() { 249 value++; 250 251 if (value > 27) { 252 if (this.getMonth().getValue() == 2 && this.getMonth().getYear().isLeapYear()) { 253 if (value > mon_maxnum[this.getMonth().getValue()] + 1) { 254 this.resetMin(); 255 this.month.monthIncrement(); 256 } 257 } else { 258 if (value > mon_maxnum[this.getMonth().getValue()]) { 259 this.resetMin(); 260 this.month.monthIncrement(); 261 } 262 } 263 } 264 } 265 266 public void dayReduction() { 267 value--; 268 if (value <= 0) { 269 this.month.monthReduction(); 270 if (this.month.getValue() <= 0) { 271 this.month.resetMax(); 272 this.month.getYear().yearReduction(); 273 } 274 this.resetMax(); 275 } 276 } 277 } 278 279 class DateUtil { 280 private Day day; 281 282 /*可以不这么干啊 283 public DateUtil() { 284 day = new Day(1, 1, 1); 285 }*/ 286 287 288 public DateUtil(int d, int m, int y) { 289 day = new Day(d, m, y); 290 } 291 public Day getDay() { 292 return this.day; 293 } 294 295 public void setDay(Day d) { 296 this.day = d; 297 } 298 299 // Check validity of date 300 public boolean checkInputValidity() { 301 return this.day.getMonth().getYear().validate() && this.day.getMonth().validate() && this.day.validate(); 302 } 303 304 // Compare two dates 305 public boolean compareDates(DateUtil date) { 306 boolean rtn = false; 307 if (this.day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue()) { 308 rtn = true; 309 } else if (this.day.getMonth().getYear().getValue() > date.getDay().getMonth().getYear().getValue()) { 310 if (this.day.getMonth().getValue() > date.getDay().getMonth().getValue()) { 311 rtn = true; 312 } else if (this.day.getMonth().getValue() == date.getDay().getMonth().getValue()) { 313 if (this.day.getValue() >= date.getDay().getValue()) { 314 rtn = true; 315 } 316 } 317 } 318 return rtn; 319 } 320 321 // Judge whether two dates are equal 322 public boolean equalTwoDates(DateUtil date) { 323 if (this.day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() 324 && this.day.getMonth().getValue() == date.getDay().getMonth().getValue() 325 && this.day.getValue() == date.getDay().getValue()) { 326 return true; 327 } 328 return false; 329 } 330 331 // output date 332 public String showDate() { 333 return this.day.getMonth().getYear().getValue() + "-" + this.day.getMonth().getValue() + "-" 334 + this.day.getValue(); 335 } 336 337 // get next n days 338 public DateUtil getNextNDays(int n) { 339 for (int i = 0; i < n; i++) { 340 this.day.dayIncrement(); 341 } 342 return this; 343 } 344 345 // get previous n days 346 public DateUtil getPreviousNDays(int n) { 347 for (int i = 0; i < n; i++) { 348 this.day.dayReduction(); 349 } 350 return this; 351 } 352 353 // get days between two dates 354 public int getDaysofDates(DateUtil date) { 355 int days = 0; 356 357 if (compareDates(date)) { 358 while (!equalTwoDates(date)) { 359 date = date.getNextNDays(1); 360 days++; 361 } 362 } else { 363 while (!equalTwoDates(date)) { 364 date = date.getPreviousNDays(1); 365 days++; 366 } 367 } 368 369 return days; 370 } 371 }
-
题目集5(7-4)
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 year = 0; 7 int month = 0; 8 int day = 0; 9 10 int choice = input.nextInt(); 11 12 if (choice == 1) { // test getNextNDays method 13 int m = 0; 14 year = Integer.parseInt(input.next()); 15 month = Integer.parseInt(input.next()); 16 day = Integer.parseInt(input.next()); 17 18 DateUtil date = new DateUtil(year, month, day); 19 20 if (!date.checkInputValidity()) { 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 25 m = input.nextInt(); 26 27 if (m < 0) { 28 System.out.println("Wrong Format"); 29 System.exit(0); 30 } 31 32 System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); 33 System.out.println(date.getNextNDays(m).showDate()); 34 } else if (choice == 2) { // test getPreviousNDays method 35 int n = 0; 36 year = Integer.parseInt(input.next()); 37 month = Integer.parseInt(input.next()); 38 day = Integer.parseInt(input.next()); 39 40 DateUtil date = new DateUtil(year, month, day); 41 42 if (!date.checkInputValidity()) { 43 System.out.println("Wrong Format"); 44 System.exit(0); 45 } 46 47 n = input.nextInt(); 48 49 if (n < 0) { 50 System.out.println("Wrong Format"); 51 System.exit(0); 52 } 53 54 System.out.print( 55 date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); 56 System.out.println(date.getPreviousNDays(n).showDate()); 57 } else if (choice == 3) { //test getDaysofDates method 58 year = Integer.parseInt(input.next()); 59 month = Integer.parseInt(input.next()); 60 day = Integer.parseInt(input.next()); 61 62 int anotherYear = Integer.parseInt(input.next()); 63 int anotherMonth = Integer.parseInt(input.next()); 64 int anotherDay = Integer.parseInt(input.next()); 65 66 DateUtil fromDate = new DateUtil(year, month, day); 67 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 68 69 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 70 System.out.println("The days between " + fromDate.showDate() + 71 " and " + toDate.showDate() + " are:" 72 + fromDate.getDaysofDates(toDate)); 73 } else { 74 System.out.println("Wrong Format"); 75 System.exit(0); 76 } 77 } 78 else{ 79 System.out.println("Wrong Format"); 80 System.exit(0); 81 } 82 } 83 } 84 85 class DateUtil { 86 private int year; 87 private int month; 88 private int day; 89 90 public DateUtil(int year, int month, int day) { 91 this.year = year; 92 this.month = month; 93 this.day = day; 94 } 95 96 public DateUtil(){} 97 98 public void setYear(int year) { 99 this.year = year; 100 } 101 102 public void setMonth(int month) { 103 this.month = month; 104 } 105 106 public void setDay(int day) { 107 this.day = day; 108 } 109 110 public int getYear() { 111 return year; 112 } 113 114 public int getMonth() { 115 return month; 116 } 117 118 public int getDay() { 119 return day; 120 } 121 122 private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 123 124 private int getDayOfMonth(int year, int month) { 125 int days = DAY_OF_MONTH[month - 1]; 126 if (month == 2 && isLeapYear(year)) { 127 days = 29; 128 } 129 return days; 130 } 131 132 public boolean checkInputValidity()//检测输入的年、月、日是否合法 133 { 134 if (year < 1820 || year > 2020) return false; 135 if (month < 1 || month > 12) return false; 136 // int _day = this.getDayOfMonth(year, month); 137 // return day <= _day; 138 return day >= 1 && day <= 31; 139 } 140 141 public boolean isLeapYear(int year)//判断year是否为闰年 142 { 143 return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; 144 } 145 146 147 public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期 148 { 149 int year = this.year; 150 int month = this.month; 151 int day = this.day; 152 // day = Math.min(day, this.getDayOfMonth(year, month)); 153 for (int i = 0; i < n; i++) { 154 day++; 155 if (day > getDayOfMonth(year, month)) { 156 day = 1; 157 month++; 158 if (month > 12) { 159 month = 1; 160 year++; 161 } 162 } 163 } 164 return new DateUtil(year, month, day); 165 } 166 167 public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期 168 { 169 int year = this.year; 170 int month = this.month; 171 int day = this.day; 172 for (int i = 0; i < n; i++) { 173 day--; 174 while (day < 1) { 175 month--; 176 if (month < 1) { 177 month = 12; 178 year--; 179 } 180 day += getDayOfMonth(year, month); 181 } 182 } 183 return new DateUtil(year, month, day); 184 } 185 186 public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后) 187 { 188 if (this.year > date.year) return true; 189 if (this.year == date.year) { 190 if (this.month > date.month) return true; 191 if (this.month == date.month) { 192 if (this.day >= date.day) return true; 193 } 194 } 195 return false; 196 } 197 198 public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等 199 { 200 if (date != null) { 201 if (year == date.year && month == date.month && day == date.day) { 202 return true; 203 } 204 } 205 return false; 206 } 207 208 private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 209 public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数 210 { 211 DateUtil dateUtil1 = this; // 小 212 DateUtil dateUtil2 = date; // 大 213 if (this.compareDates(date)) { 214 dateUtil1 = date; 215 dateUtil2 = this; 216 } 217 218 int days; 219 int leapYearNum = 0; 220 for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) { 221 if (isLeapYear(i)) { 222 leapYearNum++; 223 } 224 } 225 226 days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum; 227 228 int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0); 229 int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0); 230 return days - d1 + d2; 231 } 232 233 public String showDate()//以“year-month-day”格式返回日期值 234 { 235 return year + "-" + month + "-" + day; 236 } 237 }
接下来比较分析两种不同的日期聚合类。
先是第一波代码的SourceMonitor的生成报表
一下是第二波代码的分析图
- 首先可以直观地从行数上看到第二次的代码行数明显少于第一次带啊行数
- 其次通过分析表可以看到第二次代码的平均复杂度高于第一次代码,但第一次代码的调用次数(127)明显高于第二次(54),大概是因为第一次的代码疯狂套娃,访问一个天数需要把所用的存日期的类都调用一遍
- 第一次代码访问天数过于繁琐,访问天数需要层层调用,类的设计容错性低,第二次的代码访问天数比较简单,也易于修改。但第一次代码的数据保密性好(个人猜测)
-
题目集4(7-3)
1 //package 图形继承; 2 3 import java.util.Scanner; 4 public class Main { 5 6 public static void main(String[] args) { 7 Scanner scan = new Scanner(System.in); 8 int choice = scan.nextInt(); 9 if(choice >4 || choice<1) { 10 System.out.println("Wrong Format"); 11 return; 12 } 13 switch(choice){ 14 case 1: 15 double r = scan.nextDouble(); 16 if(r<0) { 17 System.out.println("Wrong Format"); 18 break; 19 } 20 Circle c = new Circle(); 21 c.setRadius(r); 22 System.out.println("Circle's area:"+String.format("%.2f", c.getArea())); 23 break; 24 case 2: 25 double width = scan.nextDouble(); 26 double length = scan.nextDouble(); 27 if(width<0 || length <0) { 28 System.out.println("Wrong Format"); 29 return; 30 } 31 Rectangle rec = new Rectangle(); 32 rec.setWidth(width); 33 rec.setLength(length); 34 System.out.println("Rectangle's area:"+String.format("%.2f",rec.getArea() )); 35 break; 36 case 3: 37 double radius = scan.nextDouble(); 38 if(radius<0) { 39 System.out.println("Wrong Format"); 40 break; 41 } 42 Ball b = new Ball(); 43 b.setRadius(radius); 44 System.out.println("Ball's surface area:"+String.format("%.2f",b.getArea())); 45 System.out.println("Ball's volume:"+String.format("%.2f",b.getVolume())); 46 break; 47 case 4: 48 double width2 = scan.nextDouble(); 49 double length2 = scan.nextDouble(); 50 double height = scan.nextDouble(); 51 if(width2<0 || length2 <0 || height<0) { 52 System.out.println("Wrong Format"); 53 return; 54 } 55 Box box = new Box(); 56 box.setWidth(width2); 57 box.setLength(length2); 58 box.setHeight(height); 59 System.out.println("Box's surface area:"+String.format("%.2f",box.getArea())); 60 System.out.println("Box's volume:"+String.format("%.2f",box.getVolume())); 61 62 } 63 64 } 65 66 } 67 68 class Shape{ 69 Shape(){ 70 System.out.println("Constructing Shape"); 71 } 72 //public void show(){System.out.println("Constructing Shape2");} 73 public double getArea() { 74 return 0.0; 75 } 76 } 77 78 class Circle extends Shape{ 79 private double radius; 80 Circle(){ 81 System.out.println("Constructing Circle"); 82 } 83 public double getArea() { 84 return radius*radius*Math.PI; 85 } 86 public void setRadius(double radius) { 87 this.radius = radius; 88 } 89 public double getRadius() { 90 return radius; 91 } 92 } 93 94 class Rectangle extends Shape{ 95 private double width; 96 private double length; 97 Rectangle(){ 98 System.out.println("Constructing Rectangle"); 99 } 100 public double getArea() { 101 return width*length; 102 } 103 public void setLength(double length) { 104 this.length = length; 105 } 106 public void setWidth(double width) { 107 this.width = width; 108 } 109 public void getLength(double length) { 110 this.length = length; 111 } 112 public double getWidth() { 113 return width; 114 } 115 public double getLength() { 116 return length; 117 } 118 } 119 120 class Ball extends Circle{ 121 Ball(){ 122 System.out.println("Constructing Ball"); 123 } 124 public double getArea() { 125 return 4*Math.PI*getRadius()*getRadius(); //也可return 4*super.getArea(); 126 } 127 public double getVolume(){ 128 return (getArea()/3)*getRadius(); 129 } 130 } 131 class Box extends Rectangle{ 132 private double height; 133 Box(){ 134 System.out.println("Constructing Box"); 135 } 136 public void setHeight(double height) { 137 this.height = height; 138 } 139 public double getHeight() { 140 return height; 141 } 142 public double getVolume() { 143 //都是父类的私有属性用不了 return width*length*height; 144 return super.getArea()*getHeight(); //用height也行 145 } 146 @Override 147 public double getArea() { 148 return 2*(getWidth()*getLength()+getWidth()*getHeight()+getLength()*getHeight()); 149 } 150 }
-
题目集6(7-5)
1 import java.util.*; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner scan = new Scanner(System.in); 8 int a = scan.nextInt(); 9 int b = scan.nextInt(); 10 int c = scan.nextInt(); 11 if(a<0 || b<0 || c<0) { 12 System.out.println("Wrong Format"); 13 } 14 else 15 { 16 17 Circle cir[] = new Circle[a]; 18 Rectangle r[] = new Rectangle[b]; 19 Triangle t[] = new Triangle[c]; 20 ArrayList<Double> Area = new ArrayList<Double>(); 21 22 for(int i =0; i<a; i++) { 23 double circleRadius = scan.nextDouble(); // 24 cir[i] = new Circle(circleRadius); //这两句可以写成从c[i]=new Circle(scan.nextDouble()); 25 if(cir[i].validate()==false) { 26 System.out.println("Wrong Format"); 27 System.exit(0); 28 } 29 Area.add(cir[i].getArea()); 30 } 31 for(int i =0; i<b; i++) { 32 double length = scan.nextDouble(); 33 double width = scan.nextDouble(); // 34 r[i] = new Rectangle(length,width); //这两句可以写成从c[i]=new Circle(scan.nextDouble()); 35 if(r[i].validate()==false) { 36 System.out.println("Wrong Format"); 37 System.exit(0); 38 } 39 Area.add(r[i].getArea()); 40 } 41 for(int i=0; i<c; i++) { 42 double side1 = scan.nextDouble(); 43 double side2 = scan.nextDouble(); 44 double side3 = scan.nextDouble(); 45 t[i] = new Triangle(side1,side2,side3); 46 if(t[i].validate() == false) { 47 System.out.println("Wrong Format"); 48 System.exit(0); 49 } 50 Area.add(t[i].getArea()); 51 } 52 //1 53 System.out.println("Original area:"); 54 //2 55 for(double area : Area) { 56 System.out.printf("%.2f ",area); 57 } 58 //3 59 System.out.println(); 60 System.out.print("Sum of area:"); 61 System.out.printf("%.2f\n",getSumArea(Area)); 62 63 //排序后的 64 Collections.sort(Area); 65 //4 66 System.out.println("Sorted area:"); 67 //5 68 for(double area : Area) { 69 System.out.printf("%.2f ",area); 70 } 71 //6 72 System.out.println(); 73 System.out.printf("Sum of area:%.2f\n",getSumArea(Area)); 74 } 75 scan.close(); 76 } 77 78 public static double getSumArea(ArrayList <Double> Area) { 79 double sum = 0; 80 for(double area : Area) { 81 sum+=area; 82 } 83 return sum; 84 } 85 } 86 abstract class Shape{ 87 abstract double getArea(); 88 abstract boolean validate(); 89 public abstract String toString(); 90 } 91 92 class Circle extends Shape{ 93 double radius; 94 Circle(double radius){ 95 this.radius = radius; 96 } 97 void setRadius(double radius) { 98 this.radius = radius; 99 } 100 double getRadius() { 101 return radius; 102 } 103 @Override 104 double getArea() { 105 return Math.PI*radius*radius; 106 } 107 @Override 108 boolean validate() { 109 if(radius>0) 110 return true; 111 else 112 return false; 113 } 114 @Override 115 public String toString() { 116 return null; 117 } 118 } 119 120 class Rectangle extends Shape{ 121 double length; 122 double width; 123 Rectangle(double length, double width){ 124 this.length = length; 125 this.width = width; 126 } 127 @Override 128 double getArea() { 129 return length * width; 130 } 131 132 @Override 133 boolean validate() { 134 if(width > 0 && length >0) 135 return true; 136 else 137 return false; 138 } 139 140 @Override 141 public String toString() { 142 return null; 143 } 144 } 145 146 class Triangle extends Shape{ 147 double side1; 148 double side2; 149 double side3; 150 Triangle(double side1, double side2,double side3){ 151 this.side1 = side1; 152 this.side2 = side2; 153 this.side3 = side3; 154 } 155 156 157 @Override 158 double getArea() { 159 // TODO Auto-generated method stub 160 double p = (side1+side2+side3)/2; 161 return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); 162 } 163 @Override 164 boolean validate() { 165 // TODO Auto-generated method stub 166 if(side1>0 && side2>0 && side3>0 167 && side1+side2>side3 168 && side2+side3>side1 169 && side1+side3>side2) 170 return true; 171 else 172 return false; 173 } 174 @Override 175 public String toString() { 176 // TODO Auto-generated method stub 177 return null; 178 } 179 }
-
题目集6(7-6)
1 //package 实现图形接口及多态性; 2 3 import java.util.Scanner;; 4 5 public class Main { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner scan = new Scanner(System.in); 10 double radius = scan.nextDouble(); 11 double width = scan.nextDouble(); 12 double length = scan.nextDouble(); 13 /* 14 Circle c = new Circle(radius); 15 Rectangle r = new Rectangle(width,length); 16 */ 17 Circle c = new Circle(); 18 Rectangle r = new Rectangle(); 19 c.setRadius(radius); 20 r.setLength(length); 21 r.setWidth(width); 22 if(radius<=0 || width <=0 || length <=0) { 23 System.out.println("Wrong Format"); 24 } 25 else { 26 System.out.println(String.format("%.2f",c.getArea())); 27 System.out.println(String.format("%.2f",r.getArea())); 28 } 29 scan.close(); 30 } 31 32 } 33 interface GetArea{ 34 public double getArea(); 35 } 36 class Circle{ 37 private double radius; 38 Circle(){ 39 40 } 41 Circle(double radius){ 42 this.radius = radius; 43 44 } 45 public double getArea() { 46 return radius*radius*Math.PI; 47 } 48 public void setRadius(double radius) { 49 this.radius = radius; 50 } 51 public double getRadius() { 52 return radius; 53 } 54 } 55 class Rectangle { 56 private double width; 57 private double length; 58 Rectangle(){ 59 60 } 61 Rectangle(double width, double length){ 62 this.length = length; 63 this.width = width; 64 65 } 66 public double getArea() { 67 return width*length; 68 } 69 public void setLength(double length) { 70 this.length = length; 71 } 72 public void setWidth(double width) { 73 this.width = width; 74 } 75 public void getLength(double length) { 76 this.length = length; 77 } 78 public double getWidth() { 79 return width; 80 } 81 public double getLength() { 82 return length; 83 } 84 }
下面是对这三段代码的分析表
1.图形继承
2.图形继承与多态
3.实现图形接口及多态性
- 可以看出,三次代码的复杂度逐步上升,但第三次的代码量却明显少于前两次,可能是接口增加了代码复杂度?
- 第一次题目没啥花里胡哨的要求,就是需要注意有个无参的构造方法输出 “Constructing Shape”
- 第二次作业的排序用到了ArrayList里的Collections.sort();,这里只需要传入一个对象数组名作为参数,使用默认的排序方式即可
- 第三次作业多了接口,进一步抽象了
题目集5(7-4)中Java集合框架应用的分析总结
- 读取输入代码中的每一行,将该行split为字符串数组,逐个判断是否为关键字
- 还要判断判断是否属于注释,若属于则跳过,若不属于则进行关键字筛查
- 涉及了HashMap的按值排序的思路.
- 代码的分割与空格判断比较繁琐
三、踩坑心得
-
题目集6的7-5打印有一些坑,需要用printf打印,用println如下:
原因大概是println不支持double型数组(当然也应该包括其他型的)的字符串打印。
-
ArrayList重名问题
- ArrayList拼写错误,这就是定义名称过简单省略的坏处
3.print打印同println
4.getSumArea需使用静态方法
5.最好给输出标个号,否则......
四、改进建议
- 题目集6的正则表达式前几题的测试点太水了,对于检测学号录入正确性那题,我一开始写错了,连测试样例都没过,但测试点全通过了,错误不易察觉,可以适当改进测试点(为学弟学妹们造福)
- 少一点正则表达式的检验吧,比如水文那道题,直接不想写了,当然实力也是制约因素五
五、总结
通过这三次的作业学到了很多,例如继承、多态还有接口的组合、ArrayList的一写基本语法,也进一步巩固了正则表达式,收获颇丰,感谢老师的辛勤付出,劳动节快乐。
标签:总结性,题目,int,month,Blog,year,date,public,day 来源: https://www.cnblogs.com/InquireRoad/p/14723890.html