Horse Barn Solution

The Horse free response question on the 2012 AP computer science exam was a good one. It covered several topics that you’re expected to know.

First, you’re given an interface that models a horse. Then you’re given another class that contains an array of that interface. So you’re also working with a list of objects, which is usually tested on the AP exam. And since it’s an interface you don’t have a full class to work with, but have to understand that through polymorphism any member of the spaces array has both the getName() and getWeight() methods.

Horse Interface

You’re first given an interface similar to this, although the one given on the exam is more filled out with comments and explanations.

public interface Horse {
	String getName();
	int getWeight(); 
}

What we need to know here is that any class that implements Horse will have both a getName() and a getWeight() method. We just need to know they’re there. Although, oddly the getWeight() is never actually used in this problem.

HorseBarn

You’re also given a class shell named HorseBarn that contains the methods you’ll be implementing along with an instance variable spaces.

public class HorseBarn {
	private Horse[] spaces;
	public int findHorseSpace(String name) {
		// part A
	}
	public void consolidate() {
		// part B
	}
}

findHorseSpace

On part A you’re asked to implement a method named findHorseSpace that looks through spaces to find a horse with a name matching name.

First thing is that we’ll need to call the getName() method on the horses to get their name. We can’t compare the name parameter to each horse, but can compare to their name. This is where understanding about interfaces comes in handy.

Second, we need to account for nulls in spaces. The problem description shows that some of the spaces may be empty, which is shown by them containing null rather than a horse. We can’t call getName() on a null so we need to be careful.

public int findHorseSpace( String name ) {
    for (int i=0; i<spaces.length; i++) {
        if (spaces[i] != null && spaces[i].getName().equals(name))
            return i; 
    }
    return -1; 
}

We’re basically implementing indexOf on spaces. A for loop goes through the list so we can look at each element. When we find a match we return that position. If no match is found we return -1 at the end.

Important to notice though is that we check if spaces[i] != null before calling getName() on spaces[i]. We have to do this to make sure we don’t get a NullPointerException.

consolidate

For part B you had to work with spaces pushing all of the horses to the beginning of the array and all the nulls to the end.

What I did was loop through looking for nulls. When a null was found I started another loop at the next position looking for the first non-null. If I found a horse, I swapped it’s position with the null and killed the loop.

public void consolidate() {
    for (int i=0; i<spaces.length; i++) {
        if (spaces[i] == null) {
            for (int j=i+1; j<spaces.length; j++) {
                if (spaces[j] != null) {
                    spaces[i] = spaces[j];
                    spaces[j] = null;
                    j = spaces.length; 
                }
            }
        }
    }
}

One line I’m not crazy about is j = spaces.length;. I did this to stop the inner loop. Normally though I would have used the break statement, which does the same thing. But since break is not part of the AP Java subset, I did it this way for this demo.

This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.