## Examples with arrays, methods division and comments

The following examples are beginning to look like the code to be found in professional applications. Worth mentioning the use of comments and the division in methods of the code.

## Code comments

The comments in the code are required in any serious project. At least, at the beginning of classes, interfaces and methods.

In Eclipse, we can generate comments by placing the cursor at the beginning of the class declaration and pressing <alt><shift><j>.

There are tools that automatically generate documentation from these comments.

It is also advisable to comment between lines of code. If you choose descriptive names for variables and methods, it can replace the use of comments.

## Division of the code in methods

The division of the program into procedures or methods is an obliged technique. It is important that each method encapsulates a specific task. If it is possible, it is also important that the method hides the complexity from users.

This makes programming easier and it increases quality.

## Identifier names in English

When we program it is desirable to write the source code in English. Many characters in other languages are confusing for the development tools. It is also likely that our code is read by people who do not speak our language and like it or not, the common computer language is English.

In general, I also recommend that the comments are written in English. But, if for example, the public using the code does not speak English we could make an exception.

## Simple example with arrays, maximum, average and minimum

The following program raffles ten numbers at random from 0 to 99, prints them and calculates the average, maximum and minimum.

package com.edu4java.javatutorials; import java.util.Random; /** * @author edu4java * * Example of the for statement for array manipulation */ public class ForArray { public static void main(String[] args) { int[] numbers = generateNumbers(); // prints raffled numbers print2Console("Raffled numbers: ", numbers); // prints the average System.out.println("Average:" + average(numbers)); // prints the maximum System.out.println("Maximum:" + max(numbers)); // prints the minimum System.out.println("Minimum:" + min(numbers)); } /** * generates numbers at random * * @return generated numbers */ private static int[] generateNumbers() { Random random = new Random(); int[] numbers = new int[10]; // raffles 10 numbers from 0 to 99 for (int i = 0; i < numbers.length; i++) { numbers[i] = random.nextInt(100); } return numbers; } /** * Prints on console the message followed by the numbers from the array * numbers separated by a space * * @param message * The message to be shown * @param numbers * The numbers to be printed */ private static void print2Console(String message, int[] numbers) { System.out.print(message); for (int number : numbers) { System.out.print(number + " "); } System.out.println(); } /** * returns the number with the highest value * * @param numbers * The numbers where to look for the highest value * @return The maximum */ private static int max(int[] numbers) { int max = 0; for (int number : numbers) { if (max < number) { max = number; } } return max; } /** * returns the number with the lowest value * * @param numbers * The numbers where to look for the lowest value * @return The minimum */ private static int min(int[] numbers) { int min = Integer.MAX_VALUE; for (int number : numbers) { if (min > number) { min = number; } } return min; } /** * calculates the average of the numbers * * @param numbers * Numbers to average * @return The average */ private static int average(int[] numbers) { int accumulator = 0; for (int number : numbers) { accumulator = accumulator + number; } int average = accumulator / 10; return average; } }

In the example we can see that the method *generateNumbers*(), is able to hide how the numbers are raffled from the rest of the program, including the use of the Random object. In the main method we can see how *generateNumbers*() magically returns an array with the numbers already raffled.

Using the same technique, we have created methods to print to console and to calculate the average, the maximum and the minimum numbers.

## A more difficult example with arrays: ordering numbers

In this example, we use two nested *for* statements to order a list of raffled numbers. First, the console prints the list of raffled numbers and then the same list but ordered from lowest to highest.

package com.edu4java.javatutorials; import java.util.Random; public class ForArray2 { public static void main(String[] args) { int[] numbers = generateNumbers(); print2Console("Raffled numbers: ", numbers); numbers = sort(numbers); print2Console("Ordered numbers: ", numbers); } /** * orders an array of numbers from lowest to highest * * @param numbers * Numbers to order * @return ordered numbers */ private static int[] sort(int[] numbers) { for (int i = 0; i < numbers.length - 1; i++) { for (int j = i + 1; j < numbers.length; j++) { if (numbers[i] > numbers[j]) { int auxiliary = numbers[i]; numbers[i] = numbers[j]; numbers[j] = auxiliary; } } } return numbers; } /** * generates 10 numbers at random * * @return An array of 10 integers */ private static int[] generateNumbers() { Random random = new Random(); int[] numbers = new int[10]; // raffles 10 numbers from 0 to 99 for (int i = 0; i < numbers.length; i++) { numbers[i] = random.nextInt(100); } return numbers; } /** * Prints on console the message followed by the numbers from the array * numbers separated by a space * * @param message * The message to be shown * @param numbers * The numbers to be printed */ private static void print2Console(String message, int[] numbers) { System.out.print(message); for (int number : numbers) { System.out.print(number + " "); } System.out.println(); } }

In this example the method *print2Console()* not only divides the program but avoids repeating code. We reuse code when we use it to print both the unordered and the ordered list.

Let´s take a look at the code carefully:

## Algorithm used to order the list

The idea to order is, take the first number and compare it with each of the following numbers to see if there is any other number with a lower value. Every time I find one with a lower value I exchange their position so that the one with lower value is first. At the end of the process you have the lower number of the array in the first position.

To order the second number, repeat the comparing process with the numbers which follow in the array. When finished with the second last I can be sure that the list is ordered.

for (int i = 0; i < numbers.length - 1; i++) {

for (int j = i + 1; j < numbers.length; j++) {

if (numbers[i] > numbers[j]) {

int auxiliary = numbers[i];

numbers[i] = numbers[j];

numbers[j] = auxiliary;

}

}

}

The first *for* with index *i, *ranges from 0-8, which is the penultimate position of the array. The second *for* has index *j*. For each repetition of* i, *the index *j* will range from the value i +1 to 9, which is the last position of the array.

For each iteration of *j*, we see if the value in the position *i *is higher than in the position *j*. If it is higher we exchange them so that at the end we have the lowest value in the position* i.*

## Exchange the values of two variables

When we want to exchange the content of two variables we use a third variable.

E.g.: we exchange *a* and *b*;

c=a;

a=b;

b=c;

This is what we do in the following code:

int auxiliary = numbers[i]; numbers[i] = numbers[j]; numbers[j] = auxiliary;