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...

  • 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...

  • 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.

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • Write a program which asks the user to enter an integer. Use switch statement to write...

    Write a program which asks the user to enter an integer. Use switch statement to write out the numeric word (such as ONE) if the user's input is 1; (see the sample run output for the usr input 2 or 3); otherwise, write OUT OF RANGE. Below are few sample runs: If the user enters a 1, the program will print: ONE TWO THREE Or, if the user enters a 2, the program will print: TWO THREE Or, if the...

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
ADVERTISEMENT