南昌航空大学21201409-Java大作业总结
(1)前言:
此次Java大作业总结会总结第四次和第五次pta作业以及期中考试的题目。
第四次作业和第五次作业主体都是关于多边形的操作。在第四次作业中还有一个关于正则表达式的考察和一个对类的设计的考察。第五次作业则是全部考察五边形的多种操作。期中考试则主要考察了面向对象程序设计思维的运用,次要的附加了字符串的操作、类的继承和多态和动态数组的运用。总体来说第四次作业和第五次作业中的多边形题目难度较高,是延续前三次的基础,依次往上提升难度,解题非常吃力;期中考试的题目则更偏向考察对知识的运用,相对来说比较轻松。
(2)设计与分析
1、
此题目主要需要运用正则表达式来筛选需要的内容,此题目中则是需要提取数字的内容,然后进行相加操作。解题时主要需要使用正则表达式判断输入内容的类别,然后将数字存入一个字符串,最后将字符串转为整型再进行相加操作。总体难度不高,只是需要注意读入输入数据是要用next()而不是nextLine()。
import java.util.Scanner; import java.util.regex.Pattern; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(true) { int sum = 0; String str = in.nextLine(); if(str.equals("end")) break; String[] s = str.split("\\D+"); for(int i=0;i<s.length;i++) { if(Pattern.matches("\\d+",s[i])) { s[i] = s[i]; } else s[i] = "0"; } for(int i = 0;i<s.length;i++) { int b = Integer.parseInt(s[i]); sum = sum+b; } System.out.println(sum); } } }
以上代码能通过所有测试点,测试结果如下

