Java

REVIEW TOPIC 06 – ARRAYS

 

LESSON WORK

 

 

QUESTION 1 (SELF CHECK – SOLUTION LINK BELOW)


a) Create a int array of size 50.


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):

public static void outputArray(int[] a)

This function simply outputs all of the content of an array to screen separating each element by a comma.

 

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.



e) Implement the function with the following header:

 

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.

Test your function.



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.

Test your function.

SOLUTIONS: Click here for the solutions to this question.

QUESTION 2 (SELF CHECK – SOLUTION LINK BELOW)


a) Write the function called mergeArrays that gets two integer arrays as input.  It creates and returns a new array that contains the contents of the two original arrays.

b) Test your function from above outputting to screen the contents of all three arrays.

 

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.

For example, factorCount(12) will return 6 since the number 12 has the following factors:
1, 2, 3, 4, 6, 12



b) Implement the following function:

 

public static int[] factors(int n)

 

This function returns an array that contains all of the factors of n.

For example, factors(6) would return the array {1, 2, 3, 6}.



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.

Hint: You can use your function from c) to help solve this.


e) Implement the following function:

 

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.