Menu
Question Index
...


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)]

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<Partnership> getPartnerships(List<BallActivity> ballActivities, List<String> batsmen) {
//Write code here to count the number of runs and individual contributions for various partnerships and return them.
}
}
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);
}
}

Doubts

Problems

Topic: Collection Framework In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App