views:

110

answers:

4

I'm learning Java and OOP, and have been doing the problems at Project Euler for practice (awesome site btw).

I find myself doing many of the same things over and over, like:

  • checking if an integer is prime/generating primes
  • generating the Fibonacci series
  • checking if a number is a palindrome

What is the best way to store and call these methods? Should I write a utility class and then import it? If so, do I import a .class file or the .java source? I'm working from a plain text editor and the Mac terminal.

Thanks!

+3  A: 

You can put your methods into a utility class, then import that class (not the file!).

import my.useful.UtilityClass;

...
boolean isPrime = UtilityClass.isPrime(2);

When things start to get more complicated, and you want to reuse your stuff across multiple projects, you can put it into a jar and add that jar to the projects. Then you can import and use the class the same way as above.

Péter Török
what does `my.useful.UtilityClass` refer to, the file `my.useful.UtilityClass.class`?
carillonator
No, the `my/useful/UtilityClass.class` file there relative to the classpath :)
BalusC
aha -- thanks!!
carillonator
If you get a lot of these things and name them in a consistent manner, it's often easier to use static imports (http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html ) than to litter you code with `UtilityClass.` over and over again.
jasonmp85
A: 

Why not create a jar file, a library of your own, often used components? If you compile your java code, just include the utility archive in your classpath.

The MYYN
+1  A: 

The UtilityClass idea is fine, but it also gives you the opportunity to practice TDD. For a new Euler problem, create an empty method in your UtilityClass where you'll solve that problem. Then make a bunch of JUnit tests that use this new method and depend on it being correct. The tests will all fail (or they should, because you haven't written the solution yet!)

Now solve the Euler problem and watch the tests pass! If you want to reuse the code later, the unit tests will keep you correct during refactoring and will provide a place to add regression cases for bugs you may find.

jasonmp85
thanks for the intro to TDD and JUnit.
carillonator
+1  A: 

Create a directory tree with your answers. my/math/*.java. You should specify the package my.math for the class. Create different classes for different problems sets; Primes.java, Fibonacci.java, etc. If you have already solved the problem once, don't solve it again, unless your solution is broken.

Add the directory above my/math to your classpath (java -cp directory) or jar the directory and add that to your classpath.

Libraries such as this are the lifeblood of a successful project. Creating and using a library of solutions (for which the language does not already have a library) solves a number of project problems. Use available libraries whenever possible.

BillThor
good detailed advice, thanks.
carillonator