75.0k views
3 votes
CTIVITY 2.2.2: Method call in expression. Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just one statement. Hint: Call findMax() twice in an expression.

User Kalida
by
5.5k points

1 Answer

3 votes

Answer:

The one statement is:

maxSum = maxFinder.findMax(numA, numB) + maxFinder.findMax(numY, numZ);

Step-by-step explanation:

  • The given code in the Activity contains a method named findMax() in class SumOfMax which has two parameters num1 and num2. The method returns the maximum value after comparing the values of num1 and num2.
  • In the main() function, 4 variables numA, numB, numY and numZ of type double are declared and assigned the values numA=5.0, numB=10.0, numY=3.0 and numZ=7.0. Also a variable maxSum is declared and initialized by 0.
  • After that an object named maxfinder of SumOfMax class is created using keyword new.

SumOfMax maxFinder = new SumOfMax();

  • We can invoke the method findMax() by using reference operator (.) with the object name.
  • So the task is to assign to maxSum the maximum of numA, numB plus maximum of numY, numZ in one statement.
  • Using findMax() method we can find the maximum of numA and numB and also can find the maximum numY and numZ. The other requirement is add (PLUS) the maximum of numA, numB to maximum of numY, numZ.
  • The statement used for this is given below:

maxSum = maxFinder.findMax(numA, numB) + maxFinder.findMax(numY, numZ);

  • So as per the hint given in the question statement, findMax() function is being called twice, at first to find the maximum between the values of variables numA and numB and then to find the maximum between the values of numY and numZ.
  • According to the given values of each of these variables:

numA=5.0, numB=10.0,

So numB>numA

  • Hence findMax returns numB whose value is greater than numA
  • Now findMax() is being called again for these variables:

numY=3.0 and numZ=7.0

So numZ>numY as 7.0>3.0

  • Hence findMax() returns numZ whose value is 7.0
  • Finally according to the statement maximum will be added and the result of this addition will be assigned to variable maxSum
  • 10.0 is added to 7.0 which makes 17.0
  • System.out.print("maxSum is: " + maxSum);

This statement displays the value of maxSum which is 17.0

User Chris Ammerman
by
5.4k points