Java Program to create a class CricketPlayer and method sortPlayer - IProgramX

Q. Define a class CricketPlayer (name, no_of_innings, no_times_notout, total_runs, bat_avg). Create an array of n player objects. Calculate the batting average for each player using a static method avg(). Handle appropriate exception while calculating average. Define a static method “sortPlayer” which sorts the array on the basis of average. Display the player details in sorted order.

Progarm:

import java.io.*;
class Cricket {
String name;
int inning, tofnotout, totalruns;
float batavg;
public Cricket(){
name=null;
inning=0;
tofnotout=0;
totalruns=0;
batavg=0;

}
public void get() throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name, no of innings, no of times not out, total runs: ");
name=br.readLine();
inning=Integer.parseInt(br.readLine());
tofnotout=Integer.parseInt(br.readLine());
totalruns=Integer.parseInt(br.readLine());
}
public void put(){
System.out.println("Name="+name);
System.out.println("no of innings="+inning);
System.out.println("no times notout="+tofnotout);
System.out.println("total runs="+totalruns);
System.out.println("bat avg="+batavg);

}
static void avg(int n, Cricket c[]){
try{
for(int i=0;i<n;i++){
c[i].batavg=c[i].totalruns/c[i].inning;
}
}catch(ArithmeticException e){
System.out.println("Invalid arg");
}
}
static void sort(int n, Cricket c[]){
String temp1;
int temp2,temp3,temp4;
float temp5;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(c[i].batavg<c[j].batavg){
temp1=c[i].name;
c[i].name=c[j].name;
c[j].name=temp1;

temp2=c[i].inning;
c[i].inning=c[j].inning;
c[j].inning=temp2;

temp3=c[i].tofnotout;
c[i].tofnotout=c[j].tofnotout;
c[j].tofnotout=temp3;

temp4=c[i].totalruns;
c[i].totalruns=c[j].totalruns;
c[j].totalruns=temp4;

temp5=c[i].batavg;
c[i].batavg=c[j].batavg;
c[j].batavg=temp5;
}
}
}
}
}

public class a4sa1 {
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the limit:");
int n=Integer.parseInt(br.readLine());
Cricket c[]=new Cricket[n];
for(int i=0;i<n;i++){
c[i]=new Cricket();
c[i].get();
}
Cricket.avg(n,c);
Cricket.sort(n, c);
for(int i=0;i<n;i++){
c[i].put();
}

}

}

Output:

Enter the limit:
2

Enter the name, no of innings, no of times not out, total runs: 
suresh
1
1
100

Enter the name, no of innings, no of times not out, total runs: 
rusher
2
2
150

Name=suresh
no of innings=1
no times notout=1
total runs=100
bat avg=100.0

Name=rusher
no of innings=2
no times notout=2
total runs=150

bat avg=75.0

Post a Comment

4 Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. }
    Slip3_1: Write a program to accept ‘n’ name of cities from the user and sort them in
    ascending order.

    import java.util.Scanner;
    class SortStr
    {
    public static void main(String args[])
    {
    String temp;
    Scanner SC = new Scanner(System.in);
    System.out.print("Enter the value of N: ");
    int N= SC.nextInt();
    SC.nextLine(); //ignore next line character
    String names[] = new String[N];
    System.out.println("Enter names: ");
    for(int i=0; i0)
    {
    temp=names[j-1];
    names[j-1]=names[j];
    names[j]=temp;
    }
    }
    }
    System.out.println("\nSorted names are in Ascending Order: ");
    for(int i=0;i<N;i++)
    {
    System.out.println(names[i]);
    }
    }
    }

    ReplyDelete
  3. Slip4_1: Write a program to print an array after changing the rows and columns of a given twodimensional
    array.

    import java.util.*;
    class ArrTrans
    {
    public static void main(String args[])
    {
    System.out.println("enter the row and column");
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();
    int mat[][] = new int[r][c];
    System.out.println("enter the array elts:");
    for(int i=0;i<r;i++)
    {
    for(int j=0;j<c;j++)
    {
    mat[i][j] = sc.nextInt();
    }
    }
    System.out.println("the matrix is:");
    for(int i=0;i<c;i++)
    {
    for(int j=0;j<r;j++)
    {
    System.out.print(" " +mat[j][i]);
    }
    System.out.println(" ");
    }
    }
    }

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete