Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Horse
- // Spec: See if you can implement the two methods from part a and part b
- // on paper… this is what you will be doing on the actual exam.
- public class Horsey implements Horse
- {
- private String name;
- private int weight;
- /** Default Constructor - empty name and 0 weight */
- public Horsey()
- {
- name = "";
- weight = 0;
- }
- /** Initialization Constructor - sets name and weight */
- public Horsey(String horseName, int horseWeight)
- {
- name = horseName;
- weight = horseWeight;
- }
- /** This returns the weight of the instance variable name */
- public String getName()
- {
- return name;
- }
- /** This returns the weight of the instance variable weight */
- public int getWeight()
- {
- return weight;
- }
- // This class implements the Horse interface
- // Look at that interface and add any methods
- // that need to be implemented here...
- // an interface is a contract! :)
- /** @return a String with the name of the horse
- * followed by a tab, followed by it's weight */
- public String toString()
- {
- String result = "";
- result += name + " Weight: " + weight + "\n";
- return result;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Horse
- // Spec: See if you can implement the two methods from part a and part b
- // on paper… this is what you will be doing on the actual exam.
- public interface Horse
- {
- /** @return the horse's name */
- public String getName();
- /** @return the horse's weight */
- public int getWeight();
- // There may be methods that are not shown.
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Horse
- // Spec: See if you can implement the two methods from part a and part b
- // on paper… this is what you will be doing on the actual exam.
- import java.util.Arrays;
- 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;
- /** Implemented but not currently used
- * To utilize this we would need to add an assignHorse() method
- * For now, just use it to instantiate the spaces array to the appropriate size
- */
- public HorseBarn(int size)
- {
- spaces = new Horse[size];
- }
- /** spaces[] point to the same Horse array as the parameter */
- public HorseBarn(Horse[] horses)
- {
- spaces = horses;
- }
- /** 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.
- * Horses Part A
- * Mr. A Note: We need to check for null first to avoid a NullPointerException.
- */
- public int findHorseSpace(String name)
- {
- int r = 0;
- for(int i = 0; i < spaces.length - 1; i++)
- {
- if (spaces[i] != null && spaces[i].getName().equals(name))
- {
- r = i;
- }
- else
- {
- r = -1;
- }
- }
- return r;
- }
- /** 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.
- * Horses Part B
- Tip: For consolidate, Mr. A would keep two separate indices.
- One as you go, and the other for the index of horse spaces that are not NULL.
- */
- public void consolidate()
- {
- int nextSpace = 0;
- for(int i = 0; i < spaces.length; i++)
- {
- if(spaces[i] != null)
- {
- spaces[nextSpace++] = spaces[i];
- }
- }
- for(int i = nextSpace; i < spaces.length; i++)
- {
- spaces[i] = null;
- }
- }
- /** @return a String that shows "Empty Stall" or the horses Name and Weight
- * each stall should appear on it's own line
- */
- public String toString()
- {
- String result = "Horses on the farm: \n\n";
- for (int i = 0; i < spaces.length - 1; i++)
- {
- if (spaces[i] != null)
- {
- result += spaces[i].toString();
- }
- else
- {
- result += "Empty Stall\n";
- }
- }
- return result;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Horse
- // Spec: See if you can implement the two methods from part a and part b
- // on paper… this is what you will be doing on the actual exam.
- public class HorseBarnRunner
- {
- public static void main(String[] args)
- {
- Horse trigger = new Horsey("Trigger", 1340);
- Horse one = null;
- Horse silver = new Horsey("Silver", 1210);
- Horse lady = new Horsey("Lady", 1575);
- Horse four = null;
- Horse patches = new Horsey("Patches", 1350);
- Horse duke = new Horsey("Duke", 1410);
- Horse[] horses = {trigger, one, silver, lady, four, patches, duke};
- HorseBarn mrAsFarm = new HorseBarn(horses);
- System.out.println("Trigger is in slot number " + mrAsFarm.findHorseSpace("Trigger"));
- System.out.println("Patches is in slot number " + mrAsFarm.findHorseSpace("Patches"));
- System.out.println("\nMr A's Horse Farm BEFORE Consolidation:");
- System.out.println(mrAsFarm);
- System.out.println("\nMr A's Horse Farm AFTER Consolidation:");
- mrAsFarm.consolidate();
- System.out.println(mrAsFarm);
- }
- }
- /*
- Trigger is in slot number -1
- Patches is in slot number 5
- Mr A's Horse Farm BEFORE Consolidation:
- Horses on the farm:
- Trigger Weight: 1340
- Empty Stall
- Silver Weight: 1210
- Lady Weight: 1575
- Empty Stall
- Patches Weight: 1350
- Mr A's Horse Farm AFTER Consolidation:
- Horses on the farm:
- Trigger Weight: 1340
- Silver Weight: 1210
- Lady Weight: 1575
- Patches Weight: 1350
- Duke Weight: 1410
- Empty Stall
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement