Write a java program to accept details of ‘n’ cricket player (pid, pname, totalRuns, InningsPlayed, NotOuttimes). Calculate the average of all the players. Display the details of player having maximum average. (Use Array of Object)
import java.io.*;
import java.util.Scanner;
class CricketPlayer1 {
int pid;
String pname;
int totalRuns;
int inningsPlayed;
int notOutTimes;
double average;
CricketPlayer1(int pid, String pname, int
totalRuns, int inningsPlayed, int notOutTimes) {
this.pid = pid;
this.pname = pname;
this.totalRuns = totalRuns;
this.inningsPlayed = inningsPlayed;
this.notOutTimes = notOutTimes;
this.average = (double) totalRuns /
(inningsPlayed - notOutTimes);
}
}
public class
CricketPlayer {
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the number
of cricket players: ");
int n = scanner.nextInt();
CricketPlayer1[] players = new
CricketPlayer1[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter
details for player " + (i + 1) + ":");
System.out.print("Player ID:
");
int pid = scanner.nextInt();
scanner.nextLine(); // Consume the
newline
System.out.print("Player Name:
");
String pname = scanner.nextLine();
System.out.print("Total Runs:
");
int totalRuns = scanner.nextInt();
System.out.print("Innings Played:
");
int inningsPlayed =
scanner.nextInt();
System.out.print("Not Out
Times: ");
int notOutTimes =
scanner.nextInt();
players[i] = new
CricketPlayer1(pid, pname, totalRuns, inningsPlayed, notOutTimes);
}
double maxAverage = 0;
int maxAverageIndex = 0;
for (int i = 0; i < n; i++) {
if (players[i].average >
maxAverage) {
maxAverage =
players[i].average;
maxAverageIndex = i;
}
}
System.out.println("\nPlayer with
the maximum average:");
System.out.println("Player ID:
" + players[maxAverageIndex].pid);
System.out.println("Player Name:
" + players[maxAverageIndex].pname);
System.out.println("Average:
" + players[maxAverageIndex].average);
scanner.close();
}
}