views:

78

answers:

3

For our assignment we need to write code for a neural network. The way I planned to do it was to write a Node class, which is a node within the network; a Layer class, which is a layer of nodes and a NeuralNet class, which is a network of layers.

I'm having a lot of trouble understanding the way Java is designed to work for imports. To me it seems that it should be a simple matter to include my Node class in my Layer class, and my Layer class in my NeuralNet class, however Java doesn't like importing from the default package.

What I have read suggests that anything you import needs to be in a package and packages have their own subdirectory. Because of the way I plan to structure my classes, this leaves me with what seems to me, an unwieldy and unnecessarily complex directory structure ie.

neuralpkg.layerpkg.nodepkg.Node

Can someone explain to me whether this is the only way to implement the structure that I want or whether there is some much, much simpler way that I've missed?

For what its worth I'd have no trouble writing this in C/C++, but attempting to import in a similar style has only given me heartache.

+1  A: 

Think of packages in Java like namespaces in C++. From what it sounds, it seems like you want everything in the same namespace. The default package can sort of be thought of as unnamed namespaces (I think).

What you should do is just create a simple package com.class.hw01 for example. And put all your classes in there. That way you don't even have to import anything, you can just declare/use your classes just like in C++ if they were in the same namespace.

Falmarri
@Falmarri Thanks. This is an easy way for me to think of it :)
Ben
+2  A: 

It's more like this:

Make a directory called src. This is the root under which all java files will go. Underneath it, make a series of directories called

com->ben->neural or some such. Put all your java source files in the neural folder.

Also, what IDE are you using? Score yourself Netbeans, Eclipse, or the free version of IntelliJ. Java needs a good IDE. When you create a new class, the IDE will do the package statement for you and help immensely with the imports.

Tony Ennis
@Toney Ennis Just been hacking my way around in the terminal. I've never felt a need for an IDE for C given how good makefiles are. Might check out Eclipse though given it's what my partner for this wants to use.
Ben
@Ben As projects get larger the IDE matters more and more. My IDE will allow me to change the name of a method and will rename it everywhere it is used. Or if I remove an argument, the appropriate argument is removed from all calls. Refactoring is nearly impossible without a good IDE. I would suspect there are probably plug-ins for vi (I assume you're using vi) that do some cool things too.
Tony Ennis
Dear god you're not using an IDE? Even for C you should at least use an IDE that does code checking, code completion, syntax highlighting, etc. If you're not using an IDE because "pro programmers don't use IDEs", then you've been misled by idiots.
Falmarri
@Toney Ennis @ Falmarri I find vi/vim to be present on virtually all machines so I find it convenient to do most of my work in this. It saves migrating between 10 different IDEs for 10 different locations. Also I can simply copy my .vimrc file to get all my setup shifted to a new location without messing around with the different menu setups of various IDEs. I also touch type very quickly so I find code completion useless. I can certainly replace a method name with another using vim and I don't doubt its possible to do easily across a whole project using the capabilities of bash.
Ben
You're losing out by being unable to refactor effectively. But hey, whatever works for you.
Tony Ennis
+3  A: 

You don't need to layer the package like neuralpkg.layerpkg.nodepkg, where "neural", "layer", and "node" are subpackages for the respective classes.

You can simply create a package named "ben" and put all of your classes in there. So within your source directory you'd have a layout like:

--/ben
----Layer.java
----NeuralNetwork.java
----Node.java

Each class definition should then start with the line package ben;.

Classes in the same package don't need to import each other.

matt b
every one is faster than me, damn :P
jjczopek
Thanks for explaining this. It seemed absurd that it would need to be in subpackages.
Ben
Falmarri is correct that you should just think of these as namespaces
matt b