# Javagony/Printable version

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

# 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:
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

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

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.

## Comparing 2 integers to check which one is greater

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.
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

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:
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

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:
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:
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

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.
If we run the code now, we will see that it prints the following output:
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:
and if step=-1:
The second one will be a replacement of the following loop if step=1:
and if step=-1:
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

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.

## Do/while loop

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.

## While loop

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.
What if we want the function to return the most significant digit? Well, we can easily modify this code to do so.
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.

# Common programming problems in Javagony
## Fibonacci numbers

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

## Fizz buzz

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”:

## Prime numbers

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.

# 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

* Esolangs wiki entry
* Video by Truttle1
* A blackjack program written in Javagony by Truttle1
* A post about javagony in r/programminghorror
* Javagony algorithms by darenliang
* Brainfuck in Javagony by jammy-dodgers

# Authors & Contributors

This list is updated automatically by AuthorsAndContributorsBot. Please do not update it manually.
* Slava Ukraini Heroyam Slava 123 (discuss · contribs · count · logs · block log · rfp · rights [change])