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.

April 12 Assignment

Instructions

Please log into cs.bridgewater.edu. Navigate to the csci101/hw/apr12 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 Specification

Write a method named printMatrix that takes a 2D array (matrix) of integers as an argument and prints the elements in the matrix to the screen with each row of elements appearing on a separate line.

Write a second method, also named printMatrix that takes a 2D array (matrix) of characters as an argument and prints the elements in the matrix to the screen with each row of elements appearing on a separate line.

In main() please do the following.

    • Creates a 3 x 3 2D array of characters named matrix1 with the following values:
      c a t
      a t e
      t e a
    • Print the contents of matrix1 using the printMatrix method.
    • Create a 3 x 3 2D  array of integers named matrix2.  Using nested for-loops initializes the 2D array with following values:
      1 1 1
      1 1 1
      1 1 1
    • Print the contents of matrix2 using the printMatrix method.
    • Change the element at row 1, column 1 in matrix2 to the value 7.
    • Print the contents of matrix2 using the printMatrix method.
    • Write a fragment of code that creates a 3 x 3 2D array of characters named matrix3 and using nested for-loops populate matrix3 with the values in  matrix1 (in the same order).
    • Print the contents of matrix3 using the printMatrix method.
    • Get the value of the element at row 1, column 1 in matrix3 and store it in a variable named elm.  Print the value of elm to the screen.

April 9 Assignment

Instructions

Please log into cs.bridgewater.edu. Navigate to the csci101/hw/apr9 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 Specification

    • Write a method named initArray that has two integer parameters named k and v. The method returns an integer array of size k and initializes all of elements in the array to the value v.
    • Write a method named printArray that takes an array of integers as an argument and prints the integers to the console, all on a single line, with spaces between them.
    • Write a method named sumArrays that takes two integer arrays with the same length named arr1 and arr2 as arguments and returns a new array named temp of the same length. For each index i, temp[i] = arr1[i] + arr2[i].
    • Write a method named elementExists that takes an integer named k and array of integers named arr as an arguments and returns true if the value stored in k is found in the array named arr, and returns false otherwise.
    • Write a method named main that does the following.
      • Declare an array of integers named arr1 and initialize it to an array of 10 integers, all set to the value 2, using the initArray method.
      • Print the elements in arr1 to the console by calling printArray.
      • Declare an array named arr2 and initialize it the the values 1 through 10.
      • Compute the sum of arr1 and arr2 by calling sumArrays.  Store the array that is returned by sumArray in a variable named arr3.
      • Print the elements in arr3 to the console by calling printArray.
      • Print the the screen true if, using the elementExists method, arr3 contains the value 9. Otherwise print false.
      • Print the the screen true if, using the elementExists method, arr3 contains the value 15. Otherwise print false.
      • Declare an array named arr4 and initialize it to an array that can hold 10 integers.
      • Use a for-loop to populate arr4 using data input by the user.
      • Print the elements in arr4 to the console by calling printArray.