CSE143 Notes for Wednesday, 4/7/10

I explored several issues having to do with classes by developing a class called Angle that could be used to keep track of the angles that are used to specify latitudes and longitudes. For example, Seatac Airport is at a latitude of 47 degrees 39 minutes North and a longitude of 122 degrees 30 minutes West. This will be a simple class for demonstration purposes only.

We are only going to keep track of degrees and minutes, so we began with a class that has fields for each and a constructor:

        public class Angle {
            private int degrees;
            private int minutes;
        
            public Angle(int degrees, int minutes) {
                this.degrees = degrees;
                this.minutes = minutes;
            }
        }
I said that we'd follow up on Monday's lecture by writing some client code that constructs some angles and adds them to an ArrayList:

        import java.util.*;

        public class Wed {                       
            public static void main(String[] args) {
                Angle a1 = new Angle(23, 26);
                Angle a2 = new Angle(15, 48);
                ArrayList<Angle> list = new ArrayList<Angle>();
                list.add(a1);
                list.add(a2);
                System.out.println(list);
            }
        }
When we ran this program, it produced output like the following:

        [Angle@42719c, Angle@30c221]
The text being generated in each case includes the name of the class (Angle) followed by an at sign followed by a hexadecimal (base 16) address. This is what the built-in toString method produces. To get better output, we decided to add a toString method to the class.

Ideally we would show something like 23 degrees and 26 minutes using the standard symbols used for degrees and minutes:

23° 26′
But those characters aren't easy to work with in Java, so instead we'll settle for "d" for degrees and "m" for minutes:

23d 26m
We wrote the following toString method to produce that output:

        public String toString() {
            return degrees + "d " + minutes + "m";
        }
When we ran the client code again, it produced the following output:

        [23d 26m, 15d 48m]
Then I discussed how to include a method that would add two of these angles together. In a sense, what I'd like to be able to say in the client code is something like this:

        Angle a1 = new Angle(23, 26);
        Angle a2 = new Angle(15, 48);
        Angle a3 = a1 + a2;
We can't write it that way because the "+" operator is used only for numbers and String concatenation. Some programming languages like C++ allow you to do something called "operator overloading," but Java doesn't allow that. So we have to settle for having a method called "add" that we can use to ask one of these angles to add another one to it:

        Angle a1 = new Angle(23, 26);
        Angle a2 = new Angle(15, 48);
        Angle a3 = a1.add(a2);
I pointed out that this is a common convention in the Java class libraries. For example, there is a class called BigInteger for storing very large integers that has a method called add that is similar.

We wrote this as the first version of our add method:

        public Angle add(Angle other) {
            int d = degrees + other.degrees;
            int m = minutes + other.minutes;
            return new Angle(d, m);
        }

I asked people if there is anything odd about this and someone said that we're referring to other.degrees and other.minutes, which should generate an error because they are private fields. In fact, there is no error generated by this code. This works because the understanding of the word private is that it is "private to the class." This is not at all the way that we as humans understand the meaning of private (if something is private to me, then it shouldn't be available to other humans). But in Java, one Angle object can access private elements of another Angle object because they are both of the same class.

I modified the client code to add this angle to the list as well:

        Angle a1 = new Angle(23, 26);
        Angle a2 = new Angle(15, 48);
        Angle a3 = a1.add(a2);
        ArrayList<Angle> list = new ArrayList<Angle>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        System.out.println(list);
When we ran it, we got the following output:

        [23d 26m, 15d 48m, 38d 74m]
Clearly the program has added the two angles to get a third, but the third angle is listed as having 74 minutes, which isn't right. The first thing we did was to discuss the constructor. We added the following precondition:

        // pre: minutes <= 59 and minutes >= 0 and degrees >= 0
        public Angle(int degrees, int minutes) {
            this.degrees = degrees;
            this.minutes = minutes;
        }
Of course, just documenting the precondition doesn't fix our code. This is not a very satisfying situation. We don't generally want to rely on clients to do the right thing. It is better to test the value of the degrees and minutes and to take an appropriate action if they are not legal. But what would be the appropriate action? Java has a built-in mechanism for handling such cases. In Java, you can throw an exception with a throw statement which has the following form:

throw <exception>; Exceptions are stored as objects in Java. I pointed out that there are many different kinds of exception objects. When a method is passed an illegal value to a parameter, you can construct an IllegalArgumentException. So we can rewrite the constructor as follows:

    // pre: minutes <= 59 and minutes >= 0 and degrees >= 0
    //      (throws IllegalArgumentException if not true)
    public Angle(int degrees, int minutes) {
        if (minutes < 0 || minutes > 59 || degrees < 0)
            throw new IllegalArgumentException();
        this.degrees = degrees;
        this.minutes = minutes;
    }
