Javagony/Printable version

From Wikibooks, open books for an open world
Jump to navigation Jump to search


Javagony

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Javagony

Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.

Preface

Javagony (Java + Agony) is an Object-Oriented Esoteric Programming language or a programming challenge using the Java programming language. You can use any Java IDE, but notice that the following statements are NOT allowed:

for (){}
if (){} //including else and else if
while (){}
do {} while ();
switch(){}
?:

The purpose of this book is to teach how to write code that does regular Java stuff despite the restrictions.



Conditions: Comparing 2 numbers

Introduction[edit | edit source]

As noted in the preface, Javagony does not have if/else statements. However it’s possible to use a try/catch statement instead by putting the code in the try block and causing an error if the condition is met. If an error happens in the try block, the code will move to the catch block. Otherwise, it will continue running in the try block. Therefore we can consider the catch block the if block and the try block (everything that happens after the first line) the else block. Just make sure your code doesn’t generate an error in the try block after the first line, and if you are writing unsafe code (like loading from a file), consider putting a try/catch statement inside the try.

Let’s start with the easiest one, checking if 2 numbers are equal to each other.

Checking if 2 numbers are equal to each other[edit | edit source]

As you probably already know by now, if you subtract two numbers that are equal to each other you get zero. Elementary school Mathematics teaches us that it’s impossible to divide a number by zero, and if you try to do so in java it will cause an error. The following code defines 2 variables x and y. If want to you can ask the user to enter them. We will keep thing simple for this example, and their values will be written in the code.

public class Javagony
{
   public static void main(String[] args)
   {
      int x=5;
      int y=5;
      
      try
      {
         int temp=1/(x-y);
         
         System.out.println("x =/= y");
      }
      catch(Exception IO)
      {
         System.out.println("x == y");
      }
   }
}

Comparing 2 integers to check which one is greater[edit | edit source]

Assume x and y are both integer variables, and that their values have been defined in the program. To check which integer is larger x or y we will use the method Math.addExact(x,Integet.MAX_VALUE-y). The largest number that can be stored in an integer variable is 2,147,483,647. If the result of a normal addition exceeds this value, it will round down. However, if the method Math.addExact is used, it will throw an exception. This behavior can be used to our advantage.

public class Javagony
{
   public static void main(String[] args)
   {
      int x=8;
      int y=5;
      
      try
      {
         int temp=Math.addExact(x,Integer.MAX_VALUE-y);
         
         System.out.println("x <= y");
      }
      catch(Exception IO)
      {
         System.out.println("x > y");
      }
   }
}

If we want to check if y is greater than x, and not if y is greater or equal to x we can easily swap them in line 10.


For loop

Introduction[edit | edit source]

As noted in the preface, Javagony does not allow any loops. In this chapter you will learn the alternative for a for loop in Javagony. We will be creating a recursive function that will provide the functionality of the following for loop:

for(int i=0;i<5;i+=1)
{
   //code
}

You could write i++, however I have written i+=1 on purpose to demonstrate that you can write also i+=2 or any other number you want. We will be using recursion as well as knowledge from the previous chapter to emulate the functionality of this loop.

Implementation[edit | edit source]

public class For
{
   public static void main(String[] args)
   {
      forFunction(0,10,1);
   }
   
   public static void forFunction(int firstNum,int lastNum,int step)
   {
      System.out.println(firstNum); //replace with the code you want to run
      
      firstNum+=step;
      
      try
      {
         int temp=Math.addExact(firstNum+1,Integer.MAX_VALUE-lastNum);
         forFunction(firstNum,lastNum,step);
      }
      catch(Exception IO) {}
   }
}

Notice that we have reused code from the previous chapter, Comparing 2 integers to check which one is greater.

This function gets three values: firstNum, lastNum, and step. For this section, assume that step is a positive number. We will discuss negative values of step in the next section of this chapter. This code in regular java can be written as following:

for(int i=0;i<10;i++)
{
   System.out.println(i);
}

First the function prints the value firstNum. This is in order to show that it iterates over all the numbers that a regular for loop would. You can replace it with the code you want running. Then we increase firstNum by step, and use the knowledge we gained in the previous chapter to check if firstNum is lower than lastNum.

Notice that I wrote firstNum+1 instead of just firstNum. This is because I want firstNum to be less that lastNum, but this method checks if firstNum is less or equal to lastNum. If firstNum+1<=lastNum then we can conclude that firstNum<lastNum. In this code we are only working with integer numbers. Omitting the +1 will result an Javagony version of the following regular Java code:

