108k views
2 votes
Suppose that you have created a program with only the following variables.int age = 34;int weight = 180;double height = 5.9;Suppose that you also have a method with the following header:public static void calculate(int age, double size)Which of the following method calls are legal?a. calculate(age, weight);b. calculate(age, height);c. calculate(weight, height);d. calculate(height, age);e. calculate(45.5, 120);f. calculate(12, 120.2);g. calculate(age, size);h. calculate(2, 3);i. calculate(age);j. calculate(weight, weight);

User Per Larsen
by
5.3k points

1 Answer

4 votes

Answer:

a. calculate(age, weight); // This is legal

b. calculate(age, height); // This is legal

c. calculate(weight, height); // This is legal

d. calculate(height, age); // This is not legal

e. calculate(45.5, 120); // This is not legal

f. calculate(12, 120.2); // This is legal

g. calculate(age, size); // This is not legal

h. calculate(2, 3); // This is legal

i. calculate(age); // This is not legal

j. calculate(weight, weight); // This is legal

Step-by-step explanation:

a. This is legal because the argument passed are two in numbers and they are integers but the integer for the second parameter can be cast to double.

b. This is legal because the method requires two parameter and two are passed. The method requires integer and double. The passed parameter are integer and double.

c. This is legal because the method requires two parameter and two are passed. The method requires integer and double. The passed parameter are integer and double.

d. This is not legal because even though two parameters were passed. The first one should be int but double was supplied and double cannot be downgraded to int. There is a possible lossy conversion from double to int.

e. This is not legal because even though two parameters were passed. The first one should be int but double was supplied and double cannot be downgraded to int. There is a possible lossy conversion from double to int.

f. This is legal because the method requires two parameter and two are passed. The method requires integer and double. The passed parameter are integer and double.

g. This is not legal because even though two parameters were passed. The first one should be int but the second parameter is not defined before been passed to the calculate method.

h. This is legal because the argument passed are two in numbers and they are integers but the integer for the second parameter can be cast to double.

i. This is not legal because only one parameter was passed instead of two.

j. This is legal because the argument passed are two in numbers and they are integers but the integer for the second parameter can be cast to double.

User Florian Klein
by
5.1k points