Java Program to Calculate Batting Average

In the previous article we have discussed about Java Program to Calculate Remaining Loan Amount to Pay Back

In this program we are going to see how to calculate batting average by using Java programming language.

Java Program to Calculate Batting Average

Before jumping into the program, let’s know how we calculate batting average.

The formula of  batting average is given below

Batting Average= Total runs / (innings – notOut)

Example:

Suppose totalMatches = 10
totalRuns= 600
innings= 20
notOut= 7
Then, Batting average= totalRuns/(innings-notOut)
= 600/(20-7)
= 46.15

Now let’s see different ways to compute batting average.

Method-1: Java Program to Calculate Batting Average By Using Static Input Value

Approach:

  • Declare and initialize four integer variables say totalMatches, totalRuns, innings and notOut.
  • By using the formula compute average and store it in integer variable avg.
  • Print the result.

Program:

class Main
{
    public static void main(String arg[])
    {
        // Declare and initialize four integer variables say totalMatches, , totalRuns, innings and notOut
        int totalMatches=10,totalRuns=500,innings=20,notOut=3;
        //Declare an another double  variable to store the computed value
        double avg;
        avg=totalRuns/(innings-notOut);
        //Print the result
        System.out.println("The batting average= "+avg);
   }
} 
Output:

The batting average= 29.0

Method-2: Java Program to Calculate Batting Average By using user input value

Approach:

  • Declare and initialize four integer variables say totalMatches, totalRuns, innings and notOut.
  • Prompt the user to enter the values to the corresponding variables.
  • By using the formula compute average and store it in integer variable avg.
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    public static void main(String arg[])
    {
        // Declare and initialize four integer variables say totalMatches, , totalRuns, innings and notOut
        int totalMatches,totalRuns,innings,notOut;
        //create the scanner class object
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number total matches played= ");
      	totalMatches=sc.nextInt();
        //initiate a infinite loop
        while(true)
        {
            System.out.print("Enter the number total innings played= ");
            innings=sc.nextInt();
    		if(innings<=totalMatches)
            {
                break;
            }
        }
        System.out.print("Enter total run scored= ");
        totalRuns=sc.nextInt();
        System.out.print("Enter the number of notout= ");
        notOut=sc.nextInt();
        double avg;
        //Calculate the result
        avg=totalRuns/(innings-notOut);
        //print the result
        System.out.println("The batting average= "+avg);
    }
}
Output:

Enter the number total matches played= 20
Enter the number total innings played= 20
Enter total run scored= 600
Enter the number of notout= 6
The batting average= 42.0

Method-3: Java Program to Calculate Batting Average By Using User Defined Method

Approach:

  • Declare and initialize four integer variables say totalMatches, totalRuns, innings and notOut.
  • Prompt the user to enter the values to the corresponding variables.
  • Then call a user defined method say computeValue() by passing totalMatches, totalRuns, innings and notOut as parameter.
  • By using the formula compute average and store it in integer variable avg.
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        // Declare and initialize four integer variables say totalMatches, , totalRuns, innings and notOut
        int totalMatches,totalRuns,innings,notOut;
        //create the scanner class object
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number total matches played= ");
        totalMatches=sc.nextInt();
        //initiate a infinite loop
        while(true)
        {
            System.out.print("Enter the number total innings played= ");
            innings=sc.nextInt();
            if(innings<=totalMatches)
            {
                break;
            }
        }
        System.out.print("Enter total run scored= ");
        totalRuns=sc.nextInt();
        System.out.print("Enter the number of notout= ");
        notOut=sc.nextInt();
        //calling the user defined method 
        computeValue(totalRuns,innings,notOut);
    }
    
    //define the method
    public static void computeValue(int a, int b, int c)
    {
        //declare another double variable and assigned the formulated value to it.
        double avg;
        //Calculate the result
        //avg=totalRuns/(innings-notOut);
        avg=a/(b-c);
        //print the result
        System.out.println("The batting average= "+avg);
    }
}
Output:

Enter the number total matches played= 20
Enter the number total innings played= 20
Enter total run scored= 600
Enter the number of notout= 6
The batting average= 42.0

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: