Fix the todo in ball.java
Ball.java
package hw3;
import java.awt.Color;
import java.awt.Point;
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
(<code>minX</code>, <code>minY</code>)
while the
* top right is at (<code>maxX</code>,
<code>maxY</code>).
* @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() {
center.moveTo
(center.getX()+this.vX, center.getY()+this.vY);
if (center.getX()+this.radius >=
this.maxX)
this.vX -=
1
if (center.getX()-this.radius <=
this.minX)
this.vX +=
1
if (center.getY()+this.radius >=
this.maxY)
this.vY -=
1
if (center.getY()-this.radius <=
this.minY)
this.vY +=
1
}
/**
* Determines if <code>this</code> has
collided with <code>b2</code>.
* @param b2 the other
<code>Ball</code>
* @return <code>ture</code> if
<code>this</code> has collided with
* <code>b2</code>.
*/
public boolean collision(Ball b2) {
//TODO - This code is wrong and
needs to be fixed
return false;
}
/**
* Draws the <code>Ball</code> on the
screen.
*/
public void draw() {
StdDraw.setPenColor(color);
StdDraw.filledCircle(center.getX(),
center.getY(), radius);
}
}
BouncingBalls.java
package hw3;
import java.awt.Color;
import edu.princeton.cs.algs4.StdDraw;
public class BouncingBalls {
// Number of balls
private static final int BALL_COUNT = 4;
// Window runs from (-WINDOW_MAX,-WINDOW_MAX) in
bottom left to
// (WINDOW_MAX, WINDOW_MAX) in the top right
private static final int WINDOW_MAX = 400;
// The maximum ball radius
private static final int MAX_BALL_RADIUS = 40;
// The maximam velocity in both the x direction and
the y direction.
private static final double MAX_VELOCITY = 1.0;
public static void main(String[] args) throws
Exception {
// Initialize StdDraw
StdDraw.enableDoubleBuffering();
int windowSize = 2 * WINDOW_MAX +
1;
StdDraw.setCanvasSize(windowSize,
windowSize);
StdDraw.setXscale(-WINDOW_MAX,
WINDOW_MAX);
StdDraw.setYscale(-WINDOW_MAX,
WINDOW_MAX);
StdDraw.clear(Color.BLACK);
// Let the Ball class know the min
and max coordinates of the window
Ball.setScreen(-WINDOW_MAX,
-WINDOW_MAX, WINDOW_MAX, WINDOW_MAX);
// Initialize Ball objects in
animation
Ball[] ball = new
Ball[BALL_COUNT];
for(int i = 0; i < ball.length;
i++) {
ball[i] =
createRandomBall();
}
while(true) {
// clear the
screen
StdDraw.clear(Color.BLACK);
// move and draw
the balls in window buffer
for(Ball b :
ball) {
b.move();
b.draw();
}
// draw window
buffer on screen and pause
StdDraw.show();
StdDraw.pause(5);
// If there is a
collision pause and then
// start over
with random balls.
if
(checkCollisions(ball)) {
StdDraw.pause(2000);
for(int i = 0; i < ball.length; i++)
ball[i] =
createRandomBall();
}
}
}
/**
* Checks if any of the <code>Ball</code>s
in <code>b</code>
* have collided.
* @param b an array of <code>Ball</code>
objects to check
* @return <code>true</code> if any of the
<code>Ball</code>
* objects in <code>b</code> have collided
and
* <code>false</code> otherwise.
*/
private static boolean checkCollisions(Ball[] b)
{
// TODO - this code is wrong and
needs to be fixed.
return false;
}
/**
* Creates a new <code>Ball</code> that is
randomly placed on the screen
* and given a random size (radius), a random velocity,
and a random color.
* @return a new randomly generated
<code>Ball</code> object.
*/
private static Ball createRandomBall() {
double minPos =
-WINDOW_MAX + MAX_BALL_RADIUS;
double maxPos =
WINDOW_MAX - MAX_BALL_RADIUS;
double x =
HW3Utils.randomDouble(minPos, maxPos);
double y =
HW3Utils.randomDouble(minPos, maxPos);
double r =
HW3Utils.randomDouble(0.5 * MAX_BALL_RADIUS,
MAX_BALL_RADIUS);
double vX =
HW3Utils.randomDouble(-MAX_VELOCITY, MAX_VELOCITY);
double vY =
HW3Utils.randomDouble(-MAX_VELOCITY, MAX_VELOCITY);
Color c =
HW3Utils.randomColor();
Ball b = new
Ball(x, y, r, vX, vY, c);
return b;
}
}
Point.java
package hw3;
/**
* A class to that models a 2D point.
*/
public class Point {
private double x;
private double y;
/**
* Construct the point (<code>x</code>,
<code>y</code>).
* @param x the <code>Point</code>'s x
coordinate
* @param y the <code>Point</code>'s y
coordinate
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Move the point to (<code>newX</code>,
<code>newY</code>).
* @param newX the new x coordinate for this
<code>Point</code>.
* @param newY the new y coordinate for this
<code>Point</code>.
*/
public void moveTo(double newX, double newY) {
x = newX;
y = newY;
}
/**
* 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 <code>this
Point</code> and the <code>Point p2</code>
* @param p2 the other
<code>Point</code>
* @return the distance between <code>this
Point</code> and the <code>Point p2</code>
*/
public double distance(Point p2) {
double distance =
Math.sqrt(Math.pow(this.x-p2.x,2) + Math.pow(this.y-p2.y,2));
return distance;
}
}
import java.awt.Color; import edu.princeton.cs.algs4.StdDraw; /** * * A class to that models a 2D point. * */ class Point { private double x; private double y; /** * * Construct the point (<code>x</code>, <code>y</code>). * * @param x the <code>Point</code>'s x coordinate * * @param y the <code>Point</code>'s y coordinate * */ public Point(double x, double y) { this.x = x; this.y = y; } /** * * Move the point to (<code>newX</code>, <code>newY</code>). * * @param newX the new x coordinate for this <code>Point</code>. * * @param newY the new y coordinate for this <code>Point</code>. * */ 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 <code>this Point</code> and the * <code>Point p2</code> * * @param p2 the other <code>Point</code> * * @return the distance between <code>this Point</code> and the * <code>Point p2</code> * */ 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), 2) + Math.pow((y2 - y1), 2))); return d; } } /** * 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() { double x = center.getX(); double y = center.getY(); x += vX; y += vY; double r = radius; if(x < (minX + r)) { x = minX + r; vX *= -1; } if(x > (maxX - r)) { x = maxX - r; vX *= -1; } if(y < (minY + r)) { y = minY + r; vY *= -1; } if(y > (maxY - r)) { y = maxY + r; vY *= -1; } center = new Point(x, y); } /** * Determines if this has collided with b2. * * @param b2 the other Ball * @return true if this has collided with b2. */ public boolean collision(Ball b2) { double dis = center.distance(b2.center); return (dis < (radius + b2.radius)) ; } /** * Draws the Ball on the screen. */ public void draw() { StdDraw.setPenColor(color); StdDraw.filledCircle(center.getX(), center.getY(), radius); } }
Please upvote, as i have given the exact answer as asked in
question. Still in case of any concerns in code, let me know in
comments. Thanks!
Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** *...
// ====== 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 (<code>x</code>, <code>y</code>). * @param x the <code>Point</code>'s x coordinate * @param y the <code>Point</code>'s y coordinate */ public Point(double x, double y) { this.x = x; this.y = y; } /** * Move the point to (<code>newX</code>, <code>newY</code>). * @param newX the new x coordinate for...
(JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...
Must be in Java. Show proper reasoning with step by step process. Comment on the code please and show proper indentation. Use simplicity. Must match the exact Output. CODE GIVEN: LineSegment.java LineSegmentTest.java Point.java public class Point { private double x, y; // x and y coordinates of point /** * Creates an instance of Point with the provided coordinates * @param inX the x coordinate * @param inY the y coordinate */ public Point (double inX, double inY) { this.x...
PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...
Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public class InterfaceRunner { public static void main(String[] args) { ArrayList<GeometricSolid> shapes = new ArrayList<>(); shapes.add(new Sphere(10)); shapes.add(new Sphere(1)); shapes.add(new Cylinder(1, 5)); shapes.add(new Cylinder(10, 20)); shapes.add(new RightCircularCone(1, 5)); shapes.add(new RightCircularCone(10, 20)); /* * Notice that the array list holds different kinds of objects * but each one is a GeometricSolid because it implements * the interface. * */ for (GeometricSolid shape : shapes) { System.out.printf("%.2f%n",shape.volume());...
Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...
Keep the ball in bounds by adding conditional statements after the TODO comments in the paintComponent() method to check when the ball has hit the edge of the window. When the ball reaches an edge, you will need to reverse the corresponding delta direction and position the ball back in bounds. Make sure that your solution still works when you resize the window. Your bounds checking should not depend on a specific window size This is the java code that...
Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class ImageLab { /* * This is the grayscale example. Use it for inspiration / help, but you dont * need to change it. * * Creates and returns a new BufferedImage of the same size as the input * image. The new image is the grayscale version of the input (red, green, and * blue components averaged together)....
Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...
please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Random; enum Dir{L, U, R, D}; class Food { public static int FOODSTYLE = 1; private int m = r.nextInt(Yard.WIDTH / Yard.B_WIDTH); private int n = r.nextInt(Yard.HEIGHT / Yard.B_WIDTH - 30/Yard.B_WIDTH) + 30/Yard.B_WIDTH;// 竖格 public static Random r = new Random(); public int getM() { return m; } public void setM(int m)...