A5_Solutions - / Time Spent: 6 hours import java.awt.*;...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 // Time Spent: 6 hours import java.awt.* import java.awt.Color /** Assignment A5 */ public class A5 extends Turtle { private double radius, vx, vy /** Constructor: a black turtle starting at the middle of the panel with angle 0 (looking east). East (right) is angle 0 South (down). 270. The pen is down.*/ public A5() { super() } /** Constructor: a new turtle that has a ball drawn at the turtle's position. The turtle is initially at (x, y), the pen is color c, the ball's radius is r, and the ball moves with speed (vx, vy). */ public A5(double x, double y, double r, Color c, double vX, double vY){ super(x, y, 0) setColor(c) radius= r vx= vX vy= vY fillCircle(r) } /** Constructor: a new turtle that has a ball drawn at the turtle's position. The turtle is initially at the center of the panel, the pen is black, the ball's radius is r, and the ball moves with speed (vx, vy). */ public A5(double vX, double vY, double r){ super() radius= r vx= vX vy= vY fillCircle(r) } /** Place the turtle in the center of the panel, facing east, and clear the window (make it white). */ public void placeInCenter() { jumpTo(getWidth()/2, getHeight()/2, 0) clear()
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1 Page 2 } /** Move the ball (vx, vy), making it bounce off the panel walls. Follow the the directions given in handout A5. Precondition: this object was created with one of the two ball constructors.*/ public void moveOnce() { Color originalColor= getColor() setColor(Color.white) fillCircle(radius) setColor(originalColor) if (getX() < radius || getX() > getWidth() - radius) vx= -vx if (getY() < radius || getY() > getHeight() - radius) vy= -vy jumpTo(getX() + vx, getY() + vy, 0) fillCircle(radius) } /** Put this ball in perpetual motion. Precondition: this object was created with one of the two ball constructors.*/ public void perpetualMotion() { for (int i= 1 moveOnce() pause(100) } } /** Draw a black line 30 pixels to the right (east) and then a blue line 35 pixels down (south). Precondition: the turtle is facing east.*/
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

A5_Solutions - / Time Spent: 6 hours import java.awt.*;...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online