Recipe for Java Cookies

How to make cookies

If you've spent all day programming, it's important to make sure that you have a break and some food. If you've been jibbling about with Java too much, you may find it hard to read normal cooking instructions, so here's something you may understand in order to whip up a quick tasty treat. This is version 3 of the recipe, tried and tested by many fellow jibblers :)

finished cookie
A real cookie made using this recipe. See, it works!

Buy the Ingredients

All good cookies need ingredients. This may require you to go outside in order to buy them. Cookies make ideal food for programmers, as they are full of unhealthy things like butter, sugar and chocolate. Note the ordering of the ingredients, as this will become important later on.

SuperMarket sm = new SuperMarket("Sainsburys");
ArrayList basket = new ArrayList();

basket.add("250g all-purpose flour"));
basket.add("1 tsp baking soda"));
basket.add("1/4 tsp salt"));
basket.add("200g butter"));
basket.add("165g brown sugar"));
basket.add("100g white sugar"));
basket.add("1 tsp vanilla extract"));
basket.add("1 egg"));
basket.add("100g chocolate chips"));

Preheat oven

Unlike computers, when you press the buttons on an oven, it may take a few minutes for it to actually do what you've told it to. This is commonly referred to as "preheating" and may take a bit longer than an AMD Athlon without a heatsink. If something interrupts this process, then you're a bit shafted, as the name "cookie" implies that it must be cooked.

// Preheat oven for ten minutes at 175 deg C.
Oven oven = Oven.getInstance();

try {
    Thread.sleep(600000);  // 10 minutes.
catch (InterruptedException e) {
    System.out.println("No cookies today :(");

Combine ingredients

dough mixture
I cheat and use a bread maker to mix it all up within 6 minutes :)

The ingredients are no good on their own. We have to mix them up first. This is essential to ensure that one of the cookies doesn't look like a fried egg when it comes out of the oven. Try mixing everything up until you achieve a nice, consistent dough, but leave the egg and chocolate chips until last. Try not to eat too many, they'll be better in the cookies, trust me.

Cupboard cupboard = Cupboard.getInstance();;
Collection bowl = cupboard.get("bowl");

Iterator it = basket.iterator();
while (it.hasNext()) {
    Ingredient ingredient = (Ingredient);

    if (ingredient == null) {
        throw new RuntimeException("Duh, you forgot an ingredient!");


Preparing to cook

distributed blobs
Space the dough out sensibly. Don't make the blobs too big!

Cookie dough has a tendancy to fall through the gaps in oven shelves, so here we propose the use of a revolutionary cooking device: a baking tray. This ingenious invention somehow stops the cookies falling to the bottom of the oven. You simply place blobs of the dough onto the device and after a while in the oven, they magically turn into cookies! To simplify the task of washing up, we also place a protective shield between the tray and the cookies. We call this "tin foil". There should be enough dough to make about 30 large cookies, but our oven is only large enough to hold 9 at a time. Perhaps we could use CORBA to implement a distributed cooking protocol?;
Collection tinFoil = cupboard.get("greased tin foil");
Collection tray = cupboard.get("baking tray");


for (int i = 0; i < 9; i++) {
    tinFoil.add(new Cookie(bowl));

Ready, Steady, Cook!

cooking cookies
Schtop! These cookies are not ready yet!

You are now ready to bake the cookies! Now, remember, unlike modern day computers, we may have to wait several minutes before this stage is completed. I'd suggest at least 10 minutes, however, it may take up to 20 minutes. Perhaps this could be used as a seed for a pseudorandom number generator? Keep checking the cookies until they look like they're done. Hint: If they start going black, you'd better start warming up the time machine.

// Stick the tray in the oven for at least 10 minutes.;

    Thread.sleep(600000);  // 10 minutes.
catch (InterruptedException e) {
    System.out.println("Schtop! These cookies are not ready yet!");

After about ten minutes, we should keep checking the cookies to make sure we know when they're ready!

// Check on the progress of the cookies every minute.
boolean stillWaiting = true;
while (stillWaiting) {
    try {
        Thread.sleep(60000);  // One minute.
    catch (InterruptedException e) {
        System.out.println("Stop interrupting me!");
    // Check that all the cookies are cooked!
    stillWaiting = false;
    for (int i = 0; i < tinFoil.size(); i++) {
        Cookie cookie = (Cookie) tinFoil.get(i);
        if (!cookie.isCooked()) {
            stillWaiting = true;

// Take the tray out.;
tray = (Collection) oven.remove(oven.indexOf(tray));

// Bug fix suggested by Tim Bishop.
// (We forgot to turn off the oven!)

// Wait for things to cool down.
try {
    tray.wait(300000);  // 5 minutes.
catch (InterruptedException e) {
    System.out.println("Ouch! Burnt fingers!");

The fun begins!

big pile of cookies
A big pile of lovely soft chocolate chip cookies.

Now that you've taken the cookies out of the oven and left them for a few minutes to cool down, they are ready to eat! Don't eat too many; it may lead to a nasty coredump later on.

it = tray.iterator();
while (it.hasNext()) {
    Cookie cookie = (Cookie);;

If you found this tasty, you may like to try some pies!

You may be interested to note that there are at least 3 programming errors in this cookie recipe. Can you spot them all? Are there any more? Don't worry if you're thinking of using this recipe, as the errors are purely programmatical and will not affect the outcome of the cookies.


Search this site


Copyright Paul Mutton 2001-2013
Feedback welcomed

Web Hosting

Web Hosting