temporalcohesion.co.uk

from the mind of…

Archive for the ‘Uncategorized’ Category

Building the Project Euler framework, part 3

without comments

In part 2, I showed you an improved, although still pretty basic problem runner framework for Project Euler. I did leave out some things though, and I’m going to try and explain them now.

Firstly, I haven’t really shown how I use the Problem interface. You can see it in part 1 of this series of posts. In Eclipse, you can create a new class in a package, which should bring up the “New Java Class” dialog. Give the class a name - for Project Euler problems I’ve chosen to name them “One”, “Two”, “Three” etc. You can then add an interface that the class is to implement, click ‘Add’, and type in Problem. You can also choose some methods to stub out, tick ‘public static void main(String[] args)’. Click Ok, and you should get something like this:

package co.uk.temporalcohesion.euler.problems;
 
import co.uk.temporalcohesion.euler.interfaces.Problem;
 
public class One implements Problem {
 
	public String answer() {
		// TODO Auto-generated method stub
		return null;
	}
 
	public int id() {
		// TODO Auto-generated method stub
		return 0;
	}
 
	public double time() {
		// TODO Auto-generated method stub
		return 0;
	}
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
	}
 
}

I can hear you asking why am I including then main(String[] args) function if we already have a class that is capable of running the problems (the main Euler class)? Well, what I do to create is to create a Euler object in the problems main method, and get it to run the problem, like this:

        /**
	 * @param args
	 */
	public static void main(String[] args) {
		new Euler().run(1, true);
	}

So the problem class is running itself using the Euler object, which knows how to find the problem, instantiate it and run it. I find doing things this way is easier when working on the problem, as you can simply run the problem as a java application, and it will output the result in a standard format we are expecting to to the console in Eclipse.

Thinking about it, you might be wondering - what’s the point of all this, it seems a little excessive for something that can be done fairly easily? Well - I’ve done it like this because the whole point of me doing the problems on Project Euler, is to practice problem solving and become more comfortable in my use of Java. So I think what I’ve done is pretty valid in that regard.

Written by stuart

September 12th, 2008 at 9:00 am

Posted in Uncategorized

New Year’s Resolutions are so passé

without comments

It’s true, New Years resolutions are passé. Every year, you look at what is wrong in your life and triumphantly decide that you are going to resolve these problems by issuing a bold and sweeping set of statements, or edicts.

  • Lose weight
  • Stop smoking
  • Get fit

These are what you want to achieve, chasing that ever elusive dream of making your life better. Can you? Can a simple list of statements help you achieve these goals? I’m not so sure. For some people I’m sure that they don’t even have to write them down, those super motivated people who seem to exude success.

I used to do this, on scraps of easily lost paper, “Stuart’s New Year’s Resolutions!” it would say, with a list of two or three word ambition’s for the year. Lost by February. For the last few years I haven’t even bothered with writing them down, I’d just mouth off to anyone who would listen, “I’m gonna do this, and that, this year, you wait and see”.

Which funnily enough, didn’t get me very far with achieving very much. That said, 2007 was a year I’ll never forget, for a number of reasons (not all of which I want to go into here), and it has led me to believe even more than I did, that it is all well and good to strole through life without a care, thinking everything is fine and dandy. Because it is not. Doing that will get you stuck, trapped in a rut that you can’t get out of, never having made anything of your life.

Thankfully, I’m pretty certain that I’m not in a rut. Trust me, I’ve been there and I know what it is like.

So what to do instead of making ‘New Year’s Resolutions’? It’s simple: Set goals. “But”, I hear you cry, “a goal IS a resolution?!” Which in this sense I suppose they are, however there is a crucial difference. A goal set’s out what you want to do and how you are going to achieve it.

The above list could be re-written like so:

  • Lose weight by eating healthily and exercising more to get fit.
  • Stop smoking by using patches and will power, giving up completely by Easter.
  • Get fit, by joining a gym, running and cycling and lose 2 stone by Easter

These aren’t my goals for this year… Ok, I don’t smoke, but the other two count. I’m not revealing my other goals… yet. I have several though, and I intend to get stuck in and achieve them.

Written by stuart

January 7th, 2008 at 12:18 am

Posted in Uncategorized

more cake

without comments

Something I discovered fairly quickly when coding (cake)PHP in eclipse, is that, like code completion in Models, unless you do a little extra configuration, code completion in Views won’t work.

If however, you make a slight modification to the Views index.ctp, and create a HtmlHelper() yourself, then you get code completion on $html->, and access to all the methods, and the rendered view isn’t affected at all.

For example:

<?php
if(false)
 
{
 
$html = new HtmlHelper();
 
}
 
?>

I found the answer to this after posting on the CakePHP google group, and getting a good answer from a user. Top Stuff

Written by stuart

October 10th, 2007 at 8:04 pm

Posted in Uncategorized