Archive for January, 2011

My Adventure Writing My First Quine in Java

Today, I tried to write my very first quine. What is a quine, you might ask? Well, for those that don’t know, a quine is simply a program that takes no input but whose only output is the program code itself. Easy, right? That’s what I thought. This post is going to document my adventure writing my first quine in Java.

 

Trial and error

So, I need to write a program that outputs a program…itself to be exact. I didn’t think much of it, so I dove right in. Let’s take a look at my first pass…

public class Quine {
	public static void main(String[] args) {
		System.out.println("public class Quine {\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"...fuck");
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		System.out.println("...fuck
 
*/

Fuck. This won’t work. As you can see, the problem quickly reveals itself. Essentially, there comes a point where you must print the code that you are using to print! (Even that sentence is a little confusing) This can easily degrade into an infinite loop. So, how do you overcome this?

You must write some code that you use to both store the characters
to be printed, but can ALSO be used to print itself!

I believe this is the main problem to solve when writing quines. Once you can get over this hump, the rest is simple! If you can figure this out, you’ve done 90% of the work. Really! So I started to tackle this problem, head on.

 

A string that prints itself

First, I did away with the System.out.println() method. Instead, I opted for the printf() method. With functionality very similar to the ANSII C printf() method, it has some powerful string manipulation features that will make this quine much easier to write (and read). Second, I tried to think of a way to write some code that printed itself. More specifically, I decided that I needed to figure out a way to solve the following code segment…

String s = "?";
System.out.printf(?);

What could I replace the question marks with such that it would output the string declaration itself, exactly, and nothing else? I thought about this for a while before I came up with a solution. Since I think that this step is the most important and requires the most thinking, rather than ruin it for you, I’ll let you think about it for yourself.

 

*SPOILER ALERT – ANSWER BELOW*


 

 

 

 
Willard Van Orman Quine - A Harvard Professor of philosophy for which the self-reproducing code concept was named after

Willard Van Orman Quine - A Harvard professor of philosophy for which the self-reproducing code concept was named after



 

 

 

 

After some head-scratching, I came up with…

public class Quine {
	public static void main(String[] args) {
		String s = "String s = %c%s%c;";
		System.out.printf(s, '"', s, '"');
	}
}
 
/* OUTPUT:
String s = "String s = %c%s%c;";
*/

There are many other solutions for this, but this is what I came up with. Now that I’ve got this figured out, all that’s left is filling in the stuff before and after.

 

All that’s left is everything

The self-reproducing code that I wrote above is essentially the middle of the program, and I’ve figured out a way to print it. Now, on to printing the stuff that goes before. This is quite a bit simpler than the first step, but not quite trivial. Again, here is another failed attempt…

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {\n\tpublic static void main(String[] args) {\n\t\tString s = %c%s%c;";
		System.out.printf(s, '"', s, '"');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {
	public static void main(String[] args) {
		String s = %c%s%c;";
*/

You’ll notice that hardcoding the line-feeds and horizontal tabs into the string doesn’t give us the right result. They get evaluated by the printf() twice. The solution to this is to make use of the variable arguments in printf() and reference those characters instead.

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%c%cpublic static void main(String[] args) {%c%c%cString s = %c%s%c;";
		System.out.printf(s, '\n', '\t', '\n', '\t', '\t', '"', s, '"');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%c%cpublic static void main(String[] args) {%c%c%cString s = %c%s%c;";
*/

Nice! Things are coming along, but the code is getting uglier.

 

Spring cleaning!

As you can see, we’re starting to get a lot of redundancy in the printf() call. My next step was to clean this up. Quine’s are all about reuse! The less code there is, the less code I have to worry about re-printing. So, I changed all of the arguments to printf() to be index-based instead, allowing me to reuse characters.

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;";
		System.out.printf(s, s, '"', '\n', '\t');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;";
*/

This is looking a lot cleaner now. Finally, let’s round this quine off by filling in the rest of the code.

 

Print the rest...easy! I think.

Again, this is quite a bit simpler than the previous steps, but not trivial. Another failed attempt…

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, ...fuck";
		System.out.printf(s, s, '"', '\n', '\t');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, ...fuck";
		System.out.printf(s, s, ...fuck
*/

We have hit the familiar problem of printing code that is used to print itself! This is slightly different, however. The difference is that we are trying to output characters that must be escaped in one output, but not in the other. There is a simple solution for this! Use the ASCII character codes instead! Replacing ‘”‘, ‘\n’, and ‘\t’ with their ASCII decimal equivalent codes, we are easily able to insert them into the string literal without them being interpreted by the printf() method, and without the need for escaping them. In the end, we get….

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, 34, 10, 9);%3$c%4$c}%3$c}";
		System.out.printf(s, s, 34, 10, 9);
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, 34, 10, 9);%3$c%4$c}%3$c}";
		System.out.printf(s, s, 34, 10, 9);
	}
}
*/

Eureka! My very first quine! Try writing one for yourself! Feel free to share them with me in the comments, or through e-mail at charlesb[at]adobe[dot]com! I’d love to post them :)

Enjoy!

 
Charles

Introducing “Facebook Desktop” – A Lightweight, Unobtrusive Notification Engine for Facebook Built on Adobe AIR

Disclaimer: Facebook Desktop is a personal project. It is not sponsored by or affiliated with either Adobe® or Facebook®.

Once upon a time, I was involved in a project here at Adobe called “Project San Dimas“. That project eventually grew into a full-fledged product called “eBay Desktop” which enjoyed some impressive success. I was lucky enough to be part of the protyping, designing, and launching of the very first version, and it was very exciting. More recently, I’ve thought about doing something similar. Introducing, “Facebook Desktop“!

So, what is Facebook Desktop?
Facebook Desktop is a notification engine that gives you updates about your Facebook friends in real-time! Whenever someone posts a story, comments on your photo, tags you in a status-update, whatever…Facebook Desktop will let you know. It gives unobtrusive messenger-style toast messages whenever you have a notification.

Why build Facebook Desktop?
I moved away from my home in Vancouver for work and so Facebook has been a great tool to help keep me in touch with everyone. I found, though, that the browser metaphor for the Facebook “stream” just wasn’t working for me. I didn’t like refreshing my home page constantly to see what people were up to. I also didn’t like keeping a browser open whenever I wanted to read some of my friends posts. So, I made a simple AIR app that hits the Facebook APIs, grabs the latest posts, and delivers them to me in an unobtrusive way. I ended up finding it so useful, I started adding more and more features, like the ability to pause/resume, replay missed notifications, update my status, etc. Eventually, I was encouraged by a friend to polish it up and release it for others to use, and so here we are!

Anyways, if you’re interested in trying it out, install it here:

http://www.facebookdesktop.com/

You can also keep up with the project and reach out to me on any of our community pages:

And definitely, let me know what you think! Your feedback helps me make Facebook Desktop better and better. Until next time, happy Facebook’ing ;)

 
Charles