java - How can I optimize this little piece of code -
i started java course , given assignment check if 2 triangles congruent based on values of vertices provided user. not allowed add in packages(except 1 did) nor allowed use loops. works fine there shorter more effecient solution?
import java.util.scanner; public class congruent { public static void main (string [] args) { scanner scan = new scanner (system.in); system.out.println("we calculate if 2 triangles congruent."); system.out.println("enter vertices values of first triangle."); system.out.println("please enter x1:"); float x11 = scan.nextint(); system.out.println("please enter y1:"); float y11 = scan.nextint(); system.out.println("please enter x2:"); float x12 = scan.nextint(); system.out.println("please enter y2:"); float y12 = scan.nextint(); system.out.println("please enter x3:"); float x13 = scan.nextint(); system.out.println("please enter y3:"); float y13 = scan.nextint(); system.out.println("enter vertices values of second triangle."); system.out.println("please enter x1:"); float x21 = scan.nextint(); system.out.println("please enter y1:"); float y21 = scan.nextint(); system.out.println("please enter x2:"); float x22 = scan.nextint(); system.out.println("please enter y2:"); float y22 = scan.nextint(); system.out.println("please enter x3:"); float x23 = scan.nextint(); system.out.println("please enter y3:"); float y23 = scan.nextint(); //calculating distances between vertices of first triangle , placing them in variables double a1 = math.sqrt((math.pow((x11 - x12), 2)) + (math.pow((y11 - y12), 2))); double b1 = math.sqrt((math.pow((x12 - x13), 2)) + (math.pow((y12 - y13), 2))); double c1 = math.sqrt((math.pow((x13 - x11), 2)) + (math.pow((y13 - y11), 2))); //calculating distances between vertices of second triangle , placing them in variables double a2 = math.sqrt((math.pow((x21 - x22), 2)) + (math.pow((y21 - y22), 2))); double b2 = math.sqrt((math.pow((x22 - x23), 2)) + (math.pow((y22 - y23), 2))); double c2 = math.sqrt((math.pow((x23 - x21), 2)) + (math.pow((y23 - y21), 2))); system.out.println ("the first triangle (" + x11 + ", " + y11 + ") (" + x12 + ", " + y12 + ") (" + x13 + ", " + y13 + ")."); system.out.println ("the lengths " + a1 + "," + b1 + "," + c1 + "."); system.out.println ("the second triangle (" + x21 + ", " + y21 + ") (" + x22 + ", " + y22 + ") (" + x23 + ", " + y23 + ")."); system.out.println ("the lengths " + a2 + "," + b2 + "," + c1 + "."); // if else chain compares distances between vertices calculate if triangles congruent on not , outputs result user if (a1 == a2 && b1 == b2 && c1 == c2) { system.out.println ("the triangles congruent"); } else if (a1 == a2 && b1 == c2 && c1 == b2) { system.out.println ("the triangles congruent"); } else if (a1 == b2 && b1 == c2 && c1 == a2) { system.out.println ("the triangles congruent"); } else if (a1 == b2 && b1 == a2 && c1 == c2) { system.out.println ("the triangles congruent"); } else if (a1 == c2 && b1 == b2 && c1 == a2) { system.out.println ("the triangles congruent"); } else if (a1 == c2 && b1 == a2 && c1 == b2) { system.out.println ("the triangles congruent"); } else system.out.println ("the triangles not congruent"); } }
Comments
Post a Comment