I pointed out that when a method throws an exception, it should be clearly documented for the client (including the name of the exception), as in the example above.

If you want, you can include a message as a String when you construct the exception. For example, it would be useful to let the client know what the values were that caused the exception:

        throw new IllegalArgumentException("degrees: " + degrees
                                           + " minutes: " + minutes);
When we ran our client code using this version of the class, it produced the following error message:

        Exception in thread "main" java.lang.IllegalArgumentException
                at Angle.(Angle.java:9)
                at Angle.add(Angle.java:23)
                at Wed.main(Wed.java:7)
The error message is showing a backtrace of execution, so you read it backwards. It indicates that we started in line 7 of Wed.java in the main method of Wed. Then we were in line 23 of the add method of the Angle class. Then we got to line 9 of the Angle constructor. This kind of information is helpful in understanding why the error occurred.

In this case the problem is with our add method. We need to handle the case where the minutes become larger than 59. We were able to fix this with a simple if statement:

        public Angle add(Angle other) {
            int d = degrees + other.degrees;
            int m = minutes + other.minutes;
            if (m >= 60) {
                m -= 60;
                d++;
            }
            return new Angle(d, m);
        }
It also would have been possible to use integer division and the mod operator to figure this out.

When we ran the client code again, we didn't get an error. We got this output, indicating that it had correctly added the two angles together:

        [23d 26m, 15d 48m, 39d 14m]
We then added two accessor methods to the class called getDegrees and getMinutes to allow the client to request the degrees and minutes. These are sometimes called "getters," as their names suggest. We decided not to have any "setter" methods (also known as "mutators"). I pointed out that Joshua Bloch's tip 13 in Effective Java is to "Favor immutability." There are advantages to knowing that an object cannot be changed. The String class, for example, is an immutable class.

In the last few minutes of class, I mentioned that I wanted to explore how to modify the class so that we can put a collection of Angle objects into sorted order. For example, I added the following client code to add some randomly generated Angle objects to our list and to put them into sorted order:

        Random r = new Random();
        for (int i = 0; i < 5; i++)
            list.add(new Angle(r.nextInt(90), r.nextInt(60)));
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
Unfortunately, this code did not compile. That's because we haven't told Java how to compare Angle objects to put them into order. For example, we know that an angle of 45 degrees and 15 minutes is more than an angle of 30 degrees and 55 minutes, but how is Java supposed to figure that out?

If you want to use utilities like Arrays.sort and Collections.sort, you have to indicate to Java how to compare values to figure out their ordering. There is an interface in Java known as the Comparable interface that captures this concept.

Not every class implements Comparable because not all data can be put into sorted order in an intuitive way. For example, the Point class does not implement the Comparable interface because it's not clear what would make one two-dimensional point "less than" another two-dimensional point. But many of the standard classes we have seen like String and Integer implement the Comparable interface. Classes that implement the Comparable interface can be used for many built-in operations like sorting and searching. Some people pronounce it as "come-pair-a-bull" and some people pronounce it as "comp-ra-bull". Either pronunciation is okay. The interface contains a single method called compareTo.

        public interface Comparable<T> {
            public int compareTo(T other);
        }
So what does compareTo return? The rule in Java is that:

We're going to discuss interfaces in more detail later in the quarter, but for now, the important thing to know is that we have to include an appropriate compareTo method and we have to modify the class header to look like this:

        public class Angle implements Comparable<Angle> {
            ...
        }
We completed the compareTo method fairly quickly. I pointed out that in general the degrees field tells you which Angle is larger, so we can almost get by with writing the method this way:

        public int compareTo(Angle other) {
            return degrees - other.degrees;
        }
Consider the case where an Angle object has a value for degrees that is larger than the other object's value of degrees. Then the expression above returns a positive integer, which indicates that the object is greater. If an Angle object has a lower value for degrees than the other one, then this expression returns a negative value, which indicates that it is less than the other Angle object.

The only case where this fails is when the degree values are equal. In that case, we should use the minutes fields in a similar manner to break the tie. So the right way to code this is as follows:

        public int compareTo(Angle other) {
            if (degrees == other.degrees)
                return minutes - other.minutes;
            else
                return degrees - other.degrees;
        }
When we ran the client code with this new version of the code, we got output like the following:

        [23d 26m, 15d 48m, 39d 14m]
        [23d 26m, 15d 48m, 39d 14m, 13d 57m, 57d 59m, 3d 21m, 30d 42m, 10d 19m]
        [3d 21m, 10d 19m, 13d 57m, 15d 48m, 23d 26m, 30d 42m, 39d 14m, 57d 59m]
We went through this somewhat quickly, but I said that we'd have several more examples in section.


Stuart Reges
Last modified: Wed Apr 7 17:35:01 PDT 2010