Menu
Question Index
...


Write a program to count the balls faced and runs scored by each batsman.

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]
[Gambhir Out 10(5), Rahane Out 7(9), Kohli NotOut 1(1), Yuvraj NotOut 16(3), Raina DidNotBat, Dhoni DidNotBat, Jadeja DidNotBat, Ashwin DidNotBat, Sharma DidNotBat, Kumar DidNotBat, Ahmed DidNotBat]
[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]
[Gambhir Out 9(6), Rahane Out 4(2), Kohli Out 0(2), Yuvraj Out 16(4), Raina NotOut 1(1), Dhoni NotOut 16(3), Jadeja DidNotBat, Ashwin DidNotBat, Sharma DidNotBat, Kumar DidNotBat, Ahmed DidNotBat]
[0:false, 0:false, 0:false, 0:true, 6:false, 6:false, 0:true, 4:false, 4:true, 4:false, 4:false, 0:true]
[Gambhir, Rahane, Kohli, Yuvraj, Raina, Dhoni, Jadeja, Ashwin, Sharma, Kumar, Ahmed]
[Gambhir Out 0(4), Rahane Out 0(1), Kohli Out 12(2), Yuvraj Out 16(5), Raina DidNotBat, Dhoni DidNotBat, Jadeja DidNotBat, Ashwin DidNotBat, Sharma DidNotBat, Kumar DidNotBat, Ahmed DidNotBat]
NOTE: Since the batsman got out on the last ball, there will not be any new batsman coming into the crease.

class GetBatsmenRuns

{    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 batsmanSummary = getBatsmansSummary(ballActivities, batsmanOrder);
        System.out.println("The scores are : ");
        System.out.println(batsmanSummary);

    }


private static List<BatsmanSummary> getBatsmansSummary(List<BallActivity> ballActivities, List<String> batsmen) {
//Write code here to count balls faced and runs scored by each batsman 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 BatsmanSummary {

String name;
int runs;
int balls;
boolean out;
boolean batted;

public BatsmanSummary(String name, int runs, int balls, boolean out, boolean batted) {
this.name = name;
this.runs = runs;
this.balls = balls;
this.out = out;
this.batted = batted;
}

public BatsmanSummary(String name, boolean batted) {
this.name = name;
runs = 0;
balls = 0;
out = false;
this.batted = batted;
}

@Override
public String toString() {
return name + " " + (!batted ? "DidNotBat" : (out && batted ? "Out" : "NotOut")) + (batted ? " " + runs + "(" + balls + ")" : "");
}

@Override
public boolean equals(Object obj) {
BatsmanSummary otherBatsmanSummary = (BatsmanSummary) obj;
return name.equals(otherBatsmanSummary.name) && runs == otherBatsmanSummary.runs && balls == otherBatsmanSummary.balls && out == otherBatsmanSummary.out && batted == otherBatsmanSummary.batted;
}
}

Doubts

Problems

Topic: Java Collection Framework - Collections 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