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