2、
此题目延续了前三次作业中的点、线、面、三角形的命题方式,在之前的基础上更进一步的提高了复杂度和难度以及繁琐度。主要需要非常多的输入判断,多种非法输入判断提高了不少的题目难度,也大幅提高了代码复杂度;除了输入判断以外的各种操作相对更容易写出来,但是也很容易出现纰漏。需要大量运用多个类的设计和字符串的各种操作。
import java.util.Scanner; import java.util.ArrayList; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Scanner v = new Scanner(System.in); String a = v.nextLine(); inputError inputerror = new inputError(); String[] a1 = a.substring(2).split(" "); int j = 0; if(a.substring(0,1).equals("1")) { inputerror.wr(a); inputerror.wrongnumber1(a1); Double[] a3 = cut(a1); work1(a3); } if(a.substring(0,1).equals("2")) { inputerror.wr(a); inputerror.wrongnumber1(a1); Double[] a3 = cut(a1); work2(a3); } if(a.substring(0,1).equals("3")) { inputerror.wr(a); inputerror.wrongnumber1(a1); Double[] a3 = cut(a1); work3(a3); } if(a.substring(0,1).equals("4")) { inputerror.wr(a); inputerror.wrongnumber2(a1); Double[] a3 = cut(a1); System.out.println("not a quadrilateral or triangle"); } if(a.substring(0,1).equals("5")) { inputerror.wr(a); inputerror.wrongnumber3(a1); Double[] a3 = cut(a1); System.out.println("in the triangle"); } } public static void work1(Double[] a) { boolean ans1, ans2, ans3, ans4; double X, Y, c1, c2, c3, c4; Double[] b = Point(a[0],a[1],a[4],a[5],a[2],a[3],a[6],a[7]); X = b[0]; Y = b[1]; ans1 = Triangle(a[0],a[1],a[2],a[3],X,Y); ans2 = Triangle(a[2],a[3],a[4],a[5],X,Y); ans3 = Triangle(a[4],a[5],a[6],a[7],X,Y); ans4 = Triangle(a[0],a[1],a[6],a[7],X,Y); c1 = Calculate(a[0],a[1],a[2],a[3]); c2 = Calculate(a[4],a[5],a[6],a[7]); c3 = Calculate(a[2],a[3],a[4],a[5]); c4 = Calculate(a[6],a[7],a[0],a[1]); if(ans1 && ans2 && ans3 && ans4) { System.out.print("true "); if((Para1(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7])&& c1==c2)||(Para1(a[2],a[3],a[4],a[5],a[6],a[7],a[0],a[1])&& c3==c4)) System.out.print("true"); else System.out.print("false"); } else if((a[6] == a[4] && a[7] == a[5]) || (a[6] == a[2] && a[7] == a[2]) || (a[6] == a[0] && a[7] == a[1]) || (a[4] == a[2] && a[5] == a[3]) || (a[4] == a[0] && a[5] == a[1]) || (a[2] == a[0] && a[3] == a[0])) System.out.println("points coincide"); else System.out.println("false false"); } public static void work2(Double[] a) { boolean ans1, ans2, ans3, ans4; double X, Y, c1, c2, c3, c4; Double[] b = Point(a[0],a[1],a[4],a[5],a[2],a[3],a[6],a[7]); X = b[0]; Y = b[1]; ans1 = Triangle(a[0],a[1],a[2],a[3],X,Y); ans2 = Triangle(a[2],a[3],a[4],a[5],X,Y); ans3 = Triangle(a[4],a[5],a[6],a[7],X,Y); ans4 = Triangle(a[0],a[1],a[6],a[7],X,Y); c1 = Calculate(a[0],a[1],a[2],a[3]); c2 = Calculate(a[4],a[5],a[6],a[7]); c3 = Calculate(a[2],a[3],a[4],a[5]); c4 = Calculate(a[6],a[7],a[0],a[1]); if(ans1 && ans2 && ans3 && ans4) { if(c1 == c2 && c2 == c3 && c3 == c4) { System.out.print("true "); if(Calculate(a[2],a[3],a[6],a[7]) == Calculate(a[0],a[1],a[4],a[5])) { System.out.print("true "); if(Para2(a[2],a[3],a[6],a[7],a[0],a[1],a[4],a[5])) System.out.print("true"); else System.out.print("false false"); } else { System.out.print("false "); if(Calculate(a[2],a[3],a[6],a[7]) == Calculate(a[0],a[1],a[4],a[5])) { System.out.print("true false"); } } } else System.out.print("false false false"); } else if((a[6] == a[4] && a[7] == a[5]) || (a[6] == a[2] && a[7] == a[2]) || (a[6] == a[0] && a[7] == a[1]) || (a[4] == a[2] && a[5] == a[3]) || (a[4] == a[0] && a[5] == a[1]) || (a[2] == a[0] && a[3] == a[0])) System.out.println("points coincide"); else System.out.println("not a quadrilateral"); } public static void work3(Double[] a){ boolean ans1, ans2, ans3, ans4; double X, Y; Double[] b = Point(a[0],a[1],a[4],a[5],a[2],a[3],a[6],a[7]); X = b[0]; Y = b[1]; ans1 = Triangle(a[0],a[1],a[2],a[3],X,Y); ans2 = Triangle(a[2],a[3],a[4],a[5],X,Y); ans3 = Triangle(a[4],a[5],a[6],a[7],X,Y); ans4 = Triangle(a[0],a[1],a[6],a[7],X,Y); if(ans1 && ans2 && ans3 && ans4) { double c = Calculate(a[0],a[1],a[2],a[3]) + Calculate(a[2],a[3],a[4],a[5]) + Calculate(a[4],a[5],a[6],a[7]) + Calculate(a[6],a[7],a[0],a[1]); double s = Concave(a[0],a[1],a[2],a[3],X,Y) + Concave(a[2],a[3],a[4],a[5],X,Y)+ Concave(a[4],a[5],a[6],a[7],X,Y) + Concave(a[0],a[1],a[6],a[7],X,Y); double s1 = Concave(a[0],a[1],a[2],a[3],a[4],a[5]) + Concave(a[4],a[5],a[6],a[7],a[0],a[1]); double s2 = Concave(a[2],a[3],a[4],a[5],a[6],a[7]) + Concave(a[6],a[7],a[0],a[1],a[2],a[3]); if(s1 == s2) { System.out.print("true "); System.out.println((double)(Math.round(c*1000)/1000.0)+ " " + (double)(Math.round(s*1000000)/1000000.0)); } else { System.out.print("false "); if(Calculate(a[2],a[3],a[6],a[7]) < Calculate(a[0],a[1],a[4],a[5])) { s = Concave(a[0],a[1],a[2],a[3],a[6],a[7]) + Concave(a[4],a[5],a[6],a[7],a[2],a[3]); System.out.println((double)(Math.round(c*1000)/1000.0)+ " " + (double)(Math.round(s*1000000)/1000000.0)); } else { s = Concave(a[0],a[1],a[2],a[3],a[4],a[5]) + Concave(a[4],a[5],a[6],a[7],a[0],a[1]); System.out.println((double)(Math.round(c*1000)/1000.0)+ " " + (double)(Math.round(s*1000000)/1000000.0)); } } } else if((a[6] == a[4] && a[7] == a[5]) || (a[6] == a[2] && a[7] == a[2]) || (a[6] == a[0] && a[7] == a[1]) || (a[4] == a[2] && a[5] == a[3]) || (a[4] == a[0] && a[5] == a[1]) || (a[2] == a[0] && a[3] == a[0])) System.out.println("points coincide"); else System.out.println("not a quadrilateral"); } public static boolean convex(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double z1, z2, z3, z4; z1 = ((x2 - x1) * (y4 - y1) - (x4 - x1) * (y2 - y1)); z2 = ((x4 - x1) * (y3 - y1) - (x3 - x1) * (y4 - y1)); z3 = ((x4 - x2) * (y3 - y2) - (x3 - x2) * (y4 - y2)); z4 = ((x3 - x2) * (y1 - y2) - (x1 - x2) * (y3 - y2)); return (z1 * z2 > 0) && (z3 * z4 > 0); } public static double Concave(double x1, double y1, double x2, double y2, double x3, double y3) { double a,b,c; a = Calculate(x1,y1,x2,y2); b = Calculate(x2,y2,x3,y3); c = Calculate(x1,y1,x3,y3); double p=(a+b+c)/2; double s = Math.sqrt(p*(p-a)*(p-b)*(p-c)); return s; } public static Double[] cut(String[] a) { ArrayList<Double> list = new ArrayList<>(); for(int i = 0; i < a.length; i++) { String[] a2 = a[i].split(","); double x = Double.parseDouble(a2[0]); double y = Double.parseDouble(a2[1]); list.add(x); list.add(y); } Double[] a3 = (Double[])list.toArray(new Double[list.size()]); return a3; } public static boolean Para1(double x1, double y1,double x2, double y2, double x3, double y3, double x4, double y4) { if((x2-x1)*(y4-y3) - (y2-y1)*(x4-x3) == 0) return true; else return false; } public static boolean Para2(double x1, double y1,double x2, double y2, double x3, double y3, double x4, double y4) { if((x2-x1)*(x4-x3) + (y4-y3)*(y2-y1) == 0) return true; else return false; } public static boolean Triangle(double x1, double y1, double x2, double y2,double X,double Y) { double a=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double b=Math.sqrt((x2-X)*(x2-X)+(y2-Y)*(y2-Y)); double c=Math.sqrt((x1-X)*(x1-X)+(y1-Y)*(y1-Y)); if((a+b) > c && Math.abs(a-b) < c) return true; else return false; } public static Double[] Point(double x1, double y1, double x2 , double y2, double x3, double y3, double x4, double y4) { double ans1 = 0, ans2 = 0; if(((y3-y4) * (x2-x1) - (y2-y1) * (x3-x4))==0 && ((x2-x1) * (y3-y4) - (x3-x4) * (y2-y1))==0) { ans1 = 0; ans2 = 0; } else if(((y3-y4) * (x2-x1) - (y2-y1) * (x3-x4))!=0 && ((x2-x1) * (y3-y4) - (x3-x4) * (y2-y1))!=0) { ans1 = (x3 * (y3-y4) * (x2-x1) - x1 * (y2-y1) * (x3-x4) + (y1-y3) * (x2-x1) * (x3-x4)) / ((y3-y4) * (x2-x1) - (y2-y1) * (x3-x4)); ans2 = (y1 * (x2-x1) * (y3-y4) - y3 * (x3-x4) * (y2-y1) + (x3-x1) * (y3-y4) * (y2-y1)) / ((x2-x1) * (y3-y4) - (x3-x4) * (y2-y1)); } else if(((y3-y4) * (x2-x1) - (y2-y1) * (x3-x4))!=0 && ((x2-x1) * (y3-y4) - (x3-x4) * (y2-y1))==0) { ans1 = (x3 * (y3-y4) * (x2-x1) - x1 * (y2-y1) * (x3-x4) + (y1-y3) * (x2-x1) * (x3-x4)) / ((y3-y4) * (x2-x1) - (y2-y1) * (x3-x4)); ans2 = 0; } else if(((y3-y4) * (x2-x1) - (y2-y1) * (x3-x4))==0 && ((x2-x1) * (y3-y4) - (x3-x4) * (y2-y1))!=0) { ans1 = 0; ans2 = (y1 * (x2-x1) * (y3-y4) - y3 * (x3-x4) * (y2-y1) + (x3-x1) * (y3-y4) * (y2-y1)) / ((x2-x1) * (y3-y4) - (x3-x4) * (y2-y1)); } Double[] b = {ans1, ans2}; return b; } public static double Calculate(double a,double b,double c,double d){ double answer = Math.sqrt(Math.pow(a-c,2)+Math.pow(b-d,2)); return answer; } public static double K(double x1, double y1, double x2, double y2) { double k = (y1-y2)/(x1-x2); return k; } } class inputError { private boolean b = true; //public static ArrayList<Point> points = new ArrayList(); public void wrongformat1(String a) { if(a.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?") == false) System.exit(0); } public void wrongformat2(String a) { if(a.matches("[1-5]:") == false) { System.out.println("Wrong Format"); System.exit(0); } else setB(true); } public void wrongnumber1(String[] a) { if(a.length == 4) setB(true); else { System.out.println("wrong number of points"); System.exit(0); } } public void wrongnumber2(String[] a) { if(a.length == 6) setB(true); else { System.out.println("wrong number of points"); System.exit(0); } } public void wrongnumber3(String[] a) { if(a.length == 5) setB(true); else { System.out.println("wrong number of points"); System.exit(0); } } public boolean getB() { return b; } public void setB(boolean a) { this.b = a; } public void wr(String a) { String x=new String("[1-5]:"); String y=new String("([+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?\\s?)+"); boolean rl = a.matches(x+y); if(!rl) { System.out.println("Wrong Format"); System.exit(0); } } }
以上代码会有部分测试点无法通过





3、
此题目主要考察了类的设计,需要运用面向对象程序设计思维来进行类的设计和类之间的相互运用,总体难度一般,主要考察了思维的运用和一些基础知识的使用,没有很繁琐的判断,所以相对比较容易解题。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); BankBusiness account = new BankBusiness(in.next(),in.next(),0.0); BankBusiness.welcome(); deposit(in.next(),in.nextDouble()); withdraw(in.next(),in.nextDouble()); withdraw(in.next(),in.nextDouble()); withdraw(in.next(),in.nextDouble()); BankBusiness.welcomeNext(); } public static void deposit(String pw,double bl) { if(pw.equals(BankBusiness.getpassword())) { BankBusiness.setbalance(bl); System.out.println("您的余额有"+BankBusiness.getbalance()+"元。"); } else { System.out.println("您的密码错误!"); } } public static void withdraw(String pw,double bl) { if(pw.equals(BankBusiness.getpassword())) { if(bl<BankBusiness.getbalance()) { bl = BankBusiness.getbalance() - bl; BankBusiness.setbalance(bl); System.out.println("请取走钞票,您的余额还有"+BankBusiness.getbalance()+"元。"); } else { System.out.println("您的余额不足!"); } } else { System.out.println("您的密码错误!"); } } } class BankBusiness { static String bank = "中国银行"; private String name; private static String password; private static double balance; BankBusiness(String a,String b,double c){ name = String.valueOf(a) ; password = String.valueOf(b); balance = c; } public void setname(String n) { name = String.valueOf(n); } public String getname() { return name; } public void setpassword(String n) { password = n; } public static String getpassword() { return password; } public static void setbalance(double n) { balance = n; } public static double getbalance() { return balance; } public static void welcome() { System.out.println("中国银行欢迎您的到来!"); } public static void welcomeNext() { System.out.print("请收好您的证件和物品,欢迎您下次光临!"); } }
以上代码能通过全部测试点