for(int i=0;i<=10;i++)
{
   System.out.println(i);
}

If firstNum (which at this point contains the value of the next number) is less than lastNum, then we proceed to the next iteration.

If step<0[edit | edit source]

Let’s now change the values given to the function in the main method to 0,-10,-1 and observe how the code will work.

   public static void main(String[] args)
   {
      forFunction(0,-10,-1)
   }

If we run the code now, we will see that it prints the following output:

0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10

But this is not what we want. We don’t want the code to iterate over the last value. Let’s check how the code runs to see how to fix it.

The code stars with the variable firstValue as 0 and prints it. Then it increases the variable by -1, which is the same as subtracting 1 from it and we get -1.

Now, we get to the comparing. The value of lastNum (in this case -10) is subtracted from the maximum value that is possible to store in an int. This is the same as adding 10. One would expect this to raise an error, but instead an integer overflow will occur, and we will get a negative number.

Then, using the method forFunc which does not allow for integer overflow we add the value of firstNum+1 (in our case the result is 0) to the large negative number, and if an integer overflow does not occur, we proceed to the next iteration.

What will happen after the code prints -9? Well, it will change the value of firstNum to -10, and then check if -9 plus this large negative number will result in an integer overflow. However, it does not and goes one to iterate on i=-10.

How to fix it? Well, we can fix it by omitting the +1, but then we will have the same trouble with step>0. We can have two functions, one that omits the +1, and one that doesn’t.

The first one will be a replacement of the following loop if step=1:

for(int i=0;i<=5;i++)
{
   System.out.println(i);
}

and if step=-1:

for(int i=0;i>-5;i-)
{
   System.out.println(i);
}

The second one will be a replacement of the following loop if step=1:

for(int i=0;i<5;i++)
{
   System.out.println(i);
}

and if step=-1:

for(int i=0;i>=-5;i-)
{
   System.out.println(i);
}

Now, you have got all the options covered, you just need to figure out which one is right for you in the next production app you write, since from now on you will write codes only in Javagony. Not really.


Other conditions and loops

Other conditions[edit | edit source]

We know how to write conditions in Javagony that compare two numbers. What if we want to check for other conditions? Well, this can be done using a Boolean variable, and the function Boolean.compare(b1,true) when b1 is the variable that stores the result of the condition (true or false). It will return 0 if the Boolean variable is true, and -1 if it’s false. She because we already know how to check in Javagony if a number is 0, that means we can check any condition.

public class More
{
   public static void main(String[] args)
   {
      boolean b1=7.5/5>1; //replace with another condition
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
         
         System.out.println("Condition is false");
      }
      catch(Exception IO)
      {
         System.out.println("Condition is true");
      }
   }
}

Do/while loop[edit | edit source]

Now that we know how to check if any condition is true, it’s easy to implement an alternative to a do/while loop in Javagony. All we need to do is to create a function that does something and then calls to itself if the certain condition is met.

For the purpose of this book, we will write a code that prints all powers of 3, until we reach a number that its least significant digit is 7. The last number it will print will be 27.

public class More
{
   public static void main(String[] args)
   {
      loop(1);
   }
   
   public static void loop(int n)
   {
      System.out.println(n);
      boolean b1=n%10==7;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
         
         loop(n*3);
      }
      catch(Exception IO)
      {
         
      }
   }
}

While loop[edit | edit source]

A while loop will work similar to a do/while, only this time we check for the condition at the beginning of the function and not the end. If the condition is met, we execute the code. If it isn’t, we use the return statement to exit from the function. At the end of the function, after executing the code, it will unconditionally call itself.

Let’s use this knowledge to write a code that receives a positive integer, and prints all the digits of the number - starting from the least significant one, and ending with the most significant.

public class More
{
   public static void main(String[] args)
   {
      loop(1234567890);
   }
   
   public static void loop(int n)
   {
      boolean b1=n<1;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
      }
      catch(Exception IO)
      {
         return;
      }
      
      System.out.println(n%10);
      loop(n/10);
   }
}

What if we want the function to return the most significant digit? Well, we can easily modify this code to do so.

public class More
{
   public static void main(String[] args)
   {
      System.out.println(loop(45454365));
   }
   
   public static int loop(int n)
   {
      boolean b1=n<10;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
      }
      catch(Exception IO)
      {
         return n;
      }
      
      return loop(n/10);
   }
}

