New to OOP, eager to learn good habits.
I want to make a vectorMap class. A vectorMap will have a few properties and contain a number of polyLine objects, which in turn will each will have a few properties and consist of a number of xyPoint objects.
The user will mostly interact with vectorMap objects, but may occasionally want to use polyLine and xyPoint objects outside the context of vectorMap.
Does this mean I should create three separate public classes? Would this mean three separate class modules in VBA, and in Java, three separate .java files?
My procedural gut tells me that it would be untidy to have three separate source code files for three small and simple classes with only a few lines of code each. I'm used to source code files containing packages with many functions. At this rate, a VBA project will contain tens of class modules. But maybe that's just the way it's done in OOP...
The above will be implemented in VBA and Java, so any examples in either/both of these are most welcome.
what do you mean "simple small classes"? My opinion is you should use a fresh file for each class which is testable. if (for instance) XyPoint is just a touple containing 2 elements, it will be a good idea to put it as a subclass of PolyLine.
However, as far as I see it - PolyLine and VectorMap should be in separate files, since you cannot really tell A is important only to B, and both are testable.
also, when using subclasses in java, notice their types (static/non-static,anonymous..) and choose wisely which is preferred.
p.s. a strong convention in Java is that class names start is a capital letters.
p.s.2: I assume this is done for educational purposes, otherwise you should (as #Ingo said) use built in classes, and not to reinvent the wheel...
Related
Is there a way to make a collection of class files/objects and then have them used in an interactive main file? So let's say I want to make a program to store information interactively where different classes are designed to hold different information. Then I would have an interactive main file where I made instances of these classes which would collectively hold the information I want stored. And then any changes or anything I do in this interactive main file is then saved.
I understand that this might be a very odd inquiry and maybe some other program might be useful for this. If so, feel free to point me in the right direction.
Here are two solutions that are good for the purpose you mentioned in your comment.
The first one is called Serialization. This let's you save your java object to your hard drive, and retrieve it later.
The second, (and in this case, more preferable option in my opinion), is using a Database.
A database is a compliment to your program, that stores data. You can then use "Queries" to access this data, and update it. Almost every database software is compatible with java.
I would look into MySQL
The reason I think a database would be better for your purpose is that they are already highly optimized, and are designed to have multiple people accessing and writing to them at once. If you wanted just want one person to use this program at a time however, serialization might be easier to implement.
Absolutely! Your main class would use the standard input (perhaps Scanner input = new Scanner(System.in);) and output (System.out.println()). To interact with your other classes, most simply, just put them in the same folder (if you are interested take a look at Java packages). If you have a Dog class in the same folder as your main class, you can freely create Dog objects in your main class. I hope this helps!
As a side note, because you mentioned storing information with different classes, you might be interested in the Java Collections Framework.
We are creating an android library for use with Android. That means an Eclipse-like IDE and an Ant-like build process.
The nature of the library is that it has two distinct parts, representing different levels of abstraction - let's say 'upper' and 'lower'.
Assume, for the purposes of this question, that we need to call methods in one part from the other, but would like to keep those methods hidden from the library user. I've scoured the usual references but they all stop at the point of explaining package name conventions and scope rules. I've failed to find anything that answers this on SO, though this was useful.
The immediate solution is to simply have everything in one package and for those methods to be package-private. However, for reasons of maintainability, clarity, and not-having-100-files-in-one-folder we'd prefer to split the parts into different folders.
The obvious splitting point is to split the (let's say 'wibble') package into com.me.wibble.upper and com.me.wibble.lower packages/folders, but that makes any interconnecting methods undesirably public. In mitigation they could be hidden from the javadoc with #hide.
Another thought is whether could we split the parts at the top level and instead of the classic /main and /test folders have /upper, /lower and /test and all parts share the same com.me.wibble namespace. I'm unsure if/how Eclipse would cope with that.
Is there a conventional way of doing this, or is it just not done? If there are ways, what are the pro's and con's?
hmmm......Instead of asking for the solution, sometimes it is better to give the question. WHY you want library users to have a restricted view may generate a better answer than the HOWTO. There are a few answers I thought of but didn't give because I don't know the motivation behind the question (I don't want to waste your time with an answer that is not applicable).
/upper,/lower/,/test doesn't make your situation any nicer. It just makes the project more organized. Whether they are all in the same folder or separate it doesn't affect much.
It sounds like you need public 'interfaces' for library users while having private 'interfaces' for your own use. This is possible with hacking but can be painful if this is large pre-existing collection of code.
In C++ I'm used to being able to split classes up into multiple files using the scope resolution operator (::), but in java it seems impossible to split a class between multiple files.
I've read that classes shouldn't be more than a few hundred lines, but that sounds like ideological nonsense from people who don't write significant applications.
I am writing an industrial Android application (not for consumers, for technicians using professional test and measurement equipment in conjunction with the app linked via bluetooth) and several of my android activities are more than 1000 lines long and I'm not even close to being finished. The primary activity is over 6000 lines long and I expect it to become much longer still... It's becoming very unwieldy and, like I said, in C++ I would just logically split the class among multiple source files, but I guess that's not an option here.
Is there any alternative that I am overlooking to reduce the length of my source files without actually cutting out code (which is not an option...)?
It's acceptable to have a class with more than a hundred lines (most of my classes I write are more than 100 lines long). The thing is, dealing with an Object Orientated language, one should categorize as much as possible, to make the code maintainable, intuitive and readable. Good luck with your app!
You can subclass your class. Make a base class with core functionality and subclass it. So you can have several files and at the end you have just one class you can use.
you can still use composition and implementation techniques. The Java Package is the same as C++ Namespace
Why don't you try to separate common things in different classes, and then use instances of those classes on your Activities? Something like a command pattern, but a bit simpler.
I'm just starting to learn and the books say that I can use "pre-coded" classes and objects. How do I call these classes/objects in the beginning of my program? I understand basically what they are, and the idea that I can use these classes/objects in place of writing fresh code every time, but I cannot seem to figure out where I find these things and how I implement them.
You certainly talk about the Java classes that come in JRE/JDK.
Those are used by including the jar in your classpath and provides the "default" java classes.
Like String in java.util package.
If you want to look at them, in the JDK you'll find the sources of these class.
"Pre-coded", or pre-written Java classes, are pretty much the same concept as the Java API - someone has written the code for you, was kind enough to document how you can use the code, and you may create instances (as necessary) through the prescribed way.
Say, for instance, I want an ArrayList holding Strings. I would then code ArrayList<String> words = new ArrayList<String>(). You wouldn't have to go through the process of writing a dynamic self-expanding vector.
What practices exist when it comes to organizing utility classes where some could be subset of others?
As an example, you could have a FileUtil class with methods related to Files and a subset of this class for checking File formats.
A Facade pattern is generally used more as a convenience/organizational construct rather than a hierarchical way of structuring unrelated methods. That is, when you've got several classes which appear to be used in the same manner throughout your code, you make a facade. It's purpose is to support the idiom that you don't repeat yourself (D-R-Y.)
If you've got a bunch of utility classes for various things, I'd keep them as separated as possible. If you've got a few methods which are used together in an identical manner repeated ad nauseum throughout your code, then I'd think about grouping them together in some higher "master" utility. Without seeing or looking at your code this is about the best advice I can give.