Unit 4A - Free Response - Horse Barn A
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exercise3 graded tests
Files
Unit 4A - Free Response - Horse Barn A
Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.
The following FRQ is a great example of working with an array of objects. It is a variation of 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 Past AP CSA Exams .
The original question had an interface called Horse, but the problem below has been changed to a class Horse instead of the interface. Interfaces are no longer covered on the AP CSA exam. However, you can still solve problems that have interfaces in them by changing them to a class, since an interface just describes the methods that a class must have.
Question 3. Consider a software system that models a horse barn.
public class Horse
{
/** @return the horse's name */
public String getName()
{ implementation not shown }
/** @return the horse's weight */
public int getWeight()
{ implementation not shown }
// There may be other methods that are not shown
}
Another class called HorseBarn consists of N numbered spaces where 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.
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) */
}
}
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.

Example calls and results
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).
Check your understanding: Which of the following correctly retrieves the name of a “Horse” object from the “spaces” array? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/horseBarnA.html#frhba_1
Once we have the name of the current Horse object, we need to compare this name to the name we are looking for. Here's a practice problem about String comparisons.
Check your understanding: What is the best way to compare two strings for equality? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/horseBarnA.html#frhba_2
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.
Exercise 1
FRQ HorseBarn A: Write the method findHorseSpace.
Open the
01-lcfrhba1folder and editHorseBarn.java. Press Run to try it; Run Tests checks it (3 tests).
Video - One way to code the solution
The following video is also on YouTube at https://youtu.be/sk9i_mhrc5M. It walks through coding a solution.
For teachers
Unit 4A - Free Response - Horse Barn A
Students read CSAwesome2's “Unit 4A - Free Response - Horse Barn A” in CS Room and work through its 1 Java coding exercise.
- Time: 1 class period
- Tests: 3 in 1 graded exercise, set up for you when you import the lesson
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page, with its multiple-choice questions, is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/horseBarnA.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. Questions that need CS Awesome's interactive widgets link back to it. - Students do exercise 01 in its folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-lcfrhba1 | HorseBarn.java | Autograded | 3 |
Teachers can download a reference solution after signing in.