Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am only looking for the MovingBall class. I have everything else. I\'ve attach

ID: 3602647 • Letter: I

Question

I am only looking for the MovingBall class. I have everything else. I've attached the BallMethods class for reference. Let me know if you need any addidtional info. Thanks in advance!

Here is the BallMethods class:

public interface BallMethods

{

/////////////////////////////////////////////////////////////////////////////

// Throw the ball. This method does not really move the ball, but just SETS

// it off on its throw. Thus, it merely sets the following internal params:

// - Ball's horizontal velocity (xVelocity) to the hVelocity passed

// in, which is in pixels-per-frame

// - Ball's vertical velocity (yVelocity) to 0 pixels-per-frame

// - Ball's height of it's center (ballCenterY) to the startingHeight,

// which is in pixels

// - Ball's x position (in pixels) of it's center (ballCenterX) to a

// point that places the ball just off the screen

void ThrowBall(double hVelocity, double startingHeight);

/////////////////////////////////////////////////////////////////////////////

// Roll the ball. This method does not really move the ball, but just SETS

// it off on its roll. Thus, it merely sets the following internal params:

// - Ball's horizontal velocity (xVelocity) to the hVelocity passed

// in, which is in pixels-per-frame

// - Ball's height of it's center (ballCenterY) to point so that the

// ball appears to be sitting on the ground

// - Ball's x position (in pixels) of it's center (ballCenterX) to a

// point that places the ball just off the screen

void RollBall(double hVelocity);

/////////////////////////////////////////////////////////////////////////////

// Move the ball to the right one frame. This is essentially moving the ball

// a number of pixels which is equal to the ball's xVelocity (which was set

// earlier by the RollBall method).

//

// The ball must also rotate at the CORRECT rate (e.g., if the circumference of

// a ball is 300 pixels (can compute from the ball's radius), then the ball

// must make one full rotation after moving 300 pixels.

void AdvanceRollingBallSingleFrame();

/////////////////////////////////////////////////////////////////////////////

// Move the ball to the right one frame. This is essentially moving the ball

// a number of pixels which is equal to the ball's xVelocity (which was set

// earlier by the ThrowBall method.

//

// The ball must also FALL with an acceleration of 9.8pixels/(frame*frame). That is,

// each time the ball is advanced, it's vertical velocity is also updated using the

// following velocity equation derived from physics:

// newVelocity = initialVelocity + 9.8 (we'll assume time always = 1 frame)

//

// When the ball hits the ground, it immediately loses some percent of it's speed,

// according to the bounceSpeedLoss (see below) variable in the Ball class, and then

// changes it's direction to start moving up. The ball will then begin to decelerate

// using the following equation:

// newVelocity = initialVelocity - 9.8 (we'll assume time always = 1 frame)

//

// NOTE: bounceSpeedLoss will be a number between 0-1. It should be applied as follows:

// vVelocity = vVelocity * (1 - bounceSpeedLoss);

// Thus, if your velocity was 10, and bounceSpeedLoss was .15, the new velocity will

// be 8.5.

//

// Once the ball's velocity reaches 0 on it's upward trajectory, it simply changes direction

// to start falling again.

//

// After a few bounces, the ball eventually bounces lower and lower. When you determine that the ball is

// not bouncing very high anymore, then the ball should set the internal variable ballStillBouncing to

// false. There are a number of ways to do this; for instance, you could examine the ball's vertical position

// at its peek to see if it is low, or you could examine the velocity at which the ball is bouncing back up

// as it hits the ground (for example, if the vertical velocity is very low when bouncing back up, it tells you

// the ball is essentially done bouncing.

void AdvanceThrownBallSingleFrame();

/////////////////////////////////////////////////////////////////////////////

// This method simply returns ballStillBouncing

boolean IsThrownBallStillBouncing();

}

In this project, you will write portions of a java application which roll and throw both a basketball and beachball. When a ball is rolled across a court, it moves from the left to the right of the screen and rotates at the same time, as seen in Figure 1. As seen in Figure 2, when a ball is thrown, it moves from left to right, but also bounces and obeys the laws of gravity (to a rough approximation) Figure 1: A basketball rolling from left to right. Figure 2: A basketball bouncing from left to right when thrown.

Explanation / Answer

Client.java
-------------------------------------------------------
import java.io.File;

public class Client
{
    public static void main(String[] args)
    {
        System.out.println("Moving Ball");
        System.out.println("");

        // Delete all the old output files
        //DeleteOldOutput();


        // Run one of the following 4 methods at
        // any given time
        RollBall(new Basketball());
        RollBall(new Beachball());
        ThrowBall(new Basketball());
        ThrowBall(new Beachball());

        System.out.println("Ball movement simulation complete.");
    }


    // Calls high-level code to roll the ball
    public static void RollBall(MovingBall b)
    {
        int name = 0;

        b.RollBall(20);
        while ((b.getBallCenterX() < Court.getCourtLength()))
        {
            b.AdvanceRollingBallSingleFrame();
            System.out.println("Frame " + name + " output.");
            Court.DrawBallOnCourt(b, Integer.toString(name++));

        }
    }


    // Calls high-level code to throw the ball
    public static void ThrowBall(MovingBall b)
    {
        int name = 0;
        b.ThrowBall(20, 400);
        while ((b.getBallCenterX() < Court.getCourtLength()) && b.IsThrownBallStillBouncing())
        {
            b.AdvanceThrownBallSingleFrame();
            System.out.println("Frame " + name + " output.");
            Court.DrawBallOnCourt(b, Integer.toString(name++));
        }
    }


    // Deletes all files in the "Output" directory
    public static void DeleteOldOutput()
    {
        File dir = new File("Output");
        String[] list = dir.list();
        for (int i = 0; i < list.length; i++)
        {
            File file = new File("Output", list[i]);
            file.delete();
        }
    }
}
-------------------------------------------------------------------------------------------------
Court.java
------------------------------------------------
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class Court
{
    private final static String ext = "png";
    private final static String dir = "Output";
    private final static int width = 1000;
    private final static int height = 500;

    public static void DrawBallOnCourt(Ball b, String fileName)
    {
        // Create buffered image and Graphic2D object for court
        BufferedImage court;
        Graphics2D g2d;

        // Create an image of the appropriate height for facebook
        court = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        g2d = (Graphics2D) court.getGraphics();

        // Fill the image with white
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, width);

        // Set the color of the "stroke" (pen) to black
        g2d.setColor(Color.BLACK);

        // Create a new BufferedImage, which acts as a canvas
        BufferedImage imgBall = null;
        try {
            imgBall = ImageIO.read(new File(b.getBallImageName()));
        } catch (IOException e) { e.printStackTrace(); }

        // Resize image to percentage, save in ppResized
        double w = b.getRadius()*2;
        double h = b.getRadius()*2;
        BufferedImage imgBallResized = new BufferedImage((int)w, (int)h, imgBall.getType());
        Graphics2D g = imgBallResized.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(imgBall, 0, 0, (int)w, (int)h, 0, 0, imgBall.getWidth(), imgBall.getHeight(), null);
        g.dispose();

        // Convert the (0, 0) point from top-left to bottom-right
        double x = b.getBallCenterX();
        double y = height - b.getBallCenterY();

        // Rotate the ball image around it's center
        BufferedImage imgBallRot = new BufferedImage((int)w, (int)h, imgBall.getType());
        AffineTransform trans = new AffineTransform();
        Graphics2D gr = imgBallRot.createGraphics();
        trans.rotate(Math.toRadians(b.getDegreesRotated()), w/2, h/2 );
        gr.transform(trans);
        gr.drawImage(imgBallResized, null, 0, 0);

        // Draw the resized personal picture
        g2d.drawImage(imgBallRot, null, (int)(x - imgBallResized.getWidth()/2), (int)(y - imgBallResized.getHeight()/2));

        // Write the image to a file
        writeImageToFile(fileName, court);
    }

    public static void writeImageToFile(String fileName, BufferedImage court)
    {

        File f = new File(dir + "/" + fileName + "." + ext);
        try {
            ImageIO.write(court, ext, f);
        } catch (IOException e) { System.err.println(e.getMessage()); }
    }


    public static double getCourtLength()
    {
        return width;
    }
}
------------------------------------------------------------------------------------------------
MovingBall.java
-------------------------------------------------
public class MovingBall extends Ball implements BallMethods {


    double circ = 0;
    double rate = 0;
    double prevPeak[] = {0.0};
    double newPeak;
    int count = 0;
    boolean goingUp;
    MovingBall(double rad, double bsl, String imageNameWithExt) {
        super(rad, bsl, imageNameWithExt);

    }

    @Override
    public void ThrowBall(double hVelocity, double startingHeight) {

        xVelocity = hVelocity;
        yVelocity = 0;
        ballCenterY = startingHeight;
        ballCenterX = -radius;
        ballStillBouncing = true;
    }

    @Override
    public void RollBall(double hVelocity) {
        xVelocity = hVelocity;
        ballCenterY = radius;
        ballCenterX = -radius;


    }

    @Override
    public void AdvanceRollingBallSingleFrame() {
        circ = 2*radius*Math.PI;
        ballCenterX += xVelocity;
        rate = circ/360;
        degreesRotated += rate*5;

    }

    @Override
    public void AdvanceThrownBallSingleFrame() {
        //double intialVelocity = 0;
        //intialVelocity = xVelocity;
        //double newVelocity;
        ballCenterX += xVelocity;
        degreesRotated += 9.8;


        ballCenterY += yVelocity;
        if(ballCenterY <= radius){//when the ball hits the ground
            ballCenterY = radius;
            yVelocity = -yVelocity + (yVelocity*bounceSpeedLoss);
            goingUp = true;
            if(yVelocity < 10)
                ballStillBouncing = false;

        }

        if(!goingUp)
            yVelocity -= 9.8;

        if(yVelocity >= 0){ //when ball is at its peak
            yVelocity -= 9.8;
            goingUp = false;
        }


    }

    @Override
    public boolean IsThrownBallStillBouncing() {

        return ballStillBouncing;
    }

}
------------------------------------------------------------------------------------------------

Ball.java
------------------------------------------------
public abstract class Ball
{
    protected double ballCenterX;
    protected double ballCenterY;
    protected double radius;
    protected double xVelocity;
    protected double yVelocity;
    protected double degreesRotated;
    protected double bounceSpeedLoss;
    protected boolean ballStillBouncing;
    private String ballImageName;


    Ball(double rad, double bsl, String imageNameWithExt)
    {
        ballCenterX = 0;
        ballCenterY = 0;
        degreesRotated = 0;
        ballStillBouncing = false;

        radius = rad;
        bounceSpeedLoss = bsl;
        ballImageName = imageNameWithExt;
    }

    public String getBallImageName()
    {
        return ballImageName;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getBallCenterX()
    {
        return ballCenterX;
    }

    public double getBallCenterY()
    {
        return ballCenterY;
    }

    public double getDegreesRotated()
    {
        return degreesRotated;
    }

    public double getBounceSpeedLoss()
    {
        return bounceSpeedLoss;
    }
}
---------------------------------------------------------------------------------------------
BallMethods.java
--------------------------------------------------------
public interface BallMethods
{

    void ThrowBall(double hVelocity, double startingHeight);

    void RollBall(double hVelocity);

    void AdvanceRollingBallSingleFrame();

    void AdvanceThrownBallSingleFrame();

    boolean IsThrownBallStillBouncing();
}
---------------------------------------------------------------------------------------------
Basketball.java
-------------------------------------------------------
public class Basketball extends MovingBall
{
    Basketball()
    {
        super(100, .3, "Basketball.png");
    }
}
-------------------------------------------------------------------------------------------
Beachball.java
----------------------------------------------------
public class Beachball extends MovingBall
{
    Beachball()
    {
        super(150, .6, "Beachball.png");
    }
}