Write a program that compute Bowling average for a bowler who has given 1153 runs in 18 matches taking 109 wickets.
Answers
Using C++ programming language to solve -
#include<iostream>
using namespace std;
int main()
{
// initializing the variables
int runs = 1153;
int matches = 18;
int wickets = 109;
int average;
average = runs / wickets ; // bowling average of the bowler is equal to the runs conceded by the bowler per wicket taken.
cout << "The bowling average of the bowler = "<<average; // average of the bowler will be printed .
}
Programming this in Java.
- Program 1
// This is the Simplest of the three I've given but can be used only for this question and cannot be used for other values or objects.
public class BowlingAverage{
public static void main(String[] args){
// for better precision, use double.
double runs = 1153, wickets = 109;
double ba = runs/wickets;
System.out.println("Bowling Average is "+ba);
}
}
- Program 2
// This is a Static method. Multi-use. But can only be used once for each calculation.
public class BowlingAverage{
public static double calculate(int runs, int wickets){
double r = runs, w = wickets; // to increase precision.
double bowling_average = r/w;
return bowling_average;
}
// accessor main( ) method below
public static void main(String[] args){
int runs = 1153, wickets = 109;
int ba = calculate(runs, wickets);
System.out.println("Bowling Average is "+ba);
}
}
- Program 3
// This method needs to create an Object and can be Multi-used, like one Object for each Bowler. Best, but Complicated.
public class BowlingAverage{
int runs, wickets;
BowlingAverage(int runs, int wickets){
this.runs = runs; this.wickets = wickets;
}
double calculate( ){
double r = runs, w = wickets; // to increase precision.
double bowling_average = r/w;
return bowling_average;
}
// accessor main( ) method below
public static void main(String[] args){
int Runs = 1153, Wickets = 109;
BowlingAverage ba = new BowlingAverage(Runs, Wickets);
double bowling_average = ba.calculate( );
System.out.println("Calculated Bowling Average is "+bowling_average);
}
}
________________________________