public class MathUtils {
/**
* Returns the average of two given numbers.
*
* @param num1 the first number
* @param num2 the second number
* @return the average of num1 and num2
*/
public static double average(int num1, int num2) {
return (num1 + num2) / 2.0;
}
public static void main(String[] args) {
// Calculate the average of 5 and 10
double result = average(5, 10);
System.out.println("Average: " + result);
}
}
The given code defines a class named MathUtils with a method average that calculates the average of two given numbers. The average method takes two integer parameters, num1 and num2, and returns the average as a double value. The main method is used to test the average method by invoking it with the values 5 and 10, and printing the result.
To calculate the average, the average method uses the formula (num1 + num2) / 2.0 to ensure the result is a double value.
In the main method, the average method is invoked by passing the arguments 5 and 10. The returned result is stored in a variable named result. Finally, the result is printed using the System.out.println statement.
When we run the code, the output will be:
Average: 7.5
The output shows that the average of 5 and 10 is indeed 7.5, which is the expected result.