A Java Program that Displays Itself

By Tim Farage

April, 2005

 

Did you ever wonder if a program could be written that, when executed, would display itself exactly?  If file manipulations were allowed it would be fairly easy.  You'd just write a Java program that, when executed, would open its source code file and then display the lines of that file.  You might want to try this.  It's not too hard and it's fun.

But what about a Java program that displays itself without using any file manipulations?  That's harder to do, and unless you really think about it, it's tough to come up with the right idea.  If you like a challenge, give it a shot.  There's a hint below just in case.  If you think you've got it, be sure to copy the output to a .java file, and test it. (This can be done in C/C++ as well).  If your program is different than mine, email it to me.

DNA must have some properties similar to this program.  After all, it has enough information to create a human body, including the DNA of that body.  How does it do this?  Sorry, never said I was a biologist.

 

***************************************

 

If you'd like a hint, have your program create an array of Strings that contain most of the lines of the program itself.  It can't contain all of the lines, because the array of Strings is part of the program!

For my version of the program, click here.

 

Tim Farage can be emailed at tfarage@hotmail.com.  A mini-biography is available at http://www.utdallas.edu/~tfarage/bio.html.