Question: // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a 2D point. */ public class Point { private double
// ====== FILE: Point.java ========= //
package hw3;
/**
* A class to that models a 2D point.
*/
public class Point {
private double x;
private double y;
/**
* Construct the point (x, y).
* @param x the Point's x coordinate
* @param y the Point's y coordinate
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Move the point to (newX, newY).
* @param newX the new x coordinate for this Point.
* @param newY the new y coordinate for this Point.
*/
public void moveTo(double newX, double newY) {
newX = (Math.random()*400)-x;
newY = (Math.random()*400)-y;
}
/**
* Returns the point's x coordinate
* @return the point's x coordinate
*/
public double getX() {
return x;
}
/**
* Returns the point's y coordinate
* @return the point's y coordinate
*/
public double getY() {
return y;
}
/**
* Returns the distance between this Point and the Point p2
* @param p2 the other Point
* @return the distance between this Point and the Point p2
*/
public double distance(Point p2) {
double x1 = p2.getX();
double y1 = p2.getY();
double x2 = this.x;
double y2 = this.y;
double d = Math.sqrt((Math.pow((x2-x1), x) + Math.pow((y2-y1), y)));
return d;
}
}
// ====== FILE: Ball.java ========= //
package hw3;
import java.awt.Color;
import edu.princeton.cs.algs4.StdDraw;
/** * A class that models a bounding ball */ public class Ball { // Minimum and maximum x and y values for the screen private static double minX; private static double minY; private static double maxX; private static double maxY;
private Point center; private double radius; // Every time the ball moves, its x position changes by vX; private double vX; // Every time the ball moves, its y position changes by vY; private double vY; private Color color;
/** * Sets the minimum and maximum x and y values of the screen on which the balls appear. * The bottom left of the screen is at (minX, minY) while the * top right is at (maxX, maxY). * @param minX the leftmost x value * @param minY the bottommost y value * @param maxX the rightmost x value * @param maxY the topmost y value */ public static void setScreen(double minX, double minY, double maxX, double maxY) { Ball.minX = minX; Ball.minY = minY; Ball.maxX = maxX; Ball.maxY = maxY; } /** * Constructs a ball. * @param centerX the x coordinate of the center of the ball * @param centerY the y coordinate of the center of the ball * @param r the radius of the ball * @param vX the velocity of the ball along the x-axis * @param vY the velocity of the ball along the y-axis * @param col the color of the ball */ public Ball(double centerX, double centerY, double r, double vX, double vY, Color col) { center = new Point(centerX, centerY); radius = r; this.vX = vX; this.vY = vY; color = col; } /** * Moves the ball (changes its position) by vX along the x-axis * and by vY along the y-axis. Additionally, if the ball has * reached one of the edges of the screen it changes the velocity * to reflect that the ball has bounced off the edge. */ public void move() { //TODO - This code is wrong and needs to be fixed
} /** * Determines if this has collided with b2. * @param b2 the other Ball * @return ture if this has collided with * b2. */ public boolean collision(Ball b2) { //TODO - This code is wrong and needs to be fixed return false; } /** * Draws the Ball on the screen. */ public void draw() { StdDraw.setPenColor(color); StdDraw.filledCircle(center.getX(), center.getY(), radius); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
