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.

© 2021 – 2024, Eric. All rights reserved.