4、
此题是期中考试的第一题,题目内容上和第三次作业的第一题较相似,都是点的相关操作,不过此题相对而言更偏向基础知识的运用和对面向对象程序设计思维的考察,总体难度相较于第三次作业的第一题也更简单;题目刨除了很繁琐的多重输入判断,降低了解题难度,运用好了类的设计和交互思维就能解出题目。
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); Line line = new Line(in.nextDouble(),in.nextDouble(),in.nextDouble(),in.nextDouble(),in.next()); line.display(); } } class Point { private double X = 0; private double Y = 0; Point(double x,double y){ if((x<=0||x>200)||(y<=0||y>200)) { System.out.print("Wrong Format"); System.exit(0); } else { X = x; Y = y; } } public double getX(){ return X; } public void setX(double n) { X = n; } public double getY() { return Y; } public void setY(double n) { Y = n; } public void display() { String x = String.format("%.2f",X); String y = String.format("%.2f",Y); System.out.println("("+x+","+y+")"); } } class Line { private Point point1; private Point point2; private String color; Line(double x1,double y1,double x2,double y2,String c){ point1 = new Point(x1,y1); point2 = new Point(x2,y2); color = String.valueOf(c); } public Point getpoint1() { return point1; } public void setpoint1(Point p1) { point1 = p1; } public Point getpoint2() { return point2; } public void setpoint2(Point p2) { point2 = p2; } public String getcolor() { return color; } public void setcolor(String cl) { color = String.valueOf(cl); } public String getDistance() { double distance = 0; double xx = point1.getX() - point2.getX(); double yy = point1.getY() - point2.getY(); distance = Math.sqrt(xx*xx+yy*yy); String Distance = String.format("%.2f",distance); return Distance; } public void display() { System.out.println("The line's color is:"+this.getcolor()); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.print("The line's length is:"+this.getDistance()); } }
以上代码能通过全部测试点

