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