Write a java program for a cricket player The program should accept the details from user (max 10): Player code, name, runs, innings- played and number of times not out.
Write a java program for a cricket player The program should accept the details from
user (max 10): Player code, name, runs, innings- played and number of times not out. The
program should contain following menu:
-Enter details of players.
-Display average runs of a single player.
-Display average runs of all players. (Use array of objects & function overloading)
OUTPUT :
user (max 10): Player code, name, runs, innings- played and number of times not out. The
program should contain following menu:
-Enter details of players.
-Display average runs of a single player.
-Display average runs of all players. (Use array of objects & function overloading)
import java.io.*;
class Player
{
String Name;
int TotalRuns, TimesNotOut, InningsPlayed,pcode;
float Avg;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
void getData()
{
try
{
System.out.println("Enter Player Code: ");
pcode=Integer.parseInt(br.readLine());
System.out.println("Enter Player Name: ");
Name = br.readLine();
System.out.println("Enter Total Runs: ");
TotalRuns = Integer.parseInt(br.readLine());
System.out.println("Enter Times Not Out: ");
TimesNotOut = Integer.parseInt(br.readLine());
System.out.println("Enter Innings Played: ");
InningsPlayed = Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
}
void putData()
{
System.out.println(pcode + "\t"+Name+"\t"+TotalRuns+"\t"+TimesNotOut+"\t"+InningsPlayed+"\t"+Avg);
}
void getAvg()
{
Avg= TotalRuns / (InningsPlayed - TimesNotOut);
}
static void getAvg(Player p[],int n)
{
for (int i=0;i<n;i++)
{
p[i].Avg=p[i].TotalRuns / (p[i].InningsPlayed - p[i].TimesNotOut);
}
for (int i=0;i<n;i++)
{
p[i].putData();
}
}
}
public class cricket
{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])
{
try
{
System.out.println("Enter No.of Players: ");
int n = Integer.parseInt(br.readLine());
Player p[] = new Player[n];
for(int i=0; i<n; i++)
{
p[i] = new Player();
p[i].getData();
}
System.out.println("Player Code For Avg Calculation");
int m=Integer.parseInt(br.readLine());
for(int i=0; i<n; i++)
{
if(p[i].pcode==m)
{
p[i].getAvg();
p[i].putData();
}
}
System.out.println("Average Of All The Players");
Player.getAvg(p,n);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT :
Enter No.of Players:
1
Enter Player Code:
1
Enter Player Name:
s
Enter Total Runs:
1000
Enter Times Not Out:
25
Enter Innings Played:
1000
Player Code For Avg Calculation
1
1 s 1000 25 1000 1.0
Average Of All The Players
1 s 1000 25 1000 1.0
1
Enter Player Code:
1
Enter Player Name:
s
Enter Total Runs:
1000
Enter Times Not Out:
25
Enter Innings Played:
1000
Player Code For Avg Calculation
1
1 s 1000 25 1000 1.0
Average Of All The Players
1 s 1000 25 1000 1.0