If your Java project has a class that doesn’t belong to any package (it doesn’t have the “package…” statement at the beginning), then it belongs to the default package. If you try to use that class from another class without a package there are no problems, but it’s impossible to do so (as of java 1.4) from classes that do belong to some package. Figuring this out took me a good part of the afternoon. I can already tell I got something wrong and the whole thing was much simpler…
Well, that’s the problem with the Calculador class provided by the Artificial Intelligence course. It has no package. If you’ve already organized all your code neatly into packages you won’t be able to use it. You can only do it from a class without a package… but putting all the classes in the same package ends up being very messy, and besides it’s not recommended.
But there’s a solution. We can keep our project organized and still use this blessed class.
What the professors could just do is put the Calculador class in a package and build a jar. That’s the easiest thing, obviously… but what I’m going to mention here is useful for when you come across a .class file without its source code, the programmer didn’t include the class in a package, it’s impossible to contact this person or they don’t want to change it, and you have your project organized with a package hierarchy (yes, all of these conditions have to be met 😀 It’s unlikely but this kept me entertained all afternoon).
As I said before, Java 1.4 and above don’t allow importing classes from the default package from classes that do belong to one. So what you can do is build a wrapper, compile it with java 1.3 (which does allow importing these classes) and use the wrapper in our project. There’s no need to download the old jdk… this page solves our problems.
I’m making available to you a jar file that, once imported into your project, lets you start using the Calculador from the assignment (the Calculador and Pair classes are translated into CalculadorWrapper and PairWrapper), until the professors release a corrected version.
If you run java -jar CalculadorWrapper.jar it prints to the screen a test of the class’s methods.