Java题目集1~3总结
1.前言:
前三次题目集主要关于java语言的基本程序设计,知识点涉及从最基础的命名变量到对象和类,其中包括了选择结构,循环结构,数学函数,数组,字符和字符串以及方法的定义与调用,正则表达式的使用,还有一些其他的类的方法的使用等内容。题量适中,均能在规定时间内做完,但不一定能做全对。难度循序渐进,从第一次的基础题目到第三次的进阶题目,普遍基本思路不难,但是一开始敲击代码就会发现细节方面很难把握到位,尤其是有关于输入测试的部分尤为突出,明显很难考虑到每一个方面以至于无法拿到满分,总会有测试点过不了(并且由于大部分测试点没有提示,需要分析考虑的更加全面以及反复反反复复。。。的试错)。个人认为第三次题目最难,不但体现于输入方面,在整体计算中也有体现,涉及不少数学知识,无论是第二道题目的直线之前的关系还是第三道题的三角形有关计算都不简单,转化为代码后更能复杂,需要更多的时间来分析思考。题目量随难度成反比,但是每道题目都很又代表性,值得深入去思考和反复修改。特别是第三次作业,虽然只有三道题,但是每道题的难度都很大,且题目之间层层递进,每道题都建立在上道题的基础上而衍生出新的问题。对于初步学习Java的我来说很具有挑战性。
总的来说,Java又是我们新接触的编程语言,对Java语言的运用还有些许生疏,对语法的理解和java整体结构把握不够清晰以及有关方面的设计还不清晰,需要加强训练以求熟练能够更好的应用。
2.设计与分析:
7-2 串口字符解析
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args){ 5 String s; 6 Scanner sc= new Scanner(System.in); 7 s=sc.next(); 8 boolean flag=false; 9 int count=0,i=0,num=1; 10 if(s.length()<11) { 11 System.out.print("null data"); 12 return; 13 } 14 for(i=0;i<s.length();i++){ 15 if(s.charAt(i)=='0'){ 16 flag=true; 17 break; 18 } 19 } 20 if(!flag){ 21 System.out.print("null data"); 22 return; 23 } 24 for(i=0;i<s.length()-10;i++){ 25 if(s.charAt(i)=='0'){ 26 System.out.print(num+":"); 27 num++; 28 if(s.charAt(i+10)=='1'){ 29 count=0; 30 for(int j=i+1;j<=i+8;j++){ 31 if(s.charAt(j)=='1'){ 32 count++; 33 } 34 } 35 if(((count%2==0)&&(s.charAt(i+9)=='1'))||((count%2!=0)&&(s.charAt(i+9)=='0'))){ 36 System.out.println(s.substring(i+1, i+9)); 37 } 38 else{ 39 System.out.println("parity check error"); 40 } 41 } 42 else{ 43 System.out.println("validate error"); 44 } 45 i+=10; 46 } 47 } 48 } 49 }
本题难点主要体现在对RS232以及奇偶校验位应用的陌生,以及对模拟串口接收处理程序的陌生。
代码分析与处理:
1.判断输入中的数据是否大于11位,若小于11位则数据中一定没有有效数据。
2.判断数据是否有"0"位。
3.开始找首次出现的"0"位,找到"0"后开始对他后面10位做处理判断。
4.先判断最后一位是不是结束1,再判断是否奇校验正确,最后通过两次判断结果来输出。
7-1 点线形系列1-计算两点之间的距离
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args){ 5 Scanner sc=new Scanner(System.in); 6 String point=sc.nextLine(); 7 String[] num =null; 8 double x1,y1,x2,y2; 9 if(point.length()<7){ 10 System.out.print("Wrong Format"); 11 return; 12 } 13 else{ 14 String[] data = point.split(" "); 15 for(int i=0;i<data.length;i++){ 16 num = data[i].split(","); 17 for(int j=0;j<num.length;j++){ 18 if(!num[j].matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")){ 19 System.out.print("Wrong Format"); 20 return; 21 } 22 if(num[j].equals("0.0")){ 23 System.out.print("Wrong Format"); 24 return; 25 } 26 } 27 } 28 if(data.length!=2){ 29 System.out.print("wrong number of points"); 30 return; 31 } 32 num=data[0].split(","); 33 x1=Double.valueOf(num[0]); 34 y1=Double.valueOf(num[1]); 35 num=data[1].split(","); 36 x2=Double.valueOf(num[0]); 37 y2=Double.valueOf(num[1]); 38 if(x1==x2&&y1==y2){ 39 System.out.print("Wrong Format"); 40 return; 41 } 42 System.out.print(Math.sqrt((Math.pow((x1-x2),2)+Math.pow((y1-y2),2)))); 43 } 44 } 45 }
代码分析与处理:
1.通过判断输入的数据是否大于7位,若小于7位则数据中一定没有有效数据。
2.可以通过判断每一个字符的首位来判断是否符合"-"或者"+"同时判断下一位是否为数字(如为小数之后加上判断是否为小数点'.',之后判断下一位是否为数字,以及之后无其他符号出现后结束判断),来确定输入数据是否符合正确的格式。
3.或者直接通过判断是否正确符合正则表达式,来判断是否符合正确需要的格式。
4.之后通过判断点的个数和两点重复等特殊情况来判断,最后得出结果。
分析报告:

7-2 点线形系列2-线的计算
1 import java.util.Scanner; 2 public class Main{ 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 int x1, y1, x2, y2; 6 int count=0,cnt=0; 7 String point = sc.nextLine(); 8 String data[] = null; 9 if (point.charAt(0) >= '1' && point.charAt(0) <= '5' && point.charAt(1) == ':') { 10 for (int i=2;i<point.length();i++){ 11 if(point.charAt(i)==' '){ 12 cnt++; 13 } 14 if(point.charAt(i)==','){ 15 count++; 16 } 17 } 18 if(count-1!=cnt){ 19 System.out.print("Wrong Format"); 20 return; 21 } 22 data = point.split(":|,| "); 23 for (int i = 1; i < data.length; i++){ 24 if(data[i].equals("0.00")||data[i].equals("")){ 25 System.out.print("Wrong Format"); 26 return; 27 } 28 if (!data[i].matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")){ 29 System.out.print("Wrong Format"); 30 return; 31 } 32 } 33 } else { 34 System.out.print("Wrong Format"); 35 return; 36 } 37 switch(data[0]){ 38 case "1": 39 Slope(data); 40 break; 41 case "2": 42 Vertical(data); 43 break; 44 case "3": 45 ISonline(data); 46 break; 47 case "4": 48 ISparallel(data); 49 break; 50 default: 51 ISnode(data); 52 break; 53 } 54 } 55 public static boolean ISsame(Double x1,Double y1,Double x2,Double y2){ 56 if(x1.equals(x2)&&y1.equals(y2)){ 57 return true; 58 } else { 59 return false; 60 } 61 } 62 63 public static void Slope(String[] num) { 64 if(num.length != 5) { 65 System.out.print("wrong number of points"); 66 return; 67 } 68 Double[] arr = new Double[20]; 69 for (int i = 1; i < num.length; i++) { 70 arr[i] = Double.valueOf(num[i]); 71 } 72 if(ISsame(arr[1],arr[2],arr[3],arr[4])){ 73 System.out.print("points coincide"); 74 return; 75 } 76 else if(arr[1].equals(arr[3])) { 77 System.out.print("Slope does not exist"); 78 return; 79 } else { 80 double result = (arr[4] - arr[2]) / (arr[3] - arr[1]); 81 System.out.print(result); 82 } 83 } 84 85 public static void Vertical(String[] num){ 86 if(num.length != 7){ 87 System.out.print("wrong number of points"); 88 return; 89 } 90 Double[] arr = new Double[20]; 91 for (int i = 1; i < num.length; i++) { 92 arr[i] = Double.valueOf(num[i]); 93 } 94 if(ISsame(arr[1],arr[2],arr[3],arr[4])||ISsame(arr[1],arr[2],arr[5],arr[6])||ISsame(arr[3],arr[4],arr[5],arr[6])){ 95 System.out.print("points coincide"); 96 return; 97 } 98 else{ 99 double a,b,c,result; 100 a=arr[4]-arr[6]; 101 b=arr[5]-arr[3]; 102 c=(arr[4]+arr[6])*arr[3]-(arr[3]+arr[5])*arr[4]; 103 result=Math.abs((a*arr[1]+b*arr[2]+c)/Math.sqrt(a*a+b*b)); 104 System.out.print(result); 105 } 106 } 107 public static void ISonline(String[] num){ 108 if(num.length != 7){ 109 System.out.print("wrong number of points"); 110 return; 111 } 112 Double[] arr = new Double[20]; 113 for (int i = 1; i < num.length; i++) { 114 arr[i] = Double.valueOf(num[i]); 115 } 116 if(ISsame(arr[1],arr[2],arr[3],arr[4])||ISsame(arr[1],arr[2],arr[5],arr[6])||ISsame(arr[3],arr[4],arr[5],arr[6])){ 117 System.out.print("points coincide"); 118 return; 119 } 120 else{ 121 double a,b,c; 122 boolean flag=false; 123 a=arr[4]-arr[6]; 124 b=arr[5]-arr[3]; 125 c=arr[6]*arr[3]-arr[5]*arr[4]; 126 if((a*arr[1]+b*arr[2]+c)==0){ 127 System.out.print("true"); 128 } 129 else{ 130 System.out.print("false"); 131 } 132 } 133 } 134 135 public static void ISparallel(String[] num){ 136 if(num.length != 9){ 137 System.out.print("wrong number of points"); 138 return; 139 } 140 Double[] arr = new Double[20]; 141 for (int i = 1; i < num.length; i++) { 142 arr[i] = Double.valueOf(num[i]); 143 } 144 if(ISsame(arr[1],arr[2],arr[3],arr[4])||ISsame(arr[1],arr[2],arr[5],arr[6])||ISsame(arr[1],arr[2],arr[7],arr[8])||ISsame(arr[3],arr[4],arr[5],arr[6])||ISsame(arr[3],arr[4],arr[7],arr[8])||ISsame(arr[5],arr[6],arr[7],arr[8])){ 145 System.out.print("points coincide"); 146 return; 147 } 148 else{ 149 double ax=0,ay=0,bx=0,by=0; 150 ax = arr[3]-arr[1]; 151 ay = arr[4]-arr[2]; 152 bx = arr[7]-arr[5]; 153 by = arr[8]-arr[6]; 154 if(ax * by - bx * ay==0){ 155 System.out.print("true"); 156 } 157 else{ 158 System.out.print("false"); 159 } 160 } 161 } 162 163 public static void ISnode(String[] num){ 164 if(num.length != 9){ 165 System.out.print("wrong number of points"); 166 return; 167 } 168 double[] arr = new double[20]; 169 for (int i = 1; i < num.length; i++) { 170 arr[i] = Double.valueOf(num[i]); 171 } 172 if(ISsame(arr[1],arr[2],arr[3],arr[4])||ISsame(arr[1],arr[2],arr[5],arr[6])||ISsame(arr[1],arr[2],arr[7],arr[8])||ISsame(arr[3],arr[4],arr[5],arr[6])||ISsame(arr[3],arr[4],arr[7],arr[8])||ISsame(arr[5],arr[6],arr[7],arr[8])){ 173 System.out.print("points coincide"); 174 return; 175 } 176 else{ 177 double ax=0,ay=0,bx=0,by=0; 178 ax = arr[3]-arr[1]; 179 ay = arr[4]-arr[2]; 180 bx = arr[7]-arr[5]; 181 by = arr[8]-arr[6]; 182 if(ax * by - bx * ay==0){ 183 System.out.print("is parallel lines,have no intersection point"); 184 return; 185 } 186 else{ 187 double x,y; 188 /* A = Y2 - Y1 189 B = X1 - X2 190 C = X2*Y1 - X1*Y2 191 */ 192 double a1=arr[4]-arr[2]; 193 double b1=arr[1]-arr[3]; 194 double c1=arr[3]*arr[2]-arr[1]*arr[4]; 195 double a2=arr[8]-arr[6]; 196 double b2=arr[5]-arr[7]; 197 double c2=arr[7]*arr[6]-arr[5]*arr[8]; 198 x=(b2*c1-b1*c2)/(a2*b1-a1*b2); 199 y=(a1*c2-a2*c1)/(a2*b1-a1*b2); 200 double x1= Math.max(arr[1],arr[3]); 201 double x2= Math.max(arr[5],arr[7]); 202 double x3= Math.min(arr[1],arr[3]); 203 double x4= Math.min(arr[5],arr[7]); 204 System.out.print(x+","+y); 205 if((x>x3||x>x4)&&(x<x1||x<x2)){ 206 System.out.print(" true"); 207 } 208 else{ 209 System.out.print(" false"); 210 } 211 } 212 } 213 } 214 }
代码分析与处理:
1.输入的判断,输入中有空格和字符","的出现,对输入判断不方便。
2.输入数据第一位必须是数字且为1-5,第二位必须为":"(可作为先行判断的条件)
3.输入的点的坐标必须和输入的第一位数字的对应情况对应。
4.两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。
5.每个数字前"+","-"只能有一个,必须符合基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
6.定义一个字符串,将输入数据存入其中,同时对字符串中的","和空格数量进行对比,对所给数据进行字符串进行切割。
7.之后可对所给数据进行是否符合正则表达式判断,看其是否符合基本格式。
8.再运用switch语句,根据所读入的数字确定为何种情况,根据不同的情况判断输入数据的点数以及坐标是否重合,是否符合规定等等。
9.若为情况1则计算两点的斜率。
若为情况2则运用数学公式,通过所给点坐标计算出距离,并输出。
若为情况3则计算任意两个的斜率是否相等,若相等则输出true反之不相等则输出false。
若为情况4则计算前两个点所构成的直线的斜率是否与后面一点相等(通过向量法来判断),若相等则输出true反之不相等则输出false。
若为情况5则运用数学公式计算两直线交点坐标,并通过交点坐标的x,与其他四个点对比得出是否在两线段之内。
类图:

分析报告:

7-3 点线形系列3-三角形的计算
1 import java.text.DecimalFormat; 2 import java.util.Arrays; 3 import java.util.Scanner; 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner sc = new Scanner(System.in); 7 int x1, y1, x2, y2; 8 int count = 0, cnt = 0; 9 String point = sc.nextLine(); 10 String data[] = null; 11 if (point.charAt(0) >= '1' && point.charAt(0) <= '5' && point.charAt(1) == ':') { 12 for (int i = 2; i < point.length(); i++) { 13 if (point.charAt(i) == ' ') { 14 cnt++; 15 } 16 if (point.charAt(i) == ',') { 17 count++; 18 } 19 } 20 if (count - 1 != cnt) { 21 System.out.print("Wrong Format"); 22 return; 23 } 24 data = point.split(":|,| "); 25 for (int i = 1; i < data.length; i++) { 26 if (!data[i].matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) { 27 System.out.print("Wrong Format"); 28 return; 29 } 30 } 31 } else { 32 System.out.print("Wrong Format"); 33 return; 34 } 35 switch (data[0]) { 36 case "1": 37 Isequilateral_Isosceles(data); 38 break; 39 case "2": 40 Calculate(data); 41 break; 42 case "3": 43 Istriangletype(data); 44 break; 45 case "4": 46 Piontnumber(data); 47 break; 48 default: 49 Isontriangle(data); 50 break; 51 } 52 } 53 54 public static boolean ISsame(Double x1, Double y1, Double x2, Double y2) { 55 if (x1.equals(x2) && y1.equals(y2)) { 56 return true; 57 } else { 58 return false; 59 } 60 } 61 62 public static boolean IStriangle(double x1,double y1,double x2,double y2,double x3,double y3) { 63 double Side_Length1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); 64 double Side_Length2 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2)); 65 double Side_Length3 = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2)); 66 if ((Side_Length1 + Side_Length2 > Side_Length3) && (Side_Length2 + Side_Length3 > Side_Length1) && (Side_Length1 + Side_Length3 > Side_Length2)) { 67 return true; 68 } else { 69 return false; 70 } 71 } 72 73 public static void Isequilateral_Isosceles(String[] num) { 74 if (num.length != 7) { 75 System.out.print("wrong number of points"); 76 return; 77 } 78 Double[] arr = new Double[20]; 79 for (int i = 1; i < num.length; i++) { 80 arr[i] = Double.valueOf(num[i]); 81 } 82 if (!IStriangle(arr[1],arr[2],arr[3],arr[4],arr[5],arr[6])) { 83 System.out.print("data error"); 84 return; 85 } else { 86 double x1 = arr[1], y1 = arr[2]; 87 double x2 = arr[3], y2 = arr[4]; 88 double x3 = arr[5], y3 = arr[6]; 89 double Side_Length1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); 90 double Side_Length2 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2)); 91 double Side_Length3 = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2)); 92 if ((Side_Length1 == Side_Length2) || (Side_Length2 == Side_Length3) || (Side_Length1 == Side_Length3)) { 93 System.out.print("true"); 94 if ((Side_Length1 == Side_Length2) && (Side_Length2 == Side_Length3)) { 95 System.out.print(" true"); 96 } else { 97 System.out.print(" false"); 98 } 99 return; 100 } else { 101 System.out.print("false false"); 102 return; 103 } 104 } 105 } 106 107 public static void Calculate(String[] num) { 108 if (num.length != 7) { 109 System.out.print("wrong number of points"); 110 return; 111 } 112 Double[] arr = new Double[20]; 113 for (int i = 1; i < num.length; i++) { 114 arr[i] = Double.valueOf(num[i]); 115 } 116 if (!IStriangle(arr[1],arr[2],arr[3],arr[4],arr[5],arr[6])) { 117 System.out.print("data error"); 118 return; 119 } else { 120 double x1 = arr[1], y1 = arr[2]; 121 double x2 = arr[3], y2 = arr[4]; 122 double x3 = arr[5], y3 = arr[6]; 123 double Side_Length1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); 124 double Side_Length2 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2)); 125 double Side_Length3 = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2)); 126 double circumference = Side_Length1 + Side_Length2 + Side_Length3; 127 double p = 0.5 * circumference; 128 double area = Math.sqrt(p * (p - Side_Length1) * (p - Side_Length2) * (p - Side_Length3)); 129 double gx = (arr[1] + arr[3] + arr[5]) / 3; 130 double gy = (arr[2] + arr[4] + arr[6]) / 3; 131 System.out.print(new DecimalFormat("0.0#####").format(circumference) + " "); 132 System.out.print(new DecimalFormat("0.0#####").format(area) + " "); 133 System.out.print(new DecimalFormat("0.0#####").format(gx)); 134 System.out.print("," + new DecimalFormat("0.0#####").format(gy)); 135 } 136 } 137 138 public static void Istriangletype(String[] num) { 139 if (num.length != 7) { 140 System.out.print("wrong number of points"); 141 return; 142 } 143 Double[] arr = new Double[20]; 144 for (int i = 1; i < num.length; i++) { 145 arr[i] = Double.valueOf(num[i]); 146 } 147 if (!IStriangle(arr[1],arr[2],arr[3],arr[4],arr[5],arr[6])) { 148 System.out.print("data error"); 149 return; 150 } else { 151 double x1 = arr[1], y1 = arr[2]; 152 double x2 = arr[3], y2 = arr[4]; 153 double x3 = arr[5], y3 = arr[6]; 154 double Side_Length1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); 155 double Side_Length2 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2)); 156 double Side_Length3 = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2)); 157 double[] line = {Side_Length1, Side_Length2, Side_Length3}; 158 Arrays.sort(line); 159 if (Math.pow(line[2], 2) < (Math.pow(line[1], 2) + Math.pow(line[0], 2))) { 160 System.out.print("false false true"); 161 return; 162 } else if (Math.pow(line[2], 2) - Math.pow(line[1], 2) - Math.pow(line[0], 2) < 1e-6) { 163 System.out.print("false true false"); 164 return; 165 } else if (Math.pow(line[2], 2) > (Math.pow(line[1], 2) + Math.pow(line[0], 2))) { 166 System.out.print("true false false"); 167 return; 168 } 169 } 170 } 171 172 public static void Piontnumber(String[] num) { 173 if (num.length != 11) { 174 System.out.print("wrong number of points"); 175 return; 176 } 177 Double[] arr = new Double[20]; 178 for (int i = 1; i < num.length; i++) { 179 arr[i] = Double.valueOf(num[i]); 180 } 181 if (ISsame(arr[1], arr[2], arr[3], arr[4])) { 182 System.out.print("points coincide"); 183 return; 184 } 185 if (!IStriangle(arr[5],arr[6],arr[7],arr[8],arr[9],arr[10])) { 186 System.out.print("data error"); 187 return; 188 } else { 189 double a1 = arr[4] - arr[2]; 190 double b1 = arr[1] - arr[3]; 191 double c1 = arr[3] * arr[2] - arr[1] * arr[4]; 192 if (((a1 * arr[5] + b1 * arr[6] + c1 == 0) && (a1 * arr[7] + b1 * arr[8] + c1 == 0)) || ((a1 * arr[5] + b1 * arr[6] + c1 == 0) && (a1 * arr[9] + b1 * arr[10] + c1 == 0)) || ((a1 * arr[7] + b1 * arr[8] + c1 == 0) && (a1 * arr[9] + b1 * arr[10] + c1 == 0))) { 193 System.out.print("The point is on the edge of the triangle"); 194 return; 195 } 196 double[] point1 = point(arr[5], arr[6], arr[7], arr[8], arr[1], arr[2], arr[3], arr[4]); 197 double[] point2 = point(arr[7], arr[8], arr[9], arr[10], arr[1], arr[2], arr[3], arr[4]); 198 double[] point3 = point(arr[5], arr[6], arr[9], arr[10], arr[1], arr[2], arr[3], arr[4]); 199 if((point1[0]==65536.0)&&(point2[0] == 65536.0)&&(point3[0] ==65536.0)){ 200 System.out.print("0"); 201 return; 202 } 203 if (((point1[0] == point2[0]) && (point1[1] == point2[1]) && (point3[0]==65536.0)) || ((point1[0] == point3[0]) && (point1[1] == point3[1]) && (point2[0] == 65536.0)) || ((point2[0] == point3[0]) && (point2[1] == point3[1]) && (point1[0] ==65536.0))){ 204 System.out.print("1"); 205 return; 206 } else { 207 double area=area(arr[5],arr[6],arr[7],arr[8],arr[9],arr[10]); 208 double area1=0; 209 if(point3[0]==65536.0){ 210 area1=area(arr[7],arr[8] ,point1[0] ,point1[1] ,point2[0] ,point2[1]); 211 } 212 else if(point2[0]==65536.0){ 213 area1=area(arr[5],arr[6] ,point1[0] ,point1[1] ,point3[0] ,point3[1]); 214 } 215 else if(point1[0]==65536.0){ 216 area1=area(arr[9],arr[10] ,point2[0] ,point2[1] ,point3[0] ,point3[1]); 217 } 218 else if((point1[0] == point2[0])&&(point1[1]==point2[1])){ 219 area1=area(point1[0],point1[1] ,point3[0] ,point3[1] ,arr[5] ,arr[6]); 220 } 221 else if((point1[0] == point3[0])&&(point1[1]==point3[1])){ 222 area1=area(point1[0],point1[1] ,point2[0] ,point2[1] ,arr[7] ,arr[8]); 223 } 224 else if((point2[0] == point3[0])&&(point2[1]==point3[1])){ 225 area1=area(point2[0],point2[1] ,point1[0] ,point1[1] ,arr[5] ,arr[6]); 226 } 227 double area2=area-area1; 228 if(area2<area1){ 229 double temp = area1; 230 area1 = area2; 231 area2 = temp; 232 } 233 System.out.print("2 "+new DecimalFormat("0.0#####").format(area1) +" "+new DecimalFormat("0.0#####").format(area2)); 234 return; 235 } 236 } 237 } 238 239 public static double[] point(double x1, double y1, double x2, double y2, double ax1, double ay1, double ax2, double ay2) { 240 double[] num = {65536.0,65536.0}; 241 double A = ay2 - ay1; 242 double B = ax1 - ax2; 243 double C = ax2 * ay1 - ax1 * ay2; 244 double a = y2 - y1; 245 double b = x1 - x2; 246 double c = x2 * y1 - x1 * y2; 247 double ax = ax2-ax1; 248 double ay = ay2-ay1; 249 double bx = x2-x1; 250 double by = y2-y1; 251 if(ax * by - bx * ay==0){ 252 num[0] = 65536.0; 253 num[1] =65536.0; 254 return num; 255 } 256 if (A == 0) { 257 if (a == 0) { 258 num[0] = 65536.0; 259 num[1] =65536.0; 260 return num; 261 } else if (b == 0) { 262 if ((ay1 <= y1 && ay1 >= y2) || (ay1 <= y2 && ay1 >= y1)) { 263 num[0] = ax1; 264 num[1] = ay1; 265 } 266 } else { 267 double px = (b * ay1 + c) / (-a); 268 if ((px <= x1 && px >= x2) || (px >= x1 && px <= x2)) { 269 num[0] = px; 270 num[1] = ay1; 271 } 272 } 273 } 274 if (B == 0) { 275 if (a == 0) { 276 if ((ax1 <= x1 && ax1 >= x2) || (ax1 >= x1 && ax1 <= x2)) { 277 num[0] = ax1; 278 num[1] = y1; 279 } 280 } else if (b == 0) { 281 num[0] = 65536.0; 282 num[1] =65536.0; 283 return num; 284 } else { 285 double py = (a * ax1 + c) / (-b); 286 if ((py <= y1 && py >= y2) || (py >= y1 && py <= y2)) { 287 num[0] = ax1; 288 num[1] = py; 289 } 290 } 291 } else { 292 if (a == 0) { 293 double px = (B * y1 + C) / (-A); 294 if ((px <= x1 && px >= x2) || (px >= x1 && px <= x2)) { 295 num[0] = px; 296 num[1] = y1; 297 } 298 } else if (b == 0) { 299 double py = (A * x1 + C) / (-B); 300 if ((py <= y1 && py >= y2) || (py >= y1 && py <= y2)) { 301 num[0] = x1; 302 num[1] = py; 303 } 304 } else { 305 double px = (b * C - B * c) / (a * B - A * b); 306 if ((px <= x1 && px >= x2) || (px >= x1 && px <= x2)) { 307 num[0] = px; 308 num[1] = (A * px + C) / (-B); 309 } 310 } 311 } 312 return num; 313 } 314 315 public static double area(double x1,double y1,double x2,double y2,double x3,double y3){ 316 double Side_Length1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); 317 double Side_Length2 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2)); 318 double Side_Length3 = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2)); 319 double circumference = Side_Length1 + Side_Length2 + Side_Length3; 320 double p = 0.5 * circumference; 321 double area = Math.sqrt(p * (p - Side_Length1) * (p - Side_Length2) * (p - Side_Length3)); 322 return area; 323 } 324 325 public static void Isontriangle (String[]num){ 326 if (num.length != 9) { 327 System.out.print("wrong number of points"); 328 return; 329 } 330 Double[] arr = new Double[20]; 331 for (int i = 1; i < num.length; i++) { 332 arr[i] = Double.valueOf(num[i]); 333 } 334 if (!IStriangle(arr[3],arr[4],arr[5],arr[6],arr[7],arr[8])) { 335 System.out.print("data error"); 336 return; 337 } 338 int count=0; 339 double a1=arr[6]-arr[4]; 340 double b1=arr[3]-arr[5]; 341 double c1=arr[5]*arr[4]-arr[3]*arr[6]; 342 double a2=arr[8]-arr[6]; 343 double b2=arr[5]-arr[7]; 344 double c2=arr[7]*arr[6]-arr[5]*arr[8]; 345 double a3=arr[8]-arr[4]; 346 double b3=arr[3]-arr[7]; 347 double c3=arr[7]*arr[4]-arr[3]*arr[8]; 348 if((a1*arr[1]+b1*arr[2]+c1==0)||(a2*arr[1]+b2*arr[2]+c2==0)||(a3*arr[1]+b3*arr[2]+c3==0)){ 349 System.out.print("on the triangle"); 350 return; 351 } 352 count+=numpoint(arr[3],arr[4],arr[5],arr[6],arr[1],arr[2]); 353 count+=numpoint(arr[5],arr[6],arr[7],arr[8],arr[1],arr[2]); 354 count+=numpoint(arr[3],arr[4],arr[7],arr[8],arr[1],arr[2]); 355 if(count%2==0){ 356 System.out.print("outof the triangle"); 357 return; 358 } 359 else{ 360 System.out.print("in the triangle"); 361 return; 362 } 363 } 364 365 public static int numpoint(double x1,double y1,double x2,double y2,double x,double y){ 366 int count=0; 367 double a= y2 - y1; 368 double b= x1 - x2; 369 double c= x2*y1-x1*y2; 370 if(a==0){ 371 return count; 372 } 373 if(b==0){ 374 if(x>x1){ 375 count++; 376 } 377 } 378 else{ 379 double px=(b*y+c)/(-a); 380 if((px<x1&&px>x2&&px<x)||(px>x1&&px<x2&&px<x)){ 381 count++; 382 } 383 } 384 return count; 385 } 386 }
代码分析与处理:
1.输入的判断,输入中有空格和字符","的出现,对输入判断不方便。
2.输入数据第一位必须是数字且为1-5,第二位必须为":"(可作为先行判断的条件)
3.输入的点的坐标必须和输入的第一位数字的对应情况对应。
4.两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。
5.每个数字前"+","-"只能有一个,必须符合基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
6.定义一个字符串,将输入数据存入其中,同时对字符串中的","和空格数量进行对比,对所给数据进行字符串进行切割。
7.之后可对所给数据进行是否符合正则表达式判断,看其是否符合基本格式。
8.再运用switch语句,根据所读入的数字确定为何种情况,根据不同的情况判断输入数据的点数以及坐标是否重合,判断是否点的数量符合要求,输入的数据是否能够构成正常三角形等等。
9.若为情况1则断是否是等腰三角形、等边三角形,运用三边之间的关系。
若为情况2则运用数学公式,通过所给点坐标计算出周长、面积、重心坐标。
若为情况3则运用数学公式,通过三边的关系计算出是钝角、直角还是锐角三角形。
若为情况4则计算两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积,否则直接输出交点的个数。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"。
若为情况5则使用射线法,水平做一条射线计算出与与三角形交点数量,在其左端的个数,判断是否奇数以此判断是否在后三个点所构成的三角形的内部。
类图:

分析报告:

采坑心得:
1.在7-2 串口字符解析中,此题在理解题意方面,要求准确看懂题目意思,急于下手开始敲代码反而会造成更多的错误判断,以及题目中是过滤后的数据长度大于11,而不是输入的数据长度大于11,同时注意数组中元素下标可能出现的越界问题(设置好相关限制条件)。
2.在7-1 点线形系列1-计算两点之间的距离中,有几个测试点很坑的例子如:"0.0"和"0."等等,均该输出结果wrong format。需要多加以考虑和判断来解决。
3.在7-2 点线形系列2-线的计算中,在进行相关的判断的时候要考虑斜率是否存在等问题,以及一些特殊情况输入等问题如"0.00"等,关于点是否在两线段内的判断:将交点的X坐标大于两线段的各点的最小值并且小于最大值,满足任意所述条件即可判断在线段内。
4.在7-3 点线形系列3-三角形的计算中,在输出数据方面可以使用DecimalFormat()方法来实现符合要求的数据格式;对于直角三角形的判断存在一个精度损失问题,通过约定最长边与各边之和的差小于1e-6来加以判断;计算输出前两个点所在的直线与三个点所构成的三角形相交的交点数量时,记得输出点数为"0"的情况;计算三角形的面积方法可以通过海伦公式进行计算减少数据计算量和精度损失;采用射线法判断是否在三角形内部,水平做直线寻找规定一边与三角形的交点的数量,根据个数判断是否在内部。(寻找合适的数学方法对于做题判断会有很大的帮助)。
改进建议:
由SourceMonitor以及PowerDesigner软件测评结果可以看到,上述几段程序的复杂度都高(并且还逐渐递增的趋势),且都在主类中完成。这是段效率不高的代码,究其原因,是因为if语句过多,导致程序运行的过于拖沓后,以及一些判断过于冗杂。其实想处理的更为简洁,可以使用类的形式,将数据计算处理在一个计算类中,判断条件写在判断类中,结果输出写在一个输出类中,这样可以将题目处理的更有可读性。此次对于问题没有去探寻简便的解决方法,只是单纯地完成题目要求,导致每次都错过构造类的方法,使得代码又长又繁杂,不仅写的过程中容易出错,修改更是麻烦。所以应该只是做题,更应该思考,写代码前多想想自己的方法和思考构建类的结构及方法,而不是一个又一个小功能方法填充堆积而成。弥补类设计的缺陷,学会灵活运用类与类之间的多种关系,使代码简洁清晰。 直接判断解决完成,会使得题目复杂度较高,并且对于一些BUG修改和测试比较难以发现和修改,如果不去创建新的类,单单在main里一路写下来,代码将又长又复杂。不仅仅将目光放在实现结果上,而是同时关注代码的总体结构,结构清晰分工明确,实现代码也就又快又准。
总结:
首先在第一阶段的学习中,通过循序渐进的方式从开始从借助c语言的知识来认识Java,再到正式的了解面向对象的java设计,在这过程中对于类的运用和字符串以及数组等级基本类型的运用还需加强,尤其是一些基本语法。以及明白了面向过程范式聚焦于方法的设计,它将数据与方法结合在对象中。在许多Java方法要求使用对象作为参数,下一阶段学习任务也包括学会使用ava能将数据类型合并或包装到一个对象中。平时也要培养我们解决程序设计错误的能力,要学会去看给出的错误提示,学会总结,这样不仅能够避免也能更加掌握所学。在这几次作业的练习中,我也发现自身对于Java的了解不够深刻,同时代码的书写过程也需加强,很多题目的总体代码都存在重复性过高,可读性不强等问题。对类和对象的创建应用和方法的运用还需加强,对整体的题目程序涉及方面还需加强,有很多地方可以简化运算和代码。
需要进一步学习研究的部分:自己的编码思路还是按照面向过程思想来,没有把所学的面向对象思维应用到实际编码中,以及对java的一些知识了解不够深刻,停留在表面,实际还是沿用面向过程的思维,企图闭门造车,还需要加强对面向对象程序设计的学习。
改进建议及意见:
希望PTA的测试点给出简短说明,很多题目在提交时几乎“一碧千里”,但基本没几个测试点有相关说明,导致代码改错过程中不知道自己错在哪里,需要反复测试提交,不断更改代码,浪费很多时间。如有些模糊不清的输入判断如果增加一些提示将会更好。同时对于整体题目的难度,也可以增添一些循序渐进的过程。

浙公网安备 33010602011771号