# 8.13.3. Free Response - Horse Barn B¶

The following is part a of a free response question from 2012. It was question 3 on the exam. You can see all the free response questions from past exams at https://apstudent.collegeboard.org/apcourse/ap-computer-science-a/exam-practice.

Question 3. Consider a software system that models a horse barn. Classes that represent horses implement the following interface.

```public interface Horse
{
/** @return the horse's name */
String getName();

/** @return the horse's weight */
int getWeight();
}
```

A horse barn consists of N numbered spaces. Each space can hold at most one horse. The spaces are indexed starting from 0; the index of the last space is N - 1. No two horses in the barn have the same name. The declaration of the HorseBarn class is shown below.

Part b. Write the HorseBarn method consolidate. This method consolidates the barn by moving horses so that the horses are in adjacent spaces, starting at index 0, with no empty spaces between any two horses. After the barn is consolidated, the horses are in the same order as they were before the consolidation.

Figure 1: Example calls and results

```public class HorseBarn
{
/** The spaces in the barn. Each array element holds a reference to the horse
* that is currently occupying the space. A null value indicates an empty
* space.
*/
private Horse[] spaces;

/** Consolidates the barn by moving horses so that the horses are in
*  adjacent spaces, starting at index 0, with no empty space between
*  any two horses.
* Postcondition: The order of the horses is the same as before the
*  consolidation.
*/
public void consolidate()
{ /* to be implemented in part (b) */ }
}
```

## 8.13.3.1. How to solve this problem¶

One way to solve this problem is to create a temporary array the same size as `spaces` and then loop through the current `spaces` array and if the current element isn’t null copy it to the temporary array.

7-14-1: Which loop should you use to solve this problem?

• for
• Use a for loop when you know how many times a loop needs to execute and need the index.
• for each
• Use a for each if you want to loop through all the elements in a collection and don't need an index.
• while
• Use a while loop when you don't know how many times a loop needs to execute.

While we are looping through the `spaces` array, we need to check for non-null positions.

7-14-2: How do we check if the space at the current index isn’t null?

• if (spaces.get(index) != null)
• This is the syntax for checking an element within an ArrayList.
• if (!spaces[index].null())
• Is null() a standard Java method? Comparing an object with a null value is simpler.
• if (spaces[index] != null)
• "!=" is the best way to compare an element with a null value.

Try to write the code for the method `consolidate` in the `HorseBarn` class. When you are ready click “Run” to test your solution.

## 8.13.3.2. Video - One way to code the solution¶

The following video is also on YouTube at https://youtu.be/3HytvgdLCNI. It walks through coding a solution.

Video: (3HytvgdLCNI)