Menu
Topics Index
...
`

Weekend Hack - 23 Feb 2013 - Results

Thanks to Deepika Koduri, Sulekha Metta, Tejaswini Rao, Vuppala Mounika, Radhika Bottu, Raghu Ram Bharadwaj Diddigi, Sanjay Kumar and Om Prakash for spending so much time, coming up with original and innovative answers and for submitting them. Thanks to many others who have tried to get the solution.
We have reviewed the solutions as per the criteria mentioned at Weekend Hack and included the comments below. Few minor comments found are included below in the order of the submission. These comments are not intended to point out the mistakes in the solutions but to learn from them.

The winner of the Weekend Hack 23 Feb 2013 - 1 is Vuppala Mounika. She will get a recharge or gift voucher worth Rs. 125.
Weekend Hack 23 Feb 2013 - 2 is not solved.

To get the type of the triangle formed by the given three points

Write a program to get the type of the triangle formed by the given three points.
Note: When comparing the sides for equilateral trianlge, please round them to the nearest integer.

Input (Point, Point, Point) Output (String)
[0, 0], [5, 0], [0, 5] Isosceles Right Angled Triangle
[90, 0], [0, 0], [45, 77.94] Equilateral Triangle
[0, 2], [5, 0], [0, 5] Scalene Obtuse Triangle
[3, 1], [1.5, 1.2], [2.5, 4.3] Scalene Acute Triangle
[5, 10], [10, 10], [7.5, 12] Isosceles Obtuse Triangle

Solution 1 - Submitted on Sat Feb 23 11:32:30 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        int asq = lengthsq(secondPoint, thirdPoint);
        int bsq = lengthsq(firstPoint, thirdPoint);
        int csq = lengthsq(secondPoint, firstPoint);
        double a = Math.sqrt(asq), b = Math.sqrt(bsq), c = Math.sqrt(csq);
        if (a == b + c || b == a + c || c == a + b)
            return null;
        a = (int) (a + 0.5);
        b = (int) (b + 0.5);
        c = (int) (c + 0.5);
        String out = "";
        if (a == b && b == c)
            out = "Equilateral Triangle";
        else {
            out = asq == bsq || bsq == csq || csq == asq ? "Isosceles " : "Scalene ";
            out += csq == asq + bsq || asq == csq + bsq || bsq == asq + csq ? "Right Angled Triangle" : csq > asq + bsq || asq > csq + bsq || bsq > asq + csq ? "Obtuse Triangle" : "Acute Triangle";
        }
        return out;
    }

    public static int lengthsq(Point a, Point b) {
        return (int) (Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. We could use Math.round() method to rounding the value instead of adding 0.5.
  2. Could have given better names for a, b, c, out, asq, bsq and csq.
  3. Could have given better method name for lengthsq.
  4. Using the ternary operator is good but it is a bit confusing to understand that big statements. It is better to write smaller statements.

Solution 2 - Submitted on Sat Feb 23 12:19:08 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        double p, q, r;
        String res;
        p = compute_Distance(firstPoint, secondPoint);
        q = compute_Distance(secondPoint, thirdPoint);
        r = compute_Distance(thirdPoint, firstPoint);
        res = type(p, q, r);
        return res;
    }

    public static double compute_Distance(Point p, Point q) {
        double temp = Math.sqrt((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y));
        return temp;
    }

    public static String type(double p, double q, double r) {
        String str = "";
        double x, y, z;
        long angle1, angle2, angle3;
        boolean flag = false;
        if (Math.round(p) == Math.round(q) && Math.round(q) == Math.round(r)) {
            flag = true;
            str = "Equilateral Triangle";
        } else if (p == q || q == r || r == p)
            str = "Isosceles";
        else
            str = "Scalene";
        if (!flag) {
            x = (p * p + q * q - r * r) / (2 * p * q);
            y = (q * q + r * r - p * p) / (2 * q * r);
            z = (r * r + p * p - q * q) / (2 * r * p);
            angle1 = Math.round(Math.toDegrees(Math.acos(x)));
            angle2 = Math.round(Math.toDegrees(Math.acos(y)));
            angle3 = Math.round(Math.toDegrees(Math.acos(z)));
            if (angle1 == 90 || angle2 == 90 || angle3 == 90)
                str = str + " Right Angled Triangle";
            else if (angle1 > 90 || angle2 > 90 || angle3 > 90)
                str = str + " Obtuse Triangle";
            else
                str = str + " Acute Triangle";
        }
        return (str);
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. Could have given better names for p, q, r, x, y, z, flag and str.
  2. Could have given better method name for type.
  3. We could use Math.pow() method for calculating the square.
  4. It is better to create a method for calculating the angle, so as to avoid duplicate code.

Solution 3 - Submitted on Sat Feb 23 12:32:00 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        double s1 = distance(firstPoint, secondPoint);
        double s2 = distance(secondPoint, thirdPoint);
        double s3 = distance(thirdPoint, firstPoint);
        double angle1 = calculateAngle(s1, s2, s3);
        double angle2 = calculateAngle(s2, s3, s1);
        double angle3 = calculateAngle(s1, s3, s2);
        int side1 = (int) (s1 + 0.5);
        int side2 = (int) (s2 + 0.5);
        int side3 = (int) (s3 + 0.5);
        String type = null;
        String angle = null;
        if (side1 == side2 && side1 == side3) {
            type = "Equilateral Triangle";
            return type;
        } else if (s1 == s2 || s2 == s3)
            type = "Isosceles";
        else
            type = "Scalene";
        if (angle1 == 90 || angle2 == 90 || angle3 == 90)
            angle = " Right Angled Triangle";
        else if (angle1 > 90 || angle2 > 90 || angle3 > 90)
            angle = " Obtuse Triangle";
        else
            angle = " Acute Triangle";
        type = type + angle;
        return type;
    }

    public static double distance(Point p1, Point p2) {
        double x = p1.x - p2.x;
        double y = p1.y - p2.y;
        double result = Math.sqrt(x * x + y * y);
        return result;
    }

    public static double calculateAngle(double s1, double s2, double s3) {
        double co = (s1 * s1 + s2 * s2 - s3 * s3) / (2 * s1 * s2);
        double res = (int) (Math.toDegrees(Math.acos(co)) + 0.5);
        return res;
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. For Isosceles, we are only checking s1 == s2 || s2 == s3, and missed the condition s1 == s3.
  2. The methods distance and calculateAngle are created properly to reduce duplicate code.
  3. We could use Math.round() method to rounding the value instead of adding 0.5.
  4. We could use Math.pow() method for calculating the square.
  5. Could have given better names for s1, s2, s3 and type.

Solution 4 - Submitted on Sat Feb 23 14:47:34 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        StringBuffer st = new StringBuffer("");
        double a = lengthOfSides(firstPoint, secondPoint);
        double b = lengthOfSides(secondPoint, thirdPoint);
        double c = lengthOfSides(thirdPoint, firstPoint);
        double a1 = findAngles(firstPoint, secondPoint, thirdPoint);
        double a2 = findAngles(secondPoint, thirdPoint, firstPoint);
        double a3 = findAngles(thirdPoint, firstPoint, secondPoint);
        long b1 = Math.round(a);
        long b2 = Math.round(b);
        long b3 = Math.round(c);
        if (b1 == b2 && b2 == b3 && b3 == b1) {
            st.append("Equilateral Triangle");
        } else {
            if (a == b || b == c || c == a) {
                st.append("Isosceles");
            } else {
                st.append("Scalene");
            }
            if (a1 < 90.0 && a2 < 90.0 && a3 < 90.0) {
                st.append(" Acute Triangle");
            } else {
                if (a1 == 90.0 || a2 == 90.0 || a3 == 90.0)
                    st.append(" Right Angled Triangle");
                else
                    st.append(" Obtuse Triangle");
            }
        }
        String s = new String(st);
        return s;
    }

    public static double lengthOfSides(Point t1, Point t2) {
        double x = Math.sqrt(((t1.x - t2.x) * (t1.x - t2.x)) + ((t1.y - t2.y) * (t1.y - t2.y)));
        return x;
    }

    public static double findAngles(Point firstpoint, Point secondpoint, Point thirdpoint) {
        double p = secondpoint.x - firstpoint.x;
        double q = secondpoint.y - firstpoint.y;
        double r = thirdpoint.x - firstpoint.x;
        double s = thirdpoint.y - firstpoint.y;
        double ans = (p * r + q * s) / ((Math.sqrt(r * r + s * s)) * (Math.sqrt(p * p + q * q)));
        double angle = Math.toDegrees(Math.acos(ans));
        return angle;
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. The methods lengthOfSides and findAngles are created properly.
  2. if-else statements are effectively used and the resultant string is created with out any duplicate code.
  3. Using the StringBuffer for creating the final string is a good idea. Probably we could have used for StringBuilder for better performance.
  4. Could have given better names for a, b, c, a1, a2, a3, b1, b2, b3, st and s.
  5. We could use Math.pow() method for calculating the square.

Solution 5 - Submitted on Sat Feb 23 16:40:42 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        String type = " ";
        double d12 = distanceBetweenPoints(firstPoint, secondPoint);
        double d23 = distanceBetweenPoints(secondPoint, thirdPoint);
        double d31 = distanceBetweenPoints(thirdPoint, firstPoint);
        double ang1 = angleBetween(firstPoint, secondPoint, thirdPoint);
        double ang2 = angleBetween(secondPoint, thirdPoint, firstPoint);
        double ang3 = angleBetween(thirdPoint, firstPoint, secondPoint);
        if (d12 == d23 || d23 == d31 || d31 == d12)// if any two or all side are eqaul
        {
            if ((Math.round(ang1) == 60 && Math.round(ang2) == 60 && Math.round(ang3) == 60) || (Math.round(d12) == Math.round(d23) && Math.round(d23) == Math.round(d31) && Math.round(d31) == Math.round(d12))) {
                type = "Equilateral";
            } else {
                type = "Isosceles";
                String angle = isObtuseOrAcuteOrRight(ang1, ang2, ang3);
                type = type + " " + angle;
            }
        }// if no two sides are equal
        else {
            type = "Scalene";
            String angle = isObtuseOrAcuteOrRight(ang1, ang2, ang3);
            type = type + " " + angle;
        }
        type = type + " " + "Triangle";
        return type;
    }

    public static double distanceBetweenPoints(Point p1, Point p2) {
        double d;
        d = Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
        return d;
    }

    public static double angleBetween(Point p1, Point p2, Point p3) {
        double p12 = Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
        double p13 = Math.sqrt((p3.x - p1.x) * (p3.x - p1.x) + (p3.y - p1.y) * (p3.y - p1.y));
        double p23 = Math.sqrt((p3.x - p2.x) * (p3.x - p2.x) + (p3.y - p2.y) * (p3.y - p2.y));
        double radian = Math.acos((p12 * p12 + p13 * p13 - p23 * p23) / Math.abs((2 * p12 * p13)));
        double deg = radian * 180.0 / Math.PI;
        return deg;
    }

    public static String isObtuseOrAcuteOrRight(double a1, double a2, double a3) {
        String type = " ";
        if (Math.round(a1) == 90.00 || Math.round(a2) == 90.00 || Math.round(a3) == 90.00)
            type = "Right Angled";
        else if (a1 > 90.00 || a2 > 90.00 || a3 > 90.00)
            type = "Obtuse";
        else
            type = "Acute";
        return type;
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. Good separation of responsibilities, by using the methods - distanceBetweenPoints, angleBetween, isObtuseOrAcuteOrRight.
  2. Comments are good and informative.
  3. Could have given better names for d12, d23, d13 and type.
  4. We could use Math.pow() method for calculating the square.
  5. We already calculated the distance between points and again we are calculating in angleBetween method. Instead of that we could pass those calculated distances to that method.

Solution 6 - Submitted on Sat Feb 23 18:36:38 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        String s;
        double r = 90;
        double a = Math.sqrt((firstPoint.x - secondPoint.x) * (firstPoint.x - secondPoint.x) + (firstPoint.y - secondPoint.y) * (firstPoint.y - secondPoint.y));
        double b = Math.sqrt((firstPoint.x - thirdPoint.x) * (firstPoint.x - thirdPoint.x) + (firstPoint.y - thirdPoint.y) * (firstPoint.y - thirdPoint.y));
        double c = Math.sqrt((secondPoint.x - thirdPoint.x) * (secondPoint.x - thirdPoint.x) + (secondPoint.y - thirdPoint.y) * (secondPoint.y - thirdPoint.y));
        if (Math.round(a) == Math.round(b) && Math.round(a) == Math.round(c))
            s = "Equilateral Triangle";
        else {
            double ang1 = Math.toDegrees(Math.acos(((b * b) + (c * c) - (a * a)) / (2 * b * c)));
            double ang2 = Math.toDegrees(Math.acos(((b * b) + (a * a) - (c * c)) / (2 * b * a)));
            double ang3 = Math.toDegrees(Math.acos(((a * a) + (c * c) - (b * b)) / (2 * a * c)));
            if (a == b || a == c || b == c) {
                if (Math.round(ang1) > r || Math.round(ang2) > r || Math.round(ang3) > r)
                    s = "Isosceles Obtuse Triangle";
                else {
                    if (ang1 < r && ang2 < r && ang3 < r)
                        s = "Isosceles Acute Triangle";
                    else
                        s = "Isosceles Right Angled Triangle";
                }
            } else {
                if (Math.round(ang1) > r || Math.round(ang2) > r || Math.round(ang3) > r)
                    s = "Scalene Obtuse Triangle";
                else {
                    if (Math.round(ang1) < r && Math.round(ang2) < r && Math.round(ang3) < r)
                        s = "Scalene Acute Triangle";
                    else
                        s = "Scalene Right Angled Triangle";
                }
            }
        }
        return s;
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. Creating a variable for 90 is a good idea, but it could have better name than r
  2. The code is not very clear and a bit complex and it could have been better.
  3. Could have given better names for a, b, c, r and s.
  4. We could use Math.pow() method for calculating the square.
  5. There is duplicate code and could have avoided it by using methods.

Solution 7 - Submitted on Sat Feb 23 18:47:57 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        double firstSide = calculateSide(firstPoint, secondPoint);
        double secondSide = calculateSide(secondPoint, thirdPoint);
        double thirdSide = calculateSide(thirdPoint, firstPoint);
        if (Math.rint(firstSide) == Math.rint(secondSide) && Math.rint(firstSide) == Math.rint(thirdSide) && Math.rint(firstSide) == Math.rint(thirdSide)) {
            secondSide = Math.rint(secondSide);
            firstSide = Math.rint(firstSide);
            thirdSide = Math.rint(thirdSide);
        }
        double firstAngleSide = ((secondSide * secondSide) + (thirdSide * thirdSide) - (firstSide * firstSide)) / (2 * secondSide * thirdSide);
        double secondAngleSide = ((firstSide * firstSide) + (thirdSide * thirdSide) - (secondSide * secondSide)) / (2 * firstSide * thirdSide);
        double thirdAngleSide = ((firstSide * firstSide) + (secondSide * secondSide) - (thirdSide * thirdSide)) / (2 * firstSide * secondSide);
        if (firstSide == secondSide && secondSide == thirdSide) {
            return "Equilateral Triangle";
        }
        double firstAngle = Math.rint(Math.toDegrees(Math.acos(firstAngleSide)));
        double secondAngle = Math.rint(Math.toDegrees(Math.acos(secondAngleSide)));
        double thirdAngle = Math.rint(Math.toDegrees(Math.acos(thirdAngleSide)));
        System.out.print((firstAngle) + " " + secondAngle + " " + thirdAngle);
        double a[] = new double[3];
        a[0] = firstAngle;
        a[1] = secondAngle;
        a[2] = thirdAngle;
        int u = 0, s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0;
        while (u < a.length) {
            if (a[u] == 90.0) {
                s1++;
            }
            if (a[u] == 45.0) {
                s2++;
            }
            if (a[u] > 90.0) {
                s3++;
            }
            if (a[u] < 90.0) {
                s4++;
            }
            if (a[u] == 60.0) {
                s5++;
            }
            u++;
        }
        String s = " ";
        if (s1 == 1 && s4 == 2) {
            if (a[0] != a[1] && a[1] != a[2] && a[0] != a[2]) {
                s = "Scalene Right Angled Triangle";
            } else {
                s = "Isosceles Right Angled Triangle";
            }
        } else {
            if (s3 < 3 && s3 == 1) {
                if (a[0] != a[1] && a[1] != a[2] && a[0] != a[2]) {
                    s = "Scalene Obtuse Triangle";
                } else {
                    s = "Isosceles Obtuse Triangle";
                }
            }
            if (s4 == 3) {
                if (a[0] != a[1] && a[1] != a[2] && a[0] != a[2]) {
                    s = "Scalene Acute Triangle";
                } else {
                    s = "Isosceles Acute Triangle";
                }
            }
        }
        return s;
    }

    public static double calculateSide(Point firstPoint, Point secondPoint) {
        double xDiff = (secondPoint.x - firstPoint.x) * (secondPoint.x - firstPoint.x);
        double yDiff = (secondPoint.y - firstPoint.y) * (secondPoint.y - firstPoint.y);
        return (Math.sqrt(xDiff + yDiff));
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. The code is not very clear and a bit complex and it could have been better.
  2. We could use Math.pow() method for calculating the square.
  3. Could have given better names for s1, s2, s3, s4 and s5.
  4. a[0] != a[1] && a[1] != a[2] && a[0] != a[2] -- we could create a method to check this condition. We could use ternary operator instead of if-else, since it will make code compact.
  5. Please remove the unnecessary System.out.println before final submission.

Solution 8 - Submitted on Sat Feb 23 20:24:06 2013

class ClassifyTriangle {

    public static void main(String s[]) {
        Point firstPoint = new Point(0, 0);
        Point secondPoint = new Point(5, 0);
        Point thirdPoint = new Point(0, 5);
        System.out.println("Given three points forms : " + typeOfTriangle(firstPoint, secondPoint, thirdPoint));
    }

    public static String typeOfTriangle(Point firstPoint, Point secondPoint, Point thirdPoint) {
        String result = null;
        double[] d = new double[3];
        double a[] = new double[3];
        double s1, s2, s3;;
        d[0] = dist(firstPoint, secondPoint);
        d[1] = dist(secondPoint, thirdPoint);
        d[2] = dist(thirdPoint, firstPoint);
        s1 = slope(firstPoint, secondPoint);
        s2 = slope(secondPoint, thirdPoint);
        s3 = slope(thirdPoint, firstPoint);
        a[0] = ang(s1, s2);
        a[1] = ang(s2, s3);
        a[2] = ang(s3, s1);
        sort(d, 3);
        sort(a, 3);
        /*
        s1=Math.rint(s1);
        s2=Math.rint(s2);
        s3=Math.rint(s3);*/
        if (d[0] == d[1] || d[1] == d[2] || d[2] == d[0]) {
            if ((((d[0] * d[0]) + (d[1] * d[1])) > (d[2] * d[2])))
                result = "Isosceles Acute Triangle";
            else if ((((d[0] * d[0]) + (d[1] * d[1])) < (d[2] * d[2])))
                result = "Isosceles Obtuse Triangle";
            else {
                result = "Isosceles Right Angled Triangle";
            }
        }
        if (d[0] != d[1] && d[1] != d[2] && d[2] != d[0]) {
            if ((((d[0] * d[0]) + (d[1] * d[1])) > (d[2] * d[2])))
                result = "Scalene Acute Triangle";
            else if ((((d[0] * d[0]) + (d[1] * d[1])) < (d[2] * d[2])))
                result = "Scalene Obtuse Triangle";
            else {
                result = "Scalene Right Angled Triangle";
            }
        }
        d[0] = Math.rint(d[0]);
        d[1] = Math.rint(d[1]);
        d[2] = Math.rint(d[2]);
        if (d[0] == d[1] && d[1] == d[2] && d[2] == d[0]) {
            result = "Equilateral Triangle";
        }
        if (firstPoint.x == -7.0) {
            result = "Isosceles Right Angled Triangle";
        } else if (firstPoint.x == 25.0) {
            result = "Scalene Right Angled Triangle";
        }
        return result;
    }

    public static double dist(Point p1, Point p2) {
        double r, t;
        t = (((p1.x) - (p2.x)) * ((p1.x) - (p2.x))) + (((p1.y) - (p2.y)) * ((p1.y) - (p2.y)));
        if (t < 0) {
            t = (-1) * t;
        }
        r = Math.sqrt(t);
        return r;
    }

    public static double slope(Point p1, Point p2) {
        double m;
        m = (((p2.y) - (p1.y)) / ((p2.x) - (p1.x)));
        return m;
    }

    public static double ang(double slope1, double slope2) {
        double angle;
        angle = Math.atan((slope1 - slope2) / (1 + (slope1 * slope2)));
        angle = ((angle * 180) / 3.14);
        angle = Math.rint(angle);
        return angle;
    }

    public static void sort(double a[], int n) {
        int i, j;
        double t = 0;
        for (i = 0; i < n; i++) {
            for (j = 1; j < (n - i); j++) {
                if (a[j - 1] > a[j]) {
                    t = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = t;
                }
            }
        }
    }
}

class Point {

    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

Review Comments

  1. The code is not very clear and a bit complex and it could have been better.
  2. We could use Math.pow() method for calculating the square.
  3. Could have given better names for s1, s2, s3, a and d.
  4. Could have given better method names for dist and ang.
  5. In the method dist, there is no need to check if the value of t is less than 0, since we are getting the square of every side.
  6. Original code contained commented code. Before submitting the final code, please remove the unwanted code.

To print the longest chain of one's from the given binary matrix

Write a program to print the longest chain of one's from the given binary matrix.
Note: The longest path should be represented with series of numbers (1, 2, 3,...) as shown in the requirements. When two chains are of equal size the direction preference is Right, Left, Down and Up. Use System.out.print or System.out.println for printing.

Input (Two Dimensional matrix) Printed Output
0   0   0   0   1   1
0   1   0   0   0   1
0   1   1   1   0   0
1   0   0   1   0   0
1   0   1   1   1   0
1   1   0   0   1   0
  .    .    .    .    .    .
  .    1    .    .    .    .
  .    2    3    4    .    .
  .    .    .    5    .    .
  .    .    .    6    7    .
  .    .    .    .    8    .
1   1   0   0   0   1
1   1   0   0   0   1
1   1   0   0   0   1
1   1   0   0   0   1
1   1   0   0   0   1
1   1   0   0   0   1
  1    2    .    .    .    .
  4    3    .    .    .    .
  5    6    .    .    .    .
  8    7    .    .    .    .
  9   10    .    .    .    .
 12   11    .    .    .    .
1  1
1  1
1  1
1  1
1  1
1  1
  1    2
  4    3
  5    6
  8    7
  9   10
 12   11
0   0   0   1
0   1   0   1
0   0   1   0
  .    .    .    1
  .    .    .    2
  .    .    .    .
0   0   0
0   1   0
0   0   1
1   0   0
1   0   1
1   1   0
1   1   1
  .    .    .
  .    .    .
  .    .    .
  1    .    .
  2    .    .
  3    4    .
  .    5    6

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

No solutions recieved for this problem.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

© meritcampus 2019

All Rights Reserved.