Downloadable Files
NewProductJDialog.java |
Java is more than just a ‘hello world’ applet. David Balgarnie looks at Java’s Object Oriented design and how it can be used in today’s business environment.
If you still don’t believe the best things in life are free, take a look at Java. It has matured into the fastest growing language in the industry because developers love it. In this article, I want to provide a way into Java at a level experienced AS/400 developers will enjoy. I’ll look, in particular, at Java’s Object Oriented design and explore why this technical approach is suited to the demands of modern development schedules.
At the end there is a simplistic Java application that you might like to analyse and improve upon. There are no lengthy explanations of Java syntax and flow control. There are plenty of books and online resources where you can get this information and I’ve included details of the resources I’ve found most valuable.
There’s nothing wrong with RPG. It’s been a superb workhorse and is the backbone of some of the most reliable ERP, Banking, and Insurance applications around today. Many people, including myself, have made a good living from RPG over the years so we’re not going to start knocking it now.
On the AS/400, the original report program generator, RPG, evolved into a procedural language, then a visual language, and finally something approaching an object-oriented language. If your management is screaming for you to deliver a new application next week it will certainly be more practical to look at an RPG option rather than attempt to swallow the whole of Java in a few days. However, if you’re trying to develop web applications it’s likely that your solution will be forced to compromise in some way. For example, you may choose to use an RPG program to access the database then merge the data it finds with HTML tags and output the whole to an HTML document for publication on the web. This works – but database access, business logic and presentation are mixed together
It’s not ideal when the original developer has left and the marketing department wants to revamp the presentation of the web page. They’re frustrated to learn they’re waiting on the over-worked computer department even though there is no requirement to change business logic or database access.
Java is a major language of the web
Java’s young and it’s evolved with the Internet very much in mind. It provides much more than a solution for the AS/400 ‘green screen’ problem; it’s extremely portable and it will scale from personal digital assistants to the largest enterprise systems. The investment in Java will equip developers to program for the new millennium.
Architecture
Java offers a full range of architectural choices for a distributed computing environment. We can now envisage developing genuine ‘n-tier’ solutions where the presentation of data, the business logic (data processing) and the database itself are effectively detached from each other. In effect, we have three or more layers of software components and it is possible to swap out and replace a component in one layer of the system’s architecture without affecting the other layers. The Java beans technology enables these components to be ‘wrapped’ in an object that can be stored and manipulated from a visual development environment such as IBM’s Visual Age or Symantec’s Visual Café. These visual components can then be dragged from a repository and dropped into the appropriate slot in the system design. Any complexity is hidden (encapsulated) in the visual software components.
This ability to ‘wrap’ objects in other objects and so reuse functionality in a highly leveraged and elegant way is made possible by object oriented design. To grasp this concept in Java we need to understand the relationship between classes and objects.
Classes and Objects?
An object is a construct of a computer program and it exists in a computer’s memory.
The core Java API (Application Program Interface) consists of a hierarchy of classes. Everything in Java, except a few primitive data types such as integer, is a class. A class may be thought of as a template for creating an object. It is the blueprint used to create an instance of an object in the computer’s memory. Classes are basically made up of properties (variables) and methods (functions, like RPGIV procedures). The class is the unit of programming in Java. It is the equivalent of a module in RPGIV.
We can create our own classes and we can also create instances of core Java classes. We type the source for a class into a file bearing the same name as the class and with the .java suffix. Note that everything in Java is very case sensitive! We can then use the javac command to compile this source into the byte-code file that is the Java class.
Packages
The Java API is grouped into a series of packages. Examples are java.lang, java.util, javax.swing, java.security. When we create a Java class we need to import the appropriate Java packages or individual classes so that this part of the Java API is available to us. A package is a collection of classes. Note that the package name corresponds to a directory or folder name in the file store. It is this structure of classes within packages that provides the framework for one of the key criteria for object oriented development inheritance.
Inheritance
Inheritance is a powerful way to reuse functionality. You can inherit all the properties and behaviours of an existing class, except those that are specifically hidden. Inheritance is the thing that Java’s got and RPG hasn’t. Ultimately, each class in Java inherits from the ‘Big O’ object class. Code reuse is extremely efficient and if a bug is found, it should only be necessary to change one piece of source to fix all manifestations of the problem. This lends a robustness to object-oriented applications which is reinforced by the notion of encapsulation.
Encapsulation
Encapsulation means that complexity is hidden within an object. The object is effectively a ‘black box’. You can communicate with this object using its predefined set of methods. You may pass a method a list of arguments (parameters). The method may return a list of resulting arguments. Encapsulation provides enhanced security and data integrity. Access to data is restricted through the set of methods that are supplied in the object that encapsulates the data. Note that you can achieve encapsulation using careful design with RPG, and you can ‘break’ encapsulation through sloppy design in Java.
Polymorphism
This is the third major characteristic of object-oriented design. It describes the way an object can override a method that it inherits from its parent object. Imagine a parent class called ‘Shape’ with three sub classes, ‘Square’, ‘Circle’, and ‘Triangle’. The parent class has a method called [draw()]. The three sub classes override this method with their own implementations of it. In Java, we could take a list of ‘Shape’ objects which could be any mixture of squares, circles, and triangles, and when we invoked the [draw()] method, the correct shape would be drawn.
That’s polymorphism, and it concludes one of the shortest introductions to object-oriented design you’re likely to find. In fact this is a subject which has been researched for over 30 years by a lot of very clever people, so I do recommend you read one or two more books on the subject before you call yourself an expert.
I suggest you start experimenting on your own PC. You need a run-time environment where you can execute Java applications. You also need a Java development kit. This gives us access to all the classes in the core Java API and enables you to compile .java source files into Java classes. You can use any text editor such as Notepad to edit the .java source files. You can obtain a Java Standard Edition Development Kit and runtime environment for your PC at www.javasoft.com. Go to the section on Products and APIs and follow the instructions. While you’re at this site it’s worth registering as a Java developer because there are some very useful online tutorials on all the major Java topics. Like the development kit these are free. If you browse through the sections on the site you’ll find forums where various technical issues are raised and often answered.
By default, you’ll probably be offered version 1.3. This is a little bit ahead of the AS/400’s native JVM. However, if you’re just starting with Java I recommend that you start with this version, or at least 1.2, because the GUI capability is much stronger than earlier versions of Java, and the JDBC/SQL functionality is also better. At the time of writing IBM is aiming to be an early adopter of version 1.3 so it should be available on the AS/400 by the time you read this. Visit http://as400service.rochester.ibm to find out the latest release of Java that is available for the AS/400.
Once you’ve downloaded a run-time environment and development kit, you’ll want to add the appropriate folder to your PC’s default directory path.
I thought it would be interesting to include a small application that looks a bit like an AS/400 sub-file. This does not do justice to the Java 2 graphics capabilities, but it does provide an insight into the powerful Windows programming functionality provided by the Java Swing package, javax.swing.
Once you have the JDK loaded and you have got copies of the .java source files, you can compile the Product List Application. Go to an MS DOS command prompt and switch to the directory that contains the source files.
cd c:\thedirectory
Type the following command:
javac *.java
This will compile all the .java sources in the directory and produce Java classes. A class file corresponding to each source should be produced. In addition, some inner classes are also created.
Now enter the following command:
java ProductJFrame
You should see a table appear with three products on it. Play with the application, and see what you think. Try typing characters into the quantity field. What happens?
Let’s look at the four classes that make up the application.
|
ProductJFrame ProductModel NewProductJDialog Product |
The application presents a table to the user. The user has options to add new entries to this table or delete entries from it. No validations are included, and the application is not connected to any database.
This class provides encapsulation for a product object by defining its three fields and providing getXXX methods to identify individual elements when a product object is encountered.
The following diagram uses the Unified Modelling language (UML) format for displaying the class. The first box contains the class name, the next box, the class variables and their type, and the third box, the methods that the class provides.
|
ProductCode:String ProductDescription:String ProductOnHand:int |
|
Product() GetProductCode():String GetProductDescription():String GetProductOnHand():int |
This class inherits from javax.swing.table.AbstractTableModel. Using this approach we can detach the data itself, which is held in an array-like object known as a Vector, and the view of the data, which is a JTable object.
|
ChangeListeners:EventListenerList ChangeEvent:ChangeEvent ProductVector:Vector HeadingVector:Vector |
|
ProductModel() AddProduct() DeleteProduct() GetProductVector():Vector GetHeadingVector():Vector AddChangeListener() RemoveChangeListener() FireStateChanged() GetColumnCount():int GetRowCount():int GetValueAt():Object GetColumnName():String GetColumnClass():Class IsCellEditable():boolean LoadData() |
This class creates an instance of a JDialog class and sets up some labels and text fields for data entry. Note the use of 2 JPanel objects. The component labelPanel is added to the left-hand side of the JDialog and the component textFieldPanel is added to the right-hand side.
|
Boolean:okButtonPressed Product:JtextField Description:JtextField Quantity:JtextField |
|
IsOKButtonPressed():boolean GetProduct():String GetDescription():String GetQuantity():String |
This class contains the main method. This is the method that can be invoked from a command shell using the Java command.
|
ProductModel:ProductModel View1:Jtable |
|
CreateNewProduct() DeleteProduct() main() |
Well I hope this has given you the urge to write some of your own Java applications, particularly if you are an RPG or COBOL developer who has been frustrated in the attempt to provide end-to-end host-to-web software solutions in the timescales modern business demands. Java will require a big up front learning effort, but I feel sure the rewards will be worth it.
This article has only scratched the surface if the truth is known. I’d liked to have spent time on threads, Applets, Servlets, Java beans, Enterprise Java Beans, Remote Method Invocation, JINI, XML, and all sorts of related topics, but perhaps this is enough for one session.
Next time, we’ll look at attaching our application to a database using JDBC.
|
Online Resources | |
| http://www.javasoft.com | The home of Java. This is where you can get the development kit and runtime environment. It’s also where you access the documentation of the Java API online. |
| http://gamelan.earthweb.com | An excellent resource for Java developers. Downloads and discussions on Java topics. |
| http://alphaworks.ibm.com | This site provides access to IBM’s emerging technologies, and there’s a lot happening on the Java front. |
|
Bibliography | |
JAVA and the AS/400 Daniel Darnell | |
|
JAVA for RPG programmers Phil Coulthard and George Farr | |
|
UML in a Nutshell Sinan Si Alhir | |
|
Java in a Nutshell David Flanagan | |
|
Core Java Volume 1 - Fundamentals Cay S. Horstmann and Gary Cornell. | |
|
Java Foundation Classes In a Nutshell David Flanagan | |
David Balgarnie is an independent consultant.