Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

I have done couple of works but I still can not pass the unit test. It is about inheritance. Can you guide me through 78- 259package eecs2030.lab4;import java.awt.Color;import java.util.ArrayList;impo

  1. I have done couple of works but I still can not pass the unit test. It is about inheritance. Can you guide me through 78- 259
  2. package eecs2030.lab4;
  3. import java.awt.Color;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Objects;
  7. /**
  8.  * A class that supports turtle graphics. A turtle walks between two points in a
  9.  * straight line drawing the line as it moves.
  10.  * 
  11.  *
  12.  * A turtle has-a {@code Point2} instance that represents the position of
  13.  * the turtle, a {@code Direction2} instance that represents the direction
  14.  * that the turtle is facing in, and a {@code Pen} instance that represents the pen
  15.  * that the turtle draws with.
  16.  * 
  17.  * @author EECS2030 Winter 2017-18
  18.  * 
  19.  */
  20. public class Turtle {
  21. /* DO NOT MODIFY THESE FIELDS */
  22. /* DO NOT ADD NEW FIELDS */
  23. private Point2 position;
  24. private Direction2 direction;
  25. private Pen pen;
  26. private List commands;
  27. /**
  28. * a turtle at location {@code (0.5, 0.5)} with an direction of
  29. * {@code 0.0} degrees, a pen color of {@code Color.BLACK}, and an
  30. * empty collection of commands.
  31. */
  32. public Turtle() {
  33. this(0.5, 0.5, 0.0, Color.BLACK);
  34. }
  35. /**
  36. * a turtle with the given starting position, direction, and pen.
  37. * The turtle will have an empty collection of commands.
  38. * The starting position must be inside the square with corners
  39. * {@code (0.0, 0.0)} and {@code (1.0, 1.0)}, otherwise an
  40. * {@code IllegalArgumentException} will be thrown.
  41. * 
  42. * @param x
  43. *      the x coordinate of the starting position of the turtle
  44. * @param y
  45. *      the y coordinate of the starting position of the turtle
  46. * @param angle
  47. *      the angle in degrees from the x axis that the turtle is facing
  48. *      in
  49. * @param c
  50. *      the color of the pen that the turtle should draw with
  51. * @throws IllegalArgumentException
  52. *       if the starting position is not in the square with corners
  53. *       (0.0, 0.0) and (1.0, 1.0)
  54. */
  55. public Turtle(double x, double y, double angle, Color c) {
  56. if (x >= 0. && x = 0. && y
  57. this.position = new Point2(x, y);
  58. this.direction = new Direction2(angle);
  59. this.pen = new Pen(c);
  60. this.commands = new ArrayList();
  61. }
  62. else {
  63. throw new IllegalArgumentException("Invalid starting position!");
  64. }
  65. }
  66. /**
  67. * a turtle by copying the position, direction, pen, and commands of
  68. * another turtle.
  69. * 
  70. * @param other the turtle to copy
  71. */
  72. public Turtle(Turtle other) {
  73. Turtle t = new Turtle();
  74. }
  75. /**
  76. * Walks the turtle forward by a given distance in the direction the turtle
  77. * is currently facing. A line is drawn as the turtle moves to the new
  78. * position using the current pen color.
  79. * 
  80. * @param distance
  81. *      the distance to move
  82. * @throws IllegalArgumentException
  83. *       if distance is less than zero
  84. */
  85. public void walk(double distance) {
  86. if (distance < 0.0) {
  87. throw new IllegalArgumentException();
  88. }
  89. Point2 current = new Point2(this.position);
  90. this.position.set(current.getX() + distance * this.direction.getX(),
  91. current.getY() + distance * this.direction.getY());
  92. this.pen.drawLine(current, this.position);
  93. }
  94. /**
  95. * Turns the turtle to the left (counter clockwise) by an amount
  96. * delta degrees.
  97. * 
  98. * @param delta the angle to turn counter clockwise by
  99. * 
  100. * @pre. {@code delta >= 0.0}
  101. */
  102. public void turnLeft(double delta) {
  103. this.direction.turn(delta);
  104. }
  105. /**
  106. * Turns the turtle to the right (clockwise) by an amount delta degrees.
  107. * 
  108. * @param delta the angle to turn clockwise by
  109. * @pre. {@code delta >= 0.0}
  110. */
  111. public void turnRight(double delta) {
  112. this.direction.turn(-delta);
  113. }
  114. /**
  115. * Turns the turtle so that its direction is equal to the given
  116. * angle in degrees. Any value of delta can be used, but the turtle
  117. * normalize its direction angle so that {@code 0.0
  118. * 
  119. * @param angle
  120. *      the new direction angle of the turtle
  121. */
  122. public void turnTo(double angle) {
  123. this.direction.setAngle(angle);
  124. }
  125. /**
  126. * Sets this turtle's pen to on.
  127. */
  128. public void penOn() {
  129. this.pen.on();
  130. }
  131. /**
  132. * Sets this turtle's pen to off.
  133. */
  134. public void penOff() {
  135. this.pen.off();
  136. }
  137. /**
  138. * Sets this turtle's pen color to the specified pen color.
  139. * 
  140. * @param c the new pen color
  141. */
  142. public void setPenColor(Color c) {
  143. this.pen.setColor(c);
  144. }
  145. /**
  146. * Gets the pen belonging to this turtle.
  147. * 
  148. * @return the pen belonging to this turtle 
  149. */
  150. public Pen getPen() {
  151. return new Pen(this.pen);
  152. }
  153. /**
  154. * Gets the current position of the turtle. 
  155. *  
  156. * @return the current position of the turtle
  157. */
  158. public Point2 getPosition() {
  159. return new Point2(this.position);
  160. }
  161. /**
  162. * Gets the direction that the turtle is facing in.
  163. * 
  164. * @return the direction that the turtle is facing in
  165. */
  166. public Direction2 getDirection() {
  167. return this.direction;
  168. }
  169. /**
  170. * Returns a shallow copy of this turtle's collection of commands.
  171. * 
  172. * @return a shallow copy of this turtle's collection of commands
  173. */
  174. public List getCommands() {
  175. List results = new ArrayList();
  176. for (TurtleCommand t : this.commands){
  177. results.add(t);
  178. }
  179. return results;
  180. }
  181. /**
  182. * Repeat all of the commands that this turtle has previously performed
  183. * n times. For example:
  184. * 
  185. *
  186. * Turtle t = new Turtle();
  187. * t.walk(0.2);
  188. * t.turnLeft(90);
  189. * t.repeatCommands(3); // repeat previous commands 3 more times
  190. *
  191. * 
  192. *
  193. * would cause the turtle to draw a square.
  194. * 
  195. * @param n the number of times to repeat the commands that this turtle
  196. * has previously performed
  197. * 
  198. * @pre. {@code n >= 0}
  199. * 
  200. */
  201. public void repeatCommands(int n) {
  202. Turtle t = new Turtle();
  203. for (int numsquares = 0; numsquares < n; numsquares++);
  204. t.walk(0.2);
  205. t.turnLeft(90);
  206. t.repeatCommands(3);
  207. }
  208. /**
  209. * Have this turtle execute each command in the given list of commands.
  210. * The commands are executed in order that they appear in the given
  211. * list, and are added to the end of this turtle's collection of
  212. * commands.
  213. * 
  214. * @param commands a list of commands for this turtle to execute
  215. */
  216. public void doCommands(List commands) {
  217. }
  218. /* DO NOT MODIFY THE METHODS AFTER THIS COMMENT; THEY ARE HERE FOR TESTING
  219.   PURPOSES */
  220. /**
  221. * Returns a reference to the position of this turtle.
  222. * 
  223. * @return a reference to the position of this turtle
  224. */
  225. Point2 leakPosition() {
  226. return this.position;
  227. }
  228. /**
  229. * Returns a reference to the direction of this turtle.
  230. * 
  231. * @return a reference to the direction of this turtle
  232. */
  233. Direction2 leakDirection() {
  234. return this.direction;
  235. }
  236. /**
  237. * Returns a reference to the pen of this turtle.
  238. * 
  239. * @return a reference to the pen of this turtle
  240. */
  241. Pen leakPen() {
  242. return this.pen;
  243. }
  244. /**
  245. * Returns a reference to the collection of commands of this turtle.
  246. * 
  247. * @return a reference to the collection of commands of this turtle
  248. */
  249. List leakCommands() {
  250. return this.commands;
  251. }
  252. /**
  253. * Returns a string representation of this turtle. The string representation
  254. * is:
  255. * 
  256. *
  257. * the position of the turtle (as given by
  258. * {@code Point2.toString}), followed by
  259. * a comma and a space, followed by
  260. * the direction of this turtle (as given by {@code Direction2.toString}),
  261. * followed by
  262. * a space and a comma, followed by
  263. * the pen (as given by {@code Pen.toString})
  264. *
  265. * 
  266. * @return a string representation of this turtle
  267. */
  268. @Override
  269. public String toString() {
  270. String s = String.format("%s, %s degrees, %s", this.getPosition(),
  271. this.getDirection(), this.getPen());
  272. return s;
  273. }
  274. /**
  275. * Returns a hash code for this turtle.
  276. * 
  277. * @return a hash code for this turtle
  278. */
  279. @Override
  280. public int hashCode() {
  281. return Objects.hash(this.position, this.direction, this.pen);
  282. }
  283. /**
  284. * Compares this turtle to the specified object. The result is true if
  285. * and only if the argument is not null and is a {@code Turtle} object
  286. * having a position, direction, and pen equal to this turtle's
  287. * position, direction, and pen.
  288. * 
  289. * @param obj
  290. *      the object to compare this Turtle against
  291. * @return true if the given object represents a Turtle equivalent to
  292. *     this object and false otherwise
  293. */
  294. @Override
  295. public boolean equals(Object obj) {
  296. if (this == obj) {
  297. return true;
  298. }
  299. if (obj == null) {
  300. return false;
  301. }
  302. if (getClass() != obj.getClass()) {
  303. return false;
  304. }
  305. Turtle other = (Turtle) obj;
  306. if (direction == null) {
  307. if (other.direction != null) {
  308. return false;
  309. }
  310. } else if (!direction.equals(other.direction)) {
  311. return false;
  312. }
  313. if (pen == null) {
  314. if (other.pen != null) {
  315. return false;
  316. }
  317. } else if (!pen.equals(other.pen)) {
  318. return false;
  319. }
  320. if (position == null) {
  321. if (other.position != null) {
  322. return false;
  323. }
  324. } else if (!position.equals(other.position)) {
  325. return false;
  326. }
  327. return true;
  328. }
  329. }
Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question