Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In class, we wrote the Circle class representing circle objects which have a radius, center x, and center y. It included intersects and draw methods.

In class, we wrote the Circle class representing circle objects which have a radius, center x, and center y. It included intersects and draw methods. Then, we wrote a ColorfulCircle class which included a Color property and overrode the draw method of Circle. For this assignment, you are asked to create a ColorfulBouncingCircle class. Begin with the versions of Circle and ColorfulCircle uploaded to Canvas. ColorfulBouncingCircle should extend ColorfulCircle. Unlike the other Circle classes, it will include a velocity for the x and y axes. The constructor for ColorfulBouncingCircle includes these parameters: public ColorfulBouncingCircle(double radius, double centerX, double centerY, Color color, double xVelocity, double yVelocity) Note that 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. ColorfulBouncingCircles will bounce around in a playing field which the tester will provide. The ColorfulBouncingCircle class should have a public static method to set the playing field width and height: public static void setPlayingFieldSize(double newWidth, double newHeight) ColorfulBouncingCircle will also include a method which should alter its centerX and centerY with each time tick (similar to a metronomes noise guiding a musician to play a song). The circles x position should be altered according to x velocity and y position according to y velocity. However, before changing the positions, note if the center position would after moving be either less than 0 or greater than the playing field dimensions. If so, instead of changing the center position, alter the velocity. If the ball hits the top or bottom, the sign of its vertical velocity should be flipped; if it hits the left or right, its horizontal velocity sign should be flipped. If it hits a corner, both should be. Notice that velocity may be positive or negative! The tick method which will be called by the tester is defined in this way: public void tick() Finally, we should override the intersects method to make balls bounce away from each other. If your circle intersects another circle, use similar rules to the wall bounce rules in the previous paragraph, according to whether the center of the other circle is below, above, left, or right of this circle. (Even if this circle is just a little bit below the other circle, for example, flip this circles vertical velocity. If its a little bit to the right, flip the horizontal velocity.) This is NOT the same as a true elastic collision, but it should be simpler for you to implement! To review: Write the ColorfulBouncingCircle class, where you should write the constructor, setPlayingFieldSize, and tick methods, and override the intersects method, each according to the above descriptions. You should not have to write any code which draws; instead, a test program will be provided which will animate ColorfulBouncingCircles based on your implementation. For every time tick, the test program will compare every circle against every other circle using the intersects method. 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. DO NOT ALTER Circle OR ColorfulCircle!!

-----class Circle---

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 intersects(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));

}

}

----- class ColorfulCircle---

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);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions