Question
Given the main method below, fill in the missing line that invokes a method named sum , and this method takes the two-dimensional double type
Given the main method below, fill in the missing line that invokes a method named sum, and this method takes the two-dimensional double type array nums as its only actual parameter.
public static void main (String[] args)
{
double[][] nums = {{5.2, 18.9, 2.4, 9.6}, {41.2}, {3.9, 0.4, 1.3}};
// fill in code on top of the underscore to invoke method sum that takes two-
// dimensional double type array nums as its only actual parameter.
double total=_____________________________ ;
System.out.printf ("total is %8.2f: ", total);
}
Given the main method above, implement a public method named sum in the same class of the main method. This method is invoked by the main method as indicated above. This method sum takes a two-dimensional double type array as its only formal parameter, and this method has a double return type. You need to first provide the correct method header, and in the method header line indicated below, before the method name sum, you need to fill in three words above the three underscores, with one word for each underscore. Inside the parenthesis after the method name, you need to fill in the type (occupying the 1st underscore) and the name of the formal parameter (occupying the 2nd underscore) for this method. In the method body, you need to use embedded loops to navigate the array, and calculate the sum during the navigation. Which of the following statements is correct, in order to satisfy the need of method sum?
I need to use two regular for loops to form the embedded loops
I need to use two for-each loops to form the embedded loops
I can use either two regular for loops or two for-each loops to form the embedded loops
You need to explain your reasoning in order to receive credit.
Now write down the code for the embedded loops inside the method body. After the embedded loops finish, the method should return the calculated sum value, then the method finishes. You can define local variables inside the method body as needed. Remember: you cannot hardcode array size with numeric values in the implementation of this method, that means, method sum needs to adapt to a two-dimensional array with any size in each dimension.
____________________________sum ( ____________________ )
{
} // end of method body for method sum;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started