Codeathon08_Haritha
[Question]
ROBOTIC CRICKET MATCH
you should write a program that simulate an automatic cricket match between India and Sri Lanka. The focus is the usage of Java Thread Constructs. Each Innings happens within its thread. You decide the 11 members of each team and provide two lists to the program. The first team is represented as team 1 and second team as team 2. You have to generate a random number for toss and if the number is between 0 and 1, team 1 bats first- else, if the number is between 1 and 2, then team 2 bats first. The batting happens by starting a thread. Each ball bowled is via a random number generator (that generates number rounded off, between 0 and 7). If the number is 0, batsman is out. If the number is 1, run is 1, number is 2, run is 2, number is 3, run is 3, number is 4, run is 4, number is 5, run is 5, number is 6, run is 6. If the number is exactly 7, then it is an extra 1 run and if it is 0, batsman is out). You have to do this until 10 batsmen get out in 1 team. The final score sheet will show as follows. Then you have to start another thread and do the same as above. The match ends either when the second team batsmen are out, with scores lesser than team 1 (team 1 wins), scores are tied (match is tied) and as soon score is greater than team 1 (team 2 wins). Each over is for 6 balls, if extra happens (number 7), 1 run is awarded and 1 extra ball is bowled. Extra run and Extra Ball is not part of batsman’s account. Total Overs are 10. (T101 Match)
(No Need to Take Care of Changing Batsman Strike If Score is 1, 3 or 5. No Need to Record Bowling Figures of the Bowler in the Solution. No Need to Take Care Separately of Wide, No Balls, Byes, Leg Byes. There are No Free-Hits!).
[Sample Input]
(No Input is Required)
[Sample Output]
Toss Won By SL (Team 0)
Team 0 (SL) is Batting First
India-Batting Scoresheet
Rohit Sharma 1,4,3,6,1,0=15 (6)
Shubman Gill 0=0 (1)
Virat Kohli 4,6,1,0=11 (4)
KL Rahul 3,1,4,0=8 (4)
Ishan Kishan 6,6,6,4,0 = 22 (5)
Hardik Pandya 0 = 0 (1)
Ravindra Jadeja 6,0 = 6 (2)
Washington Sundar 1,3,0 = 4 (3)
Kuldeep Yadav 1,2,3,4,0=10 (5)
Mohammed Siraj 0 = 0 (1)
Jasprit Bumrah Did Not Bat
Extras 1, 1, 1, 1, 1, 5
Total Score 81 in 5.2 overs
Sri Lanka — Batting Scoresheet
Pathum Nissanka 0=0(1)
Kusal Perera 0=0 (1)
Kusal Mendis 1,0=1(2)
Sadeera Samarawickrama 1,1,0=2 (3)
Charith Asalanka 2,0=2(2)
Dhananjaya de Silva 4,4,0 = 8 (3)
Dasun Shanaka © 1,4,6,0=11 (4)
Dunith Wellalage 6,6,0=12 (3)
Dushan Hemantha 0=0 (1)
Pramod Madushan 1,0=1(2)
Matheesha Pathirana Did Not Bat
Extras 1, 1=2
Total Score 39 in 3.4 overs
Match Result: Team 0 (India) Won By 42 Runs
Today’s Date: 17/09/2023
Areas : Core Java, Logic, Longer Problem Solving, Understanding Requirements, Java Multi- Threading, Java Async Execution, Java Collections.
Program:
import java.text.SimpleDateFormat;import java.util.Date;import java.util.Random;class Player { private String name; private int[] runsScored; private int ballsFaced; private int extras; public Player(String name) { this.name = name; this.runsScored = new int[60]; this.ballsFaced = 0; this.extras = 0; } public String getName() { return name; } public int getBallsFaced() { return ballsFaced; } public int getExtras() { return extras; } public void playInnings() { Random random = new Random(); for (int balls = 0; balls < 60; balls++) { int ballResult = random.nextInt(8); if (ballResult == 7) { extras++; } else if (ballResult == 0) { break; } else { runsScored[ballsFaced] = ballResult; ballsFaced++; } } } public int getTotalRunsScored() { int totalRuns = 0; for (int i = 0; i < ballsFaced; i++) { totalRuns += runsScored[i]; } return totalRuns; } public int[] getRunsScored() { return runsScored; }}class Team implements Runnable { private String name; private Player[] players; private int totalScore; private static int batting = 1; public Team(String name, String[] playerNames) { this.name = name; this.totalScore = 0; this.players = new Player[11]; for (int i = 0; i < 11; i++) { this.players[i] = new Player(playerNames[i]); } } public int getTotalScore() { return totalScore; } public void run() { System.out.println("Team " + name + " is Batting " + batting++); for (Player player : players) { player.playInnings(); totalScore += player.getTotalRunsScored(); } } public void displayBattingScorecard() { System.out.println(name + "-Batting Scoresheet"); for (Player player : players) { System.out.print(player.getName() + " "); int[] runs = player.getRunsScored(); for (int i = 0; i < player.getBallsFaced(); i++) { System.out.print(runs[i]); if (i < player.getBallsFaced() - 1) { System.out.print(","); } } System.out.println("=" + player.getTotalRunsScored() + " (" + player.getBallsFaced() + ")"); } System.out.print("Extras " ); for(int i=1; i<= calculateTeamExtras() ; i++) { System.out.print("1 ,"); } System.out.println(" = " + calculateTeamExtras()); } private int calculateTeamExtras() { int teamExtras = 0; for (Player player : players) { teamExtras += player.getExtras(); } return teamExtras; }}public class Codeathon08_Kishore { public static void main(String[] args) { String[] team1Players = { "Rohit Sharma", "Shubman Gill", "Virat Kohli", "KL Rahul", "Ishan Kishan", "Hardik Pandya", "Ravindra Jadeja", "Washington Sundar", "Kuldeep Yadav", "Mohammed Siraj", "Jasprit Bumrah" }; String[] team2Players = { "Pathum Nissanka", "Kusal Perera", "Kusal Mendis", "Sadeera Samarawickrama", "Charith Asalanka", "Dhananjaya de Silva", "Dasun Shanaka", "Dunith Wellalage", "Dushan Hemantha", "Pramod Madushan", "Matheesha Pathirana" }; Team team1, team2; Random random = new Random(); double tossResult = random.nextDouble() * 2; String tossWinner = (tossResult < 1.0) ? "SL" : "India"; System.out.println("Toss Won By " + tossWinner); if (tossResult < 1.0) { team1 = new Team("Sri Lanka", team2Players); team2 = new Team("India", team1Players); } else { team1 = new Team("India", team1Players); team2 = new Team("Sri Lanka", team2Players); } Thread innings1 = new Thread(team1); Thread innings2 = new Thread(team2); innings1.start(); innings2.start(); try { innings1.join(); innings2.join(); } catch (InterruptedException e) { e.printStackTrace(); } if (tossResult < 1.0) { team1.displayBattingScorecard(); System.out.println("Total Score " + team1.getTotalScore() + " in 10 overs"); System.out.println(); team2.displayBattingScorecard(); System.out.println("Total Score " + team2.getTotalScore() + " in 10 overs"); } else { team2.displayBattingScorecard(); System.out.println("Total Score " + team2.getTotalScore() + " in 10 overs"); System.out.println(); team1.displayBattingScorecard(); System.out.println("Total Score " + team1.getTotalScore() + " in 10 overs"); } SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); String matchResult = determineMatchResult(team1, team2); System.out.println("\nMatch Result: " + matchResult); System.out.println("Today's Date: " + dateFormat.format(new Date())); } private static String determineMatchResult(Team team1, Team team2) { int team1Score = team1.getTotalScore(); int team2Score = team2.getTotalScore(); if (team1Score > team2Score) { return "Team India Won By " + (team1Score - team2Score) + " Runs"; } else if (team1Score < team2Score) { return "Team Sri Lanka Won By " + (team2Score - team1Score) + " Runs"; } else { return "Match Tied"; } }}
[Explanation of Solution] Player Class: This class represents a cricket player and contains the following attributes: name: The player's name. runsScored: An array to store the runs scored by the player in each ball (up to 60 balls). ballsFaced: The number of balls faced by the player. extras: The count of extra runs scored by the player. The constructor initializes the player with a given name and initializes the other attributes. The playInnings method simulates a player's innings: It uses a random number generator to generate a random value between 0 and 7 for each ball. If the generated value is 7, it counts as an extra run.
If the generated value is 0, the player's innings ends.
Otherwise, the run is added to the runsScored array, and ballsFaced is incremented.
The getTotalRunsScored method calculates the total runs scored by the player in their innings.
Team Class:
This class represents a cricket team and implements the Runnable interface for multithreading.
It has the following attributes:
name: The name of the team.
players: An array of Player objects representing the team's players.
totalScore: The total runs scored by the team.
The constructor initializes the team with a name and an array of player names. It creates Player objects for each player.
The run method simulates the team's batting innings:
It iterates through the team's players and calls the playInnings method for each player to simulate their innings.
The total runs scored by the team are calculated during this process.
The displayBattingScorecard method prints the batting scorecard for the team, including player names, runs scored, and extras.
The calculateTeamExtras method calculates the total extras (e.g., wides, no-balls) scored by the team.
Main Class (Codeathon08_Haritha):
This class contains the main method and is responsible for orchestrating the cricket match.
It initializes two teams, "India" and "Sri Lanka," with their respective players.
It randomly selects the toss winner and sets up the batting order accordingly.
Two threads, innings1 and innings2, are created to simulate the batting innings of both teams simultaneously.
The program waits for both innings to finish using join().
After both innings are complete, it displays the batting scorecards for both teams and determines the match result based on the total runs scored.
The match result can be "Team India Won By X Runs," "Team Sri Lanka Won By X Runs," or "Match Tied."
Finally, the program prints the match result and the current date using a SimpleDateFormat.
Overall, the code simulates a cricket match with random ball-by-ball outcomes and provides a result based on the total runs scored by each team.
Haritha .P(Intern),Guard Ninjas,Data Shield Team,Enterprise Minds.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home