Monday, 3 March 2014

Running db-derby with recent releases of Java

This blog post is an errata item for my Java Fundamentals course, and will also apply for the other Virtual Pair Programmers courses where we use the db-derby database.

I discovered while recording the upcoming Groovy course that there has been a security change in the most recent release of Java (1.7.0.51) that has meant that the default configuration for db-derby no longer works. Running db-derby with the startNetworkServer command will result in an error message which says somewhere early on in the error message:

access denied ("java.net.SocketPermission" "localhost:1527" "listen,resolve")

The easiest and quickest way to overcome this seems to be to run the database on a higher port number, such as 50000 - to do this, instead of running the startNetworkServer command, run the following instead to start the db-derby database:

NetworkServerControl -p 50000 start

In your code, you'll need to change the connection strings to incorporate the new port number, so that the line of code which creates the connection includes the port number - it should look like this:

conn = DriverManager.getConnection("jdbc:derby://localhost:50000/library");

This should overcome the error - if you find you have any further unexpected errors that you can't resolve however, do get in contact via the Virtual Pair Programmers contact us page!

Saturday, 8 February 2014

Groovy Course coming soon

One of the topics that we've been asked to provide a course on at Virtual Pair Programmers a number of times over the last few years is Groovy... so I'm pleased to announce that I'm currently making good progress on creating the Groovy Course, which I hope will be released around the end of March 2014.

While I've been talking about the Groovy course to fellow Java programmers and trainers, in particular I've had a number of conversations about what it should cover , everyone seems to agree that once you start with Groovy you don't want to switch back to plain old Java, and I guess this is primarily because everything you can do in Java can be done in Groovy, often more quickly, and of course you can do more too. I've come to the conclusion that for many Java programmers it's really quite easy to get up and running in Groovy, but to really harness it's power, and get the most out of the language, it needs a really quite thorough course.

For anyone who doesn't know what Groovy is, I've pasted below a response to the question "what is groovy" taken from Stack Overflow... although this isn't the top answer on that site, it's the one that I like the most as I think it gives a real flavour of what Groovy is really about from a programmer's perspective:

Groovy is a dynamically typed language that runs on the Java platform and includes some features that a lot of people wish were in Java (ex: closures). One nice thing about Groovy is that it reduces the amount of code needed to do common tasks such as parsing XML files and accessing databases. While learning Groovy you can always mix in straight Java code. This is nice since it allows you to ease into Groovy at your own pace while delivering functional code. If you've been using Java for a while I think you'll appreciate the simplicity of using Groovy since you can program more functionality using less keystrokes. The inclusion of closures was a big selling point for me. One word of caution: if you use Groovy for production code you should make sure there is descent test coverage (since Groovy is a dynamic language). Even if you decide not to use Groovy, it's not a huge time investment to learn and experiment a bit.

The chapter list for our forthcoming course isn't quite complete, but the likely structure and outline content is as follows:
  • Introduction: The difference between Groovy and Java, installing Groovy and setting up a development environment
  • Part One: Getting started - looking at the power of key aspects of programming in Groovy, such as objects, methods, strings, operators, closures, ranges and looping and unit testing
  • Part Two: Case Study - using everything we have learned to start to build a working application - our example is going to be a hotel room booking system
  • Part Three: Going deeper - more on closures, working with files including XML, databases, Meta Object Protocol, using stubs and mocks in unit testing and finally packaging and deployment
Our case study will start after we have studied the basics, and we'll be developing a back-end system to help a small hotel manage its room bookings. As we learn more advanced topics, we will then enhance the back-end system to provide more functionality. 

We'll put an update on progress nearer the release date, but in the meantime, if you're welcome to contact me through www.virtualpairprogrammers.com

Friday, 6 December 2013

Java Fundamentals - follow up post on Automatic Resource Management

This is the second of two follow up videos for the Java Fundamentals course. In this video, I take the Java try with resources statement a little bit further to tidy up the code we created in chapter 26 of the course.

The video is hosted on youtube - for best viewing results, watch in full screen and choose the HD quality option if that doesn't come up by default when you click on play!

Saturday, 22 June 2013

Follow-up video blog on the keyword Static

One of the topics that didn't make it into the Java Fundamentals course was an explanation of the keyword static... so I've done a short follow up video which goes into detail about that. In the video, I cover what the keyword means and how to use it correctly (both static methods and static variables).

The video is hosted on youtube - for best viewing results, watch in full screen and choose the HD quality option if that doesn't come up by default when you click on play!



Thursday, 20 June 2013

Java Fundamentals now live

Well I'm pleased to say that after months and months of hard work, the Java Fundamentals course is now live and available for purchase from https://www.virtualpairprogrammers.com/training-courses/Java-Fundamentals-training.html

I'm really pleased with how it has turned out - it's a really comprehensive course now, about 12 hours in length, and I think we've covered object orientation in a really accessible way, which I'm most proud of!

Just want to thank Richard Chesterwood, my fellow VPP trainer, for his help and guidance throughout this process - and I'm looking forward now to starting work on the next one!


Thursday, 28 February 2013

Content of the new introductory Java Course

The new Introductory Java course is now being edited. As a result I have a pretty good idea of the final content and running order, although the editing and review process will highlight any key areas that need to be changed, or added, so this might not exactly match the final course content or structure.

I have written the course from the perspective of someone with some programming experience (in any language, such as C, Visual Basic, PHP or just JavaScript) and so this is very much a "conversion" to Java. The first part focuses on how to do things in Java that you are likely to be familiar with in another language, such as declaring variables, conditions and loops, and the second part then covers topics that are more unique to Java and so may be completely new.

I'm conscious that some people will want to use the course having done little programming before, so I've been careful to make sure that any concepts not fully explained are easily understandable by the novice if they are willing to spend a bit of time doing their own online research.

So here's the likely running order:

1 - Introduction to Java
2 - Installing Java and Eclipse
3 - Key aspects of Java syntax
4 - Introduction to Object Orientation
5 - Structuring applications
6 - Java libraries and Javadocs
7 - More advanced Object Orientation (including inheritance and abstract classes)
8 - Exceptions
9 - JUnit
10 - Collections
11 - Yet more object orientation (Interfaces and polymorphism)
12 - Databases
13 - Distributing your software

In any of the books or other courses I have seen on Java, I find that trainers struggle with object orientation (OO) - the issue is finding an example that individuals can understand and relate to. I have been really careful to ensure that we build up the OO aspects in a practical and logical way, and rather than talking about something that is really not related to programming, such as the number of legs on an insect (I do remember that being the example when I first learned about object orientation, and I certainly didn't get it at the time), our examples are hopefully modern, understandable, and useable in real world applications.

Throughout the course we have a key project that we work on together, and while we don't fully complete the application, we do end with a good solid framework and code base that could be extended to be a live, functional and useful application.

I've give another update as we get close to the launch date.

Wednesday, 19 December 2012

When you need to do some serious work with dates

In the Java course, which will hopefully be available on VirtualPairProgrammers.com at around the end of March, we do some basic date manipulation using the GregorianCalendar class. I explain that the Date class within java has problems, which is why many of the methods are depreciated. I don't go into detail about what is wrong with it, but there's a great post about that here:

http://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api

GregorianCalendar is acceptable for basic date usage although it's not completely intuitive and doesn't really cut the mustard if you need to do some serious date manipulation.

Something we do within the course is work out a date in the future by adding a fixed number of days to today's date - we want to create a variable called DueDate and set it's value to, say, 14 days after today. So if today is the 4th March, then DueDate should be equal to 18th March.

Programatically the code to do this, using the GregorianCalendar class looks like this:

public void CalculateDueDate() {
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DAY_OF_MONTH, 14);
Date dueDate = gc.getTime();
System.out.println(dueDate);
}


This code is reasonably straight forward - we create a new instance of the GregorianCalendar class (which by default will have an initial value of the date and time when it is instantiated) and then call it's add method to move it's value on by 14 days.

What's odd about this code, at least to me, is the use of DAY_OF_MONTH to specify that it is 14 days we want to day (days as opposed to months or years). It's odd because DAY_OF_MONTH has a specific meaning - and yet we could use this code to validly add 41 days, even though no month has more than 41. Interestingly DAY_OF_WEEK will also work in it's place, as will DAY_OF_YEAR. Now these three field values are definitely NOT the same, but the GregorianCalendar class is "clever enough" to know how to perform an add correctly, when any one of these fields is provided as the first parameter.

This leaves most programmers feeling a little uncomfortable - surely it would make more sense for the add function to take a value of calendar.DAY - but this field doesn't exist. There are fields for year, month, hour, minute and second, so this missing DAY field really doesn't feel comfortable.

Now this isn't the only reason to dump the GregorianCalendar class in favour of a nicer alternative - in fact most people would say I have gone off on a tangent, and some of the bigger issues include the lack of methods like .getMonth(). But this is a good enough one for me to say it's worth spending some time looking at JodaTime.

Now JodaTime isn't the only alternative set of date and time classes available for Java but I believe it's one of the most popular - and for good reason. The documentation for JodaTime library which can be found at http://joda-time.sourceforge.net/index.html is simply first class. If you want to see what I mean, take a look at their chronology pages. The detail they go into about the Gregorian and Julian calendars, their combination into GregorianJulian and the ISO8601 format is fascinating.

So before we can use JodaTime we need to install it - the process is straightforward - download one of the distribution files, unzip the contents, and then add the joda-time-version.jar file as an external file to you project's build path.

Using JodaTime our example above becomes:
public void CalculateDueDate() {
DateTime dt = new DateTime();
DateTime dueDate = dt.plusDays(14);
System.out.println(dueDate);
}


The .plusDays function is really nice - it's completely clear what it does, and straight forward to use.

I'll finish this blog post with one exceptionally nice feature of Joda time -the ease in formatting dates for output. In the UK it's normal to display dates in the format day/month/year. To amend my code to do that, I can specify a format in the .toString method of the DateTime class. All I need to do is amend my final line to:

System.out.println(dueDate.toString("dd/MM/yyyy"));


There's lots more to the JodaTime class but I hope you'll see that it really is an easy to use, reliable, time and date management library. Just play with it for half an hour and you'll wonder how you ever managed without it!