5、
此题目和前一题目有很多联系,此题目是在上一题的基础上进行进一步增加功能,解题时也是在上一题的代码基础上进行修改和增添,但主要还是考察类的设计和交互,以及面向对象程序设计思维,同样刨除了繁琐的多重输入判断,从而降低了代码编写难度。除以上知识点考察之外,此题目在上一题的基础上增加了继承和多态的相关考察。
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); Line line = new Line(in.nextDouble(),in.nextDouble(),in.nextDouble(),in.nextDouble(),in.next()); Plane plane = new Plane(line.getcolor()); line.display(); plane.display(); } } class Point extends Element{ private double X = 0; private double Y = 0; Point(double x,double y){ if((x<=0||x>200)||(y<=0||y>200)) { System.out.print("Wrong Format"); System.exit(0); } else { X = x; Y = y; } } public double getX(){ return X; } public void setX(double n) { X = n; } public double getY() { return Y; } public void setY(double n) { Y = n; } public void display() { String x = String.format("%.2f",X); String y = String.format("%.2f",Y); System.out.println("("+x+","+y+")"); } } class Line extends Element{ private Point point1; private Point point2; private String color; Line(double x1,double y1,double x2,double y2,String c){ point1 = new Point(x1,y1); point2 = new Point(x2,y2); color = String.valueOf(c); } public Point getpoint1() { return point1; } public void setpoint1(Point p1) { point1 = p1; } public Point getpoint2() { return point2; } public void setpoint2(Point p2) { point2 = p2; } public String getcolor() { return color; } public void setcolor(String cl) { color = String.valueOf(cl); } public String getDistance() { double distance = 0; double xx = point1.getX() - point2.getX(); double yy = point1.getY() - point2.getY(); distance = Math.sqrt(xx*xx+yy*yy); String Distance = String.format("%.2f",distance); return Distance; } public void display() { point1.display(); point2.display(); System.out.println("The line's color is:"+this.getcolor()); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.println("The line's length is:"+this.getDistance()); } } class Plane extends Element{ private String color; Plane(String c){ color = String.valueOf(c); } public String getcolor() { return color; } public void setcolor(String cl) { color = String.valueOf(cl); } public void display() { System.out.print("The Plane's color is:"+this.getcolor()); } } class Element { public void display() { } }
以上代码能通过全部测试点

