Pascal to Java: Easy Transformation Part 2





Introduction

Hi, I'm back to write some tutorials. I hope that you really like the part 1 of this writing. As I mentioned in the first part, I won't teach OOP thingies here. All I tell you is a shortcut to make your Pascal programming behavior works in Java. Of course that later on you will learn the correct OOP.

I thought that I've covered the most part at the first article. However, it seemed that I missed out some important points, which are essentially FAQs for most migrators. I suggest you to first read through the first part of this article here. I expect that you understand the points covered there before continuing.

At this stage, it would be extremely useful if you have a Java API documentation as you would like to explore some more. It can be downloaded from Sun's site here.

Import Statement

Import statement in Java is just similar to Pascal uses clause. It will enable you to use certain libraries of Java. The philosophy is not exactly the same. However, for now, you can just consider it as the replacement of uses clause. Import statements must be placed as the first statements, i.e. before any class declarations. It is just like uses clause in Pascal, which has to reside within the first statements. For example:

import java.io.*;
import java.net.*;
:
:
// Then class declarations

These tell you that you import java.io and java.net packages.

File Reading or Writing

How could we read or write files in Java? Hm, the principle is more or less the same. Open the file, then read or write to it, then close it. This involves on importing libraries from java.io package. Here is a snippet to read a text file:

LineNumberReader f = new LineNumberReader(new FileReader("something.txt"));
do {
   String s = f.readLine();
} while (s != null);  // If the string is null, then it's EOF.

// Close when done.
f.close();

To write a text file:

PrintStream f = new PrintStream(new FileOutputStream("something.txt"));
f.println("Something to write");
f.close();

Get the idea? For binary files, look into java.io package, there are lots of libraries over there to try. Each offer different capabilities. Just pick which ever suits you. No, no, no, I won't tell you. Just try. It's just similarly done.

Command Line Arguments

Still remember our main function declaration?

public static void main(String[] args)

Well, the command line arguments are passed as function parameter. It's called args and it is defined as a one-dimensional array of String. Of course you can do args.length for paramcount equivalent. Then the parameters can be accessed using array indices just as you would in normal arrays.

Using Math Functions

There are two math libraries in Java. One is the default, which is commonly used. The other resides in java.math package. I just refer the default math library here. To access math libraries, you can invoke Math.someFunction(params); For example:

x = Math.abs(y);

That's similar to Pascal's abs. For the rest of the functions, it's just similarly done. Just view Java SDK API documentation. To do random numbers, do something like this:

// This will cause n have a random number from 0 to 99.
int n = (int) Math.round(Math.random()*100);

You don't need to worry about seeding the random number. It's automatically done by Java.

Pointers or References

Pointers? No, no, no! Java does not have pointers. How can we access references, such as in linked lists and so on? Well, there IS a way! Consider the following data structure:

class MyLinkedList {
   public String name;
   public int score;
   public MyLinkedList next;
}

Aha! You get the idea! Look at the next declaration. In Pascal, you would normally define the above structure as follows:

type
   PMyLinkedList = ^TMyLinkedList;
   TMyLinkedList = record
                     name  : string;
                     score : int;
                     next  : PMyLinkedList;
                   end;

Then, you access the next field with pointers. In Java, it's no longer necessary. You can access next as if it is a normal data structure (as long as you use new properly to initialize your data structure). Easy right? Now, double linked lists, tree, and so on can be easily done.

Actually, Java has builtin data structures such as LinkedList, Stack, and Hashtable which reside in java.util package. Go look inside Java SDK API documentation and try to learn some.

OOP Gimmicks

Finally for those of you that have already learnt Pascal's OOP. In Pascal, you have 3 access previleges: public, protected, and private. Java have them plus one more access previlege: package, i.e. when you are not specifying any access previleges. To learn more on how to make packages and/or its relevant articles, please refer to Sun's Java tutorial or just wait for my upcoming tutorials. :-) The difference here is that you have to specify the access previleges in front of each methods and fields.

The rest of OOP is just the same. To mention a method as an abstract, don't forget to add abstract keyword after access previlege keywords. Of course abstract methods are required to be public. If you have one or more abstract methods, you have to declare the class containing it as abstract too.

Essentials That You Should Know More

Conclusion

Hmm... it seems that I have covered all the essentials. You should now be able to at least try to transform your programs. Why don't you start on making fibonacci series using command line arguments to denote how many elements you should display? Then, slowly peek into Java's AWT or Swing commands after you get more familiar as it gives a pretty good look to your applications (and also awfully slows down your apps).

I realize that these two articles are far... faaar from perfect. If you have some comments or suggestions or simply request some more materials on this topic, please direct it to me. Good luck.


Where to go?

Back To Main
Transformation Part One
Pascal Tutorial Index

© Roby Joehanes 2001