第一次blog作业-loveforever
- 关于Java和面向对象:
关于Java和面向对象,作为以前几乎只接触过C来说经过三次作业之后才对Java以及面向对象的思想有了初步的认识,对于Java来说我认为则是通过代码的分类,将我们以前的大块代码进行分类,各司其职,有问题直接找类,通过牺牲代码执行速度来换取代码的易于维护和可阅读性。
- 三次作业的难点:
第一次作业是为让我们尽快熟悉Java的语言规范和Java的代码格式,对此在第一次作业7-8中有一个关于Java数据精度的坑,正常对于三角形的判定我们是可以直接写成c*c-(a*a*2)=0但在题目当中我们需要将其转化为c*c-(a*a*2)))<0.000001以避免数据精度的不足。
第二次作业基本是关于日期的问题在日期合法性检验时7-3采用的是C语言思路对其进行多次if判定但在实现过程中出现一个测试点始终过不去,在参考其他人后将其写成数组判定在后续的实现过程中能直接完成。
第三次作业7-3是关于正则表达式的提取在第一次使用正则表达式时提取出现了困难,至今依旧对正则表达式其使用还未完全清楚。
- 正则表达式:
正则表达式语法:
在 Java 中,\\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。
所以,在其他的语言中(如 Perl),一个反斜杠 \ 就足以具有转义的作用,而在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也可以简单的理解在 Java 的正则表达式中,两个 \\ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\。
如何从一个给定的字符串中找到数字串:
1 import java.util.regex.Matcher; 2 import java.util.regex.Pattern; 3 4 public class RegexMatches 5 { 6 public static void main( String[] args ){ 7 8 // 按指定模式在字符串查找 9 String line = "This order was placed for QT3000! OK?"; 10 String pattern = "(\\D*)(\\d+)(.*)"; 11 12 // 创建 Pattern 对象 13 Pattern r = Pattern.compile(pattern); 14 15 // 现在创建 matcher 对象 16 Matcher m = r.matcher(line); 17 if (m.find( )) { 18 System.out.println("Found value: " + m.group(0) ); 19 System.out.println("Found value: " + m.group(1) ); 20 System.out.println("Found value: " + m.group(2) ); 21 System.out.println("Found value: " + m.group(3) ); 22 } else { 23 System.out.println("NO MATCH"); 24 } 25 } 26 }
关于正则表达式详细使用规则请参考:“Java 正则表达式 | 菜鸟教程 (runoob.com)”
- 代码分析:
第一次7-8:
7-8的题目是判断三角形类型,在代码创建过程中由于主要是通过面向过程的思想来实现因此对于Java的面向对象的思想并未能清楚理解
1 public class Main { 2 public static void main(String[] args) { 3 Scanner in = new Scanner(System.in); 4 while (in.hasNextInt()) { 5 float a = in.nextFloat(); 6 float b = in.nextFloat(); 7 float c = in.nextFloat(); 8 if((a<1||a>200)||(b<1||b>200)||(c<1||c>200)){ 9 System.out.println("Wrong Format"); 10 } 11 else if((a+b>c)&&(a+c>b)&&(b+c>a)){ 12 if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a)&&(a!=b)&&(a!=c)&&(b!=c)){ 13 System.out.println("Right-angled triangle"); 14 } 15 else if(a==b||a==c||b==c){ 16 if(a==b&&a==c&&b==c){ 17 System.out.println("Equilateral triangle"); 18 } 19 else if(((Math.abs((c*c-(a*a*2)))<0.000001)||Math.abs((b*b-(a*a*2)))<0.000001)||Math.abs((a*a-(b*b*2)))<0.000001){ 20 System.out.println("Isosceles right-angled triangle"); 21 } 22 else{ 23 System.out.println("Isosceles triangle"); 24 } 25 26 } 27 28 else{ 29 System.out.println("General triangle"); 30 } 31 32 } 33 else{ 34 System.out.println("Not a triangle"); 35 } 36 } 37 } 38 }
在代码实现后对其的代码复杂度进行了测试通过对其圈复杂度的测试(如下图)能看出其在一些地方仍然具有改进点。
-第二次作业
第二次作业7-3和7-4的题目都是Java中有关日期的判定第一次写的时候对日期的合法性检验主要是想在外面套一层循环判定
1 public class Main { 2 public static void main(String[] args) { 3 Scanner in=new Scanner(System.in); 4 int year,month,day,days=0; 5 year=in.nextInt(); 6 month=in.nextInt(); 7 day=in.nextInt(); 8 if(year>=1820&&year<=2020) { 9 if(isLeapYear(year)) { 10 if(month==1) { 11 if(day<=31&&day>0) { 12 System.out.println(year+" is a leap year."); 13 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 14 } 15 else{ 16 System.out.println("Wrong Format"); 17 } 18 } 19 else if(month==2) { 20 if(day<30&&day>0) { 21 System.out.println(year+" is a leap year."); 22 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 23 } 24 else{ 25 System.out.println("Wrong Format"); 26 } 27 } 28 else if(month==3) { 29 if(day<=31&&day>0) { 30 System.out.println(year+" is a leap year."); 31 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 32 } 33 else{ 34 System.out.println("Wrong Format"); 35 } 36 } 37 else if(month==4) { 38 if(day<=30&&day>0) { 39 System.out.println(year+" is a leap year."); 40 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 41 } 42 else{ 43 System.out.println("Wrong Format"); 44 } 45 } 46 else if(month==5) { 47 if(day<=31&&day>0) { 48 System.out.println(year+" is a leap year."); 49 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 50 } 51 else{ 52 System.out.println("Wrong Format"); 53 } 54 } 55 else if(month==6) { 56 if(day<=30&&day>0) { 57 System.out.println(year+" is a leap year."); 58 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 59 } 60 else{ 61 System.out.println("Wrong Format"); 62 } 63 } 64 else if(month==7) { 65 if(day<=31&&day>0) { 66 System.out.println(year+" is a leap year."); 67 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 68 } 69 else{ 70 System.out.println("Wrong Format"); 71 } 72 } 73 else if(month==8) { 74 if(day<=31&&day>0) { 75 System.out.println(year+" is a leap year."); 76 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 77 } 78 else{ 79 System.out.println("Wrong Format"); 80 } 81 } 82 else if(month==9) { 83 if(day<=30&&day>0) { 84 System.out.println(year+" is a leap year."); 85 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 86 } 87 else{ 88 System.out.println("Wrong Format"); 89 } 90 } 91 else if(month==10) { 92 if(day<=31&&day>0) { 93 System.out.println(year+" is a leap year."); 94 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 95 } 96 else{ 97 System.out.println("Wrong Format"); 98 } 99 } 100 else if(month==11) { 101 if(day<=30&&day>0) { 102 System.out.println(year+" is a leap year."); 103 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 104 } 105 else{ 106 System.out.println("Wrong Format"); 107 } 108 } 109 else if(month==12) { 110 if(day<=31&&day>0) { 111 System.out.println(year+" is a leap year."); 112 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 113 } 114 else{ 115 System.out.println("Wrong Format"); 116 } 117 } 118 else { 119 System.out.println("Wrong Format"); 120 } 121 122 } 123 else { 124 if(month==1) { 125 if(day<=31&&day>0) { 126 System.out.println(year+" is a not leap year."); 127 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 128 } 129 else{ 130 System.out.println("Wrong Format"); 131 } 132 } 133 else if(month==2) { 134 if(day<29&&day>0) { 135 System.out.print(year+" is a not leap year."); 136 System.out.print(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 137 } 138 else{ 139 System.out.println("Wrong Format"); 140 } 141 } 142 else if(month==3) { 143 if(day<=31&&day>0) { 144 System.out.println(year+" is a not leap year."); 145 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 146 } 147 else{ 148 System.out.println("Wrong Format"); 149 } 150 } 151 else if(month==4) { 152 if(day<=30&&day>0) { 153 System.out.println(year+" is a not leap year."); 154 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 155 } 156 else{ 157 System.out.println("Wrong Format"); 158 } 159 } 160 else if(month==5) { 161 if(day<=31&&day>0) { 162 System.out.println(year+" is a not leap year."); 163 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 164 } 165 else{ 166 System.out.println("Wrong Format"); 167 } 168 } 169 else if(month==6) { 170 if(day<=30&&day>0) { 171 System.out.println(year+" is a not leap year."); 172 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 173 } 174 else{ 175 System.out.println("Wrong Format"); 176 } 177 } 178 else if(month==7) { 179 if(day<=31&&day>0) { 180 System.out.println(year+" is a not leap year."); 181 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 182 } 183 else{ 184 System.out.println("Wrong Format"); 185 } 186 } 187 else if(month==8) { 188 if(day<=31&&day>0) { 189 System.out.println(year+" is a not leap year."); 190 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 191 } 192 else{ 193 System.out.println("Wrong Format"); 194 } 195 } 196 else if(month==9) { 197 if(day<=30&&day>0) { 198 System.out.println(year+" is a not leap year."); 199 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 200 } 201 else{ 202 System.out.println("Wrong Format"); 203 } 204 } 205 else if(month==10) { 206 if(day<=31&&day>0) { 207 System.out.println(year+" is a not leap year."); 208 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 209 } 210 else{ 211 System.out.println("Wrong Format"); 212 } 213 } 214 else if(month<=11&&day>0) { 215 if(day<=30&&day>0) { 216 System.out.println(year+" is a not leap year."); 217 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 218 } 219 else{ 220 System.out.println("Wrong Format"); 221 } 222 } 223 else if(month<=12&&day>0) { 224 if(day<=31&&day>0) { 225 System.out.println(year+" is a not leap year."); 226 System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(numOfDays(year,month,day))+"."); 227 } 228 else{ 229 System.out.println("Wrong Format"); 230 } 231 } 232 else { 233 System.out.println("Wrong Format"); 234 } 235 236 } 237 } 238 else{ 239 System.out.println("Wrong Format"); 240 } 241 }
后面通过发现可以通过数组的方法进行合法性检验因此在7-4和7-5中采用了数组方法来进行合法性检验
1 public static boolean checkInputValidity(int year,int month,int day) { 2 boolean checkInputValidity; 3 int[] a=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31}; 4 if(!isLeapYear(year)) 5 a[2] = 28; 6 checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0); 7 return checkInputValidity; 8 }
这很明显不管是代码量,代码可读性,圈复杂度来说使用数组的方法都明显好于过程判定,这也能明显看出在代码逻辑较为复杂时,面向对象的操作比面向过程确实较为友好
第三次作业:
第三次作业则主要是正则表达式的使用在正则表达式的学习过程中因为第一次接触且并不熟悉因此在正则表达式的提取过程中并未成功后面通过参考其他代码才勉强完成下面代码仅供参考
1 class DvForString{ 2 private static LinkedList<String> item; 3 private static String sign,number,regex,regex1,regex2,regex3,concat,concatend,specialnum,end;//各项正则 4 static { 5 //静态区只在类被加载时被执行一次 6 rubbish(); 7 item=new LinkedList<>(); 8 sign="(?:[+|-])"; 9 number="(?:([1-9](\\s*\\d*\\s*)*))"; 10 //全是常数项时的特判 11 specialnum="((?:(\\s*([+|-]?[1-9]+\\s*\\d*)\\s*))*)"; 12 //带x项,允许系数中间内有空格 13 //有不为0的系数和指数 14 regex="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))"; 15 //x只有指数 16 regex1="(?:(x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))"; 17 //x只有系数 18 regex2="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x))"; 19 //x前面系数,指数都没有 20 regex3="(?:x)"; 21 concat="("+regex+"|"+regex1+"|"+regex2+"|"+regex3+")"; 22 //数字和带x项或在一起构成多项式中的一项 23 concatend="("+concat+"|"+number+")"; 24 //多项式表达式 ,首项可以无+,-, 后续项必须有 25 end="(?:("+"\\s*"+sign+"?\\s*"+concatend+"(?:\\s*"+sign+"\\s*"+concatend+"\\s*)*"+"))"; 26 }
- ”source monitor报表参考“
Metrics Details For File '第一次7-8.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory D:\交易流水\分析代码\
Project Name 分析
Checkpoint Name Baseline
File Name 第一次7-8.java
Lines 40
Statements 24
Percent Branch Statements 33.3
Method Call Statements 14
Percent Lines with Comments 0.0
Classes and Interfaces 1
Methods per Class 3.00
Average Statements per Method 7.00
Line Number of Most Complex Method 4
Name of Most Complex Method Main.main()
Maximum Complexity 16
Line Number of Deepest Block 19
Maximum Block Depth 6
Average Block Depth 3.58
Average Complexity 13.50
--------------------------------------------------------------------------------------------
Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls
if(().if(() 11, 10, 6, 8
Main.main() 16, 12, 6, 9
--------------------------------------------------------------------------------------------
Block Depth Statements
0 2
1 1
2 2
3 6
4 5
5 5
6 3
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
Metrics Details For File '第二次7-3.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory D:\交易流水\分析代码\
Project Name 分析
Checkpoint Name Baseline
File Name 第二次7-3.java
Lines 287
Statements 187
Percent Branch Statements 46.0
Method Call Statements 124
Percent Lines with Comments 1.0
Classes and Interfaces 1
Methods per Class 4.00
Average Statements per Method 45.25
Line Number of Most Complex Method 4
Name of Most Complex Method Main.main()
Maximum Complexity 80
Line Number of Deepest Block 14
Maximum Block Depth 6
Average Block Depth 4.51
Average Complexity 40.50
--------------------------------------------------------------------------------------------
Most Complex Methods in 1 Class(es): Complexity, Statements, Max Depth, Calls
Main.isLeapYear() 1, 1, 2, 0
Main.main() 80, 158, 6, 124
--------------------------------------------------------------------------------------------
Block Depth Statements
0 4
1 15
2 17
3 3
4 26
5 50
6 72
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
Metrics Details For File '第二次7-4.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory D:\交易流水\分析代码\
Project Name 分析
Checkpoint Name Baseline
File Name 第二次7-4.java
Lines 72
Statements 48
Percent Branch Statements 22.9
Method Call Statements 8
Percent Lines with Comments 2.8
Classes and Interfaces 1
Methods per Class 6.00
Average Statements per Method 6.83
Line Number of Most Complex Method 37
Name of Most Complex Method Main.nextDate()
Maximum Complexity 8
Line Number of Deepest Block 45
Maximum Block Depth 4
Average Block Depth 2.31
Average Complexity 3.00
--------------------------------------------------------------------------------------------
Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls
isLeapYear().if(() 1, 1, 3, 0
Main.checkInputValidity() 2, 6, 2, 1
Main.isLeapYear() 1, 2, 3, 0
Main.main() 3, 10, 3, 4
Main.nextDate() 8, 17, 4, 2
Main.rubbish() 3, 4, 3, 0
--------------------------------------------------------------------------------------------
Block Depth Statements
0 2
1 5
2 24
3 10
4 7
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
Metrics Details For File '第二次7-5.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory D:\交易流水\分析代码\
Project Name 分析
Checkpoint Name Baseline
File Name 第二次7-5.java
Lines 109
Statements 64
Percent Branch Statements 21.9
Method Call Statements 11
Percent Lines with Comments 11.0
Classes and Interfaces 1
Methods per Class 6.00
Average Statements per Method 7.00
Line Number of Most Complex Method 40
Name of Most Complex Method Main.nextDate()
Maximum Complexity 6
Line Number of Deepest Block 16
Maximum Block Depth 3
Average Block Depth 1.59
Average Complexity 2.60
--------------------------------------------------------------------------------------------
Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls
isLeapYear().if(() 1, 1, 3, 0
Main.checkInputValidity() 2, 7, 2, 2
Main.isLeapYear() 1, 2, 3, 0
Main.main() 3, 12, 3, 5
Main.nextDate() 6, 15, 3, 1
--------------------------------------------------------------------------------------------
Block Depth Statements
0 9
1 14
2 35
3 6
4 0
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
Metrics Details For File '第三次.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory D:\交易流水\分析代码\
Project Name 分析
Checkpoint Name Baseline
File Name 第三次.java
Lines 262
Statements 168
Percent Branch Statements 28.0
Method Call Statements 68
Percent Lines with Comments 18.7
Classes and Interfaces 2
Methods per Class 4.00
Average Statements per Method 17.38
Line Number of Most Complex Method 8
Name of Most Complex Method Main.DvForString()
Maximum Complexity 1
Line Number of Deepest Block 185
Maximum Block Depth 9+
Average Block Depth 3.25
Average Complexity 1.00
--------------------------------------------------------------------------------------------
Most Complex Methods in 3 Class(es): Complexity, Statements, Max Depth, Calls
DvForString().getPolynthic() 1, 2, 3, 1
DvForString().setPolynthic() 1, 2, 3, 1
DvForString.getItem() 1, 2, 3, 1
Main.DvForString() 1, 7, 3, 2
Main.main() 1, 6, 2, 3
--------------------------------------------------------------------------------------------
Block Depth Statements
0 7
1 32
2 39
3 21
4 26
5 17
6 12
7 4
8 6
9+ 4
--------------------------------------------------------------------------------------------
- 总结:
在代码实现过程中我认为要先对整体全局进行布局先有框架再来填充这样对于Java来说才能将其对象的本质清楚分析让其各司其职
- 心得体会:
通过前三次作业对于面向对象的思想和Java语法有了大概的了解及认知基础,前三次作业量感觉适中,后面还有Java的继承和封装希望题目量别多吧狗头保命