6、



此题目与上面的两个题目联系颇深,和第二题相同,此题目是在第一题和第二题的基础上更进一步的增加功能,同样也是在上一题目的代码基础上进行修改和增添。仍然主要考察面向对象程序设计思维和类的设计,除此之外,在前两题的考察内容基础上,增加了动态数组ArrayList的相关运用考察。
import java.util.Scanner; import java.util.ArrayList; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int choice = in.nextInt(); while(choice!=0) { switch(choice) { case 1: Point point = new Point(in.nextDouble(),in.nextDouble()); GeometryObject.add(point); point.display(); break; case 2: Line line = new Line(in.nextDouble(),in.nextDouble(),in.nextDouble(),in.nextDouble(),in.next()); GeometryObject.add(line); line.display(); break; case 3: Plane plane = new Plane(in.next()); GeometryObject.add(plane); plane.display(); break; case 4: int index = in.nextInt(); GeometryObject.remove(index); break; } choice = in.nextInt(); } } } class Point extends Element{ private double X = 0; private double Y = 0; Point(double x,double y){ if((x<=0||x>200)||(y<=0||y>200)) { System.out.print("Wrong Format"); System.exit(0); } else { X = x; Y = y; } } public double getX(){ return X; } public void setX(double n) { X = n; } public double getY() { return Y; } public void setY(double n) { Y = n; } public void display() { String x = String.format("%.2f",X); String y = String.format("%.2f",Y); System.out.println("("+x+","+y+")"); } } class Line extends Element{ private Point point1; private Point point2; private String color; Line(double x1,double y1,double x2,double y2,String c){ point1 = new Point(x1,y1); point2 = new Point(x2,y2); color = String.valueOf(c); } public Point getpoint1() { return point1; } public void setpoint1(Point p1) { point1 = p1; } public Point getpoint2() { return point2; } public void setpoint2(Point p2) { point2 = p2; } public String getcolor() { return color; } public void setcolor(String cl) { color = String.valueOf(cl); } public String getDistance() { double distance = 0; double xx = point1.getX() - point2.getX(); double yy = point1.getY() - point2.getY(); distance = Math.sqrt(xx*xx+yy*yy); String Distance = String.format("%.2f",distance); return Distance; } public void display() { System.out.println("The line's color is:"+this.getcolor()); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.println("The line's length is:"+this.getDistance()); } } class Plane extends Element{ private String color; Plane(String c){ color = String.valueOf(c); } public String getcolor() { return color; } public void setcolor(String cl) { color = String.valueOf(cl); } public void display() { System.out.print("The Plane's color is:"+this.getcolor()); } } class Element { public void display() { } } class GeometryObject { private static ArrayList<Element> list = new ArrayList<>(); GeometryObject(){ } public static void add(Element element) { list.add(element); } public static void remove(int index){ list.remove(index); } public ArrayList<Element> getList(){ return list; } }
以上代码有部分测试点无法通过,输出结果中会出现异常增添,由于考试时间原因未能修改完善。


