Question

We use JAVA. Thanks.    Create an application whose main method asks the user to enter...

We use JAVA. Thanks.   

Create an application whose main method asks the user to enter an n by m integer matrix that contains nm integer numbers. n and m should be between 1 and 10. If the user enters a  

          number less than 1 or greater than 10, the program will continue to ask the user to enter an integer number between 1 and 10. The program should print the sum of the boundary elements of the matrix. The program should use the following method to calculate the sum of the boundary elements of the matrix:

          int boundarySum(int [][] matrix):

         returns the sum of the boundary elements of the matrix.

        

          Use the Scanner class to read the user’s input.

Example 1. Input/output:

Input:

Please enter the number of rows(between 1 and 10): 3

Please enter the number of columns(between 1 and 10): 3

Please enter a 3 by 3 matrix:

5      8      1      

13    2     -5    

3      10    6    

Output:

The sum of the boundary elements is: 41

Example 2. Input/output:

Input:

    Please enter the number of rows(between 1 and 10): 5

    Please enter the number of columns(between 1 and 10): 20

    Please enter the number of columns(between 1 and 10):-6

    Please enter the number of columns(between 1 and 10):4

Please enter a 5 by 4 matrix:

5      7      7       8

12    0     -5      15

3      12    9       10

7       3     8        7

9     -10    5        6

Output:

              The sum of the boundary elements is: 91

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Here is your required code */

import java.util.Scanner;
public class Main {

   public int boundarySum(int [][] matrix)
   { int m = matrix[0].length;
int n = matrix.length;
       int sum = 0;
       for (int i = 0; i < m; i++) {
           for (int j = 0; j < n; j++) {
               if (i == 0)
                   sum += matrix[i][j];
               else if (i == m - 1)
                   sum += matrix[i][j];
               else if (j == 0)
                   sum += matrix[i][j];
               else if (j == n - 1)
                   sum += matrix[i][j];
           }
       }
       return sum;
   }
   public static void main(String[] args)
   {Scanner sc=new Scanner(System.in);
   int n,m;
   Main m1=new Main();
   while(true){
   System.out.print("Please enter the number of rows(between 1 and 10):");
   n=sc.nextInt();
   if(n<1||n>10)
{
System.out.println("Please Enter number between 1 to 10");
}
else
break;
}
   while(true){
   System.out.print("Please enter the number of columns(between 1 and 10):");
   m=sc.nextInt();
   if(m<1||m>10)
{
System.out.println("Please Enter number between 1 to 10");
}
else
break;
}
   int matrix[][] = new int[n][m];
   System.out.println("Please enter a 3 by 3 matrix:");
   for(int i=0;i<n;i++)
   for(int j=0;j<m;j++)
matrix[i][j]=sc.nextInt();
       int sum = m1.boundarySum(matrix);
       System.out.println("The sum of the boundary elements is:"+sum);
   }
}

Add a comment
Know the answer?
Add Answer to:
We use JAVA. Thanks.    Create an application whose main method asks the user to enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • Please help me in thsi question i have only 30 minutes this is python Question Two....

    Please help me in thsi question i have only 30 minutes this is python Question Two. 25 points. Write the missing function such that when the following code executes: import numpy as np # Assume the user enters a valid integer #Assume the user enters o valid integer rows - intinput("Enter number of rows (1-10 ")) inti nputl Enter number of columns 1-10 : columns matrix np.random.randint(1, 11, rows columns).reshapelrows, columns) print"Matrix Shape:" matrix.shape, "MatrixIn". matrix) result sum _border(matrix) print/"The...

  • Magic Squares; Java code

    Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum ofthe elements in each row, in each column, and in the two diagonals is the same value. For example,16 3 2 135 10 11 89 6 7 124 15 14 1Write a program that reads in n2 values from the keyboard and tests whether they form a magic squarewhen arranged as a square matrix. You...

  • Hi, please this is for Python3.6 Write a program that asks the user to enter the...

    Hi, please this is for Python3.6 Write a program that asks the user to enter the number of rows and number of columns.The program would then print the number of columns not as stars but as digits (eg: 1234 if the number of columns is 4) eg: Enter the number of rows: 4 Enter the number of columns:8 12345678 12345678 12345678 12345678

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the...

    Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Function Description Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums. diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr) Parameters: a_size_rows: number of rows in array a_size_cols: number of columns in array a: array of integers to process Returns: integer value that was calculated Constraints -100 < = elements of the matrix < = 100...

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • Exercise 1 Write a program that asks the user to enter the seed for a random...

    Exercise 1 Write a program that asks the user to enter the seed for a random number generator. Then the program uses this seed to roll a dice and hence generates two integers corresponding to the faces of each dice. If their sum is odd, the user has two tries to guess the sum. The program gives the user a hint after the first try. Otherwise, the user has three tries to guess the sum. Sample run 1: Enter the...

  • Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should...

    Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.ser to input a number, and then using a loop control statement (While, For, Do-While), output to the console the numbers beginning from 1, up to and including the number input by the user.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT