Advertisement
Crenox

Monster Interface Java Program

Feb 20th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. // Name: Sammy Samkough
  2. // Prog: Monster
  3. // Spec: In this project, you will be completing methods for a Monster class. Because the Monster class implements Monsterable,
  4. // you will need to implement each of the methods as shown in the Monsterable interface. Monsterable contains accessor
  5. // and modifier methods, along with equals(), compareTo, clone() and toString() methods.
  6.  
  7. public class Monster implements Monsterable
  8. {
  9. private int myHeight;
  10. private int myWeight;
  11. private int myAge;
  12.  
  13. // write a default Constructor
  14. public Monster()
  15. {
  16. myHeight = 0;
  17. myWeight = 0;
  18. myAge = 0;
  19. }
  20.  
  21. // write an initialization constructor with an int parameter ht
  22. public Monster(int h)
  23. {
  24. myHeight = h;
  25. }
  26.  
  27. // write an initialization constructor with int parameters ht and wt
  28. public Monster(int h, int w)
  29. {
  30. myHeight = h;
  31. myWeight = w;
  32. }
  33.  
  34. // write an initialization constructor with int parameters ht, wt, and age
  35. public Monster(int h, int w, int a)
  36. {
  37. myHeight = h;
  38. myWeight = w;
  39. myAge = a;
  40. }
  41.  
  42. public void setMonster(int h, int w, int a)
  43. {
  44. myHeight = h;
  45. myWeight = w;
  46. myAge = a;
  47. }
  48.  
  49. // modifiers - write set methods for height, weight, and age
  50. public void setHeight(int h)
  51. {
  52. myHeight = h;
  53. }
  54.  
  55. public void setWeight(int w)
  56. {
  57. myWeight = w;
  58. }
  59.  
  60. public void setAge(int a)
  61. {
  62. myAge = a;
  63. }
  64.  
  65. // accessors - write get methods for height, weight, and age
  66. public int getHeight()
  67. {
  68. return myHeight;
  69. }
  70.  
  71. public int getWeight()
  72. {
  73. return myWeight;
  74. }
  75.  
  76. public int getAge()
  77. {
  78. return myAge;
  79. }
  80.  
  81. // creates a new copy of this Object
  82. public Object clone()
  83. {
  84. return new Monster(myHeight, myWeight, myAge);
  85. }
  86.  
  87. // returns true if height, weight and age are equal, otherwise false
  88. public boolean equals(Object obj)
  89. {
  90. Monster m = (Monster)obj;
  91.  
  92. if (myHeight == m.myHeight && myWeight == m.myWeight && myAge == m.myAge)
  93. {
  94. return true;
  95. }
  96. else
  97. {
  98. return false;
  99. }
  100. }
  101.  
  102. public int compareTo(Object obj)
  103. {
  104. Monster rhs = (Monster)obj;
  105.  
  106. // height
  107. if (myHeight > rhs.myHeight)
  108. {
  109. return 1;
  110. }
  111. else if (myHeight < rhs.myHeight)
  112. {
  113. return -1;
  114. }
  115. // weight
  116. else if (myWeight > rhs.myWeight)
  117. {
  118. return 1;
  119. }
  120. else if (myWeight < rhs.myWeight)
  121. {
  122. return -1;
  123. }
  124. // age
  125. else if (myAge > rhs.myAge)
  126. {
  127. return 1;
  128. }
  129. else if (myAge < rhs.myAge)
  130. {
  131. return -1;
  132. }
  133. else
  134. {
  135. return 0;
  136. }
  137. }
  138.  
  139. // write a toString() method
  140. public String toString()
  141. {
  142. String result = "";
  143.  
  144. result += "The height of the monster is " + myHeight + ".\n";
  145. result += "The weight of the monster is " + myWeight + ".\n";
  146. result += "The age of the monster is " + myAge + ".\n";
  147.  
  148. return result;
  149. }
  150.  
  151. }
  152. -------------------------------------------------------------------------------------------------------------------------------
  153. // Name: Sammy Samkough
  154. // Prog: Monster
  155. // Spec: In this project, you will be completing methods for a Monster class. Because the Monster class implements Monsterable,
  156. // you will need to implement each of the methods as shown in the Monsterable interface. Monsterable contains accessor
  157. // and modifier methods, along with equals(), compareTo, clone() and toString() methods.
  158.  
  159. public interface Monsterable
  160. {
  161. /** sets the height, weight and age for this Monster*/
  162. public void setMonster(int ht, int wt, int age);
  163.  
  164. /** sets the height for this Monster */
  165. public void setHeight(int ht);
  166.  
  167. /** sets the weight for this Monster */
  168. public void setWeight(int wt);
  169.  
  170. /** sets the age for this Monster */
  171. public void setAge(int age);
  172.  
  173. /** gets the height for this Monster */
  174. public int getHeight();
  175.  
  176. /** gets the weight for this Monster */
  177. public int getWeight();
  178.  
  179. /** gets the age for this Monster */
  180. public int getAge();
  181.  
  182. /** creates a new copy of this Object */
  183. public Object clone();
  184.  
  185. /** returns true if height, weight and age are equal, otherwise false */
  186. public boolean equals(Object o);
  187.  
  188. /** returns a value based on the criteria of height, weight and age */
  189. public int compareTo(Object o);
  190.  
  191. /** displays height, weight and age for this Monster */
  192. public String toString();
  193. }
  194. -------------------------------------------------------------------------------------------------------------------------------
  195. // Name: Sammy Samkough
  196. // Prog: Monster
  197. // Spec: In this project, you will be completing methods for a Monster class. Because the Monster class implements Monsterable,
  198. // you will need to implement each of the methods as shown in the Monsterable interface. Monsterable contains accessor
  199. // and modifier methods, along with equals(), compareTo, clone() and toString() methods.
  200.  
  201. public class MonsterRunner
  202. {
  203. public static void main(String[] args)
  204. {
  205. Monster zero = new Monster();
  206. Monster one = new Monster(8);
  207. Monster sam = new Monster(9, 4);
  208. Monster harry = new Monster(1, 2, 3);
  209. System.out.println("\nMonster zero: \n" + zero);
  210. System.out.println("\nMonster one: \n" + one);
  211. System.out.println("\nMonster sam: \n" + sam);
  212. System.out.println("\nMonster harry: \n" + harry);
  213.  
  214. System.out.println("\nChanging harry's properties... ");
  215. harry.setHeight(7);
  216. harry.setWeight(6);
  217. harry.setAge(5);
  218. System.out.println("\nMonster harry: \n" + harry);
  219.  
  220. System.out.println("\nCloning sam into harry...");
  221. sam = (Monster)harry.clone();
  222. System.out.println("\nMonster sam: \n" + sam);
  223.  
  224. Monster mOne = new Monster(33,33,11);
  225. Monster mTwo = new Monster(55,33,11);
  226.  
  227. System.out.println("\nMonster 1: \n" + mOne);
  228. System.out.println("\nMonster 2: \n" + mTwo);
  229.  
  230. System.out.print("\nmOne.equals(mTwo) = ");
  231. System.out.println(mOne.equals(mTwo));
  232.  
  233. System.out.print("\nmOne.compareTo(mTwo) = ");
  234. System.out.println(mOne.compareTo(mTwo));
  235. System.out.print("\nmTwo.compareTo(mOne) = ");
  236. System.out.println(mTwo.compareTo(mOne));
  237. }
  238. }
  239.  
  240. /*
  241.  
  242. Monster zero:
  243. The height of the monster is 0.
  244. The weight of the monster is 0.
  245. The age of the monster is 0.
  246.  
  247.  
  248. Monster one:
  249. The height of the monster is 8.
  250. The weight of the monster is 0.
  251. The age of the monster is 0.
  252.  
  253.  
  254. Monster sam:
  255. The height of the monster is 9.
  256. The weight of the monster is 4.
  257. The age of the monster is 0.
  258.  
  259.  
  260. Monster harry:
  261. The height of the monster is 1.
  262. The weight of the monster is 2.
  263. The age of the monster is 3.
  264.  
  265.  
  266. Changing harry's properties...
  267.  
  268. Monster harry:
  269. The height of the monster is 7.
  270. The weight of the monster is 6.
  271. The age of the monster is 5.
  272.  
  273.  
  274. Cloning sam into harry...
  275.  
  276. Monster sam:
  277. The height of the monster is 7.
  278. The weight of the monster is 6.
  279. The age of the monster is 5.
  280.  
  281.  
  282. Monster 1:
  283. The height of the monster is 33.
  284. The weight of the monster is 33.
  285. The age of the monster is 11.
  286.  
  287.  
  288. Monster 2:
  289. The height of the monster is 55.
  290. The weight of the monster is 33.
  291. The age of the monster is 11.
  292.  
  293.  
  294. mOne.equals(mTwo) = false
  295.  
  296. mOne.compareTo(mTwo) = -1
  297.  
  298. mTwo.compareTo(mOne) = 1
  299. Press any key to continue . . .
  300. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement