Answer:
The program to this question as follows:
Program:
class CircleStats //defining class
{
CircleStats() //creating default constructor
{
}
float calcCircleCircumf(float radius) //defining method
{
return (float)(2 * Math.PI * radius); //return value
}
double calcCircleCircumf(double diameter) //overload the method
{
return (2 * Math.PI * (diameter/2)); //return value
}
}
public class CircleStatsTester //defining class
{
public static void main(String ar[]) //defining main method
{
int diameter = 5; //defining integer variable
double radius = 2.5;//defining float variable
CircleStats cStats = new CircleStats(); //creating CircleStats class object
System.out.println("The circumference = " + cStats.calcCircleCircumf(diameter)); //print value
System.out.println("The circumference = " + cStats.calcCircleCircumf(radius)); //print value
}
}
Output:
The circumference = 31.4
The circumference = 7.853981633974483
Explanation:
In the above java program code, two-class is declared that are "CircleStats and CircleStatsTester". In the class "CircleStats" a default constructor is created then method overloading is performed in which two method is defined that have the same name but different parameters.
- In the first-time method declaration, it contains a float variable "radius" in its parameter to calculate and return its value.
- In the second time method declaration, it contains a double variable "diameter" in its parameter, calculates and returns its value.
Then another class CircleStatsTester is defined inside this class the main method is defined. In the main method, an integer and double type variable is defined that contains a value that is "diameter = 5 and radius = 2.5". In this method, an above class CircleStats object is created and call this class functions.