Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This Java program should be called Cannonball. it is about to draw the trajectory of the cannonball . In this program , you neet do

This Java program should be called Cannonball. it is about to draw the trajectory of the cannonball.

In this program , you neet do design a class Cannonball which model a cannonball that is fired into the air.

A ball has

x and y position.

x and y velocity.

to supply the following methods in the Cannonball class:

supply a constructor with an x-position (the y-position is initially 0).

A method move(double deltaSec) that moves the ball to the next position.

First compute the distance traveled in deltaSec seconds, using the current velocities,

then update the x- and y-positions;

then update the y-velocity by taking into account the gravitational acceleration of 9.81 m/s2; the x-velocity is unchanged.

A method Point getLocation() that gets the current location of the cannonball, rounded to integer coordinates.

A method ArrayList shoot(double alpha, double v, double deltaSec) whose arguments are the angle and initial velocity v.

(Compute the x-velocity as v cos and the y-velocity as v sin ; then keep calling move with the given time interval until the y-position is 0; return an array list of locations after each call to move.)

and use this class in a program that prompts the user for the starting angle(alpha) and the initial velocity(V). Then use shoot function and draw the trajectory of the cannonball.

below will be a good template as a hint to start.

//hint... public class Cannonball { double xPos; double yPos; double xVel; double yVel; //A constructor with an x-position (the y-position is initially 0).  public Cannonball(double xPos){ this.xPos = xPos; yPos = 0; } public void move(double deltaSec){ //...  } //return a point representing current x and y position  public Point getLocation(){ //...  } /*  set velocity based upon angle (alpha) and launch speed (v), move ball and record points  until ball reaches the ground, return list.  */  public ArrayList shoot(double alpha,double v, double deltaSec){ //...  } public static void main(String[] args) { //Todo auto-generate method.   Cannonball b1 = new Cannonball(10); ArrayList path = b1.shoot(Math.PI/4, 100 ,1); JFrame frame = new JFrame(); frame.setSize(500,500); JComponent component = new JComponent() { public void paintComponent (Graphics g){ for (int i = 0 ;i < path.size() -1; i++){ g.drawLine(path.get(i).x, path.get(i).y, path.get(i+1).x, path.get(i+1).y); } } }; frame.add(component); frame.setVisible(true); } } 

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

More Books

Students also viewed these Databases questions