Exceptions

I am nearing the end of the Spring college semester. This semester I am taking a Java programming class. There was one chapter near the end of the course on exceptions. We did not spend much time on it because there is not much time left. However I am still trying hard to wrap my hands around the concept.

Exceptions are not unique to the Java programming language. I know C++ has them. And I would guess many other languages have them as well. It is straight forward how to throw and catch an Exception object. You are just passing a message. However I have still not yet found a great reason to create my own exception classes. I guess I could use them to ensure the caller can separate exceptions with different catch blocks. But aren't they all going to just call getMessage() on my object?

Here is one thing I have determined about exceptions. This also applies to any classes you write. You should not name the class with too long of a name. It gets tiring to type all those characters. Yes I know you can cut and paste. But if there is a misspelling, and there are a million characters in the name, it takes a long time to figure out where I went wrong. I am going to do a bunch of programming projects where I throw and catch exceptions. Perhaps after that I will have a better feel for them.

Good or Great

I have finished reading the chapter on arrays in Java. This is for my college class. In addition to the reading, I have completed each Self-Test question from the chapter. I have written Java programs to solve all the exercises at the end of the chapter. You could say that I have achieved a good understanding of the material.

Here comes the hard part. There are also a bunch of programming projects at the end of the chapter. However there are so many other things I would rather be doing. We are behind at work. I could catch up with this week's list of tasks to be completed. It is nice outside. I can do some necessary yard work. The weather also lends itself to me taking a bike ride with friends. The girlfriend wants to see a bunch of movies. She is also hungry and wants to go out to eat.

It is easy in the middle of the night to spend some time learning Java. The hard part is now when I really don't want to do any more Java programming. After all, I have reached a good enough level of understanding. What is there to gain by going further? The answer is that there is greatness to be attained. Now I am not saying that I will be a great Java developer. But in order to have a chance at greatness, I must push on now. It is absolutely required to do each and every one of the programming projects in the back of the book.

To tell the truth, greatness will also require going beyond the projects in the book. I need to code up some other programs using Java. You know. I need to implement some non trivial application using the Java programming language. Only then will I have a slight chance at possessing great Java programming skills. Let's see where this takes me.

Selection Sort

I have been reading ahead in my textbook. The next chapter is on arrays. To help learn arrays well, the textbook goes over a number of sorting algorithms. One exercise just had me implement selection sort.

Selection sort is one of the easiest sort methods. It is an in-place algorithm, requiring no extra space in a array other than one temporary variable to do swaps.

The way selection sort works is to traverse the array, and swap the current element with the smallest value from the rest of the array. It could not get any simpler. However this technique has poor performance if you have many elements in the array.

Constructor Calling Constructor

We just started learning about class constructors in my Java programming class. In the past, we just did the new operator without knowing what was going on. Now we are getting a little more information on the subject. There is one specific constructor issue of interest to me. That is the case where a constructor calls another constructor. My book instructed me to use the “this” keyword to accomplish it as so:

public Class Pet
{
public Pet()
{
// default constructor code
}
public Pet(String name)
{
// call the default constructor
this();
}
}

I found this syntax to be a little strange. Yes I understand the “this” keyword refers to the current object. But we are talking about making a call to a method in the class. Why not use the name of the method like so:

public Class Pet
{
public Pet()
{
// default constructor code
}

public Pet(String name)
{
Pet(); // This does not work!
}
}

It seems logical to use the actual method name when making a call to it. However the Java designers must not have agreed with me. Calling the constructor by its name results in a compile time error. Oh well. It was worth a try.