Menu
Question Index
...


Write a program to count the balls faced and runs scored by each batsman and also count the number of extras.

Input (List, List) Output (List)
[2FFT, 1FFF, 1FTF, 1FTF, 0FFF, 0FFF, 1FFF, 4FFF, 1FTF, 0FFF, 1FTF, 0FFF, 1FFF, 3FFT, 1FTF, 4FFF, 1FFF, 0TFF, 1FTF, 1FTF, 1FFF, 0TFF, 2FTF ,2FFT, 4FFF, 6FFF]
[Gambhir, Rahane, Kohli, Yuvraj, Raina, Dhoni, Jadeja, Ashwin, Sharma, Kumar, Ahmed]
[Gambhir Out 5(5), Rahane Out 7(9), Kohli NotOut 1(1), Yuvraj NotOut 10(3), Raina DidNotBat, Dhoni DidNotBat, Jadeja DidNotBat, Ashwin DidNotBat, Sharma DidNotBat, Kumar DidNotBat, Ahmed DidNotBat]
Extras : 16
[0FFF, 0FFF, 4FFF, 4FFF, 1FFF, 4FFF, 0TFF, 0FFF, 0TFF, 6FFF, 4FFT, 6FFF, 0TFF, 1FFF, 0TFF, 6FFF, 4FFT, 6FFF]
[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 12(4), Raina NotOut 1(1), Dhoni NotOut 12(3), Jadeja DidNotBat, Ashwin DidNotBat, Sharma DidNotBat, Kumar DidNotBat, Ahmed DidNotBat]
Extras : 8
[0FFF, 0FFF, 0FFF, 0TFF, 6FFF, 2FTF, 6FFF, 0TFF, 4FFF, 4FFF, 4FFT, 4FFF, 1FTF 0TFF]
[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 12(5), Raina NotOut 0(0), Dhoni DidNotBat, Jadeja DidNotBat, Ashwin DidNotBat, Sharma DidNotBat, Kumar DidNotBat, Ahmed DidNotBat]
Extras : 7

class GetBatsmenRunsAndExtras

{    public static void main(String s[])
    {
        List ballActivities = new ArrayList();
        ballActivities.add(new BallActivity(0, false, false, false));
        ballActivities.add(new BallActivity(0, false, false, false));
        ballActivities.add(new BallActivity(0, false, false, false));
        ballActivities.add(new BallActivity(0, true, false, false));
        ballActivities.add(new BallActivity(1, false, false, false));
        ballActivities.add(new BallActivity(2, false, true, false));
        ballActivities.add(new BallActivity(2, false, false, false));
        ballActivities.add(new BallActivity(0, true, false, false));
        ballActivities.add(new BallActivity(3, false, false, false));
        ballActivities.add(new BallActivity(4, false, false, false));
        ballActivities.add(new BallActivity(4, false, false, true));
        ballActivities.add(new BallActivity(2, false, false, false));
        ballActivities.add(new BallActivity(1, false, true, false));
        ballActivities.add(new BallActivity(0, true, false, false));
        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");
        Extras extras = new Extras(0, 0);
        List batsmanSummary = getBatsmenSummary(ballActivities, batsmanOrder, extras);
        System.out.println("The scores are : ");
        System.out.println(batsmanSummary);
        System.out.println("The extras are : " + extras.getExtras());

    }


private static List<BatsmanSummary> getBatsmenSummary(List<BallActivity> ballActivities, List<String> batsmen, Extras extras) {
//Write code here to count balls faced and runs scored by each batsman and also count the number of extras.
}
}
class BallActivity {

int runs;
boolean wicket;
boolean noBallOrWide;
boolean legBye;

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

@Override
public String toString() {
return runs + (wicket ? "T" : "F") + (noBallOrWide ? "T" : "F") + (legBye ? "T" : "F");
}
}

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

class Extras {

int noBallsAndWides;
int legByes;

public Extras(int noBallsAndWides, int legByes) {
this.noBallsAndWides = noBallsAndWides;
this.legByes = legByes;
}

@Override
public String toString() {
return getExtras() + (" (No Balls And Wides : " + noBallsAndWides + ", Leg Byes : " + legByes + ")");
}

public int getExtras() {
return noBallsAndWides + legByes;
}
}

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