博客第一次作业
目录:
(1)前言
(2)设计与分析
(3)改进建议
(4)总结
前言:
(1)前言关于接触C之后接触java自己的一些想法和感触
物联网专业学习java的时间很晚,大三学期才开始接触java和面向对像方面的编程,与原本学习的面向过程C语言有很大不同,对于我们这样已经习惯了面向过程的程序设计的人来说,突然变为面向对象大家和我一样或多或少都会出现茫然地情况,不知从何下手,不知何为面向对象,何为面向过程,由于对两者没有一个清楚的认知,导致大家普遍都存在使用面向程序的语言进行面向对象语言的编写,我也不例外,所以在正式开始之前我想要和对于刚刚接触的java语言的好哥么们说一声如何正确的区分我们java语言与C语言。总的来说,三次题目的内容逐步渐进,不会花过多的时间在同一类型的题目存在,起到的是一个典例的作用,比如第一次作业涉及到类思想的题目占了比较多,但实际上用面向过程的C语言也能很方便的写出来,主要是介绍了各个函数的作用以及java内容的格式,更主要的是建立基础的过程,在这方面如果有C语言类的基础相信很快就能理解;第二次作业内容就更加注重于java几个十分重要的类方法啊的使用,比如Scanner 和getchar 正则表达式等内容,对第三次乃至之后的学习起到了牢固的基石作用;第三次作业内容难度明显大幅度上升,是对之前的内容进行整合和运用,完全和之前不是一个档次的,当时看到的难度就联想到一句话“先定个小目标,现写他个600行!”(战术后仰),即使是很难但也最后好在还是坚持下去了,在其他人还在面向过程时我已经进一步的接受并开始运用面向对象的编程。很喜欢我的老师在上课刚开始的时候举得一个例子——班级郊游,在这里卖一个关子,如果大家对这个故事感兴趣的话请大家务必阅读到最后,如果各位小萌新们愿意耐下心来好好阅读并感悟接下来的内容,“卷死”同学,本片blog一定会对各位的学习起到很大的帮助!是兄弟,和坤坤一起学Java!
(2)设计与分析:
第一次作业:
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String []args){ 4 Scanner input = new Scanner(System.in); 5 String xuehao = input.next(); 6 if(xuehao.length() != 8) 7 System.out.println("Wrong Format"); 8 else 9 depart(xuehao); 10 } 11 public static void depart(String xuehao) {//学号拆分 12 String ruxue_time = xuehao.substring(0,2); 13 String xueyuan = xuehao.substring(2,4); 14 String class_num = xuehao.substring(4,6); 15 String xuehao_num = xuehao.substring(6,8); 16 int xueyuan_num = Integer.parseInt(xueyuan); 17 if(xueyuan_num != 1&&xueyuan_num != 2&&xueyuan_num != 3&&xueyuan_num != 20) 18 System.out.println("Wrong Format"); 19 else { 20 ruxue_time_all(ruxue_time); 21 xueyuan_all(xueyuan); 22 class_num_all(class_num); 23 xuehao_num_all(xuehao_num); 24 } 25 } 26 public static void ruxue_time_all(String ruxue_time) {//入学时间 27 System.out.println("入学年份:20" + ruxue_time + "年"); 28 } 29 public static void xueyuan_all(String xueyuan) {//学院 30 switch(Integer.parseInt(xueyuan)) {//强制类型转换 31 case 01:System.out.println("学院:材料学院");break; 32 case 02:System.out.println("学院:机械学院");break; 33 case 03:System.out.println("学院:外语学院");break; 34 case 20:System.out.println("学院:软件学院");break; 35 } 36 } 37 public static void class_num_all(String class_num) {//班级号 38 System.out.println("班级:" + class_num); 39 } 40 public static void xuehao_num_all(String xuehao_num) {//最后两位学号 41 System.out.print("学号:" + xuehao_num); 42 } 43 }
以上是我的第一次作业书写的java代码,可以很清楚的看到我所使用的依旧是面向程序的思路,完全没有使用类和面向对象的方法,当时的我也不太了解面向对象有何意义,对代码的掌握也不熟悉,也不明白为什么调用一个读取用户输入为啥会这么麻烦,一个printf()到了这里居然要加上那么一大长串的System.out.而且当时我对System.out.println();到底是啥意思,一直很纳闷,后来翻阅资料才明白ptint(不换行直接在同一行输入),printf(不换行直接在同一行输入并且类似C语言一样接受属性),println(换行在下一行进行输入),也不清楚啥是类,就像C语言的函数一样自己疯狂的定义定义定义,当时感觉就是在写C一样,过于简单在此就不多赘述啦233
(3)今天主要对以下四个题目进行分析分别是:题目集2的7-2以及题目集3的7-1、7-2、7-3(四座大山!)
第一座大山:题目集2的7-2
代码如下:

1 import java.util.Scanner; 2 public class Main { 3 public static void main(String []args){ 4 Scanner input = new Scanner(System.in); 5 String secrat = input.next(); 6 int count = 1; 7 if(secrat.length() < 11) { 8 System.out.println("null data"); 9 return ; 10 } 11 for(int i = 0 ; i < secrat.length() ; i++) { 12 if(secrat.charAt(i) == '1') 13 continue; 14 else { 15 if(count != 1) 16 System.out.print("\n"); 17 if(secrat.charAt(i+9) != '1'&&secrat.charAt(i+10) != '1') { 18 System.out.print(count + ":validate error"); 19 i = i+10; 20 count++; 21 } 22 else if(secrat.charAt(i+9) != '1') { 23 System.out.print(count + ":parity check error"); 24 i = i+10; 25 count++; 26 } 27 else if(secrat.charAt(i+10) != '1') { 28 System.out.print(count + ":validate error"); 29 i = i+10; 30 count++; 31 } 32 else { 33 System.out.print(count + ":" + secrat.substring(i+1, i+9)); 34 i = i+10; 35 if(i+10 < secrat.length()) { 36 count++; 37 continue; 38 }else 39 return; 40 } 41 if(i+10 < secrat.length()) 42 continue; 43 else 44 return; 45 } 46 } 47 if(count == 1) 48 System.out.println("null data"); 49 } 50 }
解释和心得:
如果说上一道题目是海平面,那么这一题就可以算是我们南昌的小“梅岭了”,从这题开始逐步接近对于字符串的处理,都是之前接触很少不太了解的方面的一些东西,charAt之类的函数更是见都没见过,看以看出自己的知识储备已经是十分的欠缺了,主要是考验对于字符串的处理于运用,相较于之后的峨眉大山和珠穆朗玛峰,此题还是比较简单明了的,也基本没有使用类的一些方法,相信聪明的大家看完之后大多数有些C语言基础的小“萌新”们都能够轻松的看懂啦!
第二座大山:题目集3的7-1
代码如下:
import java.util.Scanner; class Dot_massage {//点的信息 double x; double y; public Dot_massage(){ } public Dot_massage(double x,double y){//构建点 this.x = x; this.y = y; } public double return_long(Dot_massage dot1) {//返回两点之间的距离 return Math.sqrt((this.x-dot1.x)*(this.x-dot1.x)+(this.y-dot1.y)*(this.y-dot1.y)); } } public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); Dot_massage Dot1 = new Dot_massage(); Dot_massage Dot2 = new Dot_massage(); String l = input.nextLine(); String point[] = l.split(" "); String num[] = null; for(String i:point) { num = i.split(","); for(String j:num) { //正则表达式 if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) { System.out.print("Wrong Format"); System.exit(0); } } } if(point.length !=2 ) { System.out.print("wrong number of points"); System.exit(0); } num = point[0].split(",");//分离 Dot1.x = Double.valueOf(num[0]); Dot1.y = Double.valueOf(num[1]); num = point[1].split(","); Dot2.x = Double.valueOf(num[0]); Dot2.y = Double.valueOf(num[1]); if(Dot1.x==Dot2.x&&Dot1.y==Dot2.y){ System.out.print("Wrong Format"); return; } System.out.print(Dot1.return_long(Dot2)); } }
解释和心得:
此题难度主要体现在正则表达式当中,以及正则表达式相关函数的应用,其他方面并无难点,正则表达式可以解决许许多多因为格式错误而出现的问题,是非常方便的一个工具,也是成为一名java大神的必修课,希望大家好好阅读并学习于正则表达式相关内容,当然也包括数据的类型转换,就比如上面所使用的Double.valueOf()就是把一个字符串强制转换为double类型数据,是一个相当实用的方法!
第三座大山:题目集3的7-2
代码如下:

1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args){ 4 Scanner in = new Scanner(System.in); 5 String row = in.nextLine() + " "; 6 String[] co; 7 String regexL1 = "\\d\\:(([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){0,1}"; 8 String regex1 = "\\d\\:(([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){2}"; 9 String regexF1 = "\\d\\:(([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){3,}"; 10 String regexL2 = "\\d\\:(([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){0,2}"; 11 String regex2 = "\\d\\:(([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){3}"; 12 String regexF2 = "\\d\\:(([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+|-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){4,}"; 13 String regexL3 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){0,3}"; 14 String regex3 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){4}"; 15 String regexF3 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){5,}"; 16 17 switch (row.substring(0,1)) { 18 case "1": if (row.matches(regex1)) { 19 co = row.split("[\\s\\,\\:]+"); 20 double x1 = Double.parseDouble(co[1]); 21 double y1 = Double.parseDouble(co[2]); 22 double x2 = Double.parseDouble(co[3]); 23 double y2 = Double.parseDouble(co[4]); 24 Slope slope = new Slope(x1,y1,x2,y2); 25 slope.toStr(); 26 }else if (row.matches(regexF1) || row.matches(regexL1)) { 27 System.out.println("wrong number of points"); 28 System.exit(0); 29 } else { 30 System.out.println("Wrong Format"); 31 System.exit(0); 32 }break; 33 case "2":if (row.matches(regex2)) { 34 co = row.split("[\\s\\,\\:]+"); 35 double x1 = Double.parseDouble(co[1]); 36 double y1 = Double.parseDouble(co[2]); 37 double x2 = Double.parseDouble(co[3]); 38 double y2 = Double.parseDouble(co[4]); 39 double x3 = Double.parseDouble(co[5]); 40 double y3 = Double.parseDouble(co[6]); 41 Distance distance = new Distance(x1,y1,x2,y2,x3,y3); 42 distance.toStr(); 43 }else if (row.matches(regexF2) || row.matches(regexL2)) { 44 System.out.println("wrong number of points"); 45 System.exit(0); 46 } else { 47 System.out.println("Wrong Format"); 48 System.exit(0); 49 }break; 50 case "3":if (row.matches(regex2)) { 51 co = row.split("[\\s\\,\\:]"); 52 double x1 = Double.parseDouble(co[1]); 53 double y1 = Double.parseDouble(co[2]); 54 double x2 = Double.parseDouble(co[3]); 55 double y2 = Double.parseDouble(co[4]); 56 double x3 = Double.parseDouble(co[5]); 57 double y3 = Double.parseDouble(co[6]); 58 Inoneline inOneLine = new Inoneline(x1,y1,x2,y2,x3,y3); 59 inOneLine.toStr(); 60 }else if (row.matches(regexF2) || row.matches(regexL2)) { 61 System.out.println("wrong number of points"); 62 System.exit(0); 63 } else { 64 System.out.println("Wrong Format"); 65 System.exit(0); 66 }break; 67 case "4":if (row.matches(regex3)) { 68 co = row.split("[\\s\\,\\:]+"); 69 double x1 = Double.parseDouble(co[1]); 70 double y1 = Double.parseDouble(co[2]); 71 double x2 = Double.parseDouble(co[3]); 72 double y2 = Double.parseDouble(co[4]); 73 double x3 = Double.parseDouble(co[5]); 74 double y3 = Double.parseDouble(co[6]); 75 double x4 = Double.parseDouble(co[7]); 76 double y4 = Double.parseDouble(co[8]); 77 Parallel parallel = new Parallel(x1,y1,x2,y2,x3,y3,x4,y4); 78 parallel.toStr(); 79 }else if (row.matches(regexF3) || row.matches(regexL3)) { 80 System.out.println("wrong number of points"); 81 System.exit(0); 82 } else { 83 System.out.println("Wrong Format"); 84 System.exit(0); 85 }break; 86 case "5":if (row.matches(regex3)) { 87 co = row.split("[\\s\\,\\:]+"); 88 double x1 = Double.parseDouble(co[1]); 89 double y1 = Double.parseDouble(co[2]); 90 double x2 = Double.parseDouble(co[3]); 91 double y2 = Double.parseDouble(co[4]); 92 double x3 = Double.parseDouble(co[5]); 93 double y3 = Double.parseDouble(co[6]); 94 double x4 = Double.parseDouble(co[7]); 95 double y4 = Double.parseDouble(co[8]); 96 Coordinatesofintersection coordinate = new Coordinatesofintersection(x1,y1,x2,y2,x3,y3,x4,y4); 97 coordinate.toStr(); 98 } else if (row.matches(regexF3) || row.matches(regexL3)) { 99 System.out.println("wrong number of points"); 100 System.exit(0); 101 } else { 102 System.out.println("Wrong Format"); 103 System.exit(0); 104 }break; 105 default:System.out.println("Wrong Format"); 106 System.exit(0); 107 } 108 } 109 } 110 111 //1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。 112 class Slope { 113 private double x1; 114 private double x2; 115 private double y1; 116 private double y2; 117 118 public Slope() { 119 } 120 121 public Slope(double x1, double y1, double x2, double y2) { 122 this.x1 = x1; 123 this.x2 = x2; 124 this.y1 = y1; 125 this.y2 = y2; 126 } 127 128 public double getX1() { 129 return x1; 130 } 131 132 public void setX1(double x1) { 133 this.x1 = x1; 134 } 135 136 public double getX2() { 137 return x2; 138 } 139 140 public void setX2(double x2) { 141 this.x2 = x2; 142 } 143 public double getY1() { 144 return y1; 145 } 146 147 public void setY1(double y1) { 148 this.y1 = y1; 149 } 150 151 public double getY2() { 152 return y2; 153 } 154 155 public void setY2(double y2) { 156 this.y2 = y2; 157 } 158 159 public double dealSlope() { 160 return (y1 - y2) / (x1 - x2); 161 } 162 163 public void toStr() { 164 if ((x1 == x2) && (y1 == y2)) { 165 System.out.println("points coincide"); 166 } else if(x1 == x2){ 167 System.out.println("Slope does not exist"); 168 }else { 169 System.out.println(dealSlope()); 170 } 171 } 172 } 173 //2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。 174 class Distance { 175 private double x1; 176 private double x2; 177 private double y1; 178 private double y2; 179 private double x3; 180 private double y3; 181 182 public Distance(){} 183 184 public Distance(double x1,double y1,double x2,double y2,double x3,double y3) { 185 this.x1 = x1; 186 this.x2 = x2; 187 this.y1 = y1; 188 this.y2 = y2; 189 this.x3 = x3; 190 this.y3 = y3; 191 } 192 193 public double getX1() { 194 return x1; 195 } 196 197 public void setX1(double x1) { 198 this.x1 = x1; 199 } 200 201 public double getX2() { 202 return x2; 203 } 204 205 public void setX2(double x2) { 206 this.x2 = x2; 207 } 208 209 public double getY1() { 210 return y1; 211 } 212 213 public void setY1(double y1) { 214 this.y1 = y1; 215 } 216 217 public double getY2() { 218 return y2; 219 } 220 221 public void setY2(double y2) { 222 this.y2 = y2; 223 } 224 225 public double getX3() { 226 return x3; 227 } 228 229 public void setX3(double x3) { 230 this.x3 = x3; 231 } 232 233 public double getY3() { 234 return y3; 235 } 236 237 public void setY3(double y3) { 238 this.y3 = y3; 239 } 240 241 public boolean validate() { 242 return !(y3 == y2 && x2 == x3); 243 } 244 245 public double dealDistance() { 246 return Math.abs((x3-x2) * y1-(y3-y2) * x1+x2 * y3 - y2 * x3) / Math.sqrt(Math.pow(x3 - x2,2)+ Math.pow(y3 - y2,2)); 247 } 248 249 public void toStr() { 250 if (validate()) { 251 System.out.println(dealDistance()); 252 } else { 253 System.out.println("points coincide"); 254 } 255 } 256 } 257 258 class Inoneline { 259 private double x1; 260 private double x2; 261 private double y1; 262 private double y2; 263 private double x3; 264 private double y3; 265 266 Inoneline() {} 267 268 Inoneline(double x1,double y1,double x2,double y2,double x3,double y3) { 269 this.x1 = x1; 270 this.x2 = x2; 271 this.y1 = y1; 272 this.y2 = y2; 273 this.x3 = x3; 274 this.y3 = y3; 275 } 276 277 public boolean dealInOneLine() { 278 return ((x2 - x1) * (y3 - y2) == (x3 - x2) * (y2 - y1)); 279 } 280 281 public void toStr() { 282 if ((x1 == x2 && y1 == y2) && (x3 == x1 && y3 == y1)) { 283 System.out.println("points coincide"); 284 } else 285 System.out.println(dealInOneLine()); 286 } 287 288 } 289 290 class Parallel { 291 private double x1; 292 private double x2; 293 private double y1; 294 private double y2; 295 private double x3; 296 private double y3; 297 private double x4; 298 private double y4; 299 300 Parallel() {} 301 302 Parallel(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { 303 this.x1 = x1; 304 this.x2 = x2; 305 this.y1 = y1; 306 this.y2 = y2; 307 this.x3 = x3; 308 this.y3 = y3; 309 this.x4 = x4; 310 this.y4 = y4; 311 } 312 313 public boolean parallel() { 314 return ((x2 - x1) * (y3 - y4)== (x3 - x4) * (y2 - y1)); 315 } 316 317 public void toStr() { 318 if ((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) { 319 System.out.println("points coincide"); 320 } else { 321 System.out.println(parallel()); 322 } 323 } 324 325 } 326 //5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",", 327 //并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。 328 // 若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。 329 class Coordinatesofintersection { 330 private double x1; 331 private double x2; 332 private double y1; 333 private double y2; 334 private double x3; 335 private double y3; 336 private double x4; 337 private double y4; 338 private double x0; 339 private double y0; 340 341 Coordinatesofintersection() {} 342 343 Coordinatesofintersection(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { 344 this.x1 = x1; 345 this.x2 = x2; 346 this.y1 = y1; 347 this.y2 = y2; 348 this.x3 = x3; 349 this.y3 = y3; 350 this.x4 = x4; 351 this.y4 = y4; 352 } 353 354 public boolean isParallel() { 355 return ((x2 - x1) * (y4 - y3)== (x4 - x3) * (y2 - y1)) || (x1 == x2 && x3 == x4); 356 } 357 358 public void dealCoordinatesOfIntersection() { 359 y0 = ((x4 * y3 - x3 * y4) * (y1 - y2) - (x2 * y1 - x1 * y2) * (y3 - y4)) / ((x1 - x2) * (y3 - y4) - (x3 - x4) * (y1 - y2)); 360 x0 = ((x2 - x1) * (x4 * y3 - x3 * y4) - (x4 - x3) * (x2 * y1 - x1 * y2)) / ((y2 - y1) * (x4 - x3) - (y4 - y3) * (x2 - x1)) ; 361 } 362 363 public boolean isIn() { 364 return (x1 - x0) * (x0 - x2) + (y1- y0) * (y0 - y2) > 0 || (x3 - x0) * (x0 - x4) + (y3 - y0) * (y0 - y4) > 0; 365 } 366 367 public void toStr() { 368 if (x1 == x2 && y1 == y2 || x3 == x4 && y3 == y4) { 369 System.out.println("points coincide"); 370 } else if (isParallel()) { 371 System.out.println("is parallel lines,have no intersection point"); 372 } else { 373 dealCoordinatesOfIntersection(); 374 System.out.println(x0 + "," + y0 + " " + isIn()); 375 } 376 } 377 }
解释和心得:
本体开始大规模的使用类进行代码的编写,运用的也更加熟练,开始为最后一次的代码书写做准备,慢慢发现类方法的好处并且开始接受。
第四座大山:题目集3的7-3
代码如下:

import java.util.Scanner; import java.math.BigDecimal; ; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String row = in.nextLine() + " "; String[] co; String regexL1 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){0,2}"; String regex1 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){3}"; String regexF1 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){4,}"; String regexL2 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){0,3}"; String regex2 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){4}"; String regexF2 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){5,}"; String regexL3 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){0,4}"; String regex3 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){5}"; String regexF3 = "\\d\\:(([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)[,]([+-]?(([1-9]\\d*)|0)(\\.\\d+)*)\\s){6,}"; switch (row.substring(0, 1)) { case "1": if (row.matches(regex1)) { co = row.split("[\\s\\,\\:]"); double x1 = Double.parseDouble(co[1]); double y1 = Double.parseDouble(co[2]); double x2 = Double.parseDouble(co[3]); double y2 = Double.parseDouble(co[4]); double x3 = Double.parseDouble(co[5]); double y3 = Double.parseDouble(co[6]); Triangle triangle = new Triangle(x1,y1,x2,y2,x3,y3); if(triangle.judge()) { System.out.println(triangle.isIsosceles() + " " + triangle.isEquilateral()); } else { System.out.println("data error"); System.exit(0); } } else if (row.matches(regexF1) || row.matches(regexL1)) { System.out.println("wrong number of points"); System.exit(0); } else { System.out.println("Wrong Format"); System.exit(0); } break; case "2":if (row.matches(regex1)) { co = row.split("[\\s\\,\\:]"); double x1 = Double.parseDouble(co[1]); double y1 = Double.parseDouble(co[2]); double x2 = Double.parseDouble(co[3]); double y2 = Double.parseDouble(co[4]); double x3 = Double.parseDouble(co[5]); double y3 = Double.parseDouble(co[6]); Triangle triangle = new Triangle(x1,y1,x2,y2,x3,y3); if(triangle.judge()) { System.out.println(triangle.perimeter() + " " + triangle.area() + " " + triangle.barycenter()); } else { System.out.println("data error"); System.exit(0); } }else if (row.matches(regexF1) || row.matches(regexL1)) { System.out.println("wrong number of points"); System.exit(0); } else { System.out.println("Wrong Format"); System.exit(0); }break; case "3":if (row.matches(regex1)) { co = row.split("[\\s\\,\\:]"); double x1 = Double.parseDouble(co[1]); double y1 = Double.parseDouble(co[2]); double x2 = Double.parseDouble(co[3]); double y2 = Double.parseDouble(co[4]); double x3 = Double.parseDouble(co[5]); double y3 = Double.parseDouble(co[6]); Triangle triangle = new Triangle(x1,y1,x2,y2,x3,y3); if(triangle.judge()) { System.out.println(triangle.isObtuse() + " " + triangle.isRight() + " " + triangle.isAcute()); } else { System.out.println("data error"); System.exit(0); } }else if (row.matches(regexF1)) { System.out.println("wrong number of points"); System.exit(0); } else { System.out.println("Wrong Format"); System.exit(0); }break; case "4":if (row.matches(regex3)) { co = row.split("[\\s\\,\\:]"); double x1 = Double.parseDouble(co[1]); double y1 = Double.parseDouble(co[2]); double x2 = Double.parseDouble(co[3]); double y2 = Double.parseDouble(co[4]); double x3 = Double.parseDouble(co[5]); double y3 = Double.parseDouble(co[6]); double x4 = Double.parseDouble(co[7]); double y4 = Double.parseDouble(co[8]); double x5 = Double.parseDouble(co[9]); double y5 = Double.parseDouble(co[10]); if (x1 == x2 && y1 == y2) { System.out.println("points coincide"); System.exit(0); } Triangle triangle = new Triangle(x3,y3,x4,y4,x5,y5); if(triangle.judge()) { System.out.println("1"); } else { System.out.println("data error"); System.exit(0); } }else if (row.matches(regexF3) || row.matches(regexL3)) { System.out.println("wrong number of points"); System.exit(0); } else { System.out.println("Wrong Format"); System.exit(0); }break; case "5":if (row.matches(regex2)) { co = row.split("[\\s\\,\\:]+"); double x1 = Double.parseDouble(co[1]); double y1 = Double.parseDouble(co[2]); double x2 = Double.parseDouble(co[3]); double y2 = Double.parseDouble(co[4]); double x3 = Double.parseDouble(co[5]); double y3 = Double.parseDouble(co[6]); double x4 = Double.parseDouble(co[7]); double y4 = Double.parseDouble(co[8]); Five five = new Five(x1,y1,x2,y2,x3,y3,x4,y4); five.judge(); } else if (row.matches(regexF2) || row.matches(regexL2)) { System.out.println("wrong number of points"); System.exit(0); } else { System.out.println("Wrong Format"); System.exit(0); }break; default: System.out.println("Wrong Format"); System.exit(0); } } } class Line { private double x1; private double y1; private double x2; private double y2; Line(){} Line(double x1, double y1, double x2, double y2) { this.y2 = y2; this.x1 = x1; this.x2 = x2; this.y1 = y1; } public boolean validate() { return x1 == x2 && y1 == y2; } public double line() { return (float)Math.sqrt(Math.pow(x1 - x2,2) + Math.pow(y1 - y2,2)); } } class Triangle { private double x1; private double y1; private double x2; private double y2; private double x3; private double y3; private double[] side = new double[3]; Triangle() {} Triangle(double x1, double y1, double x2, double y2, double x3, double y3) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; this.x3 = x3; this.y3 = y3; if ((x2 - x1) * (y3 - y2) == (x3 - x2) * (y2 - y1)) { System.out.println("data error"); System.exit(0); } if(new Line(x1,y1,x2,y2).validate() == false) { this.side[0] = new Line(x1, y1, x2, y2).line(); } else { System.out.println("data error"); System.exit(0); } if(new Line(x2,y2,x3,y3).validate() == false) { this.side[1] = new Line(x2,y2,x3,y3).line(); } else { System.out.println("data error"); System.exit(0); } if(new Line(x1,y1,x3,y3).validate() == false) { this.side[2] = new Line(x1,y1,x3,y3).line(); } else { System.out.println("data error"); System.exit(0); } } public boolean judge(){ java.util.Arrays.sort(side); return side[0] + side[1] > side[2]; } public boolean isIsosceles() { return side[0] == side[1] || side[0] == side[2] || side[1] == side[2]; } public boolean isEquilateral() { return side[0] == side[1] && side[0] == side[2] && side[1] == side[2]; } public double perimeter() { return ArithUtils.round(ArithUtils.add(ArithUtils.add(side[0] , side[1]), side[2]),6); } public double area() { double p = ArithUtils.div(ArithUtils.add(ArithUtils.add(side[0],side[1]),side[2]), 2); double area = ArithUtils.round(Math.sqrt(ArithUtils.mul(ArithUtils.mul(ArithUtils.mul(p,p - side[0]), p - side[1]) ,p - side[2])),6); return area; } public String barycenter() { return ArithUtils.round((x1 + x3 + x2) / 3,6) + "," + ArithUtils.round((y1 + y3 + y2) / 3,6); } public boolean isAcute() {//锐角 java.util.Arrays.sort(side); return side[0] * side[0] + side[1] * side[1] - side[2] * side[2] > 0; } public boolean isRight() {//直角三角形 java.util.Arrays.sort(side); return Math.abs(side[0] * side[0] + side[1] * side[1] - side[2] * side[2]) < 0.1; } public boolean isObtuse() {//钝角三角形 java.util.Arrays.sort(side); return side[0] * side[0] + side[1] * side[1] < side[2] * side[2]; } public void setSide(double[] side) { this.side = side; } public double[] getSide() { return side; } } class Five{ private Triangle triangle; private double x; private double y; private double x1; private double y1; private double x2; private double y2; private double x3; private double y3; Five() {} Five(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { this.x = x1; this.y = y1; this.x1 = x2; this.x2 = x3; this.y1 = y2; this.y2 = y3; this.x3 = x4; this.y3 = y4; this.triangle = new Triangle(x2,y2,x3,y3,x4,y4); if (!triangle.judge()) { System.out.println("data error"); System.exit(0); } } public double distance(double x1, double y1, double x2, double y2) { double distance = Math.abs(ArithUtils.div(ArithUtils.add(ArithUtils.sub(ArithUtils.mul(ArithUtils.sub(x2 , x1) , y) , ArithUtils.mul(ArithUtils.sub(y2 ,y1), x)), ArithUtils.sub(ArithUtils.mul(x1, y2) , ArithUtils.mul(y1 , x2))) , Math.sqrt(Math.pow(x2 - x1,2)+ Math.pow(y2 - y1,2)),6)); return distance; } public void judge(){ if (distance(x1,y1,x2,y2) == 0 || distance(x1,y1,x3,y3) == 0 || distance(x3,y3,x2,y2) == 0) { System.out.println("on the triangle"); } else if (ArithUtils.div(ArithUtils.add(ArithUtils.add(ArithUtils.mul(distance(x1,y1,x2,y2) , triangle.getSide()[0]),ArithUtils.mul(distance(x1,y1,x3,y3), triangle.getSide()[2])), ArithUtils.mul(distance(x2,y2,x3,y3), triangle.getSide()[1])), 2,6) - ArithUtils.add(triangle.area(),6) < 1){ System.out.println("in the triangle"); } else { System.out.println("outof the triangle"); } } } class ArithUtils { private static final int DEF_DIV_SCALE = 10; private ArithUtils() { } public static double add(double d1, double d2) { BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.add(b2).doubleValue(); } public static double sub(double d1, double d2) { BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.subtract(b2).doubleValue(); } public static double mul(double d1, double d2) { BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.multiply(b2).doubleValue(); } public static double div(double d1, double d2) { return div(d1, d2, DEF_DIV_SCALE); } public static double div(double d1, double d2, int scale) { if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } public static double round(double d, int scale) { if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(d)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } }
解释和心得:
本次作业相当于是对之前所有的作业的小总和,在编写过程中使用了类进行编写,实现了分类功能,并且切身感受到类带来的便利,但同时其中也出现了很多问题比如类的分工不清晰,学习到了很多有关类方法的解决问题,对自己的提升很多!
总结
首先先给大家讲完本篇blog开头留下的小故事,我们的老师上课期间为了便于我们理解将类形象的比喻成了“学校郊游”,面向过程C语言就好像是从学生上车,入座,司机开车,到站,司机开门,学生下车,按照这些流程来做就可以做完,但面向对象不一样,面向对象中,你需要把班级,学生,汽车,分别分成三个类,其中班级包括学生信息,以班级为单位,学生则是个人学生的行为,汽车则专注于车门的开关,启动,十分核心的一个观点就是“各司其职”,即每个人做好自己的分工职能所在,不干涉其余功能的进行,而面向过程完全不考虑这些,能够顺利完成任务即可,想必大家听完这个有趣的小故事之后都对面向过程的java语言有了自己的理解吧!
学习到的内容
继承
- 子类继承父类
多态
- 父类变量调用子类方法
抽象类与接口
抽象类
- 1.在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
- 2.由于抽象类不能实例化对象,所以抽象类必须被继承,才能被使用。
- 3.父类包含了子类集合的常见的方法,但是由于父类本身是抽象的,所以不能使用这些方法。
- 4.在 Java 中抽象类表示的是一种继承关系,一个类只能继承一个抽象类,而一个类却可以实现多个接口。
在 Java 语言中使用 abstract class 来定义抽象类

1 public abstract class Employee 2 { 3 private String name; 4 private String address; 5 private int number; 6 public Employee(String name, String address, int number) 7 { 8 System.out.println("Constructing an Employee"); 9 this.name = name; 10 this.address = address; 11 this.number = number; 12 } 13 public double computePay() 14 { 15 System.out.println("Inside Employee computePay"); 16 return 0.0; 17 } 18 public void mailCheck() 19 { 20 System.out.println("Mailing a check to " + this.name 21 + " " + this.address); 22 } 23 public String toString() 24 { 25 return name + " " + address + " " + number; 26 } 27 public String getName() 28 { 29 return name; 30 } 31 public String getAddress() 32 { 33 return address; 34 } 35 public void setAddress(String newAddress) 36 { 37 address = newAddress; 38 } 39 public int getNumber() 40 { 41 return number; 42 } 43 } 44 45 通过以下代码来使用 46 ```java 47 /* 文件名 : Salary.java */ 48 public class Salary extends Employee 49 { 50 private double salary; //Annual salary 51 public Salary(String name, String address, int number, double 52 salary) 53 { 54 super(name, address, number); 55 setSalary(salary); 56 } 57 public void mailCheck() 58 { 59 System.out.println("Within mailCheck of Salary class "); 60 System.out.println("Mailing check to " + getName() 61 + " with salary " + salary); 62 } 63 public double getSalary() 64 { 65 return salary; 66 } 67 public void setSalary(double newSalary) 68 { 69 if(newSalary >= 0.0) 70 { 71 salary = newSalary; 72 } 73 } 74 public double computePay() 75 { 76 System.out.println("Computing salary pay for " + getName()); 77 return salary/52; 78 } 79 }
课程改进建议及意见
- 1.我觉得上课的时候可以多让同学上前回答问题,多提高一下同学们的积极性和能动性
- 2.希望老师能够每次在讲解时多举一些实在的例子帮助大家理解和运用