Question: Please write a class ColorfulBouncingCircle as described below. It should extend ColorfulCircle. Begin with the versions of Circle.java, ColorfulCircle.java, and ColorfulBouncingCircleTest.java uploaded to Canvas. DO
Please write a class ColorfulBouncingCircle as described below. It should extend ColorfulCircle. Begin with the versions of Circle.java, ColorfulCircle.java, and ColorfulBouncingCircleTest.java uploaded to Canvas. DO NOT CHANGE ANY OF THESE CLASSES. These classes, along with the class you create, all need to be in the same project in Eclipse. Your ColorfulBouncingCircle class should have private fields for the x and y velocities, and private static fields for the width and height of the playing field. It should also include these methods: public ColorfulBouncingCircle(double radius, double centerX, double centerY, Color color, double xVelocity, double yVelocity) This is the constructor. It should call the constructor from the superclass. public static void setPlayingFieldSize(double newWidth, double newHeight) This simple method just sets the size of the playing field that all the circles will move around in. Note that it is static, since there is just one common playing field for all the different circle objects. public void tick() This method will be called by the tester program many times per second, and is how you will implement the animation (think of it like one tick of a clock). The method will do two things: If the circle is NOT on the edge of the playing field, then it should alter the circles center by adding the x and y velocities to their corresponding center coordinates. If, after moving, the new center position would be outside the playing field (either less than zero, or above the width or height), then dont change the center position at all. Instead, alter the velocity to make the circle bounce off the wall. If the circle would hit the top or bottom, flip the sign of the y velocity, and if the circle would hit the left or right side, flip the sign of the x velocity. If the circle would hit a corner, flip both velocities. public boolean overlaps(Circle c) This method should override the overlaps method from the Circle class. It will be used to make the circles bounce off of each other. First, call the overlaps method from the superclass to see if this circle overlaps the other circle. If they do overlap, then alter this circles velocity, depending on how it is positioned relative to the other circle: If the center of this circle is above or below the center of the other circle, flip this circles y velocity. If this circles center is to the left or right of the other circles, then flip this circles x velocity. As in tick(), both may be flipped. There are several things to note: Please review the velocity changing rules carefully; they are easy to get wrong!! We are treating x as increasing to the right and y as increasing down, as with Javas graphics coordinates system. This was also seen with Circles draw method. The sign flipping will sometimes cause the circles to vibrate when caught between each other. This is fine. You should not have to write any code which draws; instead, the tester program will animate ColorfulBouncingCircles based on your implementation. The test program will ask you to press Enter in the console to launch the automated tests which will assign a tentative score based on your implementation. I am going to run your code using MY Circle and ColorfulCircle classes, as provided on Canvas. Do not change these! You only need to submit your ColorfulBouncingCircle.java file for grading.
(So you will be having 3 classes within the same project)
The first class, Circle class code layout is here:
import java.awt.Graphics2D;
// UPDATE 3/8/2018: Added setCenterCoordinates method public class Circle { private double radius; // X and Y are in Java graphics coordinate system // (0, 0 in upper left corner) private double centerX, centerY; public Circle(double radius, double centerX, double centerY) { if (radius < 0) { throw new IllegalArgumentException(); } this.radius = radius; this.centerX = centerX; this.centerY = centerY; } public double getRadius() { return radius; } public double getCenterX() { return centerX; } public double getCenterY() { return centerY; } public void setRadius(double radius) { if (radius < 0) { throw new IllegalArgumentException(); } this.radius = radius; } public void setCenterCoordinates(double centerX, double centerY) { this.centerX = centerX; this.centerY = centerY; } public double area() { return Math.PI * radius * radius; } public boolean overlaps(Circle c) { // First, calculate distance double dx = this.centerX - c.centerX; double dy = this.centerY - c.centerY; double dist = Math.sqrt(dx*dx + dy*dy); return dist <= this.radius + c.radius; }
public void draw(Graphics2D g) { g.fillOval((int)(centerX-radius), (int)(centerY-radius), (int)(2*radius), (int)(2*radius)); } }
The second class code ColorfulCircles is right here:
import java.awt.Color; import java.awt.Graphics2D;
public class ColorfulCircle extends Circle {
private Color color; public ColorfulCircle(double radius, double centerX, double centerY, Color color) { super(radius, centerX, centerY); this.color = color; }
// The Override annotation below is not necessary, but it ensures that this method // correctly matches up with a method in Circle or another superclass. @Override public void draw(Graphics2D g) { Color c = g.getColor(); g.setColor(color); super.draw(g); g.setColor(c); }
}
(For the third class you will be creating your own with the information above / I also wanted to let you know this a beginning 142 CS class so we have not yet talked about chars this assignment is based on objects and Inheritance!)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
