Moving on from Hello World to Hello Caractacus

A “Hello world” program is a computer program that outputs “Hello, world” on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language. ~ [Wikipedia](wikipedia.org/wiki"Hello_world_program).

An example using the Java programming language looks like

// Hello World in Java

public class HelloWorld {

  static public void main( String args[] ) {
    System.out.println( "Hello World!" );
  }
}

Again, all this program does is output “Hello World” to the screen.

A wonderfully rich collection of “Hello World” program examples is maintained here.

A slightly more complex programming task for the beginner would be a “Hello Caractacus” program. The goal of this program is to output the lyrics to Rolf Harris’ deeply philosophical song “The Court of King Caractacus”. This task has more oomph to it because it requires the programmer to understand how the song builds upon itself.

Consider the following example (again in Java).

public class HelloCaractacus {

  public static final String TITLE = "the court of king caractacus by rolf harris\n";

  public static final String [] lyrics = {
    "the ladies of the harem of the court of king caractacus ",
    "the noses on the faces of ",
    "the boys who put the powder on ",
    "the fascinating witches who put the scintillating stitches in the britches of ",
  };
  public static final String[] BEGINNINGS = {
   "now ",
   "if you want to take a picture of "
  };
  public static final String[] ENDINGS = {
    "were just passing by.",
    "well it's too late! coz they've just passed by!"
  };
  public static final String[] COMMANDS = {
    "[all together]"
   };

  public static void main (String [] args) {
    jotln(TITLE);
    // for each verse
    for (int i=0;i<4;i++) {
      // repeat three times
      for (int j=0;j<4;j++) {
        if (j==1) {
          jotln(COMMANDS[0]);
        }
        jot(BEGINNINGS[0]); // now
        // join the appropriate bits together
        for (int k=i;k>=0;k) {
          jot(lyrics[k]);
        }
        jotln(ENDINGS[0]); // were just passing by
      }
      jotln(""); // blank line
    }
    // photo opportunity
    for (int i=0;i<2;i++) {
      jot(BEGINNINGS[i]);
    }
    // the longest line
    for (int k=3;k>=0;k) {
      jot(lyrics[k]);
    }
    jotln(ENDINGS[1]); // you are too late
  }

  public static void jot(String s)   { System.out.print(s); }   // for
  public static void jotln(String s) { System.out.println(s); } // brevity

}

The program outputs the following:

the court of king caractacus by rolf harris

now the ladies of the harem of the court of king caractacus were just passing by.
[all together]
now the ladies of the harem of the court of king caractacus were just passing by.
now the ladies of the harem of the court of king caractacus were just passing by.
now the ladies of the harem of the court of king caractacus were just passing by.

now the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
[all together]
now the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
now the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
now the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.

now the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
[all together]
now the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
now the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
now the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.

now the fascinating witches who put the scintillating stitches in the britches of the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
[all together]
now the fascinating witches who put the scintillating stitches in the britches of the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
now the fascinating witches who put the scintillating stitches in the britches of the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.
now the fascinating witches who put the scintillating stitches in the britches of the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus were just passing by.

now if you want to take a picture of the fascinating witches who put the scintillating stitches in the britches of the boys who put the powder on the noses on the faces of the ladies of the harem of the court of king caractacus well it’s too late! coz they’ve just passed by!

THOUGHTS

  1. Writing Hello Caractacus is not an overly complex task. High school students should have no trouble with it. The requirement itself is clear. As an exercise in logical thinking, it works well.
  2. There is always more than one way to do any task. The way I have done it here is the way my fingers prefer to do it; quick and dirty some may say. Any approach is valid, especially if, rather than just expressing personal style, they are clever, obscure and playful.
  3. Examples in other programming languages would be good to see as well. I suggest that if you are up to the task, and have a language you can volunteer an example using, feel free to contribute (comment).
  4. If anyone knows of a site that allows you to paste simple Java source code (like that above) and does an online compile and run, please post a link.

(orignally published Thursday, February 21, 2008)

Subscribe Share
×

Subscribe

The latest articles sent straight to your inbox.