How to Code Intermediate Java Functions Using Arrays for New Coders

by JamesErwell13 in Workshop > Science

186 Views, 2 Favorites, 0 Comments

How to Code Intermediate Java Functions Using Arrays for New Coders

nerd1.png

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)

Part One: Min Value Functions

MinValue 1.png

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.

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.

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)

Close the Entire "for" Loop and Return Result.

MinValue 2.png

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

Conclusion of Part One: Min Value Function

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

Part Two: Min Position Function

minPos 1.png

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

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.

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)

Close the Entire "for" Loop and Return the Current MinPos.

MinPos2.png

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

Conclusion of Part Two: Min Position Function

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

Part Three: Distance Between Min and Max Function

minPos and maxPos.png

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

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.​

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

Write an "Else" If Statement

ifElse.png

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

Initialize Distance As a Variable

Distance should be maxPos - minPos as an equation.

Return the Absolute Value of Distance

distance.png

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

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.

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.