20.5k views
2 votes
Answer in java. Do not use print or println, only return. This is a codingbat problem.

Complete the getMaxAverage method that will accept three numbers and return the average of the two maximum numbers.


getMaxAverage(8, 20, 2) → 14.0
getMaxAverage(1, 1, 3) → 2.0
getMaxAverage(1, 2, 3) → 2.5

Code given below.

public double getMaxAverage(double first, double second, double third) {

User Tom Sun
by
8.3k points

1 Answer

3 votes

Final answer:

The 'getMaxAverage' Java method calculates the average of the two highest numbers among three arguments by first finding the smallest number and excluding it from the average calculation.

Step-by-step explanation:

The getMaxAverage method in Java is designed to calculate the average of the two maximum numbers among the three provided as arguments. To find the average of the two maximum numbers, one can first identify the smallest number using conditional statements. Once the smallest number is found, it can be eliminated from the calculation, and then the average of the remaining two numbers is calculated.

To implement the method, you could start by initializing a variable to store the smallest number. Then, use a series of if-else conditional statements to determine the smallest of the three numbers. After identifying the smallest number, you can proceed to sum the other two numbers and divide by two to find their average. Be aware that in Java, the division of two integers results in an integer, so one or both of the numbers should be cast to a double before division to get the average as a decimal.

Below is a possible implementation:

public double getMaxAverage(double first, double second, double third) {
double min = Math.min(first, Math.min(second, third));
double sum = first + second + third;
return (sum - min) / 2.0;
}
User Harmeet
by
6.8k points