Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 8, Problem 8.1PE
Expert Solution & Answer
Check Mark
Program Plan Intro

Sum of elements column by column

Program Plan:

  • Include the “Scanner” package to get input values from user.
  • Define the class named “Test”.
    • Define the main method.
      • Define the object “obj” for “Scanner” class to get input.
      • Declare the multi-dimensional array named “array”, and prompt the user to get array input.
      • Define the “for” loops, that get the input values for the variable “array”.
      • Define the “for” loop, that call the method “sumColumn()” with two arguments, they are array and index of array.
    • Define the method named “sumColumn()” with two arguments. One is a array variable “m” in type of “double” and another one is “columnIndex” with integer data type.
      • Declare the variable “total” in type of “double” and initialize the variable with “0”.
      • Set the “for” loop, the loop executes from “0” to length of array “m”.
        • Add the column values and store it into the “total”.
        • Return the value of “total”.
Program Description Answer

The following JAVA code is to sum the elements of array column by column using the method “sumColumn(double[][] m, int columnIndex)”.

Explanation of Solution

Program:

//Insert package

import java.util.Scanner;

//Class definition

public class Test

{

//Main method

public static void main(String[] args)

{

//Assign the object for "Scanner" class

Scanner obj = new Scanner(System.in);

//Print statement

System.out.print("Enter a 3 by 4 matrix row by row: ");

//Declaration of variable

double[][] array = new double[3][4];

//Outer loop

for (int i = 0; i < array.length; i++)

//Inner Loop

for (int j = 0; j <array[i].length; j++)

//Get input from user

array[i][j] = obj.nextDouble();

//Loop

for (int j = 0; j < array[0].length; j++)

{

//Print statement with function call

System.out.println("Sum of the elements at column " + j + " is " + sumColumn(array, j));     

}

}

//Function definition

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

{

//Declaration of variable

double total = 0;

//Loop

for (int i = 0; i < m.length; i++)

//Add  the array values into variable

total += m[i][columnIndex];

//Return statement

return total;

}

}

Sample Output

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

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Need Java method please. Thank you.
Need Java method please. Thank you.
3. Write two nested loops to generate the following output. (Note: There is one space between each number, and any extra line shown is intentional.) 12 10 8 6 18 15 12 24 20 30 2 3 3 6 48 12 5 10 15 20 6 12 18 24 30
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
9.1: What is an Array? - Processing Tutorial; Author: The Coding Train;https://www.youtube.com/watch?v=NptnmWvkbTw;License: Standard Youtube License