April 19 Assignment

Instructions

Please log into cs.bridgewater.edu. Navigate to the csci101/hw/apr19 directory in your git repository. Write a program in a file named Practice.java that satisfies the following program specifications. When finished, push your code to GitHub.

Program Requirements

Write a method named createMatrix that takes two integers as arguments and returns a 2D array with a length (number of rows) equal to the value in the first parameter and whose elements (rows) have a length equal to the value in the second parameter.

Write a method named printMatrix that takes a 2D array of integers as an argument and prints the contents of the array to the screen with each row printed on a separate line and with spaces between the elements.

Write a method named computeSum that takes a 2D array of integers as an argument and returns the sum of all of the elements in the array.  Do not assume that each row has the same number of elements.

In main do the following.

    • Ask the user for two integers, read them in from the keyboard, and store them in variables named numRows and numCols.
    • Create a 2D array having the dimensions equal to the values in numRows and numCols by calling createMatrix.  Store the reference to the new array in a variable named matrix1.
    • Print the contents of matrix1 to the screen using printMatrix.
    • Compute the sum of the elements in matrix1 using computeSum.  Store the sum in a variable named sum.  Print the value in sum to the screen.
    • Ask the user to enter an integer, read in the value from the keyboard, and store the value in a variable named newValue.
    • Set all of the elements in matrix1 that have an even row index (e.g. 0,2,4,…) AND an even column index to the value stored in the variable named newValue.
    • Print the contents of matrix1 to the screen using printMatrix.

© 2021 – 2024, Eric. All rights reserved.