Question

JAVA Write a method that returns the sum of all the elements in a specified column...

JAVA

Write a method that returns the sum of all the elements in a specified column in a matrix using

the following header:

public static double sumColumn(double [][] m, int columnIndex)

Write another method that returns the sum of all the elements in a specified row in a matrix using

the following header:

public static double sumRow( double [][] m, int rowIndex)

Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum of each row.

the result should print the sum of both the row and column respectively

this is what the result should look like

Enter a 3 by 4 matrix row by row:

1.5 2 3 4

5.5 6 7 8

9.5 1 3 1

Sum of the elements at column 0 is 16.5

Sum of the elements at column 1 is 9.0

Sum of the elements at column 2 is 13.0

Sum of the elements at column 3 is 13.0

Sum of the elements at row 0 is 10.5

Sum of the elements at row 1 is 26.5

Sum of the elements at row 2 is 14.5

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class SumRowsColumns {

    public static double sumColumn(double[][] m, int columnIndex) {
        double sum = 0;
        for (int i = 0; i < m.length; i++) {
            sum += m[i][columnIndex];
        }
        return sum;
    }

    public static double sumRow(double[][] m, int rowIndex) {
        double sum = 0;
        for (int i = 0; i < m[0].length; i++) {
            sum += m[rowIndex][i];
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a 3 by 4 matrix row by row:");
        double[][] m = new double[3][4];
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                m[i][j] = in.nextDouble();
            }
        }
        for (int i = 0; i < m[0].length; i++) {
            System.out.println("Sum of the elements at column " + i + " is " + sumColumn(m, i));
        }
        for (int i = 0; i < m.length; i++) {
            System.out.println("Sum of the elements at row " + i + " is " + sumRow(m, i));
        }
        in.close();
    }
}

Add a comment
Know the answer?
Add Answer to:
JAVA Write a method that returns the sum of all the elements in a specified column...
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
  • Sum elements column by column

    (Sum elements column by column) Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public staticdouble sumColumn( double[] [] m, int columnIndex) Write a test program that reads a 3- by- 4 matrix and displays the sum of each column.

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • Write a method to multiply two matrices. The header of the method is: public static double[][]...

    Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

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