JaVa
   

Creating Your Own Packages

Okay, so now you know how to include the standard packages. Let's look at how we can create our own packages. By creating your own packages, it is possible for you to create collections of reusable classes, which is excellent from a game programmer's point of view, as many algorithms can be packaged and reused in many projects. Let's now look at how to create a very simple mathematics package, which will contain two classes. One class will contain a static method for adding two integers, whereas the other class will contain a static method for subtracting two integers. To create a package, we must first store it in its own directory, which must be named the same as the package. In this case, we will call our package SimpleMaths and the directory will be:

C:\MyPackages\simplemaths


To make a class part of a package, we need to use the package keyword, followed by the name of the package that we wish to make it a part of at the top of each of the source files in the package. We will need to include the following line of code at the top of any source (.java) files we wish to make part of our simplemaths package. (Also note that we need to ensure the files are all contained within the simplemaths directory.)

package simplemaths;


NoteĀ 

It is convention to keep package names in lowercase.

Okay, now let's look at the source code for the two classes, Addition and Subtraction, we are going to make part of the package.

Code Listing 5-1: Addition.java
package simplemaths;
 public class Addition
{
 public static int add(int number1, int number2)
 {
 int result = number1 + number2;
 return result;
 }
}


Java End example
Code Listing 5-2: Subtraction.java
package simplemaths;
 public class Subtraction
{
 public static int substract(int number1, int number2)
 {
 int result = number1 - number2;
 return result;
 }
}


Java End example

As we mentioned before, both of these files should be placed within our simplemaths directory. The next step would be to compile the classes, which we can do by means of a batch file. Here is the listing of the batch file, which we have named compile.bat; it should be stored in the same directory as the source files.

Code Listing 5-3: compile.bat
C:\j2sdk1.4.1_01\bin\javac Addition.java C:\j2sdk1.4.1_01\bin\javac Subtraction.java pause


Java End example

Obviously, if you have installed the 1.4 SDK to a different directory, you would need to change the paths in the compile.bat file. When we execute the batch file, it will compile both the source files into the .class counterparts, which will then also be in the simplemaths directory. That is our created package. Let's now create a simple console app to test the package out. We will first look at the complete source for the test app and then go into more detail as to how we compiled it.

Code Listing 5-4: The test app (TestApp.java)
import simplemaths.*;
 public class TestApp
{
 public static void main(String args[])
 {
 int num1 = 10;
 int num2 = 20;
 int res = 0;
 res = Addition.add(num1, num2);
 System.out.println(num1 + " + " + num2 + " = " + res);
 res = Subtraction.substract(num2, num1);
 System.out.println(num2 + " - " + num1 + " = " + res);
 }
}


Java End example

When we run the example, the following output can be expected in the console.

Java Click To expand
Screenshot-1: Testing our own package

As you can see by using the import statement at the start of the code, we imported our own package simplemaths. This can be seen in the following line of code:

import simplemaths.*;


Note how we used the asterisk so both the Addition and Subtraction classes were included from the package. However, to include the package, we need to specify the classpath that the compiler should look in to find the package we are trying to include (as it will not be registered in the global environment). To do this, we use the -classpath parameter of both the compiler and interpreter when compiling and running the test app. The command line to compile the app can be seen here:

javac -classpath "c:\MyPackages" TestApp.java


For the above command to work, the simplemaths folder would need to be placed within the c:\MyPackages folder. To run the test app, you would use the following command:

java -classpath "c:\MyPackages" TestApp


JaVa
   
Comments