Java - Array methods assignment

From WLCS
Revision as of 08:12, 21 October 2015 by Admin (talk | contribs) (Created page with "=== 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 * ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

  1. Open NetBeans and create a new project named ArrayMethodsProject
  2. Create a new class file named ArrayMethods.java (it should not contain a main() method)
  3. 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: We did this in eIMACS in the Arrays chapter
    • int max(int[] intArray)
      • Traverse the array and find the largest element. Return the maximum
      • Hint: We did this in eIMACS in the Arrays chapter
    • int indexOf(int key)
      • Traverse the array 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.

Testing ArrayMethods

  1. Create a new Java class with a main() method
  2. Within the main() method, use the ArrayMethods to create an array and test out each of your methods