{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

A5(2)_Solutions

A5(2)_Solutions - Time Spent 6 hours import java.awt import...

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

Sheet1 // 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() Page 1

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

View Full Document
Sheet1 } /** 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.*/ public void drawTwoLines() { Color save= getColor() moveAhead(30) addAngle(270) setColor(Color.blue)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 15

A5(2)_Solutions - Time Spent 6 hours import java.awt import...

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

View Full Document
Ask a homework question - tutors are online