India Map Pattern in Java
In this section, we will create Java programs that print India map pattern using for loop, while loop, and using obfuscated code.
We can also print different design pattern just by modifying the following statement in the above program.
In the above program, replace the print statement with the above statement. After modifying the program, execute the code again. It shows the following India map pattern.
Let's change the value in following statement.
Let's put another value and see the pattern.
We observe that only by modifying the value in the above statement, we can print different India map patterns. Note that the value must lies between 32 to 39 or 42 to 49.
Using Obfuscated Code
The following code is an obfuscated code. Obfuscation is the deliberate act of creating source or machine code that is difficult for humans to understand. Writing and reading obfuscate source code is a brain teaser process.
In the following program, we have defined a String s that is unreadable. It represents the run-length encoding of the India map. The string handles spacing and character or symbol to print. It means that how many times space and symbol (that we want to print) will print on the console.
Notice [b+++21] at the end of the encoded string. As b+++21 is equivalent to (b++ + 21) which will evaluate to 31 (10 + 21), the first 31 characters of this string are ignored and does not contribute to anything. The remaining encoded string contains instructions for drawing the map. The individual characters determine how many spaces or exclamation marks to draw consecutively.
This loop goes over the characters in the string. Each iteration increases the value of b by one and assigns the next character in the string to a.
This loop draws individual characters and a newline whenever it reaches the end of the line. Consider the following print statement.
As 'Z' represents number 90 in ASCII, 90/9 will give us 10, a newline character. Decimal 33 is ASCII for '!'. Toggling the low-order bit of 33 gives you 32, which is ASCII for space. This causes! to be printed if b is odd and a blank space to be printed if b is even.
Using for Loop