Question: Sum Diagonal (Java) Write a program that does the following: Creates a 4 x 4 array Uses a method to fill the array Uses another

Sum Diagonal (Java)

Write a program that does the following:

Creates a 4 x 4 array

Uses a method to fill the array

Uses another method to display the contents of the array (in 4 x 4 format)

Uses a third method to sum the contents of the major diagonal values in the array

The major diagonal values refer to the diagonal that starts from the top left most value and goes to the bottom right most value.

You will need:

1. A 2D array with 4 rows and 4 columns

2. A method to fill the array

The method should include a Scanner object; in other words, dont create a Scanner object in main

The method needs a nested for loop to fill the array

The method should accept a 2D array and does not return anything

3. A method to display the array

The method should accept a 2D array and does not return a value

Instead, the method outputs the contents of the array in a 4 x 4 format

The method needs a nested for loop to display the contents

Tip: See lecture 9B slide 7 to for code that shows how this is done

4. A method to display the sum of the diagonal

This method should accept a 2D array and returns a floating point value (which is the sum)

This method only needs a single for loop to sum the appropriate values

A sample of the output is shown below:

Enter a 4 by 4 matrix row by row: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Your 4 x 4 array is 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 Sum of the elements in the major diagonal is 34.0

Picture version: Sum Diagonal (Java) Write a program that does the following: Creates a

Sum Diagonal Write a program that does the following Creates a 4 x 4 array . Uses a method to fill the array . Uses another method to display the contents of the array (in 4 x 4 format) . Uses a third method to sum the contents of the major diagonal values in the array The major diagonal values refer to the diagonal that starts from the top left most value and goes to the bottom right most value You will need: 1. A 2D array with 4 rows and 4 columns 2. A method to fill the array . The method should include a Scanner object; in other words, don't create a Scanner object in main The method needs a nested for loop to fill the array . The method should accept a 2D array and does not return anything 3. A method to display the array . The method should accept a 2D array and does not return a value o Instead, the method outputs the contents of the array in a 4x 4 format . The method needs a nested for loop to display the contents o Tip: See lecture 9B slide 7 to for code that shows how this is done 4. A method to display the sum of the diagonal . This method should accept a 2D array and returns a floating point value (which is the sum) . This method only needs a single for loop to sum the appropriate values A sample of the output is shown below: Enter a 4 by 4 matrix row by row: 1 2 3456789 10 11 12 13 14 15 16 Your 4x 4 array is 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 Sum of the elements in the major diagonal is 34.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!