Unit 4A - Free Response - Horse Barn B
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exercise2 graded tests
Files
Unit 4A - Free Response - Horse Barn B
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 is a variation of part b 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
}
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.
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) */
}
}
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.

Example calls and results
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. What kind of loop should you use? A for loop or an enhanced for loop would work for this problem. You will need an index for at least the temporary array.
While we are looping through the spaces array, we need to check for non-null positions. Here's a practice problem about checking for null values.
Check your understanding: How do we check if the space at the current index isn’t null? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/horseBarnB.html#frhbb_2
Exercise 1
Try to write the code for the method consolidate in the HorseBarn class. When you are ready click “Run” to test your solution.
Open the
01-lcfrhbb1folder and editHorseBarn.java. Press Run to try it; Run Tests checks it (2 tests).
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.
For teachers
Unit 4A - Free Response - Horse Barn B
Students read CSAwesome2's “Unit 4A - Free Response - Horse Barn B” in CS Room and work through its 1 Java coding exercise.
- Time: 1 class period
- Tests: 2 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/horseBarnB.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-lcfrhbb1 | HorseBarn.java | Autograded | 2 |
Teachers can download a reference solution after signing in.