前三次作业总结
一,前言
1,第一次作业
入学第一次作业,主要让你懂得代码的入口是在主类,以及输入输出是在某个类里的方法,其他的过程和C语言相差不大。本次作业题量为9,难度低。
2,第二次作业
第二次作业在前一次的基础上增加了字符串的定位str.charAt()以及字符串长度方法str.length()。比起C语言来都不用循环找字符循环求长度了。本此作业题量为3,难度为中。
3,第三次作业
第三次终于增加了类的使用,以及正则表达式的使用。除此之外还涉及了较多的数学公式,例如直线平行,线段交点,判断三角形,求三角形周长面积。本次作业题量为3,难度较高。
二,设计与分析
由于第一次作业,和第二次作业的第1,第2题难度较低,就不做分析了!
1,咱们来到第二次作业第二题(7-2)
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。
输入格式:
由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111
输出格式:
过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data",
若某个数据的结束符不为1,则输出“validate error”。
若某个数据奇偶校验错误,则输出“parity check error”。
若数据结束符和奇偶校验均不合格,输出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error
设计:
首先我们需要找到第一个1的位置
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') count1++;
}
count1用来接收第几位是1,接着我们从第count1位开始找八个数字,存储1的个数判断第九位的奇偶校验准不准确,以及最后一位是不是结束符0,整体的代码如下
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int count = 0, temp = 0, i, count1 = 0, sum = 0;
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') count1++;
}
if (count1 == str.length() || str.length() < 11) System.out.println("null data");
else {
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') continue;
count = i;
if ((str.substring(count)).length() < 11) return;
else {
for (int j = count + 1; j <= count + 8; j++) {
if (str.charAt(j) == '0') sum++;
}
sum = sum % 2;
if (((str.charAt(count + 9) == '1' && sum == 0) || (str.charAt(count + 9) == '0' && sum == 1)) && str.charAt(count + 10) == '1') {
System.out.println(temp + 1 + ":" + str.substring(count + 1, count + 9));
temp++;
i += 10;
} else if (((str.charAt(count + 9) == '0' && sum == 0) || (str.charAt(count + 9) == '1' && sum == 1)) && str.charAt(count + 10) == '1') {
System.out.println(temp + 1 + ":" + "parity check error");
temp++;
i += 10;
} else {
System.out.println(temp + 1 + ":" + "validate error");
temp++;
i += 10;
}
}sum=0;
}
}
}
}
写完代码后采用SourceMonitor看看代码质量

行数 43
声明 36
分行报表百分比 36.1
方法调用语句 21
带有评论的百分比行 0.0
类和接口 1
每类方法 1
每种方法的平均语句 32
最复杂方法的行号 6
最复杂方法的名称 Main. main()
由于只有一个主类,类包也比较简短

总体来说这题还是比较简单的按照以上方法来思路还是比较清晰
2,废话不多说,直接来到难度飙升版第三次作业(7-1)
输入连个点的坐标,计算两点之间的距离
输入格式:
4个double类型的实数,两个点的x,y坐标,依次是x1、y1、x2、y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若输入格式非法,输出"Wrong Format"。
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”。
输出格式:
计算所得的两点之间的距离。例如:1.4142135623730951
先说说我的思路:首先除了主类我还建造了一个判断类,然而我把输入也放在了这个类里,输入完以后直接判断是不是符合格式(后知后觉这样做是非常不合理的,后面做题将此方法改进)
class calculating{ double x1,y1,x2,y2; String zz="[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; Scanner in = new Scanner(System.in); String str = in.nextLine(); String[] array=str.split(","); int momma_number=array.length-1; public void panduan(){ if(!str.matches(zz)&&momma_number>2) System.out.println("wrong number of points"); else if(!str.matches(zz)) System.out.println("Wrong Format"); } }
在主函数中再用以上类的str分割成坐标计算(说实话分割这种东西还是交给另一个类做比较好),思路差不多就是这样,来看看全部代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
calculating a_b=new calculating();
a_b.panduan();
if(a_b.str.matches(a_b.zz))
{
String[] c=a_b.str.split(",",2);
a_b.x1=Double.parseDouble(c[0]);
String[] c1=c[1].split(" ",2);
a_b.y1=Double.parseDouble(c1[0]);
String[] c2=c1[1].split(",",2);
a_b.x2=Double.parseDouble(c2[0]);
a_b.y2=Double.parseDouble(c2[1]);
System.out.println(Math.sqrt(Math.pow(a_b.x1-a_b.x2,2)+Math.pow(a_b.y1-a_b.y2,2)));
}
}
}
class calculating{
double x1,y1,x2,y2;
String zz="[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?";
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] array=str.split(",");
int momma_number=array.length-1;
public void panduan(){
if(!str.matches(zz)&&momma_number>2) System.out.println("wrong number of points");
else if(!str.matches(zz)) System.out.println("Wrong Format");
}
}
来看看代码质量

