I can’t get into too much detail on this assignment however in general – a user would access a simple Java web server. The user would make a request of the server and some text would be displayed in their browser.

There was to be a collection of text items which would be sent back to the user in random order – and no duplicates until the entire collection had been run thru.

So – a list of strings, some way of randomizing the list and then a way to keep track of what was being sent to the user were parts of the core requirements.

My solution was to start with a Java LinkedHashMap – using integers for the Keys, and then the text would be the Value part of the pair. And no, I would not be doing some kind of random loading of the list – the providing random text comes from another piece. An array would work here as well – both have O(1) look-up, there isn’t any iteration nor adding/deleting from the list.

Oh, and one other requirement – the user needed to add a nickname so as to personalize the return. So my method looked something like this:

/**
* Takes a String name, places it into the line of text and then return.
*
* @param name String containing a nickname supplied by a user.
* @return LinkedHashMap containing collection of text.
*
*/

static LinkedHashMap getrt(String name){

LinkedHashMap lhm = new LinkedHashMap();

String rt = “A. ” + name + “, what goes 99 click? A centipede with a wooden leg.”;
lhm.put(1, rt);
rt = “B. ” + name + “, how many seconds are in a year? There are 12 seconds in a year. January 2nd, February 2nd, March 2nd…”;
lhm.put(2, rt);
rt = “C. ” + name + “, here is one for you. Little four-year-old Julie was looking at her new baby brother for the first time. He was fast asleep. After staring at her tiny, motionless baby brother for a few minutes, Julie looked up at her mother and asked why didn’t he come with batteries?”;
lhm.put(3, rt);
rt = “D. ” + name + “, you can sure pity the dad with three kids in college. He tells his wife that they are getting poorer by degrees.”;
lhm.put(4, rt);
rt = “E. You know ” + name + “, when I die I want to go peacefully — like my grandfather did — in his sleep. Not screaming like the passengers in his car.”;
lhm.put(5, rt);
return lhm;
}

Next part was create a random Queue – this gets passed back to the user as part of their cookie, and gets used to deliver the text from the LinkedHashMap:

The first value in the string gets ‘popped’ off, as what would happen in a queue – the string value gets used to find a corresponding Key in the LinkedHashMap – and the user gets served a line of text. The string is returned to the user as part of their cookie. Once all the values from the string are ‘popped’ off – in other words the string is empty – another call is placed to the outputQueue method and a fresh string is sent back to the user. Here is the code for that method:

/**
* Gets a nickname from client and returns text using the name to output stream.
*
* @param lhm LinkedHashMap containing text.
* @param name String containing a nickname supplied by a user.
* @param jokeIndex String holding the list of text yet to be told.
* @param out PrintStream sent to client containing text with the supplied nickname.
* @exception IllegalArgumentException Thrown to indicate that a method has been passed an illegal or inappropriate argument.
*
*/