It’s been a while since I decided to use Java as my new programming language of choice. Since my last post I have been honing my Java skills, with my first java project.
I had a project I needed to do that is about analyzing academic papers, comparing them in various ways, sorting them, writing out reports etc.
My first instinct was to use PHP (which I learnt a few years ago – and I really like the language) but the only development environment I have is on a server. I could set up a development environment on my desktop, but it did not seem worth it for the times that I would use that environment in anger.
The other option was C++, but that went against what I was trying to do for this project, as you’ll understand if you read earlier posts in this project.
So, the obvious solution was to use Java.
As my first Java project, it seemed to have suitable complexity for a first time Java user, but allowing for the fact that I have a programming background. I suspect that the project would not be suitable for a complete newbie to programming.
The details of what the project has to deliver is not that important but the lessons learnt are important, as these are things that I can use in later, larger, projects. These included:
- Reading in a CSV (Comma Separated Variable) file. This is always a useful thing to be able to do. It’s one of those fall backs that is useful to have in your armoury as it makes accessing files such as spreadsheets and databases easy to do as they invariably have an option to save as a CSV file. Of course, it’s usually possible to access spreadsheets and databases directly (which is what I used to do under C++ using ODBC) but for this project I thought I would get to grips with CSV first. After some searching, I came across a package called CsvReader. It’s not the most basic package (which is a good thing), and it did have good reviews. I had some challenges installing it, but that was not to do with the package but the fact that this is the first time I have installed a Java package. Once I had sorted that out, it worked perfectly.
- Writing out a latex/PDF file. One of the things I wanted to do was produce a half decent looking report. My initial thought was to write out a text file and then use Word to manually format it. This, for many reasons, is not a good idea; not least of all as it would be labour intensive every time I produced a new version of the report. I like to think that I had a flash of inspiration (but perhaps it is the obvious thing to do) and I decided to write out a latex file that I would convert to PDF via a suitable editor (my editor of choice at the moment is TexStudio, although I have used WinEdt in the past). This seems to work pretty well and I can now produce nice looking reports, without having to worry too much about the look of the final document as Latex will handle this as long as I have some idea of the structure as I develop the program. Of course, the beauty is that the report is complete, once the program has been run, without any need for any other processing/formatting. In a future blog I’ll provide a few more details as to how I did this as a) I think it’s interesting and b) I am sure that there are better ways of doing things and I’d like to get some ideas for developing the system further.
- Writing files. An obvious thing I had to do when writing out latex files, was to learn out to write out files. As any Java programmer will know, this is very easy using the PrintWriter package.
- Sorting arrays. I had a need to sort an array on one of the fields. In fact (see below), this involved sorting an array of class instantiations. This was probably the most difficult thing I did when developing my first Java project, and it took a while before it came together. But following a few examples from the web, and I had this working pretty quickly. It certainly seems easier than C++, which always seemed complicated and involved having to have friends of classes. I’m sure that there are easier ways in C++ but I never really got my head around it.
- Classes and data. This is not really a Java thing and maybe I am totally wrong, but I quickly found that my data and member functions were making the class quite large, so I decided to have a class called (say) ‘Papers’ and another class called ‘PapersData’. The PapersData class simply holds all the data and the Papers class provides access to it, as well as providing all the other functionality. This leads to (at least in the way I do it) too many getters and setters, but it does separate the data/functions. But, the main reason I did this is because I wanted to hold different data types for my various objects and an ArrayList (or other array type objects) would not allow this. I am happy to be corrected but I was trying to recreate the struct (i.e. a class) type concept of C++. Anyhow, it seemed to work for what I wanted, but whether it would scale to larger projects is another matter.
The system I have ended up with, seems to work well. Whether it is scalable to larger projects remains to be seen, but it has certainly been a very good learning experience. I have doubts that if I was trying to learn C++, even with a good programmimg background, I would have progressed as fast as I have with Java.
No doubt, other people would pick up C++ faster than I would have done but, for me, Java is a lot easier to learn.
The other big bonus is the Eclipse IDE. No doubt, I have only scratched the surface but the autocomplete (Ctrl/0) and the suggested error correction (Ctrl/1) are my new best friends!
So, as my first Java project, I think, has been a worth while exercise and I have learnt a great deal.