# Free Response - Horse Barn A¶

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 a. Write the HorseBarn method findHorseSpace. This method returns the index of the space in which the horse with the specified name is located. If there is no horse with the specified name in the barn, the method returns -1.

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;

/** Returns the index of the space that contains the horse with the specified
*  name.
* Precondition: No two horses in the barn have the same name.
* @param name the name of the horse to find
* @return the index of the space containing the horse with the specified
*      name;
*      -1 if no horse with the specified name is in the barn.
*/
public int findHorseSpace(String name)
{ /* to be implemented in part (a) */ }
}


## How to solve this problem¶

In order to find the index of the horse with the same name we are looking for, we will need to loop through the array spaces. As we loop, we will compare the name we are looking for with the Horse object’s name at the current index. We will have to watch out for spaces that are empty (are null).

7-13-1: Which of the following correctly retrieves the name of a “Horse” object from the “spaces” array?
• (A) spaces[index].name;
• Getter methods are needed to access private class variables.
• (B) spaces[index].getName();
• This is the syntax for getting the value of an element in an array.
• (C) spaces.get(index).getName();
• This is the syntax for getting the value of an element in an arrayList.

Once we have the name of the current Horse object, we need to compare this name to the name we are looking for.

7-13-2: What is the best way to compare two strings for equality?
• (A) str.compareTo(anotherString);
• This String method is used for comparing two strings alphabetically. It returns 0 if they are equal so you would need to check the return value.
• (B) str == anotherString;
• This would only return true if the two variables refer to the same object.
• (C) str.equals(anotherString);
• This String method will compare the characters in both strings and return true if they are the same.

## Try It!¶

Try to write the code for the method findHorseSpace in the HorseBarn class. When you are ready click “Run” to test your solution. There are 3 tests so if you only see output for 1 or 2 check for errors below the code.

## Video - One way to code the solution¶

Next Section - Free Response - Horse Barn B