Question

public class PracticeExam { public static void main(String[] args) { int[][] data = { { 0,...

public class PracticeExam {

public static void main(String[] args) {

int[][] data = {

{ 0, 1, 2, 3, 4, 5, 6 },

{ 0, 1, 2, 3, 4, 5, 6 },

{ 0, 1, 2, 3, 4, 5, 6 },

{ 0, 1, 2, 3, 4, 5, 6 },

{ 0, 1, 2, 3, 4, 5, 6 },

{ 0, 1, 2, 3, 4, 5, 6 },

{ 0, 1, 2, 3, 4, 5, 6 },

{ 0, 1, 2, 3, 4, 5, 6 } };

// input: TODO prompt the user for

// 1) a row number

// 2) a column number

// processing: TODO implment the methods below

int rowSum = sumRow(rowNum, data);

int colSum = sumColumn(colNum, data);

double all = overallAverage(data);

// output: TODO display the three answers

// using a method named displayResults

}

}

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// PracticeExam.java

import java.util.Scanner;

public class PracticeExam {

    /**

    * method to find the sum of elements in a specified row

    *

    * @param rowNum

    *            row number between 0 and data.length-1

    * @param data

    *            2d array

    * @return the sum of elements in rowNum row of data

    */

    static int sumRow(int rowNum, int data[][]) {

         int sum = 0;

         // looping through each column in rowNum row of data, summing values

         for (int c = 0; c < data[rowNum].length; c++) {

             sum += data[rowNum][c];

         }

         return sum;

    }

    /**

    * method to find the sum of elements in a specified column

    *

    * @param colNum

    *            column number between 0 and data[0].length-1

    * @param data

    *            2d array

    * @return the sum of elements in colNum column of data

    */

    static int sumColumn(int colNum, int data[][]) {

         int sum = 0;

         // looping through each row in data

         for (int r = 0; r < data.length; r++) {

             // adding element at row=r and column=colNum to sum

             sum += data[r][colNum];

         }

         return sum;

    }

    /**

    * method to find the average of all values in data

    *

    * @param data

    *            2d array

    * @return average in double format

    */

    static double overallAverage(int data[][]) {

         int sum = 0; // total sum

         int count = 0; // total number of cells in data array

         // looping through each row and column, summing values

         for (int r = 0; r < data.length; r++) {

             for (int c = 0; c < data[r].length; c++) {

                 sum += data[r][c];

                 count++;

             }

         }

         // finding and returning average

         double avg = (double) sum / count;

         return avg;

    }

    /**

    * method to display the results

    *

    * @param rowSum

    *            sum of elements of a specified row

    * @param colSum

    *            sum of elements of a specified column

    * @param average

    *            average of all elements in an array

    */

    static void displayResults(int rowSum, int colSum, double average) {

         System.out.println("Sum of elements in specified row: " + rowSum);

         System.out.println("Sum of elements in specified column: " + colSum);

         System.out.println("Overall average of elements: " + average);

    }

    public static void main(String[] args) {

         int[][] data = {

         { 0, 1, 2, 3, 4, 5, 6 },

         { 0, 1, 2, 3, 4, 5, 6 },

         { 0, 1, 2, 3, 4, 5, 6 },

         { 0, 1, 2, 3, 4, 5, 6 },

         { 0, 1, 2, 3, 4, 5, 6 },

         { 0, 1, 2, 3, 4, 5, 6 },

         { 0, 1, 2, 3, 4, 5, 6 },

         { 0, 1, 2, 3, 4, 5, 6 } };

         Scanner sc = new Scanner(System.in);

         // input: prompting the user for

         // 1) a row number

         System.out.print("Enter a row number: ");

         int rowNum = sc.nextInt();

         // 2) a column number

         System.out.print("Enter a column number: ");

         int colNum = sc.nextInt();

         // processing: assuming row and column numbers are valid, finding each

         // sum

         int rowSum = sumRow(rowNum, data);

         int colSum = sumColumn(colNum, data);

        

         //finding average

         double all = overallAverage(data);

         // output: displaying the three answers

         // using a method named displayResults

         displayResults(rowSum, colSum, all);

    }

}

/*OUTPUT*/

Enter a row number: 3

Enter a column number: 2

Sum of elements in specified row: 21

Sum of elements in specified column: 16

Overall average of elements: 3.0

Add a comment
Know the answer?
Add Answer to:
public class PracticeExam { public static void main(String[] args) { int[][] data = { { 0,...
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
  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • What is printed by running the following code? public static void main(String[] args) { int[] nums...

    What is printed by running the following code? public static void main(String[] args) { int[] nums = {2, 3, 4}; int n = 5; changeMe1(n, nums); System.out.print( n ); System.out.print(nums[0]); changeMe2(n, nums); System.out.print( n ); System.out.print(nums[0]); } public static void changeMe1(int number, int[] list) { number++; list[0]++; } public static void changeMe2(int number, int[] list) { number = 9; list = new int[1]; list[0] = 99; }

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • public class MovieAudience {    public static void main(String[] args) {        // input (hard-coded)...

    public class MovieAudience {    public static void main(String[] args) {        // input (hard-coded) Do not change.        short[] agesOfAudience = { 39, 17, 67, 15, 56, 15, 92, 25, 62, 61, 79, 42, 85, 76, 58, 41, 22, 59, 83, 84, 72,                77, 31, 48, 44, 19, 43, 98, 96, 41, 64, 27, 60, 22, 6, 99, 67, 14, 31, 15, 97, 42, 87, 62, 79, 37, 46,               ...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr =...

    import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr = new Scanner(System.in); // TODO: get user choice    // TODO: call printTable method passing choice as the parameter    } public static void printTable(int stop) { // TODO: print header    // TODO: loop to print table rows up to stop value    } Write a Java program where the main () method prompts the user to select an integer value between 1 and...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

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