1. /**
  2. * Class SharkAttack - Simulates a shark attacking a swimmer.
  3. *
  4. * @author
  5. * @version (1.0)
  6. */
  7. import ou.*;
  8. public class SharkAttack
  9. {
  10. /* instance variables */
  11. private Square sea;
  12. private Triangle mountain1;
  13. private Triangle mountain2;
  14. private Circle sun;
  15. private Circle head;
  16. private Circle body;
  17. private Square legs;
  18. private Triangle shark;
  19. private Circle blood;
  20.  
  21. /**
  22. * Constructor for objects of class SharkAttack
  23. */
  24. public SharkAttack(Square s1, Triangle t1, Triangle t2, Circle c1, Circle c2, Circle c3, Square s2, Triangle t3, Circle c4)
  25. {
  26. super();
  27.  
  28. // set the instance variables to the arguments
  29. this.sun = c1;
  30. this.mountain1 = t1;
  31. this.mountain2 = t2;
  32. this.sea = s1;
  33. this.head = c2;
  34. this.body = c3;
  35. this.legs = s2;
  36. this.shark = t3;
  37. this.blood = c4;
  38.  
  39. // initialises mountain1
  40. mountain1.setYPos(110);
  41. mountain1.setXPos(180);
  42. mountain1.setHeight(40);
  43. mountain1.setWidth(50);
  44. mountain1.setColour(OUColour.GREEN);
  45.  
  46. // initialise mountain2
  47. mountain2.setYPos(60);
  48. mountain2.setHeight(90);
  49. mountain2.setWidth(100);
  50. mountain2.setColour(OUColour.GREEN);
  51. mountain2.setXPos(200);
  52.  
  53. // initialise the sun
  54. sun.setDiameter(50);
  55. sun.setColour(OUColour.YELLOW);
  56. sun.setXPos(150);
  57.  
  58. // initialise the sea
  59. sea.setLength(300);
  60. sea.setColour(OUColour.BLUE);
  61. sea.setYPos(145);
  62. sea.setXPos(0);
  63.  
  64. // initialise the swimmer, shark and blood
  65. this.reset();
  66. }
  67.  
  68.  
  69. /**
  70. * Resets the positions of the 'moveable' elements of the seascape
  71. * (the shark, the swimmer and the blood) back to their default
  72. * colours/positions
  73. */
  74. public void reset()
  75. {
  76. this.getHead().setDiameter(20);
  77. this.getHead().setColour(OUColour.PINK);
  78. this.getHead().setXPos(116);
  79. this.getHead().setYPos(140);
  80.  
  81. this.getBody().setDiameter(40);
  82. this.getBody().setColour(OUColour.PINK);
  83. this.getBody().setXPos(80);
  84. this.getBody().setYPos(135);
  85.  
  86. this.getLegs().setColour(OUColour.PINK);
  87. this.getLegs().setYPos(140);
  88. this.getLegs().setXPos(70);
  89. this.getLegs().setLength(20);
  90.  
  91. this.getShark().setXPos(0);
  92. this.getShark().setYPos(135);
  93. this.getShark().setColour(OUColour.BLACK);
  94. this.getShark().setWidth(30);
  95. this.getShark().setHeight(12);
  96.  
  97. this.getBlood().setYPos(145);
  98. this.getBlood().setXPos(138);
  99. this.getBlood().setDiameter(0);
  100. this.getBlood().setColour(OUColour.BLUE);
  101. }
  102.  
  103.  
  104. /**
  105. * Returns the shark
  106. */
  107. public Triangle getShark()
  108. {
  109. return this.shark;
  110. }
  111.  
  112.  
  113. /**
  114. * Returns the head of the swimmer
  115. */
  116. public Circle getHead()
  117. {
  118. return this.head;
  119. }
  120.  
  121.  
  122. /**
  123. * Returns the body of the swimmer
  124. */
  125. public Circle getBody()
  126. {
  127. return this.body;
  128. }
  129.  
  130.  
  131. /**
  132. * Returns the legs of the swimmer
  133. */
  134. public Square getLegs()
  135. {
  136. return this.legs;
  137. }
  138.  
  139.  
  140. /**
  141. * Returns the blood
  142. */
  143. public Circle getBlood()
  144. {
  145. return this.blood;
  146. }
  147.  
  148.  
  149. /**
  150. * Causes the shark to move one unit to the right of its current position.
  151. */
  152. public void moveShark()
  153. {
  154. this.getShark().setXPos(this.getShark().getXPos() + 1);
  155. }
  156.  
  157.  
  158. /**
  159. * Sets the x-position of the shark to the value of the argument
  160. */
  161. public void setXPosShark(int x)
  162. {
  163. // to be completed for part(i)
  164. }
  165.  
  166.  
  167. /**
  168. * Sets the x-position of each of the legs, body and head of the swimmer.
  169. * The x position of the legs is set to the argument, the x position of
  170. * the body is set to the argument plus 10, and the x position of the
  171. * head is set to the argument plus 46.
  172. */
  173.  
  174. public void setXPosSwimmer(int x)
  175. {
  176. // to be completed for part(ii)
  177. }
  178.  
  179.  
  180. /**
  181. * Makes the swimmer appear to disappear by increasing the
  182. * y-position of each of the head, body and legs by 50, and
  183. * turning their colour blue.
  184. */
  185. public void sinkSwimmer()
  186. {
  187. //to be completed for part(iii)
  188. }
  189.  
  190.  
  191. /**
  192. * Causes the swimmer to move one unit to the right of its current position.
  193. */
  194. public void moveSwimmer()
  195. {
  196. // to be completed for part(iv)
  197. }
  198.  
  199.  
  200. /**
  201. * Asks the user to enter a number between 20 and 200
  202. * as a headstart for the swimmer. Then the x position
  203. * of the swimmer is incremented by this inputted value
  204. */
  205. public void giveHeadStart()
  206. {
  207. // to be completed for part(v)
  208. }
  209.  
  210.  
  211. /**
  212. * Sets the colour of the blood to red, and its x position
  213. * to be the same as the x position of the body of the swimmer.
  214. * Then the following sequence of events are performed 12 times:
  215. * diameter of the blood increased by 5,
  216. * x position of the blood increased by 3,
  217. * a delay of 200 milliseconds is caused.
  218. * Finally, the method sinks the swimmer.
  219. */
  220. public void eat()
  221. {
  222. // to be completed for part(vi)
  223. }
  224.  
  225.  
  226. /**
  227. * Prompts the user to provide a vaue for the swimmer's headstart.
  228. * While the shark has not caught the swimmer (i.e. the x position
  229. * of the shark is less than the x position of the swimmer's legs),
  230. * and the swimmer has not yet escaped (i.e. the swimmer's head has an
  231. * x position of less than 280), the following actions are performed:
  232. * the shark moves,
  233. * the swimmer moves,
  234. * the shark moves again,
  235. * a delay of 200 milliseconds is caused.
  236. * If the swimmer has been caught, the shark eats him, and a dialog box
  237. * is produced to say "Shark eaten swimmer". Otherwise a dialog box is
  238. * produced to say "Swimmer escaped"
  239. */
  240. public void chase()
  241. {
  242. // to be completed for part (vii)
  243. }
  244.  
  245.  
  246. private void delay(int time)
  247. {
  248. try
  249. {
  250. Thread.sleep(time);
  251. }
  252. catch (Exception e)
  253. {
  254. System.out.println(e);
  255. }
  256. }
  257.  
  258. }
  259.  
  260.  
  261.  
  262.  
  263. QUESTIONS
  264. (i) Complete the method sexXPosShark() which sets the x-position of the shark to the method's int arguement
  265.  
  266. (ii) Complete the method setXPos Swimmer() which sets the x-position of the legs of the swimmer to the arguement x, the x-position of the body to x plus 10 and the x-position of the head to x plus 46.
  267.  
  268. (iii) Complete the method sinkSwimmer (). The method should move each component (head, body and legs) of the swimmer down by 50 units, and make the colour of each component blue.
  269.  
  270. (iv) Complete the method moveSwimmer() which causes the components of the swimmer to move one unit to the right of their current positions.
  271.  
  272.  
  273.