gen-e-sis.com

getting there is all the fun

Monday, September 15, 2008

Moving on from Hello World to Hello Caractacus

According to Wikipedia

A “hello world” program is a computer program that prints out “Hello, World!” on a display device. It is used in many introductory tutorials for teaching a programming language. Such a program is typically one of the simplest programs possible in a computer language.

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.

I have written the following example (again in Java).

—————-

public class HelloCaractacus {

  public static final String TITLE = "The Court Of King Caracticus 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 stiches 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 Caracticus 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 stiches 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 stiches 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 stiches 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 stiches 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 stiches 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. 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 let me know.

3. There are always more ways than one 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.

Alternative approaches are welcome, especially if (rather than express personal style) they add value. This is not a scenario where design patterns and other software engineering fluff should be applied. However, alternatives that are clever, obscure and playful are most welcome, provided the basic version comes first.

4. Ideally 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 would like to volunteer and example, quite a collection could be built up.

posted by CatWhisperer at 9:24 pm  

1 Comment »

  1. Interesting – useful –
    however it reminded me – when I compiled it and ran it and got a run time error – that it is good to check the filename and the public class and the name of the person (in this case) to whom it refers. What this really says is that I built it in textpad and ran it OK but when I tried to run it from cmd line directly I noticed that Caracticus had a different spelling ..tacus. This was a useful reminder as it occurs sometimes in my MIDlets –
    I’ll look more at your website – I was actually searching for something else “siscom 2008 maze generator”
    Mike

    Comment by Mike — October 3 2011 @ 10:20 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress