Introduction: How to Code Intermediate Java Functions Using Arrays for New Coders

The following is a list of step-by-step instructions on how to code simple java functions using arrays. Anyone with access to a working IDE of java can code these functions. These functions include a min value function, a min position function, and a distance between min and max function. Since there are three separate functions, the instruction set will be divided into three separate parts.

Supplies

An eclipse IDE is recommended for this project, but any IDE will work. (Note: modifications may need to be made)

Step 1: Part One: Min Value Functions

Start by initializing a variable to list[0]. You are going to want this variable to have a meaningful name, as this will return the min value. I named my variable result.

Step 2: Create a "for" Loop

Create a "for" loop in which you initialize a variable to 1. When the variable is less than the array length, iterate through the function. In this case, my variable will be the letter i.

Step 3: Write an If Statement.

When the variable in your list (list[i]) is less than the current result, your new result should be equivalent to the variable. (result = i). (NOTE: the variable in the brackets is the letter i, not 1)

Step 4: Close the Entire "for" Loop and Return Result.

Check to make sure indentations and semi-colons match before testing the function.

Step 5: Conclusion of Part One: Min Value Function

Congratulations! You should have a working min value function which resembles the above picture

Step 6: Part Two: Min Position Function

Initialize a variable to 0. Since we are finding the min position, choose a meaningful name like minPos.

Step 7: Create a "for" Loop

Initialize a variable to 1 in your for loop. When that variable is less than the array length, iterate through the function. In this case, my variable will be the letter i.

Step 8: Write an If Statement.

If your chosen variable (list[i]) is less than the list at current minPos, update your minPos to index i. (Note: I am using the letter i, not 1)

Step 9: Close the Entire "for" Loop and Return the Current MinPos.

Make sure your indentations and semi-colons are in the right place before testing your code.

Step 10: Conclusion of Part Two: Min Position Function

Congratulations! You should now have a finished min position function that resembles the above picture.

Step 11: Part Three: Distance Between Min and Max Function

Initialize two variables this time to 0 (one for min position and one for max position). Make sure they have meaningful names (in this case I am doing minPos and maxPos, respectively).

Step 12: Create a "for" Loop

Initialize a variable to 1 in your "for" loop. When the variable is less than the array length, iterate through the entire function. In this case, my variable will be the letter i.​

Step 13: Write an If Statement

If your variable (list[i]) is less than the list at index minPos, minPos should now be equal to i. (Note: I am using the letter i, not 1).

Step 14: Write an "Else" If Statement

Else, if your variable (list[i]) is greater than the list at maxPos' index, maxPos should equal i. (Note: I am using the letter i, not 1).

Step 15: Initialize Distance As a Variable

Distance should be maxPos - minPos as an equation.

Step 16: Return the Absolute Value of Distance

At this point, make sure the indentations and semi-colons match, before testing your function.

Step 17: Conclusion of Distance Between Min and Max Position

Congratulations! You should have a working distance between min and max function that resembles the above picture.

Step 18: Conclusion

You're done! You have now created three basic functions in java. You're on your way to becoming a full-time programmer. To improve your coding skills, continue to practice these functions.