Let’s modify this code to return an ArrayList that will include all the digits of the number starting from the most significant and ending with the least significant one.

import java.util.ArrayList;

public class More
{
   public static void main(String[] args)
   {
      System.out.println(loop(3874897));
   }
   
   public static ArrayList<Integer> loop(int n)
   {
      boolean b1=n<10;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
      }
      catch(Exception IO)
      {
         ArrayList<Integer> a=new ArrayList<Integer>();
         a.add(n);
         return a;
      }
      
      ArrayList<Integer> a=loop(n/10);
      a.add(n%10);
      return a;
   }
}


Common programming problems in Javagony

Fibonacci numbers[edit | edit source]

This is a recursive method in Javagony that returns the number in position n of the Fibonacci Sequence.

public class Fibonacci
{
   public static void main(String[] args)
   {
      System.out.println(fibo(10));
   }
   
   public static int fibo(int n)
   {
      try
      {
         int temp=1/n;
      }
      catch(Exception IO)
      {
         return 0;
      }
      
      try
      {
         int temp=1/(n-1);
      }
      catch(Exception IO)
      {
         return 1;
      }
      
      return fibo(n-1)+fibo(n-2);
   }
}

Fizz buzz[edit | edit source]

This is a code in Javagony that prints all the numbers from 1 to 100, but instead of numbers that can be divided by 3, it will print “Fizz”, instead of numbers that can be divided by 5, it will print “Buzz”, and instead of numbers that can be divided by both 3 and 5, it will print “Fizz Buzz”:

public class Fizzbuzz
{
   public static void main(String[] args)
   {
      forFunction(1,101,1);
   }
   
   public static void forFunction(int firstNum,int lastNum,int step)
   {
      boolean fizz=firstNum%3==0;
      boolean buzz=firstNum%5==0;
      boolean fizzBuzz=fizz&&buzz;
      
      try
      {
         int temp=Boolean.compare(fizzBuzz,true);
         temp=1/temp;
         
         try
         {
            temp=Boolean.compare(fizz,true);
            temp=1/temp;
            
            try
            {
               temp=Boolean.compare(buzz,true);
               temp=1/temp;
               
               System.out.println(firstNum);
            }
            catch(Exception IO)
            {
               System.out.println("Buzz");
            }
         }
         catch(Exception IO)
         {
            System.out.println("Fizz");
         }
      }
      catch(Exception IO)
      {
         System.out.println("Fizz Buzz");
      }
      
      firstNum+=step;
      
      try
      {
         int temp=Math.addExact(firstNum+1,Integer.MAX_VALUE-lastNum);
         forFunction(firstNum,lastNum,step);
      }
      catch(Exception IO) {}
   }
}

Prime numbers[edit | edit source]

This is a method in Javagony that gets a number and returns the boolean value true if it’s a prime number. Otherwise, it returns the boolean value false.

public class PrimeNumber
{
   public static void main(String[] args)
   {
      System.out.println(prime(7));
      System.out.println(prime(8));
   }
   
   public static boolean prime(int n)
   {
      try
      {
         int temp=1/(n-1);
      }
      catch(Exception IO)
      {
         return false;
      }
      
      return forFunction(2,n,1);
   }
   
   public static boolean forFunction(int firstNum,int lastNum,int step)
   {
      boolean b=lastNum%firstNum==0&&lastNum!=2;
      try
      {
         int temp=Boolean.compare(b,true);
         temp=1/temp;
      }
      catch(Exception IO)
      {
         return false;
      }
      
      firstNum+=step;
      
      try
      {
         int temp=Math.addExact(firstNum+1,Integer.MAX_VALUE-lastNum);
         return forFunction(firstNum,lastNum,step);
      }
      catch(Exception IO)
      {
         return true;
      }
   }
}


Conclusions

In this book, you have learned how to write programs in Javagony that manage to do regular Java tasks despite the limitation of the programming language. Please do not use Javagony for serious projects as it will result in poor performance as well as a messy code. However, you can use Javagony to challenge yourself.


External links

  1. Esolangs wiki entry
  2. Video by Truttle1
  3. A blackjack program written in Javagony by Truttle1
  4. A post about javagony in r/programminghorror
  5. Javagony algorithms by darenliang
  6. Brainfuck in Javagony by jammy-dodgers


Authors & Contributors

This list is updated automatically by AuthorsAndContributorsBot. Please do not update it manually.

  1. Slava Ukraini Heroyam Slava 123 (discuss · contribs · count · logs · block log · rfp · rights [change])