面向对象程序设计第一次blog
一、前言
前三次的题目集中主要是关于Java语言的基本程序设计,前两次是熟悉Java语言语法,从最基础的命名变量、选择、循环、函数、字符及字符串到第三次作业的对象和类。题目集一题量较多,但是难度不高,考察的是Java基本语法,根据以前学过的C的知识能够完成,唯一注意的是数据的精度问题以及输入输出的格式问题。题目集二难度适中,考察Java中String的使用。而题目集三虽然题量不大,但是难度却比前两次高得多,初学表示非常艰难,且很多测试点根本想不出来。
总而言之,Java是本学期首次接触的语言,存在许多问题不足,还需要多多加强训练这样才能进步
二、设计分析
1.第二次作业(7-2 串口字符解析)
代码如下:
import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s = in.nextLine(); int temp = 0;//记录十一位均为1 if(s.length()<11)//小于最小长度 { System.out.println("null data"); return; } for(int i=0;i<11;i++) { if(s.charAt(i) == '1') temp++; } if(temp==11)//输入数据全1,没有起始位 { System.out.println("null data"); return; } if(s.matches("^[1]*$")) { System.out.println("null data"); return; } int amount = 1, num = 0,i = 0,a = 0; boolean flag = false,index = false; for(i=0; i<s.length()-10;i++)//判断无起始点且数据数不足 { if(s.charAt(i)=='0')//初始位置 { System.out.print(amount+":"); amount++; if(s.charAt(i+10)=='0')//奇偶校验位错误 { flag = false; // System.out.println("parity check error"); } else { num = 0; flag = true; for(a = i+1; a<i+9;a++) { if(s.charAt(a)=='1') { num++; } } if(num%2==0) { if(s.charAt(i+9)=='1') index = true; else index = false; } else { if(s.charAt(i+9)=='0') index = true; else index = false; } } if(flag==true) { if(index==true) { for(a=i+1; a<i+9;a++) System.out.print(s.charAt(a));//输出 System.out.print("\n"); } else { System.out.println("parity check error");//奇偶校验错误 } } else { System.out.println("validate error");//奇偶与结束符错误 } i = i+10; } } } }
总结:本题对我来说难度不大,主要在于不同情况下数据读取判断,使用多组for循环来进行读取判断和输出,导致圈复杂度很高
2.第三次作业(7-1 点线形系列1-计算两点之间的距离)
代码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s = in.nextLine(); int m = 0;//遇到逗号 int n = 0;//遇到空格 int flag = 1;//输入合法性检验 for(int i=0;i<s.length();i++) { if(s.charAt(i)==',') { m++; } if(s.charAt(i)==' ') { n++; } if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='.') { if(s.charAt(i+1)>='0'&&s.charAt(i+1)<='9') {//合法输入 continue; } else { flag = 0; break; } } } if(m-n!=1) { flag = 0; } if(flag == 1) { if(m!=2) {//坐标点数量超过两个 System.out.println("wrong number of points"); } else { String []a = new String[4];//存放数据 for(int i = 0;i<4;i++) { a[i] = new String(); } int p = 0; for(int i = 0;i<s.length();i++) {//存放 ,或空格 if(s.charAt(i)!=' '&&s.charAt(i)!=',') { a[p] = a[p] + s.substring(i,i+1); } else { p++; } } int index = 1; for(int i = 0;i<4;i++) {//判断每个数字前 + - 是否只有一个 if(a[i].charAt(0)=='+'||a[i].charAt(0)=='-') { if(a[i].charAt(1)=='0'&&a[i].charAt(2)!='.') { index = 0; } } else { if(a[i].length()>=2) { if(a[i].charAt(0)=='0'&&a[i].charAt(1)!='.') { index = 0; } } } if(index == 0) { break; } } if(index == 0) { System.out.println("Wrong Format"); } else{ double x1,x2,y1,y2; x1 = Double.parseDouble(a[0]); y1 = Double.parseDouble(a[1]); x2 = Double.parseDouble(a[2]); y2 = Double.parseDouble(a[3]); double distance = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));//计算 System.out.println(distance); } } } else { System.out.println("Wrong Format"); } } }
总结:这道题的题目要求很简单,就是计算两点间距离,但是它对数据输入输出的格式有很高的要求,如果输入数据不符合坐标形式,程序就要报错提醒,并且也需要格外注意坐标点数量。由于输入数据中包含空格以及逗号,我们输入时应选择字符串输入,并要对数据进行校验。在做好对输入数据的校验后,我用数组进行存放数据,最后通过距离公式来进行计算。此外,本题可以设计一个点类来完成。
3.题目三(7-2 点线形系列2-线的计算)
代码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s = in.nextLine(); String []arry = new String[20]; String [] str= s.split(":"); int n = str.length; int j = 0,k = 0;//数组下标 int index = 2;//坐标从第三个字符开始 int flag = 0,temp = 0; if(n==2){ if(s.charAt(0)<=0&&s.charAt(0)>=6) { System.out.println("Wrong Format"); return; } if(s.charAt(1)!=':') { System.out.println("Wrong Format"); return; } for(int i=2;i<s.length();i++) { if(s.charAt(i)==' ') { temp++; } if(s.charAt(i)==','){ k++; } } for(int i=2;i<s.length();i++) {//记录点 if(s.charAt(i)==','||s.charAt(i)==' ') { arry[j] = s.substring(index,i); index = i+1; j++; flag = i; } } if(k == 2) {//两个点 arry[3] = s.substring(flag+1,s.length()); double x1 = Double.parseDouble(arry[0]); double y1 = Double.parseDouble(arry[1]); double x2 = Double.parseDouble(arry[2]); double y2 = Double.parseDouble(arry[3]); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='1'&&s.charAt(i+1)==':') { Point point = new Point(x1,y1,x2,y2); point.slope(); } if(temp == 2) { System.out.println("wrong number of points"); } } } else if(k == 3) {//三个点 arry[5] = s.substring(flag+1,s.length()); double x1 = Double.parseDouble(arry[0]); double y1 = Double.parseDouble(arry[1]); double x2 = Double.parseDouble(arry[2]); double y2 = Double.parseDouble(arry[3]); double x3 = Double.parseDouble(arry[4]); double y3 = Double.parseDouble(arry[5]); if(s.charAt(0)=='2') {//选项2 Point point = new Point(x1,y1,x2,y2,x3,y3); point.threepointdistance(); } if(s.charAt(0)=='3') {//选项3 Point point = new Point(x1,y1,x2,y2,x3,y3); point.threepointline(); } if(temp == 3){ System.out.println("wrong number of points"); } } else if(k == 4) {//四个点 arry[7] = s.substring(flag+1,s.length()); double x1 = Double.parseDouble(arry[0]); double y1 = Double.parseDouble(arry[1]); double x2 = Double.parseDouble(arry[2]); double y2 = Double.parseDouble(arry[3]); double x3 = Double.parseDouble(arry[4]); double y3 = Double.parseDouble(arry[5]); double x4 = Double.parseDouble(arry[6]); double y4 = Double.parseDouble(arry[7]); if(s.charAt(0)=='4') { Point point = new Point(x1,y1,x2,y2,x3,y3,x4,y4); point.fourpointsparalle(); } if(s.charAt(0)=='5') { Point point = new Point(x1,y1,x2,y2,x3,y3,x4,y4); point.jiaodian(); } if(temp == 4) { System.out.println("wrong number of points"); } } else if(k>4) {//输入点数量不符合要求 System.out.println("wrong number of points"); } } else{ System.out.println("Wrong Format"); } } } class Point { private double x1,y1,x2,y2,x3,y3,x4,y4; public Point() { super(); } public Point(double x1,double y1,double x2,double y2) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public Point(double x1,double y1,double x2,double y2,double x3,double y3) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; } public Point(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; this.x4 = x4; this.y4 = y4; } public double getx1() { return x1; } public void setx1(double x1) { this.x1 = x1; } public double gety1() { return y1; } public void sety1(double y1) { this.y1 = y1; } public double getx2() { return x2; } public void setx2(double x2) { this.x2 = x2; } public double gety2() { return y2; } public void sety2(double y2) { this.y2 = y2; } public double getx3() { return x3; } public void setx3(double x3) { this.x3 = x3; } public double gety3() { return y3; } public void sety3(double y3) { this.y3 = y3; } public double getx4() { return x4; } public void setx4(double x4) { this.x4 = x4; } public double gety4() { return y4; } public void sety4(double y4) { this.y4 = y4; } public boolean chonghe(double x1,double y1,double x2,double y2) {//判断两点是否重合 if(x1==x2&&y1==y2) { return true; } else { return false; } } public void slope() {//斜率 double k = 0; k = (y1-y2)/(x1-x2); if(chonghe(x1,y1,x2,y2)) { System.out.println("points coincide");//两点重合 } else { if(x1==x2) { //分母为0 线条垂直于x轴 System.out.println("Slope does not exist"); } else { System.out.println(k); } } } public void threepointdistance() {//第一个点与另外两条点所成线距离 double distance = Math.abs((y2-y3)*x1+(x3-x2)*y1+x2*y3-y2*x3)/Math.sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3)); if(!chonghe(x2,y2,x3,y3)) { System.out.println(distance); } else { System.out.println("points coincide"); } } public void threepointline() { double k1,k2; k1 = (y1-y2)/(x1-x2); k2 = (y1-y3)/(x1-x3); if(!chonghe(x1,y1,x2,y2)&&!chonghe(x1,y1,x3,y3)) { if(x1==x2&&x1==x3) { System.out.println("true"); } else { if(k1==k2) { System.out.println("true"); } else { System.out.println("false"); } } } else { System.out.println("points coincide"); } } public void fourpointsparalle() { if(chonghe(x1,y1,x2,y2)||chonghe(x3,y3,x4,y4)) { System.out.println("points coincide"); } else { if((x1-x2)*(y3-y4)==(y1-y2)*(x3-x4)) { System.out.println("true"); } else { System.out.println("false"); } } } public double max(double x1,double x2) { if(x1>x2) { return x1; } else { return x2; } } public double min(double x1,double x2) { if(x1>x2) { return x2; } else { return x1; } } public boolean isinline(double x,double y) {//判断交点是否在线段上 double max1 = max(x1,x2); double max2 = max(x3,x4); double min1 = min(x1,x2); double min2 = min(x3,x4); if((x>min1||x>min2)&&(x<max1||x<max2)) { return true; } else { return false; } } public void jiaodian() { double k1,k2; double x,y; x = (y3*x2*x4-x3*y4*x2-y3*x1*x4+y4*x1*x3-y1*x4*x2+y2*x4*x1+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3); y = (-y2*x4*y3+y2*x3*y4+y3*x4*y1-y4*x3*y1+y4*x2*y1-y1*x2*y3-y4*x1*y2+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3); k1 = (y2-y1)/(x2-x1); k2 = (y4-y3)/(x4-x3); if(chonghe(x1,y1,x2,y2)||chonghe(x3,y3,x4,y4)) { System.out.println("points coincide"); } else { if(k1==k2||(x1==x2&&x3==x4)) { System.out.println("is parallel lines,have no intersection point"); } else { System.out.println(x+","+y+" "+isinline(x,y)); } } } }
总结:这道题属于7-1的进阶版,由单纯点的问题转为线的问题,出现四五个点的情况,题目中需求也变得极为多,测试点也极为***钻。这道题我设计点类来对这些需求进行计算,在此之前,还是需要对输入数据进行校验以及处理,要将输入字符拆分存放入数组,采用if判断语句来进行不同要求间算法的求解。但由于我对输入格式的判断存在问题,数据格式的输入测试点过不去,本题没有拿到满分。
SourceMonitor分析图:
4.作业3(7-3 点线形系列3-三角形的计算)
代码如下:
// package 作业第三题; import java.util.Scanner; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s = in.nextLine(); String []arry = new String[20]; int j = 0,k = 0;//数组下标 int index = 2;//坐标从第三个字符开始 int flag = 0,temp = 0; boolean a = false; if(s.charAt(0)<=0&&s.charAt(0)>=6) { System.out.print("Wrong Format"); return; } for(int i=0;i<s.length();i++) { if(s.charAt(i)==' ') { a = true; } } if(s.charAt(1)!=':'||a==false) { System.out.print("Wrong Format"); return; } if(!s.matches("^[1-5][:](([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))[,]([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))\\s?)+$")){ System.out.println("Wrong Format"); return; } for(int i=2;i<s.length();i++) { if(s.charAt(i)==' ') { temp++; } if(s.charAt(i)==',') { k++; } } for(int i=2;i<s.length();i++) {//记录点 if(s.charAt(i)==','||s.charAt(i)==' ') { arry[j] = s.substring(index,i); index = i+1; j++; flag = i; } } if(k == 3) {//三个点 arry[5] = s.substring(flag+1,s.length()); double x1 = Double.parseDouble(arry[0]); double y1 = Double.parseDouble(arry[1]); double x2 = Double.parseDouble(arry[2]); double y2 = Double.parseDouble(arry[3]); double x3 = Double.parseDouble(arry[4]); double y3 = Double.parseDouble(arry[5]); if(s.charAt(0)=='1') {//选项1 Point point = new Point(x1,y1,x2,y2,x3,y3); point.threetriangle(); } if(s.charAt(0)=='2') {//选项2 Point point = new Point(x1,y1,x2,y2,x3,y3); point.threepointscalculate(); } if(s.charAt(0)=='3') {//选项3 Point point = new Point(x1,y1,x2,y2,x3,y3); point.panduantriangle(); } } else if(k == 4) {//四个点 arry[7] = s.substring(flag+1,s.length()); double x1 = Double.parseDouble(arry[0]); double y1 = Double.parseDouble(arry[1]); double x2 = Double.parseDouble(arry[2]); double y2 = Double.parseDouble(arry[3]); double x3 = Double.parseDouble(arry[4]); double y3 = Double.parseDouble(arry[5]); double x4 = Double.parseDouble(arry[6]); double y4 = Double.parseDouble(arry[7]); if(s.charAt(0)=='5') {//选项五 Point point = new Point(x1,y1,x2,y2,x3,y3,x4,y4); point. } } else if(k == 5) { //五个点 arry[9] = s.substring(flag+1,s.length()); double x1 = Double.parseDouble(arry[0]); double y1 = Double.parseDouble(arry[1]); double x2 = Double.parseDouble(arry[2]); double y2 = Double.parseDouble(arry[3]); double x3 = Double.parseDouble(arry[4]); double y3 = Double.parseDouble(arry[5]); double x4 = Double.parseDouble(arry[6]); double y4 = Double.parseDouble(arry[7]); double x5 = Double.parseDouble(arry[8]); double y5 = Double.parseDouble(arry[9]); if(s.charAt(0)=='4') {//选项四 Point point = new Point(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5); point.fivepointstriangle(); } } else if(k>5) { System.out.println("wrong number of points"); } } } // package 作业第三题; // import java.text.DecimalFormat; class Point { private double x1,y1,x2,y2,x3,y3,x4,y4,x5,y5; private double x,y; public Point() { super(); } public Point(double x1,double y1,double x2,double y2) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public Point(double x1,double y1,double x2,double y2,double x3,double y3) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; } public Point(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; this.x4 = x4; this.y4 = y4; } public Point(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; this.x4 = x4; this.y4 = y4; this.x5 = x5; this.y5 = y5; } public double getx1() { return x1; } public void setx1(double x1) { this.x1 = x1; } public double gety1() { return y1; } public void sety1(double y1) { this.y1 = y1; } public double getx2() { return x2; } public void setx2(double x2) { this.x2 = x2; } public double gety2() { return y2; } public void sety2(double y2) { this.y2 = y2; } public double getx3() { return x3; } public void setx3(double x3) { this.x3 = x3; } public double gety3() { return y3; } public void sety3(double y3) { this.y3 = y3; } public double getx4() { return x4; } public void setx4(double x4) { this.x4 = x4; } public double gety4() { return y4; } public void sety4(double y4) { this.y4 = y4; } public double getx5() { return x5; } public void setx5(double x5) { this.x5 = x5; } public double gety5() { return y5; } public void sety5(double y5) { this.y5 = y5; } public boolean chonghe(double x1,double y1,double x2,double y2) {//判断两点是否重合 if(x1==x2&&y1==y2) { return true; } else { return false; } } public boolean triangle(double x1,double y1,double x2,double y2,double x3,double y3) {//判断是否为三角形 double a,b,c; a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); c = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); if(a+b>c&&a+c>b&&b+c>a) { return true; } else { return false; } } public void threetriangle() { double a,b,c; a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); c = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); if(!triangle(x1,y1,x2,y2,x3,y3)) { System.out.println("data error"); } else { if(a==b||a==c||b==c) { System.out.print("true"); } else { System.out.print("false"); } System.out.print(" "); if(a==b&&a==c&&b==c) { System.out.print("true"); } else { System.out.print("false"); } } } public void threepointscalculate() { double a,b,c; a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); c = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double C = a+b+c;//周长 double S = Math.sqrt((C/2)*(C/2-a)*(C/2-b)*(C/2-c));//面积 double X = (x1+x2+x3)/3; double Y = (y1+y2+y3)/3; if(!triangle(x1,y1,x2,y2,x3,y3)) { System.out.println("data error"); } else { // System.out.println(C+" "+S+" "+X+","+Y); // System.out.printf("%.6f %.6f %.6f %.6f",C,S,X,Y); // System.out.print(C); // System.out.print(" "); // System.out.print(S); // System.out.print(" "); // System.out.print(X); // System.out.print(","); // System.out.print(Y); System.out.printf(new DecimalFormat("0.0#####").format(C)); System.out.print(" "); System.out.printf(new DecimalFormat("0.0#####").format(S)); System.out.print(" "); System.out.printf(new DecimalFormat("0.0#####").format(X)); System.out.print(","); System.out.printf(new DecimalFormat("0.0#####").format(Y)); } } public void panduantriangle() { double a,b,c,A,B,C; a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); c = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); A = (b*b+c*c-a*a)/(2*b*c); B = (a*a+c*c-b*b)/(2*a*c); C = (b*b+a*a-c*c)/(2*b*a); if(A>0&&B>0&&C>0) { System.out.println("false false true");//锐角 return; } else if((Math.abs(b*b+c*c-a*a)<=0.000001)||(Math.abs(a*a+c*c-b*b)<=0.000001)||(Math.abs(b*b+a*a-c*c)<=0.000001)) { System.out.println("false true false");//直角 return; } else { System.out.println("true false false");//钝角 return; } } public void jiaodian() { double k1,k2,a1= 0,b1 = 0,c1 = 0,S1 = 0,S2 = 0,C = 0,B = 0,A = 0; double X1,Y1,X2,Y2; X1 = (y3*x2*x4-x3*y4*x2-y3*x1*x4+y4*x1*x3-y1*x4*x2+y2*x4*x1+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3); Y1 = (-y2*x4*y3+y2*x3*y4+y3*x4*y1-y4*x3*y1+y4*x2*y1-y1*x2*y3-y4*x1*y2+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3); X2 = (y3*x2*x5-x3*y5*x2-y3*x1*x5+y5*x1*x3-y1*x5*x2+y2*x5*x1+y1*x2*x3-y2*x1*x3)/(x5*y2-x5*y1-x3*y2+x3*y1-x2*y5+x2*y3+x1*y5-x1*y3); Y2 = (-y2*x5*y3+y2*x3*y5+y3*x5*y1-y5*x3*y1+y5*x2*y1-y1*x2*y3-y5*x1*y2+y2*x1*y3)/(y5*x2-y5*x1-y3*x2+x1*y3-y2*x5+y2*x3+y1*x5-y1*x3); k1 = (y2-y1)/(x2-x1); k2 = (y4-y3)/(x4-x3); if(chonghe(x1,y1,x2,y2)||chonghe(x3,y3,x4,y4)) { System.out.println("points coincide"); } else { if(k1==k2||(x1==x2&&x3==x4)) { return; } else { // System.out.println(x+","+y+" "+isinline(x,y)); a1 = Math.sqrt((x3-X1)*(x3-X1)+(y3-Y1)*(y3-Y1)); b1 = Math.sqrt((x3-X2)*(x3-X2)+(y3-Y2)*(y3-Y2)); c1 = Math.sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2)); A = Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)); B = Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)); C = Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); S1 = 0.5 * a1 * b1 * Math.sqrt(1-((b1*b1+c1*c1-a1*a1)/2*b1*c1)*((b1*b1+c1*c1-a1*a1)/2*b1*c1)); S2 = 0.5 * B * C * Math.sqrt(1-((B*B+C*C-A*A)/2*B*C)*((B*B+C*C-A*A)/2*B*C)); if(S1<S2) System.out.println("2 "+S1+" "+S2); else System.out.println("2 "+S2+" "+S1); } } } public void fivepointstriangle() { // double a,b,c,x,y,X,Y; boolean judge = false; // a = Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); // b = Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)); // c = Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)); if(x1==x2&&y1==y2) System.out.print("points coincide"); else { if((x3==x4&&y3==y4)||(x3==x5&&y3==y5)||(x4==x5&&y4==y5)||(x3==x4&&x3==x5)) judge=false; else { if(x3==x4||x3==x5||x4==x5) judge=true; else { double slope1=(y3-y4)/(x3-x4); double slope2=(y3-y5)/(x3-x5); if(slope1!=slope2) judge=true; } } if(judge) { double distance1=((y1-y2)*x3+(x2-x1)*y3+x1*y2-y1*x2)/Math.sqrt((y1-y2)*(y1-y2)+(x1-x2)*(x1-x2)); double distance2=((y1-y2)*x4+(x2-x1)*y4+x1*y2-y1*x2)/Math.sqrt((y1-y2)*(y1-y2)+(x1-x2)*(x1-x2)); double distance3=((y1-y2)*x5+(x2-x1)*y5+x1*y2-y1*x2)/Math.sqrt((y1-y2)*(y1-y2)+(x1-x2)*(x1-x2)); if((distance1>0&&distance2>0&&distance3>0)||(distance1<0&&distance2<0&&distance3<0)) System.out.print("0"); else if((distance1==0&&distance2*distance3>0)||(distance2==0&&distance1*distance3>0)||(distance3==0&&distance1*distance2>0)) System.out.print("1"); else if((distance1==0&&distance2==0)||(distance1==0&&distance3==0)||(distance2==0&&distance3==0)) System.out.print("The point is on the edge of the triangle"); else { jiaodian(); } } else System.out.print("data error"); } } public void fourpoints(){ } public void input(double a,double b) { x = a; y = b; } //读取x的值// public double read_x() { return x; } //读取y的值// public double read_y() { return y; } //计算两点的距离// public double caluate_distance(Point p) { return Math.sqrt((x-p.x )*(x-p.x )+(y-p.y )*(y-p.y)); } //检查线的两点是否重合// public boolean chack_point(Point p) { boolean wssa = false ; if(Math.abs(x-p.read_x())<1e-15&&Math.abs(y-p.read_y())<1e-15) { wssa = true ; } return wssa; } } class Line { Point p1 = new Point(); Point p2 = new Point(); //检查线段的斜率是否存在// public boolean chack_slope() { boolean wssa = true ; if(Math.abs(p1.read_x()-p2.read_x())<1e-15) { wssa = false ; } return wssa; } //计算线段的斜率// public double caluate_slope() { return (p1.read_x()-p2.read_y())/(p1.read_x()-p2.read_y()); } //计算点到直线的距离// public double caluate_distance(Point a) { double distance ; if(if_on_line(a)){ distance = 0; } else { distance = Math.abs(a.read_x()*p1.read_y()+p1.read_x()*p2.read_y()+p2.read_x()*a.read_y()-a.read_x()*p2.read_y()-p1.read_x()*a.read_y()-p2.read_x()*p1.read_y())/p1.caluate_distance(p2); } return distance ; } //判断点是否再直线上// public boolean if_on_line(Point p) { boolean jes = false ; Line l1 = new Line(); Line l2 = new Line(); l1.p1 = p1; l1.p2 = p; l2.p1 = p2; l2.p2 = p; if(l1.caluate_slope()==l2.caluate_slope()) { jes = true; } return jes; } //判断点是否在线段内// //在线段内返回true,在线段外返回false// public boolean if_in_line(Point p) { boolean jes = true ; if(p.caluate_distance(p1)>p1.caluate_distance(p2)||p.caluate_distance(p2)>p1.caluate_distance(p2)) { jes = false ; } return jes; } //判断两直线是否平行// //若两直线平行返回1,若两直线重合返回0,若两直线相交返回-1 public int if_parallel(Line l) { int cnt ; double x1 = (p1.read_x()-p2.read_x())*(l.p1.read_x()-l.p2.read_x())+(p1.read_y()-p2.read_y())*(l.p1.read_y()-l.p2.read_y()); if(x1/(p1.caluate_distance(p2)*l.p1.caluate_distance(p2))!=1) { cnt=-1; } else { if(l.caluate_distance(p1)==0) { cnt=0; } else { cnt=1; } } return cnt; } //计算两直线交点,返回Point// public Point caluate_intersection(Line a){ Point p = new Point(); double dx1 = p1.read_x()-p2.read_x(); double dx2 = a.p1.read_x()-a.p2.read_x(); double dy1 = p1.read_x()-p2.read_x(); double dy2 = a.p1.read_x()-a.p2.read_x(); double mid1 = dy1/dx1; double mid2 = dy2/dx2; double rx = (a.p2.read_y()-p2.read_y()-a.p2.read_y()*mid2+p2.read_y()*mid1)/(mid1-mid2); double ry = (rx-p2.read_x())*mid1+p2.read_y(); p.input(rx, ry); return p; } } class Triangle { Point p1 = new Point(); Point p2 = new Point(); Point p3 = new Point(); //检查三点是否构成三角形// //若构成三角形返回false,若不构成三角形返回true// public boolean chack_triangle() { boolean err = true ; if(Math.abs(p1.caluate_distance(p2)-p1.caluate_distance(p3))<p2.caluate_distance(p3)&&Math.abs(p1.caluate_distance(p3)+p1.caluate_distance(p2))>p2.caluate_distance(p3)){ err = false ; } return err ; } //判断是否为等边三角形,或等腰三角形// //若为等腰三角形,返回0// //若为等边三角形,返回1// public int shape_line_triangle() { int shape = 0; if(Math.abs(p1.caluate_distance(p2)-p1.caluate_distance(p3))<1e-15&&Math.abs(p2.caluate_distance(p1)-p2.caluate_distance(p3))<1e-15) { shape = 1; } return shape; } //计算三角形面积,返回面积// public double caluate_S() { return Math.abs(p1.read_x()*p2.read_y()+p2.read_x()*p3.read_y()+p3.read_x()*p1.read_y()-p1.read_x()*p3.read_y()-p2.read_x()*p1.read_y()-p3.read_x()*p2.read_y())/2; } //计算三角形的周长,返回周长// public double caluate_C() { return p1.caluate_distance(p2)+p1.caluate_distance(p3)+p2.caluate_distance(p3); } //计算三角形的重心坐标,返回Point// public Point caluate_gravity_point() { Point p = new Point(); double dx = (p1.read_x()+p2.read_x()+p3.read_x())/3; double dy = (p1.read_y()+p2.read_y()+p3.read_y())/3; p.input(dx, dy); return p; } //判断三角形是锐角三角形还是直角三角形或钝角三角形// //若是锐角三角形返回1,若是直角三角形返回0,若是钝角三角形则返回-1// public int shape_horn_triangle() { int shape ; double j1 = (p2.read_x()-p1.read_x())*(p3.read_x()-p1.read_x())+(p2.read_y()-p1.read_y())*(p3.read_y()-p1.read_y()); double j2 = (p3.read_x()-p2.read_x())*(p1.read_x()-p2.read_x())+(p3.read_y()-p2.read_y())*(p1.read_y()-p2.read_y()); double j3 = (p1.read_x()-p3.read_x())*(p2.read_x()-p3.read_y())+(p1.read_y()-p3.read_y())*(p2.read_y()-p3.read_y()); if(j1<0||j2<0||j3<0) { shape = 1; } else if(j1==0||j2==0||j3==0) { shape = 0 ; } else { shape = -1; } return shape; } //计算直线与三角形的交点// //返回交点个数// public int caluate_sum_intersection(Line a) { int sum = 0; Line l1 = new Line(); l1.p1=p1;l1.p2=p2; Line l2 = new Line(); l2.p1=p1;l2.p2=p3; Line l3 = new Line(); l3.p1=p2;l3.p2=p3; if(l1.if_parallel(a)==1) { Point point1 = a.caluate_intersection(l2); if(l2.if_in_line(point1)) { sum++; } Point point2 = a.caluate_intersection(l3); if(l3.if_in_line(point2)) { sum++; } } else if(l2.if_parallel(a)==1) { Point point1 = a.caluate_intersection(l1); if(l1.if_in_line(point1)) { sum++; } Point point2 = a.caluate_intersection(l3); if(l3.if_in_line(point2)) { sum++; } } else if(l3.if_parallel(a)==1) { Point point1 = a.caluate_intersection(l1); if(l1.if_in_line(point1)) { sum++; } Point point2 = a.caluate_intersection(l2); if(l2.if_in_line(point2)&&point1.chack_point(point2)) { sum++; } } else { Point point1 = a.caluate_intersection(l1); if(l1.if_in_line(point1)) { sum++; } Point point2 = a.caluate_intersection(l2); if(l2.if_in_line(point2)&&point1.chack_point(point2)) { sum++; } Point point3 = a.caluate_intersection(l2); if(l2.if_in_line(point3)&&point1.chack_point(point3)&&point2.chack_point(point3)) { sum++; } } return sum; } //判断点是否再三角形内部// public boolean if_inside(Point a) { boolean rea = false ; Line l1 = new Line(); l1.p1=p1; l1.p2=a; Line l2 = new Line(); l2.p1=p2; l2.p2=a; Line l3 = new Line(); l3.p1=p3; l3.p2=a; Line l4 = new Line(); l4.p1=p1; l4.p2=p2; Line l5 = new Line(); l5.p1=p2; l5.p2=p3; Line l6 = new Line(); l6.p1=p3; l6.p2=p1; if(!l5.if_in_line(l1.caluate_intersection(l5))||!l4.if_in_line(l3.caluate_intersection(l4))||!l6.if_in_line(l2.caluate_intersection(l6))) { rea = true; } return rea; } }
总结:一开始写这道题时,想着只用点类来进行,但是在最后两个需求上,单用点类非常难实现,在前面进行字符串分割,以及前三个需求时,点类可以用的很简单,但是到了四五需求,不知道该如何设计方法,参数传递彻底搞晕,最后也尝试设计线类以及三角形类,但还是没能完成该题。
SourceMonitor分析图:
三、踩坑心得
1.这四道题我并没有全拿满分,有部分题目的部分测试点过不去,在字符串的处理上有很多地方做的不好,导致输入数据格式存在错误。
2.对类的认识还不是很充分,有很多在Main中的其实可以将其封装,导致代码冗长易出错
3.在计算上不是数学上的范围,例如判断直角三角形,应该是两边平方和减第三边平方小于一个极小值,而不是等于0,题目中就有一个测试点是关于此的,我改了很久。
4.不要怕麻烦,无论是学习正则表达式还是去多设计类,能用类的就用类,到后期会越来越方便。
四、改进建议
1.switch语句的复杂度比if语句更低,在之后的编码中能用switch替代就尽量不使用if。
2.编写代码前提前构建好类图,效率更高
3.多用类,多用方法,不要一直Main
4.String类型判断使用正则表达式很快
5.边写代码边调试,多写注释
五、总结
第一阶段的学习已经过去,我逐渐明白Java和c的相似以及不同,基本语法虽大体相同,但角度却不同了。我更熟练的运用字符串及其相关函数,并且慢慢习惯运用方法和类,但是并不熟练。对于整体的题目设计方面还需要大大加强,这样才能简化运算以及代码,可读性也会更强。另外,一定要合理规划好时间,不要拖拉,作业发布最好就快速开始,遇到问题积极寻求帮助,多想多学多问,问题总会解决的