Advertisement
jdalbey

Cooties Lab Solution

Apr 12th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. /**
  2. * Cooties App creates a cootie based on dice roll input.
  3.  
  4. * @version 2.0
  5. */
  6. public class CootieApp
  7. {
  8. Die die;
  9. Cootie cootie;
  10. /**
  11. * The entry point for this application
  12. * @param args (ignored)
  13. */
  14. public static void main(String[] args)
  15. {
  16. new CootieApp().run();
  17. }
  18. // Keep obtaining user input until cootie is complete
  19. public void run()
  20. {
  21. cootie = new Cootie();
  22. die = new Die();
  23.  
  24. //Loop until cootie is done
  25. while (!cootie.complete())
  26. {
  27. int diceRoll = die.roll();
  28. cootie.takeTurn(diceRoll);
  29. }
  30. //give congrats message
  31. cootie.showDone();
  32. }
  33. }
  34. import java.util.List;
  35. import java.util.ArrayList;
  36.  
  37. /**
  38. * Cootie is an imaginary bug-like creature.
  39. */
  40. public class Cootie
  41. {
  42. // number of turns take
  43. int turns;
  44. // The main parts of a Cootie
  45. Tail tail;
  46. Head head;
  47. Body body;
  48. List<BodyPart> partsList;
  49. /**
  50. * Constructor for objects of class Cootie
  51. */
  52. public Cootie()
  53. {
  54. turns = 0;
  55. body = new Body();
  56. tail = new Tail();
  57. head = new Head();
  58. // create a list of the body parts
  59. partsList = new ArrayList<BodyPart>();
  60. partsList.add(head);
  61. partsList.add(body);
  62. partsList.add(tail);
  63. }
  64.  
  65. /**
  66. * Perform a turn given a roll of the dice
  67. * @param diceRoll the input number
  68. */
  69. public void takeTurn(int diceRoll)
  70. {
  71. turns += 1;
  72. // See if a body can be added
  73. body.add(diceRoll);
  74. // If we have a body, try to add head and tail
  75. if (body.hasBody())
  76. {
  77. head.add(diceRoll);
  78. tail.add(diceRoll);
  79. }
  80.  
  81. // Draw cootie
  82. for (BodyPart part: partsList)
  83. {
  84. System.out.println(part.draw());
  85. }
  86. }
  87.  
  88. /**
  89. * Is the cootie finished?
  90. * @return true if all parts are drawn
  91. */
  92. public boolean complete()
  93. {
  94. return (body.complete() && tail.complete() && head.complete());
  95. }
  96.  
  97. /**
  98. * Show a congratulations message.
  99. */
  100. public void showDone()
  101. {
  102. System.out.println(" Congratulations you have completed your cootie!");
  103. System.out.println(" It took you " + turns + " turns to finish your cootie. ");
  104. }
  105.  
  106. }
  107.  
  108. /**
  109. * Antenna for a cootie.
  110. */
  111. public class Antenna
  112. {
  113. private int numAntenna;
  114. public static final int kMaxAntenna = 2;
  115. /**
  116. * Constructor for objects of class Tail
  117. */
  118. public Antenna()
  119. {
  120. numAntenna = 0;
  121. }
  122. /**
  123. * Determine if a part can be added.
  124. */
  125. public void add()
  126. {
  127. if (numAntenna < kMaxAntenna)
  128. {
  129. numAntenna += 1;
  130. }
  131. }
  132. /**
  133. * Is the part completed?
  134. * @return true if the part has been completely assembled
  135. */
  136. public boolean complete()
  137. {
  138. return numAntenna >= kMaxAntenna;
  139. }
  140. /**
  141. * Draw the part.
  142. * @return String representation of the part.
  143. */
  144. public String draw()
  145. {
  146. String result = " ";
  147. if (numAntenna == 1)
  148. {
  149. result = " ! ";
  150. }
  151. if (numAntenna == 2)
  152. {
  153. result = " ! !";
  154. }
  155. return result + "\n";
  156. }
  157.  
  158. }
  159.  
  160. /**
  161. * Body is the core of the Cootie
  162. */
  163. public class Body implements BodyPart
  164. {
  165. private boolean hasBody;
  166. private Legs legs;
  167.  
  168. /**
  169. * Constructor for objects of class Body
  170. */
  171. public Body()
  172. {
  173. hasBody = false;
  174. legs = new Legs();
  175. }
  176.  
  177. @Override
  178. public void add(int diceRoll)
  179. {
  180. if (diceRoll == 1)
  181. {
  182. hasBody = true;
  183. }
  184. else if (diceRoll == 3)
  185. {
  186. if (hasBody)
  187. {
  188. legs.add();
  189. }
  190. }
  191. }
  192. /**
  193. * Has a body been added?
  194. * @return true if it has a body
  195. */
  196. public boolean hasBody()
  197. {
  198. return hasBody;
  199. }
  200. @Override
  201. public boolean complete()
  202. {
  203. return legs.complete() && hasBody;
  204. }
  205. @Override
  206. public String draw()
  207. {
  208. String bodyOut = "";
  209. //loop until all legs are created
  210. for (int segment = 0; segment < Legs.kMaxLegs; segment = segment + 2)
  211. {
  212. // Add a leg on left side?
  213. if (legs.count() > segment)
  214. {
  215. bodyOut += "- ";
  216. }
  217. else
  218. {
  219. bodyOut += " ";
  220. }
  221. if (hasBody)
  222. {
  223. bodyOut += "[ ]";
  224. }
  225. // Add a leg on right side?
  226. if (legs.count() > segment + 1)
  227. {
  228. bodyOut += " - ";
  229. }
  230. bodyOut += "\n";
  231. }
  232.  
  233. return bodyOut.substring(0, bodyOut.length()-1);
  234. }
  235.  
  236. }
  237.  
  238. /**
  239. * BodyPart tells what parts of a cootie can do.
  240. *
  241. */
  242. public interface BodyPart
  243. {
  244. /**
  245. * Determine if a part can be added.
  246. * @param diceRoll the input number
  247. */
  248. public void add(int diceRoll);
  249. /**
  250. * Draw the part.
  251. * @return String representation of the part.
  252. */
  253. public String draw();
  254. /**
  255. * Is the part completed?
  256. * @return true if the part has been completely assembled
  257. */
  258. public boolean complete();
  259. }
  260.  
  261. import java.util.Scanner;
  262.  
  263. /**
  264. * Die is a cube with numbers on each side.
  265. */
  266. public class Die
  267. {
  268. Scanner console;
  269.  
  270. /**
  271. * Constructor for objects of class Die
  272. */
  273. public Die()
  274. {
  275. console = new Scanner(System.in);
  276. }
  277.  
  278. // Get user input
  279. public int roll()
  280. {
  281. System.out.println(" Input dice roll: ");
  282. int diceRoll = console.nextInt();
  283. // Repeat until a valid number is entered
  284. while (diceRoll < 1 || diceRoll > 6)
  285. {
  286. System.out.println(" Invalid input.");
  287. diceRoll = console.nextInt();
  288. }
  289. return diceRoll;
  290. }
  291.  
  292. }
  293.  
  294. /**
  295. * Eyes of a cootie.
  296. */
  297. public class Eyes
  298. {
  299. private int numEyes;
  300. public static final int kMaxEyes = 2;
  301. /**
  302. * Constructor for objects of class Tail
  303. */
  304. public Eyes()
  305. {
  306. numEyes = 0;
  307. }
  308. /**
  309. * Determine if a part can be added.
  310. */
  311. public void add()
  312. {
  313. if (numEyes < kMaxEyes)
  314. {
  315. numEyes += 1;
  316. }
  317. }
  318. /**
  319. * Is the part completed?
  320. * @return true if the part has been completely assembled
  321. */
  322. public boolean complete()
  323. {
  324. return numEyes >= kMaxEyes;
  325. }
  326. /**
  327. * Draw the part.
  328. * @return String representation of the part.
  329. */
  330. public String draw()
  331. {
  332. String result = " ";
  333. if (numEyes == 1)
  334. {
  335. result = "o ";
  336. }
  337. if (numEyes == 2)
  338. {
  339. result = "o o";
  340. }
  341. return result;
  342. }
  343.  
  344. }
  345.  
  346. /**
  347. * the head of a cootie.
  348. */
  349. public class Head implements BodyPart
  350. {
  351. private boolean hasHead;
  352. private Eyes eyes;
  353. private Antenna feelers;
  354.  
  355. /**
  356. * Constructor for objects of class Head
  357. */
  358. public Head()
  359. {
  360. eyes = new Eyes();
  361. feelers = new Antenna();
  362. hasHead = false;
  363. }
  364.  
  365. @Override
  366. public void add(int diceRoll)
  367. {
  368. // add a head?
  369. if (diceRoll == 2)
  370. {
  371. hasHead = true;
  372. }
  373. // add eyes?
  374. if (diceRoll == 5)
  375. {
  376. if (hasHead)
  377. {
  378. eyes.add();
  379. }
  380. }
  381. // add antenna?
  382. if (diceRoll == 4)
  383. {
  384. if (hasHead)
  385. {
  386. feelers.add();
  387. }
  388. }
  389. }
  390. @Override
  391. public String draw()
  392. {
  393. if (hasHead)
  394. {
  395. return feelers.draw() + " (" + eyes.draw() + ")";
  396. }
  397. return "";
  398. }
  399. @Override
  400. public boolean complete()
  401. {
  402. return hasHead && eyes.complete() && feelers.complete();
  403. }
  404. }
  405.  
  406. /**
  407. * Legs of a Cootie
  408. */
  409. public class Legs
  410. {
  411. private int numLegs;
  412. public static final int kMaxLegs = 6;
  413. /**
  414. * Constructor for objects of class Tail
  415. */
  416. public Legs()
  417. {
  418. numLegs = 0;
  419. }
  420. /**
  421. * Determine if a part can be added.
  422. */
  423. public void add()
  424. {
  425. if (numLegs < kMaxLegs)
  426. {
  427. numLegs += 1;
  428. }
  429. }
  430. /** Accessor to number of legs
  431. * @return current number of legs
  432. */
  433. public int count()
  434. {
  435. return numLegs;
  436. }
  437. /**
  438. * Is the part completed?
  439. * @return true if the part has been completely assembled
  440. */
  441. public boolean complete()
  442. {
  443. return numLegs >= kMaxLegs;
  444. }
  445.  
  446.  
  447. }
  448.  
  449. /**
  450. * Tail of a cootie.
  451. */
  452. public class Tail implements BodyPart
  453. {
  454. private boolean hasTail;
  455.  
  456. /**
  457. * Constructor for objects of class Tail
  458. */
  459. public Tail()
  460. {
  461. hasTail = false;
  462. }
  463. @Override
  464. public void add(int diceRoll)
  465. {
  466. if (diceRoll == 6)
  467. {
  468. hasTail = true;
  469. }
  470. }
  471. @Override
  472. public String draw()
  473. {
  474. if (hasTail)
  475. {
  476. return " T";
  477. }
  478. return "";
  479. }
  480. @Override
  481. public boolean complete()
  482. {
  483. return hasTail;
  484. }
  485.  
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement