Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. (20 points) Horse.java and HorseBarn.java: Consider a software system that models a horse barn. public class Horse { /** @return the horse's name
2. (20 points) Horse.java and HorseBarn.java: 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 data fields and methods that are not shown you must implement those } 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 */ } } 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 */ } /** 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 */ } The HorseBarn class should also have methods to add(Horse) and remove(Horse) a horse to the barn, and a getSpaces() method that returns the array of horses. Write the HorseBarn method find HorseSpace(String horseName). 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. 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. Total points: 30 points
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started