Question
Java URGENT: Please go through all the 10 steps making class for Cannonball and finally running the 11th step code to confirm it worked. Design
Java URGENT: Please go through all the 10 steps making class for Cannonball and finally running the 11th step code to confirm it worked.
Design a class Cannonball to model a cannonball that is fired into the 3 air. A Cannonball has xPosition and yPosition as well as xVelocity and yVelocity. This class will use a Point Class.
1 Define a class Point that represents a Point in two dimensional plane. In this class, define a parameterized constructor to create a Point at the given coordinates.
2 Define getter and setter methods to get and set the value of xCoord and yCoord of the point object.
3 Override toString method to print the coordinates.
4 Define a class Cannonball that represents a Cannon Ball. Import java.util.ArrayList.
5 In this class, store the position and velocity of the ball in instance variables.
6 Define a constructor to initialize the Cannonball (xPosition) to start at the given location whereas initially yPosition=0.
7 Define a method move (double deltaSec) that moves the ball to the next location in deltaSec time period and returns the new location as a Point object. First, compute the distance traveled in deltaSec seconds, using the current velocities, then update the xPosition and yPosition; then update the yVelocity by taking into account the gravitational acceleration of -9.81 m/s2 ; the xVelocity is unchanged.
8 Define method Point getLocation() that returns the current location of the Cannonball, rounded to integer coordinates.
9 Define a method ArrayListshoot(double alpha, double v, double deltaSec) whose arguments are the angle alpha and initial velocity v (compute the xVelocity as cos alpha and the yVelocity as sin alpha); then keep calling move with the given time interval deltaSec until the yPosition is 0; return the array list of locations after each call to move. In this method, you can define an ArrayList to store Point objects.
10 Initialize the xVelocity and yVelocity using the given angle and initial velocity. Call move method and add the result to ArrayList. Return the ArrayList.
11 Use the class cannonBalltrajectory to plot the movement of the Cannonball ( BELOW)
Below is The class to use after step 10 is done
** Below is the CannonballTrajectory to use after step 10 is done **
*** Below is the CannonballTrajectory to use after step 10 is done ***
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CannonballTrajectory extends JPanel {
ArrayList
public CannonballTrajectory(ArrayList
{
this.trajectory=trajectory;
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
int height=getHeight();
Point start=trajectory.get(0);
Point current;
g2.setColor(Color.BLACK);
for(int idx=1; idx { current=trajectory.get(idx); g2.drawLine((int)start.getxCoord(),height-(int)start.getyCoord(),(int)current.getxCoord(),height-(int)current.getyCoord()); start=current; } } // public static void main(String[]args) { JFrame frame=new JFrame("Cannon Ball" + "Trajectory"); Cannonball myCannon=new Cannonball(0); CannonballTrajectory trajectory=new CannonballTrajectory(myCannon.shoot(45,70,1)); CannonballTrajectory trajectory1=new CannonballTrajectory(myCannon.shoot(70,80,1)); frame.add(trajectory); frame.setSize(550,350); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started