Coding the sort

Maven and the IDE created the files for the sort program. They form the skeleton for our code, and now it is time to grow some muscles on them to let it move. We spent quite some time to set up the project by visiting the different build tools, only to learn how to compile the code. I hope that this did not distract you much, but anyhow, we deserve to see some real code.

First, we will create the code for the sorting code, and after that, the code that invokes the sorting. The code that invokes the sorting is a kind of testing code. For simplicity, we will now simply use a public static void main method to start the code. We will use the test framework in later chapters.

As for now, the code for the sorting will look like this:

package packt.java9.by.example.stringsort; 

public class Sort {

public void sort(String[] names) {
int n = names.length;
while (n > 1) {
for (int j = 0; j < n - 1; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
final String tmp = names[j + 1];
names[j + 1] = names[j];
names[j] = tmp;
}
}
n--;
}
}
}

This is the class that does the sorting. There is only one method in this class that does the sorting. The argument to the method is an array containing the strings, and the method sorts this array. The method has no return value. This is denoted in the declaration using the pseudo type void. Methods use their arguments to perform some tasks, and may return one value. The arguments to the method are passed by value, which means that the method cannot modify the variable passed as argument. However, it can modify the objects the arguments contain. In this case, the array is modified and we will sort it. On the other hand, the actualNames variable will point to the same array and the sort method cannot do anything to make this variable point to a different array.

There is no main method in this class, which means that it cannot be started from the command line on its own. This class can only be used from some other class, as every Java program should have a class that has a public static void main method that we created separately.

I could also put a main method into the class to make it executable, but that is not a good practice. Real programs are composed of many classes, and one class should not do many things. Rather, it's the opposite. The single responsibility principle says that a single class should be responsible for one single thing; therefore, class sort does the sorting. Executing the application is a different task, and thus it has to be implemented in a different class.

Often, we do not implement the class containing the main method. Often, a framework provides it. For example, writing a servlet that runs in a servlet container requires containing a class that implements the javax.servlet.Servlet interface. In this case, the program seemingly does not have a main method. The actual implementation of the servlet container does. The Java command line starts the container and the container loads the servlets when they are needed.

In the following example code, we implemented the App class containing the main method:

package packt.java9.by.example; 

import packt.java9.by.example.stringsort.Sort;

public class App {
public static void main(String[] args) {
String[] actualNames = new String[]{
"Johnson", "Wilson",
"Wilkinson", "Abraham", "Dagobert"
};
final Sort sorter = new Sort();
sorter.sort(actualNames);
for (final String name : actualNames) {
System.out.println(name);
}
}
}

This code contains a string array initialized to contain constant values, creates a new instance of the Sort class, invokes the sort method, and then prints out the code to the standard output.

In real programs, we almost never have such constants in program codes; we put them into resource files and have some code to read the actual values. This separates the code from data and eases maintenance, eliminating the risk of accidental modification of code structure when only the data is to be changed. Similarly, we will almost never write anything to standard output using System.out. Usually, we will use logging possibilities that are available from different sources. There are different libraries that provide logging functionalities and logging is also available from the JDK itself.

As for now, we will focus on simple solutions so as to not distract your focus from Java by the plethora of different libraries and tools. In the following section, we will look at the Java language constructs that we used to code the algorithm. First, we will look at them generally, and then, in a bit more detail. These language features are not independent of each other: one builds up on the other, and therefore, the explanation will first be general, and we will go into details in the subsections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.223