行数32
声明 27
分支语句百分比 11.1
方法调用语句 11
带注释的行的百分比 0.0
类和接口 2
每类的方法数 1.00
每个方法的平均语句数 7.50
最复杂方法的行数 28
最复杂计算方法名称
最大复杂度 4
最深块的行数 10
最大块深度 3
平均区块深度 1.70
平均复杂度 3.00

上图是代码类包
3,第三次作业(7-2)
用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is
parallel lines,have no intersection point"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide"
先上代码
import java.util.Scanner; public class Main { public static void main(String[] args) { calculating thing = new calculating(); char num = thing.str.charAt(0); if (num == '1') { thing.panduan(2); if (thing.str1[1].matches(thing.gre2)) { if (thing.x1 == thing.x2 && thing.y1 != thing.y2) System.out.println("Slope does not exist"); else if (thing.x1 == thing.x2 && thing.y1 == thing.y2) System.out.println("points coincide"); else System.out.println((thing.y2 - thing.y1) / (thing.x2 - thing.x1)); } } else if (num == '2') { thing.panduan(3); if (thing.str1[1].matches(thing.gre3)) { if (thing.x2 == thing.x3 && thing.y2 == thing.y3) System.out.println("points coincide"); else { double a = thing.y3 - thing.y2; double b = thing.x2 - thing.x3; double x = thing.x3 * thing.y2 - thing.x2 * thing.y3; System.out.println(Math.abs(a * thing.x1 + b * thing.y1 + x) / Math.pow(a * a + b * b, 0.5)); } } } else if (num == '3') { thing.panduan(3); if (thing.str1[1].matches(thing.gre3)) { if ((thing.x2 == thing.x3 && thing.y2 == thing.y3) || (thing.x1 == thing.x3 && thing.y1 == thing.y3) || (thing.x1 == thing.x2 && thing.y1 == thing.y2)) System.out.println("points coincide"); else if ((thing.x2 - thing.x1) * (thing.y3 - thing.y2) != (thing.y2 - thing.y1) * (thing.x3 - thing.x2)) System.out.println("false"); else System.out.println("true"); } } else if (num == '4') { thing.panduan(4); if (thing.str1[1].matches(thing.gre4)) { if ((thing.x1 == thing.x2 && thing.y1 == thing.y2) || (thing.x3 == thing.x4 && thing.y3 == thing.y4)) System.out.println("points coincide"); else if (((thing.y1 - thing.y2) / (thing.x1 - thing.x2) == (thing.y3 - thing.y4) / (thing.x3 - thing.x4)) || (thing.x1 == thing.x2 && thing.x3 == thing.x4)) System.out.println("true"); else System.out.println("false"); } } else if (num == '5') { thing.panduan(4); if (thing.str1[1].matches(thing.gre4)) { if ((thing.x1 == thing.x2 && thing.y1 == thing.y2) || (thing.x3 == thing.x4 && thing.y3 == thing.y4)) System.out.println("points coincide"); else if (((thing.y1 - thing.y2) / (thing.x1 - thing.x2) == (thing.y3 - thing.y4) / (thing.x3 - thing.x4)) || (thing.x1 == thing.x2 && thing.x3 == thing.x4)) System.out.println("is parallel lines,have no intersection point"); else { double x1=thing.x1,x2=thing.x2,x3=thing.x3,x4=thing.x4,y1=thing.y1,y2=thing.y2,y3=thing.y3,y4=thing.y4; double a=(y3*x4*x2-y4*x3*x2-y3*x4*x1+y4*x3*x1-y1*x2*x4+y2*x1*x4+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3); double b=(-y3*x4*y2+y4*x3*y2+y3*x4*y1-y4*x3*y1+y1*x2*y4-y1*x2*y3-y2*x1*y4+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3); if ((Math.abs(Math.sqrt(Math.pow(a - thing.x1, 2) + Math.pow(b - thing.y1, 2)) + Math.sqrt(Math.pow(a - thing.x2, 2) + Math.pow(b - thing.y2, 2)) - Math.sqrt(Math.pow(thing.x1 - thing.x2, 2)+Math.pow(thing.y1 - thing.y2, 2)))<0.01&&Math.sqrt(Math.pow(a - thing.x1, 2) + Math.pow(b - thing.y1, 2))!=0&&Math.sqrt(Math.pow(a - thing.x2, 2) + Math.pow(b - thing.y2, 2))!=0)|| (Math.abs(Math.sqrt(Math.pow(a - thing.x3, 2) + Math.pow(b - thing.y3, 2)) + Math.sqrt(Math.pow(a - thing.x4, 2) + Math.pow(b - thing.y4, 2)) - Math.sqrt(Math.pow(thing.x3 - thing.x4, 2)+Math.pow(thing.y3 - thing.y4, 2)))<0.01&&Math.sqrt(Math.pow(a - thing.x3, 2) + Math.pow(b - thing.y3, 2))!=0&&Math.sqrt(Math.pow(a - thing.x4, 2) + Math.pow(b - thing.y4, 2))!=0)) System.out.println(a + "," + b + " " + "true"); else System.out.println(a + "," + b + " " + "false"); } } } else System.out.println("Wrong Format"); } } class calculating { double x1, y1, x2, y2, x3, y3, x4, y4,x5,y5; String gre1 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; String gre2 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; String gre3 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; String gre4 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; Scanner in = new Scanner(System.in); String str = in.nextLine(); String[] array = str.split(","); int momma_number = array.length - 1;int count=0; String[] str1=str.split(":",2); String[] temp=str1[1].split(" "); int geshi_num=geshi(temp); public int geshi(String[] temp1){ for (String s : temp1) { if (!s.matches(gre1)) count++; } return count; } public void panduan(int num) { if (geshi_num>0) { System.out.println("Wrong Format"); return; } else if (geshi_num==0 && momma_number != num) { System.out.println("wrong number of points"); return; } fuzhi(); } public void fuzhi(){ String[] array1 = str1[1].split(",", 2); x1 = Double.parseDouble(array1[0]); String[] array2 = array1[1].split(" ", 2); y1 = Double.parseDouble(array2[0]); String[] array3 = array2[1].split(",", 2); x2 = Double.parseDouble(array3[0]); String[] array4 = array3[1].split(" ", 2); y2 = Double.parseDouble(array4[0]); if(momma_number==2)return; String[] array5 = array4[1].split(",", 2); x3 = Double.parseDouble(array5[0]); String[] array6 = array5[1].split(" ", 2); y3 = Double.parseDouble(array6[0]); if(momma_number==3)return; String[] array7 = array6[1].split(",", 2); x4 = Double.parseDouble(array7[0]); String[] array8 = array7[1].split(" ", 2); y4 = Double.parseDouble(array8[0]); if(momma_number==4)return; String[] array9 = array8[1].split(",", 2); x5 = Double.parseDouble(array9[0]); y5 = Double.parseDouble(array9[1]); } }
题目有五个选项,我想到的就是五个if条件或者五个case,分支里面该调用的就调用。和上一题类似的就是是一题的判断,我把它直接拿过来,因为这题需要坐标计算,我还把分割坐标放进了判断类里。首先用类里的输入,并且切割坐标,然后返回主函数,进入分支,进行进一步判断。分割坐标我用了split函数对字符串切割,保存在字符串数组,再用Double.parseDouble对字符串转化位double型,然后赋值给每一个坐标的x,y。(这里每一个class还是要有自己的分工比较好)这里有一个样例最后是带空格的,他不是坑,做到后面才知道有这样一个样例,以至于更改了正则表达式(作业4用的终极正则表达式([1-9]:)([+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? ?)+"),这题我用的正则表达式还是比较麻烦,只能有几个点表达式就有多长。主函数分支就按面向过程做了套用数学公式直接计算。
代码质量

行数117
声明 113
分支报表百分比 30.1
方法调用语句 67
带注释的行的百分比 0.0
类和接口 2
每类 2.00 的方法数
每种方法的平均语句数 23.00
最复杂方法的行号 5
最复杂方法名称 main.main()
最大复杂度 51
最深块的行数 20
最大块深度 5
平均区块深度 2.67
平均复杂度 15.50
类图:

3,7-3 点线形系列3-三角形的计算
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The
point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0
选项4中所输入线的两个点坐标重合,输出"points coincide",
这题的代码就比较长了
import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void Print(double data){ DecimalFormat df = new DecimalFormat("0.0#####"); df.setRoundingMode(RoundingMode.HALF_UP); System.out.print(df.format(data)); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine();//输入一串字符 judgment judging = new judgment(str);//进行格式判断以及点数判断 if(judging.f)return; char num = str.charAt(0);//提取冒号前的选项 String[]str1=str.split(":",2); String new_str=str1[1];//冒号后的字符串new_str dot the_dot=new dot(new_str); the_dot.sf(new_str);//对new_str进行点的采取 if (num == '1') { judging.dot_num(3); if (!judging.k) { triangle tri=new triangle(the_dot.dots[1], the_dot.dots[2],the_dot.dots[3]); if(tri.flag==1)return; line len1_2=new line(the_dot.dots[1], the_dot.dots[2]); line len1_3=new line(the_dot.dots[1], the_dot.dots[3]); line len2_3=new line(the_dot.dots[2], the_dot.dots[3]); if(len1_2.len==len1_3.len||len1_2.len==len2_3.len||len2_3.len==len1_3.len) System.out.print("true"+" "); else System.out.print("false"+" "); if(len1_2.len==len1_3.len&&len1_2.len==len2_3.len) System.out.println("true"); else System.out.println("false"); } } else if (num == '2') { judging.dot_num(3); if (!judging.k) { triangle tri=new triangle(the_dot.dots[1], the_dot.dots[2],the_dot.dots[3]); if(tri.flag==1)return; line len1_2=new line(the_dot.dots[1], the_dot.dots[2]); line len1_3=new line(the_dot.dots[1], the_dot.dots[3]); line len2_3=new line(the_dot.dots[2], the_dot.dots[3]); DecimalFormat df = new DecimalFormat("#0.000000"); double l=len1_2.len+len1_3.len+len2_3.len; double p=(len1_2.len+len2_3.len+len1_3.len)/2; double s= Math.sqrt(p*(p-len1_3.len)*(p-len1_2.len)*(p-len2_3.len)); double x=(the_dot.dots[1].x+the_dot.dots[2].x+the_dot.dots[3].x)/3; double y=(the_dot.dots[1].y+the_dot.dots[2].y+the_dot.dots[3].y)/3; Print(l); System.out.print(" "); Print(s); System.out.print(" "); Print(x); System.out.print(","); Print(y); } } else if (num == '3') { judging.dot_num(3); if (!judging.k) { triangle tri=new triangle(the_dot.dots[1], the_dot.dots[2],the_dot.dots[3]); if(tri.flag==1)return; line len1_2=new line(the_dot.dots[1], the_dot.dots[2]); line len1_3=new line(the_dot.dots[1], the_dot.dots[3]); line len2_3=new line(the_dot.dots[2], the_dot.dots[3]); double a = len1_2.len, b = len1_3.len, c = len2_3.len,k=0; double max_len=Math.max(Math.max(a,b),Math.max(c,b)); if(max_len==a)k=((b*b)+(c*c)-(a*a))/(2*b*c); else if(max_len==b)k=((c*c)+(a*a)-(b*b))/(2*c*a); else k=((a*a)+(b*b)-(c*c))/(2*a*b); if (Math.toDegrees(Math.acos(k))>90.001) System.out.print("true" + " "); else System.out.print("false" + " "); if (Math.toDegrees(Math.acos(k))<90.0000000000001&&Math.toDegrees(Math.acos(k))>=90) System.out.print("true" + " "); else System.out.print("false" + " "); if (Math.toDegrees(Math.acos(k))<90) System.out.print("true"); else System.out.print("false"); } } else if (num == '4') { judging.dot_num(5); if (!judging.k) { line ab_3=new line(the_dot.dots[1],the_dot.dots[2],the_dot.dots[3]); double l3_ab=ab_3.len_c_ab; line ab_4=new line(the_dot.dots[1],the_dot.dots[2],the_dot.dots[4]); double l4_ab=ab_4.len_c_ab; line ab_5=new line(the_dot.dots[1],the_dot.dots[2],the_dot.dots[5]); double l5_ab=ab_5.len_c_ab; double lab=ab_5.len_ab; if(lab==0) {System.out.println("points coincide");return;} triangle tri=new triangle(the_dot.dots[3],the_dot.dots[4],the_dot.dots[5]); if(tri.flag==1)return; else if((l3_ab==0&&l4_ab==0)||(l5_ab==0&&l4_ab==0)||(l3_ab==0&&l5_ab==0)) System.out.println("The point is on the edge of the triangle"); else{ line l=new line(the_dot.dots[1],the_dot.dots[2]); triangle exp=new triangle(l,the_dot.dots[3],the_dot.dots[4],the_dot.dots[5]); } } } else if (num == '5') { judging.dot_num(4); if (!judging.k) { triangle tri=new triangle(the_dot.dots[2],the_dot.dots[3],the_dot.dots[4]); if(tri.flag==1)return; triangle lsp=new triangle(the_dot.dots[1],the_dot.dots[2],the_dot.dots[3],the_dot.dots[4]); } } } } class triangle { public static void Print(double data){ DecimalFormat df = new DecimalFormat("0.0#####"); df.setRoundingMode(RoundingMode.HALF_UP); System.out.print(df.format(data)); } point a;point b;point c;int flag=0; triangle(point a,point b,point c){//三个点判断能不能构成三角形 this.a=a; this.b=b; this.c=c; line a_b=new line(a,b); line a_c=new line(a,c); line b_c=new line(b,c); if(a_b.len + a_c.len<=b_c.len||a_b.len +b_c.len <=a_c.len||b_c.len + a_c.len<=a_b.len) { System.out.println("data error");flag=1; } } triangle(line l,point a,point b,point c){//一线三点求面积以及判断有没有交点 point m=new point(l.x1,l.y1); point n=new point(l.x2,l.y2); line l1=new line(m,n,a,b); line l2=new line(m,n,a,c); line l3=new line(m,n,b,c); if(((l.A*a.x)+(l.B*a.y)+l.C==0||(l.A*b.x)+(l.B*b.y)+l.C==0||(l.A*c.x)+(l.B*c.y)+l.C==0)&&!l1.jud&&!l2.jud&&!l3.jud) {System.out.println(1);return;} if((l.A*a.x)+(l.B*a.y)+l.C==0) { line h=new line(b,c,a);//高 line lab1=new line(m,n,b,c);//返回交点 point new_point=new point(lab1.jiaodian_a, lab1.jiaodian_b); line len1=new line(b,new_point); double s1=h.len_c_ab*len1.len/2; line len2=new line(c,new_point); double s2=h.len_c_ab*len2.len/2; System.out.println(2+" "); Print(Math.min(s1,s2)); System.out.print(" "); Print(Math.max(s1,s2)); } else if((l.A*b.x)+(l.B*b.y)+l.C==0) { line h=new line(a,c,b);//高 line lab1=new line(m,n,a,c);//返回交点 point new_point=new point(lab1.jiaodian_a, lab1.jiaodian_b); line len1=new line(a,new_point); double s1=h.len_c_ab*len1.len/2; line len2=new line(c,new_point); double s2=h.len_c_ab*len2.len/2; System.out.println(2+" "); Print(Math.min(s1,s2)); System.out.print(" "); Print(Math.max(s1,s2)); } else if((l.A*c.x)+(l.B*c.y)+l.C==0) { line h=new line(a,b,c);//高 line lab1=new line(m,n,a,b);//返回交点 point new_point=new point(lab1.jiaodian_a, lab1.jiaodian_b); line len1=new line(a,new_point); double s1=h.len_c_ab*len1.len/2; line len2=new line(b,new_point); double s2=h.len_c_ab*len2.len/2; System.out.println(2+" "); Print(Math.min(s1,s2)); System.out.print(" "); Print(Math.max(s1,s2)); } else{ if(!l1.jud&&!l2.jud&&!l3.jud) {System.out.println(0);return;} if(l1.jud&&l2.jud){ line lab1=new line(m,n,a,b);//返回交点 point new_point1=new point(lab1.jiaodian_a, lab1.jiaodian_b); line lab2=new line(m,n,a,c);//返回交点 point new_point2=new point(lab2.jiaodian_a, lab2.jiaodian_b); line h=new line(new_point1,new_point2,a);//顶到截线 double s1=h.len_c_ab*h.len_ab/2; line bottom=new line(b,c,a);//顶到底 double s2= bottom.len_c_ab* bottom.len_ab/2-s1; System.out.println(2+" "); Print(Math.min(s1,s2)); System.out.print(" "); Print(Math.max(s1,s2)); } if(l1.jud&&l3.jud) { line lab1=new line(m,n,b,a);//返回交点 point new_point1=new point(lab1.jiaodian_a, lab1.jiaodian_b); line lab2=new line(m,n,b,c);//返回交点 point new_point2=new point(lab2.jiaodian_a, lab2.jiaodian_b); line h=new line(new_point1,new_point2,b);//顶到截线 double s1=h.len_c_ab*h.len_ab/2; line bottom=new line(a,c,b);//顶到底 double s2= bottom.len_c_ab* bottom.len_ab/2-s1; System.out.println(2+" "); Print(Math.min(s1,s2)); System.out.print(" "); Print(Math.max(s1,s2)); } if(l2.jud&&l3.jud) { line lab1=new line(m,n,c,a);//返回交点 point new_point1=new point(lab1.jiaodian_a, lab1.jiaodian_b); line lab2=new line(m,n,c,b);//返回交点 point new_point2=new point(lab2.jiaodian_a, lab2.jiaodian_b); line h=new line(new_point1,new_point2,c);//顶到截线 double s1=h.len_c_ab*h.len_ab/2; line bottom=new line(a,b,c);//顶到底 double s2= bottom.len_c_ab* bottom.len_ab/2-s1; System.out.println(2+" "); Print(Math.min(s1,s2)); System.out.print(" "); Print(Math.max(s1,s2)); } } } triangle(point m,point a,point b,point c){ line lab=new line(a,b); line lac=new line(a,c); line lbc=new line(b,c); if(lab.A*m.x+lab.B*m.y+lab.C==0||lab.A*m.x+lac.B*m.y+lac.C==0||lab.A*m.x+lbc.B*m.y+lbc.C==0) { System.out.println("on the triangle");return; } line lab1=new line(a,b,m); line lac1=new line(a,c,m); line lbc1=new line(b,c,m); line s=new line(a,b,c); if(lab1.len_c_ab*lab1.len_ab+lac1.len_c_ab*lac1.len_ab+lbc1.len_c_ab*lbc1.len_ab ==s.len_ab*s.len_c_ab) System.out.println("in the triangle"); else System.out.println("outof the triangle"); } } class line { double len,x1,x2,y1,y2,x3,y3,x4,y4,len_ab,len_c_ab; double A,B,C; double jiaodian_a,jiaodian_b;boolean jud=false; line(){} line(point a,point b){//两个点算直线方程,算两点长度 this.x1=a.x;this.x2=b.x;this.y1=a.y;this.y2=b.y; this.len=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); A = y2 - y1;B = x1- x2;C = x2 * y1 - x1 * y2; } line(point a,point b,point c){//三个点算ab长度,算点c到ab的长度 this.x1=a.x;this.x2=b.x;this.x3=c.x;this.y1=a.y;this.y2=b.y;this.y3=c.y; len_ab=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); len_c_ab =(Math.abs((y2 - y1) * x3 +(x1 - x2) * y3 + ((x2 * y1) -(x1 * y2))))/Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x1 - x2, 2)); } line(point dot1,point dot2,point dot3,point dot4){//判断两条直线有没有交点 this.x1=dot1.x;this.x2=dot2.x;this.x3=dot3.x;this.x4=dot4.x; this.y1=dot1.y;this.y2=dot2.y;this.y3=dot3.y;this.y4=dot4.y; jiaodian_a=(y3*x4*x2-y4*x3*x2-y3*x4*x1+y4*x3*x1-y1*x2*x4+y2*x1*x4+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3); jiaodian_b=(-y3*x4*y2+y4*x3*y2+y3*x4*y1-y4*x3*y1+y1*x2*y4-y1*x2*y3-y2*x1*y4+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3); if(Math.abs(Math.sqrt(Math.pow(jiaodian_a - x3, 2) + Math.pow(jiaodian_b- y3, 2)) + Math.sqrt(Math.pow(jiaodian_a - x4, 2) + Math.pow(jiaodian_b - y4, 2)) - Math.sqrt(Math.pow(x3 - x4, 2)+Math.pow(y3 - y4, 2)))<0.01 &&Math.sqrt(Math.pow(jiaodian_a - x3, 2) + Math.pow(jiaodian_b- y3, 2))!=0&&Math.sqrt(Math.pow(jiaodian_a - x4, 2) + Math.pow(jiaodian_b- y4, 2))!=0) jud=true; } } class judgment{ boolean f=false,k=false;String str; String gre = "([1-9]:)?([+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? )+[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; judgment(String str){ this.str=str; if(!str.matches(gre)) {System.out.println("Wrong Format");f=true;} } public void dot_num(int point_num){ String[] array=str.split(" "); if(array.length!=point_num) {System.out.println("wrong number of points");k=true;} } } class dot { String str; dot(String str){ this.str=str; } int momma_num; point[] dots= new point[6]; String[] array=new String[30]; public void sf(String str){ array[1]=str; String[] temp=str.split(","); momma_num=temp.length-1; for(int i=1;i<6;i++) { array=array[1].split(",",2); double a=Double.parseDouble(array[0]); array=array[1].split(" ",2); double b=Double.parseDouble(array[0]); dots[i]=new point(a,b);if(momma_num==i)return; } } } class point{ double x,y; point(double x,double y){ this.x=x; this.y=y; } }
较于上一题这题我改进了很多,就正则表达式而言不再用一个点一个点的复制了,中间采用加号,可以只有一个点,可以有多个点。输入我把它放到了主函数,输入完就有一个判断类,格式错误的直接输出。然后点的分割我把他放进另一个类,并且加了一个点类,用来储存分割出来的点,接着把字符串第一位切下来看看要进入哪个分支。除了点类,我还增加了线类,传进点去计算长度,斜率,表达式,交点,还有点到线的距离都可以计算。最后还增加了三角形类,三个点判断是不是三角形,四个点判断点在不在三角形内。最后还有一个输出方法,就是题目要求的保留六位小数,只需要把DecimalFormat设置成"0.0#####" setRoundingMode 的模式位RoundingMode.HALF_UP就可以完美把小数后六位四舍五入,不足六位的保留。这题难度还是挺大的,主要是对与类的设计,考虑好怎么样设计才会有最优算法。
本题代码质量

行数361
声明 292
分支报表百分比 15.8
方法调用语句 91
带注释的行数百分比 7.6
类和接口 6
每类方法 2.83
每个方法的平均语句数 14.76
最复杂方法的行数 13
最复杂方法名称 main.main()
最大复杂度 41
最深块的行数 92
最大块深度 5
平均区块深度 2.83
平均复杂度 5.59
类图:

三,踩坑心得
第二次7-2,这题的坑在于奇偶校验,注意题目说的本题默认奇校验,我原本是这样想的,只要判断第九位是是不是1就完事了,但是测试点就是过不去。。。
if(str.charAt(count+9)=='1'&&str.charAt(count+10)=='1') { System.out.println(temp+1+":"+str.substring(count+1,count+9));temp++;i+=10;} else if(str.charAt(count+9)=='0'&&str.charAt(count+10)=='1') {System.out.println(temp+1+":"+"parity check error");temp++;i+=10;} else {System.out.println(temp+1+":"+"validate error");temp++;i+=10;}

原来根本没有那么简单,还需要测试1的个数进一步判断奇偶校验位是不是正确的。
for (i = 0; i < str.length(); i++) { if (str.charAt(i) == '1') count1++; } if (count1 == str.length() || str.length() < 11) System.out.println("null data"); else { for (i = 0; i < str.length(); i++) { if (str.charAt(i) == '1') continue; count = i; if ((str.substring(count)).length() < 11) return; else { for (int j = count + 1; j <= count + 8; j++) { if (str.charAt(j) == '0') sum++; } sum = sum % 2; if (((str.charAt(count + 9) == '1' && sum == 0) || (str.charAt(count + 9) == '0' && sum == 1)) && str.charAt(count + 10) == '1') { System.out.println(temp + 1 + ":" + str.substring(count + 1, count + 9)); temp++; i += 10; } else if (((str.charAt(count + 9) == '0' && sum == 0) || (str.charAt(count + 9) == '1' && sum == 1)) && str.charAt(count + 10) == '1') { System.out.println(temp + 1 + ":" + "parity check error"); temp++; i += 10; } else { System.out.println(temp + 1 + ":" + "validate error"); temp++; i += 10; } }sum=0;

最后终于正确!
第三次作业7-2
注意原文表述选项5:输出交叉点是否在两条线段之内,我的理解:两条直线有交点,交点在一定要在两条线段内。题目意思:两条直线有交点,交点可以在其中一条线段上也可以在了一条线段上,甚至可以在两条线段中。
我原本是这样写的:
if (Math.abs(Math.sqrt(Math.pow(a - thing.x1, 2) + Math.pow(b - thing.y1, 2)) + Math.sqrt(Math.pow(a - thing.x2, 2) + Math.pow(b - thing.y2, 2)) - Math.sqrt(Math.pow(thing.x1 - thing.x2, 2)+Math.pow(thing.y1 - thing.y2, 2)))<0.01&& Math.abs(Math.sqrt(Math.pow(a - thing.x3, 2) + Math.pow(b - thing.y3, 2)) + Math.sqrt(Math.pow(a - thing.x4, 2) + Math.pow(b - thing.y4, 2)) - Math.sqrt(Math.pow(thing.x3 - thing.x4, 2)+Math.pow(thing.y3 - thing.y4, 2)))<0.01) System.out.println(a + "," + b + " " + "true"); else System.out.println(a + "," + b + " " + "false");
甚至还没有考虑端点,中间用的且

后来经过改写
if ((Math.abs(Math.sqrt(Math.pow(a - thing.x1, 2) + Math.pow(b - thing.y1, 2)) + Math.sqrt(Math.pow(a - thing.x2, 2) + Math.pow(b - thing.y2, 2)) - Math.sqrt(Math.pow(thing.x1 - thing.x2, 2)+Math.pow(thing.y1 - thing.y2, 2)))<0.01&&Math.sqrt(Math.pow(a - thing.x1, 2) + Math.pow(b - thing.y1, 2))!=0&&Math.sqrt(Math.pow(a - thing.x2, 2) + Math.pow(b - thing.y2, 2))!=0)|| (Math.abs(Math.sqrt(Math.pow(a - thing.x3, 2) + Math.pow(b - thing.y3, 2)) + Math.sqrt(Math.pow(a - thing.x4, 2) + Math.pow(b - thing.y4, 2)) - Math.sqrt(Math.pow(thing.x3 - thing.x4, 2)+Math.pow(thing.y3 - thing.y4, 2)))<0.01&&Math.sqrt(Math.pow(a - thing.x3, 2) + Math.pow(b - thing.y3, 2))!=0&&Math.sqrt(Math.pow(a - thing.x4, 2) + Math.pow(b - thing.y4, 2))!=0)) System.out.println(a + "," + b + " " + "true"); else System.out.println(a + "," + b + " " + "false");
不包含端点中间用或

第三次作业7-3
这个题有一个基本格式错误困扰了很久

最后想不到错误在哪,应该就是有情况没有判断到。看看我原来写的判断
class judgment{ String gre1 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; String gre3 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; String gre4 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; String gre5 = "[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? [+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; String str; String[] str1 ,temp ,array; int momma_number,count = 0,geshi_num;boolean f=false; judgment(String str){ str1 = str.split(":", 2); temp = str1[1].split(" "); geshi_num = geshi(temp); this.str=str; array = str.split(","); momma_number = array.length - 1; } public int geshi(String[] temp1) {//判断最小点单位与最小点单位正则 for (String s : temp1) {f=true; if (!s.matches(gre1)) count++; }if(!f)return 1; return count; } public void panduan(int point_num) {//判断格式与点数符不符合 if (geshi_num > 0) { System.out.println("Wrong Format"); } else if (geshi_num == 0 && momma_number != point_num) { System.out.println("wrong number of points"); } } }

两个点就写两个表达式,三个点就写三个表达式,本来就写的太麻烦了,而且就还有错误,这里就重写了这个部分解决这个错误,全部输入都可以用一行表达式匹配。
class judgment{ boolean f=false,k=false;String str; String gre = "([1-9]:)?([+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)? )+[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?"; judgment(String str){ this.str=str; if(!str.matches(gre)) {System.out.println("Wrong Format");f=true;} } public void dot_num(int point_num){ String[] array=str.split(" "); if(array.length!=point_num) {System.out.println("wrong number of points");k=true;} } }
写完这个以后代码简单且目的明确,提交果然就全对了

四,改进建议
对于构造器的用法我也是刚刚接触,在最后一题中,line类,接收一组数据我就在构造器里进行大量过程,其实这是很不美观的,对于面向对象来说也是很过程化的。后面需要改进的是构造器接收完数据要创建一种方法,用这个方法去过程化,才能做到目的明确且美观。此外,在第三题的三角形类里的面积计算中,我采用的是用传进来的点直接原来计算,而且是那种重复同一种方法的计算,重复冗长,完全可以把传进来的点存储到数组,用数组循环判断是哪种类型,带哪些点进入其他类。计算面积的算法甚至还可以不用那么麻烦,比如可以用三个数组,分别存储1:直线与三角形的交点2:直线一边的三角形端点3:直线另一边的三角形端点。然后判断直线与三角形的有没有是三角形端点的,如果有就是一种算法,如果没有就是另一种算法。最后,类的分类尽量要明确,不要像第一题那样(第二次作业7-2),不仅判断了格式准不准确,甚至还在下面切割点用变累存储起来,这样是不符合面向对象的,我们要做到类的分工明确,简单,其中的方法也要简短,如果方法很长,可能是自己的算法有问题(这些题目算法用对真的很重要,你的算法决定你的代码长度)。
五,总结
在这之前我对java面向对象还是一个很模糊的概念,经过这三次练习,我慢慢学会了使用类,我也慢慢理解到了class的可移植化,例如在第三次作业里,判断类和点分割类就是可以给7-3用的;Java的面向对象性真的很强,分几个class,给与每个class特定的任务,而且每个类只需要完成各自的任务整个事件就能完整运行。

浙公网安备 33010602011771号