Java REVIEW TOPIC 06 – ARRAYS LESSON WORK QUESTION 1
(SELF CHECK – SOLUTION LINK BELOW)
b)
Using a for loop, assign a random
value between 1 and 10 to each element. c)
Implement the function with the following function header (function
prototype): Make
sure to test your function by calling it with the array your created in a and
b.
d)
Make use of Arrays.toString() to output the content of the array using a
single line of code. Of course, your
solution in c) is more flexible than this approach, but this approach is very
quick.
public
static int occurenceCounter(int[] a, int value) This
function gets an array and a value and returns the amount of times that value
can be found in the array.
f)
Implement the function with the following header: public
static int maxIndex(int[] a) This
function returns the index of the maximum value in the array. SOLUTIONS: Click here for the solutions to this
question.
c)
Optional: If you have time and would
like to a challenge, write the function called mergeSortedArrays that gets
two sorted integer arrays as input and creates and returns a new array that
contains the sorted content of the two original arrays. For example, if {1, 5, 8, 9} and {2, 9, 11}
are passed to the function then the resulting array would be {1, 2, 5, 8, 9,
9, 11}. SOLUTIONS: Click here for solutions to Question 2A
& B and click here for
solutions to Question C. QUESTION 3 a)
Implement the following function: public
static int factorCount(int n) This
function returns the number of factors that are in n.
b)
Implement the following function: public
static int[] factors(int n) This
function returns an array that contains all of the factors of n.
c)
Implement the following function: public
static boolean contains(int[] arr, int v) This
function returns true only if the array arr contains at least one occurrence
of the value v. d)
Implement the following function: public
static boolean mutuallyExclusive(int[] a, int[] b) This
function returns true only if no values found in array a are also in array b. public
static boolean coprimes(int n1, int n2) This
function returns true if both numbers n1 and n2 contain NO common factors
with the exclusion of the value 1. |