Code these three methods.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Code these three methods.

public class TwoDArrayMethods
{
/* Method #1
* Write a static method named matrixAdd that accepts a pair of two-dimensional arrays of
integers as parameters,
treats the arrays as 2D matrices and adds them, returning the result.
The sum of two matrices A and B is a matrix C where
for every row i and column j, Cij
* You may assume that the arrays passed as parameters have the same dimensions.
* /
Aij + Bij.
/* Method #2
* Write a static method called loadArray that takes in a 2D array of integers and will
fill
* the array with values from 1 to the number of elements in the array in reverse
row
order.
* This call to loadArray(some2DArray), where some2DArray is a 2D array of integers that
is 3x4
* would result in some2DArray containing the values:
*
4 3 2 1
8 7 6 5
12 11 10 9
* /
/* Method 3
* Write a method called allRowSums that takes in a 2D array of integers and calculates
the row sum for every row.
Index i of the return array contains the sum of elements in row i.
The method should return each of the sums in an array.
Example: allRowSums ({1, 2, 3},
{ 4, 5, 6})
→ [6, 15]
*
public static void main(String args[])
{
//Test all three of your methods to ensure they work!
Transcribed Image Text:public class TwoDArrayMethods { /* Method #1 * Write a static method named matrixAdd that accepts a pair of two-dimensional arrays of integers as parameters, treats the arrays as 2D matrices and adds them, returning the result. The sum of two matrices A and B is a matrix C where for every row i and column j, Cij * You may assume that the arrays passed as parameters have the same dimensions. * / Aij + Bij. /* Method #2 * Write a static method called loadArray that takes in a 2D array of integers and will fill * the array with values from 1 to the number of elements in the array in reverse row order. * This call to loadArray(some2DArray), where some2DArray is a 2D array of integers that is 3x4 * would result in some2DArray containing the values: * 4 3 2 1 8 7 6 5 12 11 10 9 * / /* Method 3 * Write a method called allRowSums that takes in a 2D array of integers and calculates the row sum for every row. Index i of the return array contains the sum of elements in row i. The method should return each of the sums in an array. Example: allRowSums ({1, 2, 3}, { 4, 5, 6}) → [6, 15] * public static void main(String args[]) { //Test all three of your methods to ensure they work!
Expert Solution
method 1

1 st method 

 

public int[][] matrixAdd(int[][] a, int[][] b) {
    for(int i = 0; i < a.length; i++) {
        for(int j = 0; j < a[0].length; j++) {
            a[i][j] = a[i][j] + b[i][j];
        }
    }
    return a;
}

 

 

or

 

public class MatrixAdditionExample{  
public static void main(String args[]){  
//creating two matrices    
int a[][]={{1,3,4},{2,4,3},{3,4,5}};    
int b[][]={{1,3,4},{2,4,3},{1,2,4}};    
    
//creating another matrix to store the sum of two matrices    
int c[][]=new int[3][3];  //3 rows and 3 columns  
    
//adding and printing addition of 2 matrices    
for(int i=0;i<3;i++){    
for(int j=0;j<3;j++){    
c[i][j]=a[i][j]+b[i][j];    //use - for subtraction  
System.out.print(c[i][j]+" ");    
}    
System.out.println();//new line    
}    
}}  

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Hiring Problem
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education