Question

// ====== FILE: Point.java ========= // package hw3; /** * A class to that models a...

// ====== 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 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), 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);
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * 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;

        if(x < minX) {
            x = minX;
            vX *= -1;
        }
        if(x > maxX) {
            x = maxX;
            vX *= -1;
        }
        if(y < minY) {
            y = minY;
            vY *= -1;
        }
        if(y > maxY) {
            y = maxY;
            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) {
        return (center.distance(b2.center) < (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!

Add a comment
Know the answer?
Add Answer to:
// ====== FILE: Point.java ========= // package hw3; /** * A class to that models a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** *...

    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...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (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...

    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...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Create a UML diagram with 3 lines per class/interface including all constructors. public class Point {...

    Create a UML diagram with 3 lines per class/interface including all constructors. public class Point { public double X, Y; public Point() { this(0, 0); } public Point(double newX, double newY) { X = newX; Y = newY; } public static double distance(Point A, Point B) { return Math.sqrt(Math.pow(A.X-B.X, 2) + Math.pow(A.Y-B.Y, 2)); } } public interface Polygon { public int getNumberOfSides();    public double getPerimeter();    public double getArea();    } public abstract class Simple_polygon implements Polygon{ public Point...

  • What is wrong with the following code: class Point { private : int x, y; public...

    What is wrong with the following code: class Point { private : int x, y; public : Point (int u, int v) : x(u), y(v) {} int getX () { return x; } int getY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p.setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; }

  • In Pl you will create an interface, Drawable, and an abstract class Polygon. The Point class...

    In Pl you will create an interface, Drawable, and an abstract class Polygon. The Point class is provided. Briefly go over the JavaDocs comments to see what is available to you. «interface Drawable +printToConsole(): void Polygon - points : List<Point> - colour : String Point + Polygon(String colour) + getPoints(): List<Point> + addPoint(Point p): void + equals(Object other): boolean +getArea(): double • The Polygon constructor initializes the List to a List collection of your choosing • add Point: Adds a...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    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 <...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT