4.7k views
0 votes
This program currently fails to compile because the parameters cannot be automatically converted to Double in the statement tripleSum = item1 + item2 + item3;. Because TheType is bound to the class Number, the Number class' doubleValue() method can be called to get the value of the parameters as a double value. Modify tripleAvg() method to use the doubleValue() method to convert each of the parameters to a double value before adding them.

public class ItemMinimum {
public static
Double tripleAvg(TheType item1, TheType item2, TheType item3) {
Double tripleSum = 0.0;
tripleSum = item1 + item2 + item3;
return tripleSum / 3.0;
}
public static void main(String[] args) {
Integer intVal1 = 55;
Integer intVal2 = 99;
Integer intVal3 = 66;
Double doubleVal1 = 14.5;
Double doubleVal2 = 12.3;
Double doubleVal3 = 1.75;
// Try tripleAvg method with Integers
System.out.println("Items: " + intVal1 + " " + intVal2 + " " + intVal3);
System.out.println("Avg: " + tripleAvg(intVal1, intVal2, intVal3) + "\\");
// Try tripleAvg method with Doubles
System.out.println("Items: " + doubleVal1 + " " + doubleVal2 + " " + doubleVal3);
System.out.println("Avg: " + tripleAvg(doubleVal1, doubleVal2, doubleVal3) + "\\");
return;
}
}

1 Answer

3 votes

Answer:

public class Main {

public static

Double tripleAvg(double item1, double item2, double item3) {

Double tripleSum = 0.0;

tripleSum = item1 + item2 + item3;

return tripleSum / 3.0;

}

public static void main(String[] args)

{

Integer intVal1 = 55;

Integer intVal2 = 99;

Integer intVal3 = 66;

Double doubleVal1 = 14.5;

Double doubleVal2 = 12.3;

Double doubleVal3 = 1.75;

System.out.println("Items: " + intVal1 + " " + intVal2 + " " + intVal3);

System.out.println("Avg: " + tripleAvg(intVal1, intVal2, intVal3) + "\\");

System.out.println("Items: " + doubleVal1 + " " + doubleVal2 + " " + doubleVal3);

System.out.println("Avg: " + tripleAvg(doubleVal1, doubleVal2, doubleVal3) + "\\");

}

}

User Tom Tang
by
5.6k points