Algorithms in Javagony


programming

The Javagony

First coined by flawr, Javagony is Java with a few statements made not available.

Here are the statements that are not available:

for (){}
if (){} else if (){} else {}
while (){}
do {} while ()
switch (){}
?:

These statements can be substituted for try catch statements and recursion.

Although Javagony is not the least bit fun to write, it makes you think about clever tricks that utilize many of Java’s obscure features.

Here is an example of counting to n in Javagony:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Loops {
    private static final int[] ints = {1, 0};
    private static int temp;

    private static void countToN(int n) {
        countToN(n, 1);
    }

    private static void countToN(int n, int i) {
        try {
            temp = 1 / ints[((n - i) >> 31) & 1];
        } catch (ArithmeticException e) {
            return;
        }
        try {
            temp = 1 / (n - i);
        } catch (ArithmeticException e) {
            System.out.println(n);
            return;
        }
        System.out.println(i);
        countToN(n, i + 1);
    }

    public static void main(String[] args) {
        countToN(10);
    }
}

Please note that there are multiple and equally creative ways to implement algorithms! The sky’s the limit!

The above example isn’t easily digestible, so I’ve created a GitHub repo that implements a handful of examples and goes through the process of implementing conditionals.

Try it yourself, you’ll love or hate it!

Visit JavagonyAlgorithms on GitHub

Comments

You can avoid authenticating giscus by commenting directly on the discussion page.