Menu
Topics Index
...
`

Weekend Hack - 09 Feb 2013 - Results

Thanks to Deepika Koduri, Srihari Adelli, Srinivas Rao Bolla and Tejaswini Rao Potlapally 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 09 Feb 2013 - 1 is Deepika Koduri. She will get a recharge or gift voucher worth Rs. 150.
The winner of the Weekend Hack 09 Feb 2013 - 2 is Tejaswini Rao Potlapally. She will get a recharge or gift voucher worth Rs. 150.

Find the largest acute triangle which can be formed using the points from the given list

Write a program to find the largest acute triangle which can be formed using the points from the given list. The points p1, p2, p3 in the returned triangle, should be in the same order as the input list. Also implement the equals method in the Triangle class such that the triangles are considered equal when points in both the triangles are same irrespective of their order.

Input (List) Output (Triangle)
{[71,1], [0,70], [12,5], [72,9], [0,55], [13,2], [0,0], [11,2]} {[0,70] - [72,9] - [0,0]}
{[-225, 100], [-175, 0], [0, 50], [300, 0], [30, 45], [20, 20], [-50, 25], [-75, 125]} {[-175,0] - [20,20] - [-75,125]}
{[0, 0], [10, 0], [5, 8], [50, 0], [25, 43], [2, 3], [4, 4]} {[0,0] - [50,0] - [25,43]}
{[2, 3], [4, 3], [6, 6], [3, 18], [2, 14], [5, 0], [-1, -3], [-8, 4], [-4, 2]} {[3,18] - [5,0] - [-8,4]}
{[0, -63], [63, -63], [99, -18], [-9, 63], [-27, 45], [-72, 27], [63, -117], [-63, -117]} {[99,-18] - [-72,27] - [-63,-117]}
{[0, 10], [25, 18], [32, 45], [28, 66], [29, 33], [11, 18]} null

Solution 1 - Submitted on Sat Feb 9 15:48:31 2013

import java.util.ArrayList;
import java.util.List;

class FindLargestAcuteTriangle {

    public static void main(String s[]) {
        List points = new ArrayList();
        points.add(new Point(71, 1));
        points.add(new Point(0, 70));
        points.add(new Point(12, 5));
        points.add(new Point(72, 9));
        points.add(new Point(0, 55));
        points.add(new Point(13, 2));
        points.add(new Point(0, 0));
        points.add(new Point(11, 2));
        System.out.println("Largest Acute Triangle is : " + getLargestAcuteTriangle(points));
    }

    public static Triangle getLargestAcuteTriangle(List points) {
        int size = points.size();
        int index = 0;
        int count = 0;
        double[] area = new double[100];
        List triangles = new ArrayList();
        for (int i = 0; i < size - 2; i++) {
            int j = i + 1;
            while (j < size - 1) {
                for (int k = j + 1; k < size; k++) {
                    Point p1 = points.get(i);
                    Point p2 = points.get(j);
                    Point p3 = points.get(k);
                    double s1 = distance(p1, p2);
                    double s2 = distance(p2, p3);
                    double s3 = distance(p3, p1);
                    double angle1 = calculateAngle(s1, s2, s3);
                    double angle2 = calculateAngle(s2, s3, s1);
                    double angle3 = calculateAngle(s1, s3, s2);
                    if (angle1 < 90 && angle2 < 90 && angle3 < 90) {
                        area[index] = calculateArea(points, i, j, k);
                        index++;
                        triangles.add(new Triangle(points.get(i), points.get(j), points.get(k)));
                    }
                }
                j++;
            }
        }
        double max = area[0];
        int max1 = 0;
        for (int i = 1; i < index; i++) {
            if (max < area[i]) {
                max = area[i];
                max1 = i;
            }
        }
        if (index == 0)
            return null;
        else
            return (Triangle) triangles.get(max1);
    }

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

    public static double calculateArea(List points, int i, int j, int k) {
        Point p1 = points.get(i);
        Point p2 = points.get(j);
        Point p3 = points.get(k);
        double area = Math.abs((p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y)) / 2);
        return area;
    }

    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;
    }
}

class Point {

    int x;
    int y;

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

    @Override
    public String toString() {
        return "[" + x + "," + y + "]";
    }
}

class Triangle {

    Point p1;
    Point p2;
    Point p3;

    public Triangle(Point p1, Point p2, Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    @Override
    public String toString() {
        return "{" + p1 + " - " + p2 + " - " + p3 + "}";
    }

    public boolean equals(Triangle t1) {
        int count = 0;
        List array = new ArrayList();
        array.add(new Point(p1.x, p1.y));
        array.add(new Point(p2.x, p2.y));
        array.add(new Point(p3.x, p3.y));
        List array1 = new ArrayList();
        array1.add(new Point(t1.p1.x, t1.p1.y));
        array1.add(new Point(t1.p2.x, t1.p2.y));
        array1.add(new Point(t1.p3.x, t1.p3.y));
        for (Point temp : array) {
            for (Point temp1 : array1) {
                if (temp.x == temp1.x && temp.y == temp1.y) {
                    count++;
                    break;
                }
            }
        }
        if (count != 3)
            return false;
        else
            return true;
    }
}

Review Comments

  1. The way functionality is split into multiple methods depending upon their resposibility is good.
  2. Initializing the size of the area array to 100 is not a good idea, since it will fails when input triangles are more or inefficient when they are less.
  3. Instead of creating the area array, we could have used only one variable which stores the maximum area. This way we could have avoided the second for loop of going through the areas and finding the largest area.
  4. In the equals method, the return statement could have been return (count == 3);. This way we dont need the if-else condition.

Solution 2 - Submitted on Sat Feb 9 18:36:28 2013

import java.util.ArrayList;
import java.util.List;

class FindLargestAcuteTriangle {

    public static void main(String s[]) {
        List points = new ArrayList();
        points.add(new Point(71, 1));
        points.add(new Point(0, 70));
        points.add(new Point(12, 5));
        points.add(new Point(72, 9));
        points.add(new Point(0, 55));
        points.add(new Point(13, 2));
        points.add(new Point(0, 0));
        points.add(new Point(11, 2));
        System.out.println("Largest Acute Triangle is : " + getLargestAcuteTriangle(points));
    }

    public static Triangle getLargestAcuteTriangle(List points) {
        Triangle t = null, temp;
        double area = 0;
        for (int i = 0; i < points.size() - 2; i++) {
            for (int j = i + 1; j < points.size() - 1; j++) {
                for (int k = j + 1; k < points.size(); k++) {
                    temp = new Triangle(points.get(i), points.get(j), points.get(k));
                    if (temp.istriandisacute() && temp.area() > area) {
                        area = temp.area();
                        t = temp;
                    }
                }
            }
        }
        return t;
    }
}

class Point {

    int x;
    int y;

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

    @Override
    public String toString() {
        return "[" + x + "," + y + "]";
    }
}

class Triangle {

    Point p1;
    Point p2;
    Point p3;

    public Triangle(Point p1, Point p2, Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    @Override
    public String toString() {
        return "{" + p1 + " - " + p2 + " - " + p3 + "}";
    }

    public boolean istriandisacute() {
        double csq = (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y), asq = (p2.x - p3.x) * (p2.x - p3.x) + (p2.y - p3.y) * (p2.y - p3.y), bsq = (p3.x - p1.x) * (p3.x - p1.x) + (p1.y - p3.y) * (p1.y - p3.y);
        double a1 = (Math.acos((bsq + csq - asq) / (2 * Math.sqrt(bsq * csq))) * 180 / Math.PI), b1 = (Math.acos((asq + csq - bsq) / (2 * Math.sqrt(asq * csq))) * 180 / Math.PI), c1 = (Math.acos((asq + bsq - csq) / (2 * Math.sqrt(asq * bsq))) * 180 / Math.PI);
        return (a1 < 90 && b1 < 90 && c1 < 90);
    }

    public double area() {
        return Math.abs((p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y)) / 2);
    }

    public boolean equals(Triangle t) {
        Point a[] = new Point[3];
        a[0] = t.p1;
        a[1] = t.p2;
        a[2] = t.p3;
        boolean found = false;
        for (int i = 0; i < 3; i++) {
            if (p1.x == a[i].x && p1.y == a[i].y) {
                found = true;
                a[i] = a[2];
            }
        }
        if (!found)
            return false;
        for (int i = 0; i < 2; i++) {
            if (p2.x == a[i].x && p2.y == a[i].y) {
                found = true;
                a[i] = a[1];
            }
        }
        if (!found)
            return false;
        return p3.x == a[0].x && p3.y == a[0].y;
    }
}

Review Comments

  1. The way code is split into multiple methods and using member functions instead of static methods is good.
  2. The logic of finding the largest traingle, using the three for loops and the way the largest triangle is tracked is efficient.
  3. The method istriandisacute is not easy to read.It would have been better, if we created one more method for getting the angle. Similarly equals method could be made simple by using Collections instead of arrays.

Solution 3 - Submitted on Mon Feb 11 00:15:31 2013

import java.util.ArrayList;
import java.util.List;

class FindLargestAcuteTriangle {

    public static void main(String s[]) {
        List points = new ArrayList();
        points.add(new Point(71, 1));
        points.add(new Point(0, 70));
        points.add(new Point(12, 5));
        points.add(new Point(72, 9));
        points.add(new Point(0, 55));
        points.add(new Point(13, 2));
        points.add(new Point(0, 0));
        points.add(new Point(11, 2));
        System.out.println("Largest Acute Triangle is : " + getLargestAcuteTriangle(points));
    }

    public static Triangle getLargestAcuteTriangle(List points) {
        Triangle result = new Triangle(new Point(0, 0), new Point(0, 0), new Point(0, 0));
        for (int index = 0; index + 2 < points.size(); index++)
            result = Triangle.findingTheTriangle(index, index + 1, index + 2, points, result);
        if (Triangle.lengthofside(result.p1, result.p2) == 0 && Triangle.lengthofside(result.p2, result.p3) == 0 && Triangle.lengthofside(result.p3, result.p1) == 0)
            return null;
        else
            return result;
    }
}

class Point {

    int x;
    int y;

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

    @Override
    public String toString() {
        return "[" + x + "," + y + "]";
    }
}

class Triangle {

    Point p1;
    Point p2;
    Point p3;

    public Triangle(Point p1, Point p2, Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    @Override
    public String toString() {
        return "{" + p1 + " - " + p2 + " - " + p3 + "}";
    }

    public static Triangle findingTheTriangle(int index1, int index2, int index3, List points, Triangle result) {
        Triangle temp = null;
        temp = AcuteTriangle(index1, index2, index3, points, result);
        Triangle output = temp;
        if (index3 < points.size() - 1) {
            index3++;
            output = findingTheTriangle(index1, index2, index3, points, temp);
            while (index2 < index3 - 1) {
                index2++;
                output = AcuteTriangle(index1, index2, index3, points, output);
            }
        }
        return output;
    }//End of the findingTheTriangle method.

    public static Triangle AcuteTriangle(int i, int j, int k, List points, Triangle output) {
        Triangle sampletriangle = new Triangle(points.get(i), points.get(j), points.get(k));
        boolean flag = false;
        double Area, maximumArea;
        double len1 = lengthofside(sampletriangle.p1, sampletriangle.p2);
        double len2 = lengthofside(sampletriangle.p2, sampletriangle.p3);
        double len3 = lengthofside(sampletriangle.p3, sampletriangle.p1);
        int count = len1 > len2 ? (len1 > len3 ? 1 : 3) : (len2 > len3 ? 2 : 3);
        /*To find whether the triangle is acute or not*/
        /*i.e., square of the largest side is less than the sum of the squares of the other two.*/
        switch (count) {
            case 1 :
                flag = CheckingAcute(len1, len2, len3);
                break;
            case 2 :
                flag = CheckingAcute(len2, len1, len3);
                break;
            case 3 :
                flag = CheckingAcute(len3, len1, len2);
                break;
        }
        if (flag) {
            Area = Areaoftriangle(len1, len2, len3);
            maximumArea = Areaoftriangle(lengthofside(output.p1, output.p2), lengthofside(output.p2, output.p3), lengthofside(output.p3, output.p1));
            if (maximumArea < Area)
                output = new Triangle(sampletriangle.p1, sampletriangle.p2, sampletriangle.p3);
        }
        return output;
    }//End of the AcuteTriangle method.

    public static boolean CheckingAcute(double side1, double side2, double side3) {
        return Math.pow(side1, 2) < (Math.pow(side2, 2) + Math.pow(side3, 2));
    }//End of the CheckingAcute method.

    public static double lengthofside(Point p1, Point p2) {
        double length = Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)); //finding the length of the sides of the triangle.
        return length;
    }//End of the lengthofside method.

    public static double Areaoftriangle(double side1, double side2, double side3) {
        double s = (side1 + side2 + side3) / 2;
        double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); //heron's Formula to find the area of the triangle.
        return area;
    }//End of the Areaoftriangle method.
     //Implementing the equals method.

    @Override
    public boolean equals(Object obj) {
        Triangle triangle = (Triangle) obj;
        boolean flag = false;
        if (checkPoint(p1, triangle)) {
            if (checkPoint(p2, triangle)) {
                if (checkPoint(p3, triangle))
                    flag = true;
            }
        }
        return flag;
    }//End of the equals method.

    public static boolean checkPoint(Point p, Triangle triangle) {
        boolean flag = false;
        if (p.x == triangle.p1.x && p.y == triangle.p1.y)
            flag = true;
        else {
            if (p.x == triangle.p2.x && p.y == triangle.p2.y)
                flag = true;
            else if (p.x == triangle.p3.x && p.y == triangle.p3.y)
                flag = true;
        }
        return flag;
    }//End of the checkPoint method.
}

Review Comments

  1. The way functionality is split into multiple methods is good, but we could have used member functions instead of static methods.
  2. It is not a good idea to combine the logic of finding the acute triangle and area of triangle into one method.
  3. The logic of equals method is effecient, but we could have created one more method for comparing two points. That way, checkPoint method would have been further simplified.

To get all the partnerships and individual contributions of each batsman in a cricket match

Write a program to get all the partnerships and individual contributions of each batsman in a cricket match. The number of runs scored for each ball and whether the batsman got out is given. Assume that there are no wides or no balls or extras.

Input (List, List) Output (List)
[2:false, 1:false, 0:false, 0:false, 1:false, 4:false, 0:false, 0:false, 1:false, 3:false, 4:false, 1:false, 0:true, 1:false, 0:true, 6:false, 4:false, 6:false]
[Gambhir, Rahane, Kohli, Yuvraj, Raina, Dhoni, Jadeja, Ashwin, Sharma, Kumar, Ahmed]
[17(Gambhir:10, Rahane:7), 1(Gambhir:0, Kohli:1), 16(Kohli:0, Yuvraj:16)]
[0:false, 0:false, 4:false, 4:false, 1:false, 4:false, 0:true, 0:false, 0:true, 6:false, 4:false, 6:false, 0:true, 1:false, 0:true, 6:false, 4:false, 6:false]
[Gambhir, Rahane, Kohli, Yuvraj, Raina, Dhoni, Jadeja, Ashwin, Sharma, Kumar, Ahmed]
[13(Gambhir:9, Rahane:4), 0(Rahane:0, Kohli:0), 16(Rahane:0, Yuvraj:16), 1(Yuvraj:0, Raina:1), 16(Raina:0, Dhoni:16)]
[0:false, 0:false, 0:false, 0:true, 6:false, 6:false, 0:true, 4:false, 4:false, 4:false, 4:false, 0:true]
[Gambhir, Rahane, Kohli, Yuvraj, Raina, Dhoni, Jadeja, Ashwin, Sharma, Kumar, Ahmed]
[0(Gambhir:0, Rahane:0), 12(Rahane:0, Kohli:12), 16(Kohli:0, Yuvraj:16)]

Solution 1 - Submitted on Sun Feb 10 14:39:08 2013

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class GetPartnerships {

    public static void main(String s[]) {
        List ballActivities = new ArrayList();
        ballActivities.add(new BallActivity(0, false));
        ballActivities.add(new BallActivity(0, false));
        ballActivities.add(new BallActivity(0, false));
        ballActivities.add(new BallActivity(0, true));
        ballActivities.add(new BallActivity(2, false));
        ballActivities.add(new BallActivity(1, false));
        ballActivities.add(new BallActivity(0, true));
        ballActivities.add(new BallActivity(3, false));
        ballActivities.add(new BallActivity(4, false));
        ballActivities.add(new BallActivity(2, false));
        ballActivities.add(new BallActivity(4, false));
        ballActivities.add(new BallActivity(0, true));
        List batsmanOrder = new ArrayList();
        batsmanOrder.add("Gambhir");
        batsmanOrder.add("Rahane");
        batsmanOrder.add("Kohli");
        batsmanOrder.add("Yuvraj");
        batsmanOrder.add("Raina");
        batsmanOrder.add("Dhoni");
        batsmanOrder.add("Jadeja");
        batsmanOrder.add("Ashwin");
        batsmanOrder.add("Sharma");
        batsmanOrder.add("Kumar");
        batsmanOrder.add("Ahmed");
        List partnerships = getPartnerships(ballActivities, batsmanOrder);
        System.out.println("The Partnerships are : ");
        System.out.println(partnerships);
    }

    private static List getPartnerships(List ballActivities, List batsmen) {
        List batlist = new ArrayList();
        Iterator iterate = ballActivities.iterator();
        Iterator names = batsmen.iterator();
        String firstbm = (String) names.next();
        String secondbm = (String) names.next();
        int runs_by_bat1 = 0, runs_by_bat2 = 0, balls = 0;
        boolean strike = true; //initially giving strike to first batsman
        BallActivity obj;
        while (iterate.hasNext()) {
            obj = (BallActivity) iterate.next();
            while (!obj.wicket) //iterate till any batsman gets out or balls are over
            {
                if (strike)
                    runs_by_bat1 += obj.runs;
                else
                    runs_by_bat2 += obj.runs;
                balls++;
                if (balls % 6 == 0 ^ obj.runs % 2 != 0) //change strike if batsmen score 1,3 or over up but dont change strike if both occurs
                    strike = !strike;
                if (iterate.hasNext())
                    obj = (BallActivity) iterate.next();
                else
                    break;
            }
            balls++;
            Partnership p = new Partnership(new BatsmanContribution(firstbm, runs_by_bat1), new BatsmanContribution(secondbm, runs_by_bat2));
            batlist.add(p);
            runs_by_bat1 = 0;
            runs_by_bat2 = 0;
            if (strike) { //if first batsman get out then second batsman will become first and strike is given to second batsman
                firstbm = secondbm;
                strike = !strike;
            }
            secondbm = (String) names.next();
        }
        return batlist;
    }
}

class BallActivity {

    int runs;
    boolean wicket;

    public BallActivity(int runs, boolean wicket) {
        this.runs = runs;
        this.wicket = wicket;
    }

    @Override
    public String toString() {
        return runs + ":" + wicket;
    }
}

class BatsmanContribution {

    String name;
    int runs;

    public BatsmanContribution(String name, int runs) {
        this.name = name;
        this.runs = runs;
    }

    @Override
    public boolean equals(Object obj) {
        return name.equals(((BatsmanContribution) obj).name) && runs == ((BatsmanContribution) obj).runs;
    }
}

class Partnership {

    BatsmanContribution first;
    BatsmanContribution second;

    public Partnership(BatsmanContribution first, BatsmanContribution second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public String toString() {
        return (first.runs + second.runs) + "(" + first.name + ":" + first.runs + ", " + second.name + ":" + second.runs + ")";
    }

    @Override
    public boolean equals(Object obj) {
        Partnership other = (Partnership) obj;
        return first.equals(other.first) && second.equals(other.second);
    }
}

Review Comments

  1. The idea of using runs_by_bat1, runs_by_bat2 for tracking the runs is good.
  2. The logic written is easy to read and the comments are helpful.
  3. We could have used the iterators with Generics, so as to avoid the unnecessary class casting to String and BallActivity
  4. The names of the variables are apt and convey the meaning.

Solution 2 - Submitted on Sun Feb 10 17:37:46 2013

import java.util.ArrayList;
import java.util.List;

class GetPartnerships {

    public static void main(String s[]) {
        List ballActivities = new ArrayList();
        ballActivities.add(new BallActivity(0, false));
        ballActivities.add(new BallActivity(0, false));
        ballActivities.add(new BallActivity(0, false));
        ballActivities.add(new BallActivity(0, true));
        ballActivities.add(new BallActivity(2, false));
        ballActivities.add(new BallActivity(1, false));
        ballActivities.add(new BallActivity(0, true));
        ballActivities.add(new BallActivity(3, false));
        ballActivities.add(new BallActivity(4, false));
        ballActivities.add(new BallActivity(2, false));
        ballActivities.add(new BallActivity(4, false));
        ballActivities.add(new BallActivity(0, true));
        List batsmanOrder = new ArrayList();
        batsmanOrder.add("Gambhir");
        batsmanOrder.add("Rahane");
        batsmanOrder.add("Kohli");
        batsmanOrder.add("Yuvraj");
        batsmanOrder.add("Raina");
        batsmanOrder.add("Dhoni");
        batsmanOrder.add("Jadeja");
        batsmanOrder.add("Ashwin");
        batsmanOrder.add("Sharma");
        batsmanOrder.add("Kumar");
        batsmanOrder.add("Ahmed");
        List partnerships = getPartnerships(ballActivities, batsmanOrder);
        System.out.println("The Partnerships are : ");
        System.out.println(partnerships);
    }

    private static List getPartnerships(List ballActivities, List batsmen) {
        List partnerships = new ArrayList();
        BatsmanContribution batsman1 = new BatsmanContribution(batsmen.get(0), 0);
        BatsmanContribution batsman2 = new BatsmanContribution(batsmen.get(1), 0);
        int i = 0, j = 1, count = 0;
        int index = 0;
        for (BallActivity temp : ballActivities) {
            index++;
            if (count == 0 && temp.wicket == false) {
                batsman1.runs += temp.runs;
                if (index % 6 != 0)
                    count = temp.runs % 2 == 0 ? 0 : 1;
                else
                    count = temp.runs % 2 == 0 ? 1 : 0;
            } else {
                if (count == 1 && temp.wicket == false) {
                    batsman2.runs += temp.runs;
                    if (index % 6 != 0)
                        count = temp.runs % 2 == 0 ? 1 : 0;
                    else
                        count = temp.runs % 2 == 0 ? 0 : 1;
                }
            }
            if (temp.wicket == true || index == ballActivities.size()) {
                partnerships.add(new Partnership(batsman1, batsman2));
                batsman1 = null;
                batsman2 = null;
                int k = count == 0 ? j : i;
                batsman1 = new BatsmanContribution(batsmen.get(k), 0);
                i = k;
                batsman2 = new BatsmanContribution(batsmen.get(++j), 0);
                count = 1;
            }
        }
        return partnerships;
    }
}

class BallActivity {

    int runs;
    boolean wicket;

    public BallActivity(int runs, boolean wicket) {
        this.runs = runs;
        this.wicket = wicket;
    }

    @Override
    public String toString() {
        return runs + ":" + wicket;
    }
}

class BatsmanContribution {

    String name;
    int runs;

    public BatsmanContribution(String name, int runs) {
        this.name = name;
        this.runs = runs;
    }

    @Override
    public boolean equals(Object obj) {
        return name.equals(((BatsmanContribution) obj).name) && runs == ((BatsmanContribution) obj).runs;
    }
}

class Partnership {

    BatsmanContribution first;
    BatsmanContribution second;

    public Partnership(BatsmanContribution first, BatsmanContribution second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public String toString() {
        return (first.runs + second.runs) + "(" + first.name + ":" + first.runs + ", " + second.name + ":" + second.runs + ")";
    }

    @Override
    public boolean equals(Object obj) {
        Partnership other = (Partnership) obj;
        return first.equals(other.first) && second.equals(other.second);
    }
}

Review Comments

  1. Although the logic is compact, it is a bit difficult to understand.
  2. We could have used the variables temp.wicket or !temp.wicket instead of comparing with temp.wicket == true or temp.wicket == false;
  3. Could have given better names for i, j, k and count.

© meritcampus 2019

All Rights Reserved.

Open In App