Java Program to calculate the batting average. The following program is written in five different ways by using standard values, user-defined method, scanner class, through creating a separate class. Do check it out.
Methods we used in this Cricket Batting Program:
- Using standard Method
- Using Scanner Class
- Using Command Line Arguments
- User Define Method
- Separate Class
By Using Standard Values
- There you go, by using the basic standard input values we wrote this program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class BattingAverage { public static void main(String arg[]) { int Matches=5,totalruns=200,innings=5,notout=1; double avg; avg=totalruns/(innings-notout); System.out.println("batting average="+avg); } } |
1 |
batting average=50.0 |
2. By Using Scanner Class
- By using scanner class: There you method: 2 just by using scanner class. Do check it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.Scanner; class BattingAverage { public static void main(String arg[]) { long Matches,runs,innings,notout; double avg; Scanner sc=new Scanner(System.in); System.out.println("enter the number of matches played"); Matches=sc.nextLong(); while(true) { System.out.println("enter the number innings batted"); innings=sc.nextLong(); if(innings<=Matches) break; System.out.println("enter the number innings batted correctly <=matches"); } while(true) { System.out.println("enter number of times notout"); notout=sc.nextLong(); if(notout<=innings) break; System.out.println("enter the number times became notout correctly <=innings"); } System.out.println("enter runs scored"); runs=sc.nextLong(); if(innings==notout) avg=runs; else avg=runs/(innings-notout); System.out.println("batting average="+avg); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
1. enter the number of matches played 12 enter the number innings batted 10 enter number of times notout 0 enter runs scored 1000 batting average=100.0 2. enter the number of matches played 50 enter the number innings batted 50 enter number of times notout 9 enter runs scored 4000 batting average=97.0 3. enter the number of matches played 66 enter the number innings batted 55 enter number of times notout 10 enter runs scored 3005 batting average=66.0 |
3. Using Command Line Arguments
By using command line arguments: If you have no idea about what are command line arguments do check out our guide: Here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Scanner; class BattingAverage { public static void main(String args[]) { double Mat,inn,notout,avg,run; Mat=Double.parseDouble(args[0]); inn=Double.parseDouble(args[1]); notout=Double.parseDouble(args[2]); run=Double.parseDouble(args[3]); System.out.println("Matches="+(long)Mat); System.out.println("Innings="+(long)inn); System.out.println("Notout="+(long)notout); System.out.println("Total runs="+(long)run); if(inn<=Mat&¬out<=inn) { avg=run /(inn-notout); System.out.println("batting average="+avg); } else { System.out.println("please enter innings <= matches and notout<=innings" ); } } } |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
javac BattingAverage.java java BattingAverage 65 62 2 5454 Matches=65 Innings=62 Notout=2 Total runs=5454 batting average=90.9 javac BattingAverage.java java BattingAverage 5 4 0 300 Matches=5 Innings=4 Notout=0 Total runs=300 batting average=75.0 javac BattingAverage.java java BattingAverage 171 163 23 7212 Matches=171 Innings=163 Notout=23 Total runs=7212 batting average=51.51428571428571 |
4.By Using User Define Method
There you go another program using a user-defined method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import java.util.Scanner; class BattingAverage { public static void main(String arg[]) { double Matches,runs,innings,notout; double avg; Scanner sc=new Scanner(System.in); System.out.println("enter the number of matches played"); Matches=sc.nextDouble(); while(true) { System.out.println("enter the number innings batted"); innings=sc.nextDouble(); if(innings<=Matches) break; System.out.println("enter the number innings batted correctly <=matches"); } while(true) { System.out.println("enter number of times notout"); notout=sc.nextDouble(); if(notout<=innings) break; System.out.println("enter the number times became notout correctly <=innings"); } System.out.println("enter runs scored"); runs=sc.nextDouble(); avg=avgCalculation(runs,innings,notout); System.out.println("batting average="+avg); } static double avgCalculation(double r,double i,double n) { if(i!=n) { return r/(i-n); } else { return r; } } } |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
output:1 enter the number of matches played 4 enter the number innings batted 4 enter number of times notout 4 enter runs scored 100 batting average=100.0 output:2 enter the number of matches played 300 enter the number innings batted 286 enter number of times notout 32 enter runs scored 13288 batting average=52.31496062992126 output:3 enter the number of matches played 100 enter the number innings batted 90 enter number of times notout 20 enter runs scored 2000 batting average=28.571428571428573 |
5. Separate Class
Java program for calculating batting average through creating a separate class(AvgCalculation ) and taking inputs through scanner class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import java.util.Scanner; class AvgCalculation { double avg=0; AvgCalculation(long r,long i,long n) { if(i==n) { avg=r; } else { avg=r/(i-n); } } } class BattingAverage { public static void main(String arg[]) { long Matches,runs,innings,notout; Scanner sc=new Scanner(System.in); System.out.println("enter the number of matches played"); Matches=sc.nextLong(); while(true) { System.out.println("enter the number innings batted"); innings=sc.nextLong(); if(innings<=Matches) break; System.out.println("enter the number innings batted correctly <=matches"); } while(true) { System.out.println("enter number of times notout"); notout=sc.nextLong(); if(notout<=innings) break; System.out.println("enter the number times became notout correctly <=innings"); } System.out.println("enter runs scored"); runs=sc.nextLong(); AvgCalculation a=new AvgCalculation(runs,innings,notout); System.out.println("batting average="+a.avg); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
output:1 enter the number of matches played 5 enter the number innings batted 5 enter number of times notout 5 enter runs scored 100 batting average=100.0 output:2 enter the number of matches played 50 enter the number innings batted 44 enter number of times notout 6 enter runs scored 1862 batting average=49.0 output:3 enter the number of matches played 100 enter the number innings batted 96 enter number of times notout 4 enter runs scored 4507 batting average=48.0 |