Write a JAVA program of a player class || JAVA program on cricket
Problem Statements:
1. Define a class player having a player name, player_id, team, player type, Run rate,no-of-centuries,no-of-wickets, and method print_info().
2. Define two constructors for creating objects like Batsman, Bowler having fields like innings_played, run rate, wickets taken, score, etc.
3. Create methods update_stats(), Comp_stats(Player ob1,Player ob2)-to compare 2 player & return higher one(performance is determined on the basis of score made (in-case of batsman), wickets taken (in-case of bowler)).
4. Add method overloading for update_stats() method and print_info() method. Use objects as parameters to the methods.
5. Use static for generating player-id, final for the team name, and this for constructor chaining.
6.Use array of objects to store players, and print the top scorer batsman and highest wicket-taker among all.
CODE :
import java.util.Scanner;
public class Player
{
String player_name;
int player_id;
String player_type;
double strike_rate;
double economy_rate;
int player_score;
int no_centuries;
int no_wickets;
static int count=1;
static int max_score=0;
static int max_wickets=0;
static String max_batsman;
static String max_bowler;
Player(){
player_id=count++;
}
Player(String name,String type,double rate,int centuries,int score){
this.player_name=name;
this.player_type=type;
this.player_score=score;
this.strike_rate=rate;
this.no_centuries=centuries;
}
Player(String name,String type,int wickets, double ec_rate){
this.player_name=name;
this.player_type=type;
this.no_wickets=wickets;
this.economy_rate=ec_rate;
}
static void comp_stats(Player obj1,Player obj2){
if(!((obj1.player_type).equalsIgnoreCase(obj2.player_type))){
System.out.println("Different type of players cannot be compared");
}
else if((obj1.player_type).equalsIgnoreCase("Batsman") &&
(obj2.player_type).equalsIgnoreCase("Batsman")){
if((obj1.player_score)>obj2.player_score){
System.out.println(obj1.player_name+" has better performance than "+obj2.player_name);
}
else if(obj1.strike_rate==obj2.strike_rate){
System.out.println(obj1.player_name+" has same performance as that of "+obj2.player_name);
}
else{
System.out.println(obj2.player_name+" has better performance than "+obj1.player_name);
}
}
else{
if((obj1.no_wickets)>obj2.no_wickets){
System.out.println(obj1.player_name+" has better performance than "+obj2.player_name);
}
else if(obj1.no_wickets==obj2.no_wickets){
System.out.println(obj1.player_name+" has same performance as that of "+obj2.player_name);
}
else{
System.out.println(obj2.player_name+" has better performance than "+obj1.player_name);
}
}
System.out.println("------------------------------------------------");
}
static void update_stats(Player obj,int score,double rate, int centuries){
obj.player_score=score;
obj.strike_rate=rate;
obj.no_centuries=centuries;
System.out.println("Player info updated");
System.out.println("------------------------------------------------");
}
static void update_stats(Player obj,int wickets,double ec_rate){
obj.no_wickets=wickets;
obj.economy_rate=ec_rate;
System.out.println("Player info updated");
System.out.println("------------------------------------------------");
}
void print_info(int i, int ent){
System.out.println("Player name: "+player_name);
System.out.println("Player id: "+player_id);
System.out.println("Player type: "+player_type);
if((player_type).equalsIgnoreCase("Bowler")){
System.out.println("No of wickets taken: "+no_wickets);
if(no_wickets>=max_wickets){
max_wickets=no_wickets;
max_bowler=player_name;
}
}
else{
System.out.println("Runrate = "+strike_rate);
System.out.println("No. of centuries hit = "+no_centuries);
System.out.println("Score = "+player_score);
if(player_score>=max_score){
max_score=player_score;
max_batsman=player_name;
}
}
System.out.println("------------------------------------------------");
if(i==(ent-1)){
System.out.println("Top Scoring Batsman "+max_batsman+" with a score of "+max_score);
System.out.println("Top Scoring Bowler "+max_bowler+" with number of wickets "+max_wickets);
System.out.println("------------------------------------------------");
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of entries: ");
int ent=in.nextInt();
System.out.println("------------------------------------------------");
Player arr[]=new Player[ent];
System.out.print("Enter team name: ");
final String team_name = in.next();
System.out.println("------------------------------------------------");
for(int i=0;i<ent;i++){
System.out.print("Enter player"+(i+1)+" name: ");
String name = in.next();
System.out.print("Enter player type: ");
String type = in.next();
if(type.equalsIgnoreCase("Bowler")){
System.out.print("Enter number of wickets: ");
int wickets = in.nextInt();
System.out.print("Enter Economy Rate: ");
double ec_rate=in.nextDouble();
arr[i]=new Player(name,type,wickets,ec_rate);
}
else{
System.out.print("Enter run rate: ");
double rate = in.nextDouble();
System.out.print("Enter number of centuries: ");
int centuries = in.nextInt();
System.out.print("Enter score: ");
int score=in.nextInt();
arr[i]=new Player(name,type,rate,centuries,score);
}
System.out.println("------------------------------------------------");
}
System.out.println("Information Stored!");
System.out.println("------------------------------------------------");
int n=1;
while(n!=0){
System.out.println("Press 1 to update any player, 2 to compare any two player,3 to print all player data, 4 to exit");
n=in.nextInt();
switch(n){
case 1:
System.out.print("Enter player name whose info you want to update: ");
String upname=in.next();
int i;
for(i=0;i<ent;i++){
if((arr[i].player_name).equalsIgnoreCase(upname)){
if((arr[i].player_type).equalsIgnoreCase("Batsman")){
System.out.print("Enter new score: ");
int newscore=in.nextInt();
System.out.print("Enter new runrate: ");
double newrate=in.nextDouble();
System.out.print("Enter new number of centuries: ");
int newcenturies=in.nextInt();
update_stats(arr[i],newscore,newrate,newcenturies);
break;
}
else{
System.out.print("Enter new number of wickets: ");
int newwickets=in.nextInt();
System.out.print("Enter new Economy rate: ");
double new_ec_rate=in.nextDouble();
update_stats(arr[i],newwickets,new_ec_rate);
break;
}
}
}
if(i==ent){
System.out.println("Player record not found");
}
break;
case 2:
System.out.println("Enter two player name whose info you want to compare: ");
String player1=in.next();
String player2=in.next();
int pos1=0;
int pos2=0;
for(i=0;i<ent;i++){
if((arr[i].player_name).equalsIgnoreCase(player1)){
pos1=i;
}
else if((arr[i].player_name).equalsIgnoreCase(player2)){
pos2=i;
}
}
comp_stats(arr[pos1],arr[pos2]);
break;
case 3:
System.out.println("TEAM NAME:"+team_name);
for(i=0;i<ent;i++){
arr[i].print_info(i,ent);
}
break;
case 4:
System.out.println("\nExit!");
n=0;
break;
default:
System.out.println("Wrong Choice!");
System.out.println("------------------------------------------------");
}
}
}
}
OUTPUT :