|
The Java Specialists' Newsletter
Issue 067 2003-03-31
Category:
Language
Java version: BASIC Javaby Dr. Heinz M. Kabutz
Welcome to the 67th edition of The Java(tm) Specialists' Newsletter, sent to 6299 Java
Specialists in 94 countries.
In the western world, we celebrate the 1st of April with jokes,
trying to catch out unsuspecting and gullible people. Last
year, I sent out a newsletter
in which I announced that from that day forth, you would have
to pay to become unsubscribed, and that the unsubscription fee
would automatically be deducted off your credit card.
Naturally, quite a few subscribers sent me emails such as:
"haha, look at the date". Others sent me outraged emails
telling me that they had not agreed to this and that I could not
do it. My wife Helene had warned me against putting in the joke, but being
a stubborn South African German, I naturally did not listen.
This year, I was going to repeat my folly, but having thought about it for a
while, have decided that with a hundred countries on my list, an April fool's
newsletter would only serve to confuse a significant number of readers.
HOWEVER, in this newsletter I have some lighthearted material on
how you can write BASIC code in Java. In the middle of March I
was in Germany visiting my grandmother for her 90th birthday,
and while I was there, managed to present a
Design Patterns
Course to a company in Frankfurt. Our friend
Carl Smotricz, who used to
host our newsletter on his website, was one of the 13 course participants.
While Carl was taking me to the train station after the second day of lectures,
Carl told me that it was possible to program GOTO in Java. Naturally
I was curious, so I asked Carl to give me an example. The theme sounded like
an ideal opportunity for an April 1st joke, so I will send this newsletter to
you with the warning that Carl and I were basically having fun
and seeing how far we could push the Java Programming Language
to look like BASIC.
Thanks for reading this newsletter on our website. We also have a mailing list. That is where the real action takes place (webinars, free reports, etc.). Maybe subscribe today?
Advanced Java Courses on Crete:Java Specialists Master Course 18-21 June 2013 and
Concurrency Specialists Course 6-9 August 2013.
BASIC Java
Carl and I have something in common. Both of us started programming in BASIC.
BASIC stands for Beginners All-purpose Symbolic
Instruction Code and is the easiest language in the world to
understand. It is not surprising that Visual BASIC is so popular.
We even have plans to produce a Design Patterns Course for Visual Basic .NET
(the Delphi version is already underway - and that is not an April Fools joke ;-)
BASIC has the following advantages over Java:
- BASIC is for beginners. Most programmers in
the world today have less than 10 years experience and therefore
should be considered as beginners. Java is definitely not for
beginners. It is for expert programmers. It gives you more
ways of shooting yourself in the foot than an AK47. (I do not
consider VB.NET to be BASIC, but rather just another flavour
of Java.)
- BASIC allows global variables.
Global variables reduce the number of instances of variables
needed, they reduce the namespace. Global variables make it
easy to write a program, although maintenance becomes
almost impossible. Do not you believe me that global variables
make coding easier? Think of a Design Pattern. Think of any
Design Pattern. Any one. Name it out
loud, and then please go to the end of this email and look at the P.S.
- BASIC supports the GOTO statement. GOTO is perhaps
not the darling of
Edsger W.
Dijkstra but it does allow us to write interesting
succinct code, which certainly ensures that we will be employed
in a maintenance role for many years to come.
- BASIC is widely understood and supported. Say
"goodbye" to expensive finder fees from recruitment agencies.
Hire one of thousands of BASIC programmers and pay them very
little. Or, since BASIC is for beginners, take someone straight
out of arts to do the programming for you.
Consider the following BASIC program. Don't just
skip over it, try to understand what is happening. If you
read it carefully, you will see that it constructs a list of
10 numbers, then sorts them using the Bubble Sort algorithm.
(Thanks Carl for this piece of code.)
10 DIM N(10):RANDOMIZE TIMER
20 I = 0
30 N(I) = INT(1000 * RND)
40 I = I + 1: IF I < 10 GOTO 30
50 S% = 1 : I = 0
60 IF N(I) < N(I + 1) GOTO 80
70 T = N(I) : N(I) = N(I + 1) : N(I + 1) = T : S% = 0
80 I = I + 1 : IF I < 9 GOTO 60
90 IF S% = 0 GOTO 50
100 I = 0
110 PRINT N(I)
120 I = I + 1 : IF I < 10 GOTO 110
130 STOP
When we run this with Microsoft QuickBasic 3.0, we see something
like:
91
100
460
589
618
622
650
730
888
918
If we wanted to convert this to Java, we could either hire an expensive
Java programmer (who probably would not understand the above code), or we
could write a tool to automatically translate the above code to "Java".
Remember the GIGO principle - Garbage in - Garbage out.
Is it possible to translate this to Java? Yessir!
public class Bubble extends Basic {
// Original Code:
// 10 DIM N(10):RANDOMIZE TIMER
// 20 I = 0
// 30 N(I) = INT(1000 * RND)
// 40 I = I + 1: IF I < 10 GOTO 30
// 50 S% = 1 : I = 0
// 60 IF N(I) < N(I + 1) GOTO 80
// 70 T = N(I) : N(I) = N(I + 1) : N(I + 1) = T : S% = 0
// 80 I = I + 1 : IF I < 9 GOTO 60
// 90 IF S% = 0 GOTO 50
// 100 I = 0
// 110 PRINT N(I)
// 120 I = I + 1 : IF I < 10 GOTO 110
public static void main(String[] args) {
int I=0;int S=0;int N[] = null;
while(jump != -1) {
try {
switch(jump) {
case 10: N = DIM(10);
case 20: I = 0;
case 30: N[I] = INT(1000 * RND());
case 40: I = I + 1; if (I < 10) GOTO (30);
case 50: S = 1; I = 0;
case 60: if( N[I] < N[I + 1]) GOTO (80);
case 70: int T = N[I] ; N[I] = N[I + 1] ; N[I + 1] = T ; S = 0;
case 80: I = I + 1 ; if (I < 9) GOTO (60);
case 90: if (S == 0) GOTO (50);
case 100: I = 0;
case 110: PRINT (N[I]);
case 120: I = I + 1; if (I < 10) GOTO (110);
case 130: STOP();
}
// if there was no GOTO then we want to end the program
STOP();
} catch(GotoException ex) {
// GOTO was called, and a GotoException has caused the
// control to pass outside of the switch statement
}
}
}
}
The output would be something along the lines of:
49
102
179
212
351
425
488
877
893
909
What does the code for Basic.java look like?
public class Basic {
// GotoException is thrown by GOTO to avoid having to call
// "break" after the call to GOTO in the switch statement.
// I warned you before that Exceptions were dangerous ;-)
protected static final class GotoException
extends RuntimeException {
private GotoException() {}
}
private static final GotoException gotoEx =
new GotoException();
public static int jump=10;
public static void GOTO(int line) {
jump = line;
throw gotoEx;
}
// STOP changes the current line to be -1, which ends the
// program
public static void STOP() {
GOTO (-1);
}
public static void PRINT(String s) {
System.out.println(s);
}
public static void PRINT(int i) {
System.out.println(i);
}
public static int[] DIM(int n) {
return new int[n];
}
public static double RND() {
return Math.random();
}
public static int INT(double d) {
return (int)d;
}
}
What makes me scared is that the code runs and actually works.
Perhaps one day Carl and I will write a tool to automatically
convert legacy BASIC code to Java ......
Kind regards, and don't get caught out tomorrow!
Heinz
P.S. You probably thought of the Singleton? The Singleton's
intent is to provide a global access point to an object. Is
that not a global variable?
Language Articles
Related Java Course
Discuss at The Java Specialist Club
|