Advertisement
Crenox

Horse Java Program

Mar 6th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. // Sammy Samkough
  2. // Horse
  3. // Spec: See if you can implement the two methods from part a and part b
  4. // on paper… this is what you will be doing on the actual exam.
  5.  
  6. public class Horsey implements Horse
  7. {
  8. private String name;
  9. private int weight;
  10.  
  11. /** Default Constructor - empty name and 0 weight */
  12. public Horsey()
  13. {
  14. name = "";
  15. weight = 0;
  16. }
  17.  
  18. /** Initialization Constructor - sets name and weight */
  19. public Horsey(String horseName, int horseWeight)
  20. {
  21. name = horseName;
  22. weight = horseWeight;
  23. }
  24.  
  25. /** This returns the weight of the instance variable name */
  26. public String getName()
  27. {
  28. return name;
  29. }
  30.  
  31. /** This returns the weight of the instance variable weight */
  32. public int getWeight()
  33. {
  34. return weight;
  35. }
  36.  
  37. // This class implements the Horse interface
  38. // Look at that interface and add any methods
  39. // that need to be implemented here...
  40. // an interface is a contract! :)
  41.  
  42. /** @return a String with the name of the horse
  43. * followed by a tab, followed by it's weight */
  44. public String toString()
  45. {
  46. String result = "";
  47.  
  48. result += name + " Weight: " + weight + "\n";
  49.  
  50. return result;
  51. }
  52. }
  53.  
  54. ------------------------------------------------------------------------------------------------------------------------------
  55. // Sammy Samkough
  56. // Horse
  57. // Spec: See if you can implement the two methods from part a and part b
  58. // on paper… this is what you will be doing on the actual exam.
  59.  
  60. public interface Horse
  61. {
  62. /** @return the horse's name */
  63. public String getName();
  64.  
  65. /** @return the horse's weight */
  66. public int getWeight();
  67.  
  68. // There may be methods that are not shown.
  69. }
  70. ------------------------------------------------------------------------------------------------------------------------------
  71. // Sammy Samkough
  72. // Horse
  73. // Spec: See if you can implement the two methods from part a and part b
  74. // on paper… this is what you will be doing on the actual exam.
  75.  
  76. import java.util.Arrays;
  77.  
  78. public class HorseBarn
  79. {
  80. /** The spaces in the barn. Each array element holds a reference to the horse
  81. * that is currently occupying the space. A null value indicates an empty space.
  82. */
  83. private Horse[] spaces;
  84.  
  85. /** Implemented but not currently used
  86. * To utilize this we would need to add an assignHorse() method
  87. * For now, just use it to instantiate the spaces array to the appropriate size
  88. */
  89. public HorseBarn(int size)
  90. {
  91. spaces = new Horse[size];
  92. }
  93.  
  94. /** spaces[] point to the same Horse array as the parameter */
  95. public HorseBarn(Horse[] horses)
  96. {
  97. spaces = horses;
  98. }
  99.  
  100. /** Returns the index of the space that contains the horse with the specified name.
  101. * Precondition: No two horses in the barn have the same name.
  102. * @param name the name of the horse to find
  103. * @return the index of the space containing the horse with the specified name;
  104. * -1 if no horse with the specified name is in the barn.
  105. * Horses Part A
  106. * Mr. A Note: We need to check for null first to avoid a NullPointerException.
  107. */
  108. public int findHorseSpace(String name)
  109. {
  110. int r = 0;
  111.  
  112. for(int i = 0; i < spaces.length - 1; i++)
  113. {
  114. if (spaces[i] != null && spaces[i].getName().equals(name))
  115. {
  116. r = i;
  117. }
  118. else
  119. {
  120. r = -1;
  121. }
  122. }
  123.  
  124. return r;
  125. }
  126.  
  127. /** Consolidates the barn by moving horses so that the horses are in adjacent spaces,
  128. * starting at index 0, with no empty space between any two horses.
  129. * Postcondition: The order of the horses is the same as before the consolidation.
  130. * Horses Part B
  131. Tip: For consolidate, Mr. A would keep two separate indices.
  132. One as you go, and the other for the index of horse spaces that are not NULL.
  133. */
  134. public void consolidate()
  135. {
  136. int nextSpace = 0;
  137.  
  138. for(int i = 0; i < spaces.length; i++)
  139. {
  140. if(spaces[i] != null)
  141. {
  142. spaces[nextSpace++] = spaces[i];
  143. }
  144. }
  145.  
  146. for(int i = nextSpace; i < spaces.length; i++)
  147. {
  148. spaces[i] = null;
  149. }
  150. }
  151.  
  152. /** @return a String that shows "Empty Stall" or the horses Name and Weight
  153. * each stall should appear on it's own line
  154. */
  155. public String toString()
  156. {
  157. String result = "Horses on the farm: \n\n";
  158.  
  159. for (int i = 0; i < spaces.length - 1; i++)
  160. {
  161. if (spaces[i] != null)
  162. {
  163. result += spaces[i].toString();
  164. }
  165. else
  166. {
  167. result += "Empty Stall\n";
  168. }
  169. }
  170.  
  171. return result;
  172. }
  173. }
  174. ------------------------------------------------------------------------------------------------------------------------------
  175. // Sammy Samkough
  176. // Horse
  177. // Spec: See if you can implement the two methods from part a and part b
  178. // on paper… this is what you will be doing on the actual exam.
  179.  
  180. public class HorseBarnRunner
  181. {
  182. public static void main(String[] args)
  183. {
  184. Horse trigger = new Horsey("Trigger", 1340);
  185. Horse one = null;
  186. Horse silver = new Horsey("Silver", 1210);
  187. Horse lady = new Horsey("Lady", 1575);
  188. Horse four = null;
  189. Horse patches = new Horsey("Patches", 1350);
  190. Horse duke = new Horsey("Duke", 1410);
  191.  
  192. Horse[] horses = {trigger, one, silver, lady, four, patches, duke};
  193.  
  194. HorseBarn mrAsFarm = new HorseBarn(horses);
  195.  
  196. System.out.println("Trigger is in slot number " + mrAsFarm.findHorseSpace("Trigger"));
  197. System.out.println("Patches is in slot number " + mrAsFarm.findHorseSpace("Patches"));
  198.  
  199. System.out.println("\nMr A's Horse Farm BEFORE Consolidation:");
  200. System.out.println(mrAsFarm);
  201. System.out.println("\nMr A's Horse Farm AFTER Consolidation:");
  202. mrAsFarm.consolidate();
  203. System.out.println(mrAsFarm);
  204. }
  205. }
  206. /*
  207. Trigger is in slot number -1
  208. Patches is in slot number 5
  209.  
  210. Mr A's Horse Farm BEFORE Consolidation:
  211. Horses on the farm:
  212.  
  213. Trigger Weight: 1340
  214. Empty Stall
  215. Silver Weight: 1210
  216. Lady Weight: 1575
  217. Empty Stall
  218. Patches Weight: 1350
  219.  
  220.  
  221. Mr A's Horse Farm AFTER Consolidation:
  222. Horses on the farm:
  223.  
  224. Trigger Weight: 1340
  225. Silver Weight: 1210
  226. Lady Weight: 1575
  227. Patches Weight: 1350
  228. Duke Weight: 1410
  229. Empty Stall
  230.  
  231. Press any key to continue . . .
  232. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement