2012 Horse Barn

<< 2012 RetroBug | APQuestionsTrailIndex | 2012 GrayImage >>

Click here to see the questions from 2012

Horse.java

public interface Horse
{
	/** @return the horse's name */ 
	String getName();
	/** @return the horse's weight */ 
	int getWeight();
	// There may be methods that are not shown.
}

HorseBarn.java

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) */ 

	}
	/** 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) */ 
	}
	// There may be instance variables, constructors, and methods that are not shown.
	public HorseBarn(int size)
	{
		spaces=new Horse[size];
	}
	public void add(int i, Horse h)
	{
		spaces[i]=h;
	}
	public void remove(int i)
	{
		spaces[i]=null;
	}
	public String toString()
	{	
		String s1="", s2="", s3="";
		for (int i=0;i<spaces.length;i++){
			s1=s1+String.format("[ %7d ]", i);
			if(spaces[i]==null){
				s2=s2+String.format("[ %7s ]", "null");
				s3=s3+String.format("[ %7s ]", " ");
			}else {
				s2=s2+String.format("[ %7s ]", spaces[i].getName());
				s3=s3+String.format("[ %7d ]", spaces[i].getWeight());
			}
		}
		return s1+"\n"+s2+"\n"+s3;
	}
}

Palomino.java

class Palomino implements Horse
{
	private String name;
	private int weight;
	public Palomino(String name, int weight)
	{
		this.name=name;
		this.weight=weight;
	}
	@Override
	public String getName() {
		return name;
	}

	@Override
	public int getWeight() {
		return weight;
	}

}

HorseBarnTester.java


public class HorseBarnTester 
{

	public static void main(String[] args)
	{
		HorseBarn sweetHome = new HorseBarn(7);
		sweetHome.add(0, new Palomino("Trigger", 1340));
		sweetHome.add(2, new Palomino("Silver", 1210));
		sweetHome.add(3, new Palomino("Lady", 1575));
		sweetHome.add(5, new Palomino("Patches", 1350));
		sweetHome.add(6, new Palomino("Duke", 1410));
		System.out.println("Part A\n"+sweetHome);
		System.out.println("sweetHome.findHorseSpace(\"Trigger\") should return 0 ");
		System.out.println("your code returns "+sweetHome.findHorseSpace("Trigger"));
		System.out.println("sweetHome.findHorseSpace(\"Silver\") should return 2 ");
		System.out.println("your code returns "+sweetHome.findHorseSpace("Silver"));
		System.out.println("sweetHome.findHorseSpace(\"Coco\") should return -1 ");
		System.out.println("your code returns "+sweetHome.findHorseSpace("Coco"));

		sweetHome.remove(3);
		System.out.println("\nPart B should consolidate\n"+sweetHome);
		sweetHome.consolidate();
		System.out.println("\nafter your consolidate code we should have all the null spaces at the end: 4,5,6\n"+sweetHome);
	}
}