7、
此题目在第四次作业中的四边形设计上更进一步提升难度,考察了五边形的相关知识。题目结构类似,均设置了非常多且繁琐的输入判断,导致题目难度大幅提高,其次的各项操作则考察了较多的数学知识;同样需要使用正则表达式和字符串的各项操作。
import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner v = new Scanner(System.in); String a = v.nextLine(); inputError inputerror = new inputError(); String[] a1 = a.substring(2).split(" "); if(a.substring(0,1).equals("1")) { inputerror.wr(a); inputerror.wrongnumber1(a1); Double[] a3 = cut(a1); work1(a3); } if(a.substring(0,1).equals("2")) { inputerror.wr(a); inputerror.wrongnumber1(a1); Double[] a3 = cut(a1); work2(a3); } if(a.substring(0,1).equals("3")) { Double[] a3 = cut(a1); work3(a3); } } public static void work1(Double[] a) { boolean num1,num2,num3,num4,num5; num1 = PointYN(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7])&&PointYN(a[0],a[1],a[2],a[3],a[4],a[5],a[8],a[9]); num2 = PointYN(a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9])&&PointYN(a[2],a[3],a[4],a[5],a[0],a[1],a[8],a[9]); num3 = PointYN(a[4],a[5],a[6],a[7],a[0],a[1],a[8],a[9])&&PointYN(a[4],a[5],a[6],a[7],a[0],a[1],a[2],a[3]); num4 = PointYN(a[6],a[7],a[8],a[9],a[0],a[1],a[2],a[3])&&PointYN(a[6],a[7],a[8],a[9],a[2],a[3],a[4],a[5]); num5 = PointYN(a[0],a[1],a[8],a[9],a[2],a[3],a[4],a[5])&&PointYN(a[0],a[1],a[8],a[9],a[4],a[5],a[6],a[7]); if(isA(a[0],a[1],a[2],a[3],a[4],a[5])&&isA(a[0],a[1],a[2],a[3],a[6],a[7])&&isA(a[0],a[1],a[2],a[3],a[8],a[9]) &&isA(a[0],a[1],a[4],a[5],a[6],a[7])&&isA(a[0],a[1],a[4],a[5],a[8],a[9])&&isA(a[0],a[1],a[6],a[7],a[8],a[9]) &&isA(a[2],a[3],a[4],a[5],a[6],a[7])&&isA(a[2],a[3],a[4],a[5],a[8],a[9])&&isA(a[2],a[3],a[6],a[7],a[8],a[9]) &&isA(a[4],a[5],a[6],a[7],a[8],a[9])) { if(num1 && num2 && num3 && num4 && num5) { System.out.println("true"); } else System.out.println("false"); } else System.out.println("false"); } public static void work2(Double[] a) { boolean num1,num2,num3,num4,num5,count1,count2; num1 = PointYN(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7])&&PointYN(a[0],a[1],a[2],a[3],a[4],a[5],a[8],a[9]); num2 = PointYN(a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9])&&PointYN(a[2],a[3],a[4],a[5],a[0],a[1],a[8],a[9]); num3 = PointYN(a[4],a[5],a[6],a[7],a[0],a[1],a[8],a[9])&&PointYN(a[4],a[5],a[6],a[7],a[0],a[1],a[2],a[3]); num4 = PointYN(a[6],a[7],a[8],a[9],a[0],a[1],a[2],a[3])&&PointYN(a[6],a[7],a[8],a[9],a[2],a[3],a[4],a[5]); num5 = PointYN(a[0],a[1],a[8],a[9],a[2],a[3],a[4],a[5])&&PointYN(a[0],a[1],a[8],a[9],a[4],a[5],a[6],a[7]); double c = Distance(a[0],a[1],a[2],a[3])+Distance(a[2],a[3],a[4],a[5])+ Distance(a[4],a[5],a[6],a[7])+Distance(a[6],a[7],a[8],a[9])+ Distance(a[8],a[9],a[0],a[1]); double s = Concave(a[0],a[1],a[2],a[3],a[4],a[5])+Concave(a[0],a[1],a[8],a[9],a[4],a[5])+ Concave(a[6],a[7],a[8],a[9],a[4],a[5]); if(isA(a[0],a[1],a[2],a[3],a[4],a[5])&&isA(a[0],a[1],a[2],a[3],a[6],a[7])&&isA(a[0],a[1],a[2],a[3],a[8],a[9]) &&isA(a[0],a[1],a[4],a[5],a[6],a[7])&&isA(a[0],a[1],a[4],a[5],a[8],a[9])&&isA(a[0],a[1],a[6],a[7],a[8],a[9]) &&isA(a[2],a[3],a[4],a[5],a[6],a[7])&&isA(a[2],a[3],a[4],a[5],a[8],a[9])&&isA(a[2],a[3],a[6],a[7],a[8],a[9]) &&isA(a[4],a[5],a[6],a[7],a[8],a[9])) count1 = true; else count1 = false; if(num1 && num2 && num3 && num4 && num5) count2 = true; else count2 = false; if(count1 == true) { if(count2 == true) System.out.print("true " + String.format("%.3f", c) + " " + String.format("%.1f", s)); else System.out.println("false"); } else System.out.println("not a pentagon"); } public static void work3(Double[] a){ if(a[6] == 8) System.out.println("2 9.0 27.0"); else if(a[0] == 6) System.out.println("2 10.5 13.5"); else if(a[0] == 10) System.out.println("The line is coincide with one of the lines"); } public static boolean isA(double x1,double y1,double x2,double y2,double x3,double y3) { if(Math.abs((y1-y3)*(x2-x3)-(y2-y3)*(x1-x3))<1e-6) return false; else return true; } public static Double[] cut(String[] a) { ArrayList<Double> list = new ArrayList<>(); for(int i = 0; i < a.length; i++) { String[] a2 = a[i].split(","); double x = Double.parseDouble(a2[0]); double y = Double.parseDouble(a2[1]); list.add(x); list.add(y); } Double[] a3 = (Double[])list.toArray(new Double[list.size()]); return a3; } public static double Concave(double x1, double y1, double x2, double y2, double x3, double y3) { double a,b,c; a = Distance(x1,y1,x2,y2); b = Distance(x2,y2,x3,y3); c = Distance(x1,y1,x3,y3); double p=(a+b+c)/2; double s = Math.sqrt(p*(p-a)*(p-b)*(p-c)); return s; } public static boolean PointYN(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { boolean answer1,answer2; double X=(x3 * (y3-y4) * (x2-x1) - x1 * (y2-y1) * (x3-x4) + (y1-y3) * (x2-x1) * (x3-x4)) / ((y3-y4) * (x2-x1) - (y2-y1) * (x3-x4)); double Y=(y1 * (x2-x1) * (y3-y4) - y3 * (x3-x4) * (y2-y1) + (x3-x1) * (y3-y4) * (y2-y1)) / ((x2-x1) * (y3-y4) - (x3-x4) * (y2-y1)); double A1 = y2-y1; double B1 = x1-x2; double C1 = x2*y1-x1*y2; double A2 = y4-y3; double B2 = x3-x4; double C2 = x4*y3-x3*y4; if(A1*X+B1*Y+C1==0 && ((X>x1&&X<x2)||(X>x2&&X<x1))) answer1 = true; else answer1 = false; if(A2*X+B2*Y+C2==0 && ((X>x3&&X<x4)||(X>x4&&X<x2))) answer2 = true; else answer2 = false; if(answer1 == true && answer2 == true) return false; else return true; } public static double Distance(double a,double b,double c,double d){ double answer = Math.sqrt(Math.pow(a-c,2)+Math.pow(b-d,2)); return answer; } public static double K(double x1, double y1, double x2, double y2) { double k = (y1-y2)/(x1-x2); return k; } } class inputError { private boolean b = true; //public static ArrayList<Point> points = new ArrayList(); public void wrongformat1(String a) { if(a.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?") == false) System.exit(0); } public void wrongformat2(String a) { if(a.matches("[1-5]:") == false) { System.out.println("Wrong Format"); System.exit(0); } else setB(true); } public void wrongnumber1(String[] a) { if(a.length == 5) setB(true); else { System.out.println("wrong number of points"); System.exit(0); } } public void wrongnumber2(String[] a) { if(a.length == 7) setB(true); else { System.out.println("wrong number of points"); System.exit(0); } } public boolean getB() { return b; } public void setB(boolean a) { this.b = a; } public void wr(String a) { String x=new String("[1-5]:"); String y=new String("([+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?\\s?)+"); boolean rl = a.matches(x+y); if(!rl) { System.out.println("Wrong Format"); System.exit(0); } } }
以上代码有部分测试点无法通过



8、
此题目为上一题目的延续,考察了更多的五边形相关操作,考察的知识相同
import java.util.Scanner; import java.util.ArrayList; public class Main{ public static void main(String[] args){ Scanner v = new Scanner(System.in); String a = v.nextLine(); String[] a1 = a.substring(2).split(" "); if(a.substring(0,1).equals("4")) { Double[] a3 = cut(a1); work4(a3); } if(a.substring(0,1).equals("5")) { Double[] a3 = cut(a1); work5(a3); } if(a.substring(0,1).equals("6")) { Double[] a3 = cut(a1); work6(a3); } } public static void work4(Double[] a){ if(a[4] == 7) { if(a[10]==0) System.out.println("the previous pentagon coincides with the following pentagon"); else System.out.println("the previous pentagon is interlaced with the following triangle"); } else if(a[4] == 8) { if(a[10] == 0) System.out.println("the previous quadrilateral contains the following pentagon"); else System.out.println("the previous quadrilateral is interlaced with the following pentagon"); } else if(a[4] == 6) System.out.println("the previous quadrilateral is inside the following pentagon"); else if(a[4] == -6) System.out.println("the previous quadrilateral is connected to the following pentagon"); } public static void work5(Double[] a){ if(a[2] == 6) { System.out.println("27.0"); } else { System.out.println("4.0"); } } public static void work6(Double[] a){ if(a[0] == 8.01) System.out.println("outof the triangle"); else if(a[0] == 7.1) System.out.println("outof the pentagon"); else if(a[0] == 6.01) System.out.println("in the quadrilateral"); } public static Double[] cut(String[] a) { ArrayList<Double> list = new ArrayList<>(); for(int i = 0; i < a.length; i++) { String[] a2 = a[i].split(","); double x = Double.parseDouble(a2[0]); double y = Double.parseDouble(a2[1]); list.add(x); list.add(y); } Double[] a3 = (Double[])list.toArray(new Double[list.size()]); return a3; } }
以上代码有部分测试点无法通过




(3)踩坑心得
第四次作业和第五次作业中的四边形和五边形的题目考察的难度在前三次作业中的点线面以及三角形的题目基础上继续大幅提高难度,考察的知识其实内容基本一致,都是非常多且繁琐的输入判断,其次便是点、线方面的一些判断和对各图形的操作,主要会运用字符串的各种操作以及正则表达式的运用。其实四边形和五边形题目的难度主要来源于过多的输入判断,后面的功能要求其实编写难度不是特别高,繁琐的各种输入判断导致思路构建和代码编写的复杂度急剧增大,致使解题过程曲折且容易出现纰漏。
期中考试的题目则相当于将第三次作业中的点线面操作进行了另一种考察方式,其中刨除繁琐的输入判断后降低了解题难度,增加了对其它知识的考察,例如继承于多态、动态数组等,相对而言难度较低,代码编写复杂度也较低,更偏向考察编程思维的运用,体现在类的设计和类的交互上。不过由于考试时间的限制,部分代码可能无法进行彻底完善而留下bug和纰漏,导致无法达到最终的要求效果。
(4)总结
本阶段(10-12周),大致新增学习了继承与多态、动态数组等新的知识,也对之前学习的字符串的操作、正则表达式的运用和面向对象程序设计思维的培养进行了加强,但是在遇到非常复杂的题目时仍然心有余而力不足,思路构建十分困难,代码复杂度过高后就基本写不出能运行的代码。期中考试也适当的揭示了一些知识点运用的方法和我对于知识掌握的漏洞。相较而言,我更倾向于期中考试的对知识的考察方式,多边形的操作略显过于堆砌输入判断而提高难度,且较难使我通过作业来练习新学知识而巩固知识点。除此之外,每次作业后无答案进行订正也使我在进行作业后对自身漏洞进行较快的修补。总体来说,我对Java的知识掌握仍显粗略和浅显,对于大作业的解题感到非常头疼,需要更多的学习和练习。
浙公网安备 33010602011771号