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 namedk
andv
. 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
-
- 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
-
- Write a method named
sumArrays
that takes two integer arrays with the same length namedarr1
andarr2
as arguments and returns a new array namedtemp
of the same length. For each index i,temp[i] = arr1[i] + arr2[i].
- Write a method named
-
- Write a method named
elementExists
that takes an integer namedk
and array of integers namedarr
as an arguments and returns true if the value stored in k is found in the array namedarr
, and returns false otherwise.
- Write a method named
-
- 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 theinitArray
method. - Print the elements in
arr1
to the console by callingprintArray
. - Declare an array named
arr2
and initialize it the the values 1 through 10. - Compute the sum of
arr1
andarr2
by callingsumArrays
. Store the array that is returned bysumArray
in a variable namedarr3
. - Print the elements in
arr3
to the console by callingprintArray
. - 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 callingprintArray
.
- Declare an array of integers named
- Write a method named
© 2021 – 2024, Eric. All rights reserved.