{"id":1385,"date":"2014-03-31T06:34:35","date_gmt":"2014-03-31T06:34:35","guid":{"rendered":"http:\/\/graham-kendall.com\/blog\/?p=1385"},"modified":"2020-09-22T01:58:17","modified_gmt":"2020-09-22T01:58:17","slug":"first-java-project","status":"publish","type":"post","link":"https:\/\/graham-kendall.com\/blog\/first-java-project\/","title":{"rendered":"My First Java Project"},"content":{"rendered":"<figure id=\"attachment_1344\" aria-describedby=\"caption-attachment-1344\" style=\"width: 213px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/graham-kendall.com\/blog\/wp-content\/uploads\/2014\/03\/Java-Programming-Book.jpg\"><img fetchpriority=\"high\" decoding=\"async\" class=\"size-medium wp-image-1344 \" alt=\"My first Java project\" src=\"https:\/\/graham-kendall.com\/blog\/wp-content\/uploads\/2014\/03\/Java-Programming-Book-213x300.jpg\" width=\"213\" height=\"300\" srcset=\"https:\/\/graham-kendall.com\/blog\/wp-content\/uploads\/2014\/03\/Java-Programming-Book-213x300.jpg 213w, https:\/\/graham-kendall.com\/blog\/wp-content\/uploads\/2014\/03\/Java-Programming-Book.jpg 427w\" sizes=\"(max-width: 213px) 100vw, 213px\" \/><\/a><figcaption id=\"caption-attachment-1344\" class=\"wp-caption-text\">Java Programming. Downloaded from Googled, labelled as free to reuse, under Wikipedia Commons. URL: http:\/\/commons.wikimedia.org\/wiki\/File:Java_Programming_Cover.jpg<\/figcaption><\/figure>\n<p>It\u2019s 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.<\/p>\n<p>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.<\/p>\n<p>My first instinct was to use PHP (which I learnt a few years ago \u2013 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.<\/p>\n<p>The other option was C++, but that went against what I was trying to do for this project, as you&#8217;ll understand if you read earlier posts in this <a title=\"Link to Prediction Project posts\" href=\"https:\/\/graham-kendall.com\/blog\/tag\/prediction_project\/\">project<\/a>.<\/p>\n<p>So, the obvious solution was to use Java.<\/p>\n<p>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.<\/p>\n<p>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:<\/p>\n<ul>\n<li><strong>Reading in a CSV (Comma Separated Variable) file<\/strong>. This is always a useful thing to be able to do. It\u2019s 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\u2019s usually possible to access spreadsheets and databases directly (which is what I used to do under C++ using <a title=\"Link to ODBC on wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/ODBC\" target=\"_blank\" rel=\"noopener noreferrer\">ODBC<\/a>) but for this project I thought I would get to grips with CSV first. After some searching, I came across a package called <a title=\"Link to CsvReader\" href=\"http:\/\/opencsv.sourceforge.net\/apidocs\/au\/com\/bytecode\/opencsv\/CSVReader.html\" target=\"_blank\" rel=\"noopener noreferrer\">CsvReader<\/a>. It\u2019s 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.<\/li>\n<li><strong>Writing out a latex\/PDF file<\/strong>. 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 <a title=\"Link to Latex\" href=\"http:\/\/www.latex-project.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">latex<\/a> file that I would convert to <a title=\"LInk to PDF decription\" href=\"http:\/\/www.adobe.com\/sea\/products\/acrobat\/adobepdf.html\" target=\"_blank\" rel=\"noopener noreferrer\">PDF<\/a> via a suitable editor (my editor of choice at the moment is <a title=\"Link to TexStudio\" href=\"http:\/\/texstudio.sourceforge.net\/\" target=\"_blank\" rel=\"noopener noreferrer\">TexStudio<\/a>, although I have used <a title=\"Link to WinEdt\" href=\"http:\/\/www.winedt.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">WinEdt<\/a> 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\u2019ll provide a few more details as to how I did this as a) I think it\u2019s interesting and b) I am sure that there are better ways of doing things and I\u2019d like to get some ideas for developing the system further.<\/li>\n<li><strong>Writing files<\/strong>. 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 <a title=\"Link to PrintWriter class\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/PrintWriter.html\" target=\"_blank\" rel=\"noopener noreferrer\">PrintWriter<\/a> package.<\/li>\n<li><strong>Sorting arrays<\/strong>. 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\u2019m sure that there are easier ways in C++ but I never really got my head around it.<\/li>\n<li><strong>Classes and data<\/strong>. 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) \u2018Papers\u2019 and another class called \u2018PapersData\u2019. 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.<\/li>\n<\/ul>\n<p>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.<\/p>\n<p>No doubt, other people would pick up C++ faster than I would have done but, for me, Java is a lot easier to learn.<\/p>\n<p>The other big bonus is the Eclipse IDE.\u00a0 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!<\/p>\n<p>So, as my first Java project, I think, has been a worth while exercise and I have learnt a great deal.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[177,158,1],"tags":[157],"class_list":["post-1385","post","type-post","status-publish","format-standard","hentry","category-archive","category-programming","category-uncategorized","tag-prediction_project"],"_links":{"self":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts\/1385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/comments?post=1385"}],"version-history":[{"count":5,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts\/1385\/revisions"}],"predecessor-version":[{"id":1615,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts\/1385\/revisions\/1615"}],"wp:attachment":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/media?parent=1385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/categories?post=1385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/tags?post=1385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}