Java - Array methods assignment
From WLCS
Objective(s)
- You will be able to create a class comprised of static methods
- You will be able to define static methods
- You will be able to use/call static methods
- You will be able to implement various array algorithms
- printing an array
- generating a random array
- sum all the elements
- finding the minimum
- finding the maximum
- finding any element
Directions
- Open NetBeans and create a new project named ArrayMethodsProject
- Create a new class file named ArrayMethods.java (it should not contain a main() method)
- Define the following methods:
- void print(int[] intArray)
- Traverse the array and print out each element on one long line
- int[] generateRandom(int size, int lower, int upper)
- Within this method, you will create a new array of size length. You will populate it with random integers that range from lower to upper. Return the array
- int sum(int[] intArray)
- Traverse the array and sum all the elements. Return the sum.
- int min(int[] intArray)
- Traverse the array and find the smallest element. Return the minimum
- Hint: Create a variable to "remember" the minimum as you traverse the array. If the current element is smaller than your minimum, then update the minimum to the new element
- int max(int[] intArray)
- Traverse the array and find the largest element. Return the maximum
- Hint: This algorithm is almost identical to finding the minimum
- int indexOf(int[] intArray, int key)
- Traverse the intArray and check if any elements in the array match key. If so, return the index of the match, otherwise, return -1 if there is no match.
- void print(int[] intArray)
Testing ArrayMethods
- Create a new Java class with a main() method
- Within the main() method, use the ArrayMethods to create an array and test out each of your methods
//Example:
int[] randomNums = ArrayMethods.generateRandom(10, 0, 100);
ArrayMethods.print(randomNums);