import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;
public class Cannonball {
public static void main(String[] args) {
//Part 1: Open Scanner
Scanner keyboard = new Scanner(System.in);
//Part 2: Create a new cannonball
Ball bowling = new Ball(0);
//Part 3: Ask user for initial angle and starting velocity
System.out.println("Alright, give us the angle at which to fire: ");
bowling.setAngle(keyboard.nextDouble());
System.out.println("And what is the initial velocity: ");
bowling.setVel(keyboard.nextDouble());
//Part 4: Return the points of the cannonball's flight
for(int i=0; i<bowling.shoot.size(); i++) System.out.println(bowling.shoot);
//Part x: Close input
keyboard.close();
}
}
class Ball{
private double xPos, yPos, deltaSec;
private double alpha, v;
private double yVel, xVel;
private static final double gravity = -9.81;
public Ball(double xPos){
this.xPos=xPos;
yPos=0;
}
public Point move(double deltaSec){
xPos += xVel*deltaSec;
yPos += yPos*deltaSec;
return new Point();
}
public void yVel(){
yVel=v*Math.sin(alpha)*(deltaSec*gravity);
}
public void xVel(){
xVel=v*Math.cos(alpha);
}
public Point getLocation(double xPos, double yPos){
return new Point((int)xPos, (int)yPos); //Point only takes int coordinates
}
public void setAngle(double aplha){
this.alpha=alpha;
}
public void setVel(double v){
this.v=v;
}
public ArrayList<Point> shoot = new ArrayList<Point>();
{
while(deltaSec<60){
move(deltaSec);
shoot.add(getLocation(xPos, yPos));
deltaSec++;
}
}
}
What I am trying to accomplish here is to output an array of X and Y coordinates as the cannonball is in flight. However, when I run the program, I get a bunch of Point[x=0,y=0] within the array.
attach is the assignment instruction can somebody help
Question
atyemail
What I am trying to accomplish here is to output an array of X and Y coordinates as the cannonball is in flight. However, when I run the program, I get a bunch of Point[x=0,y=0] within the array.
attach is the assignment instruction can somebody help
CannonBall-Instructions-JavaDocAbstract.pdf
Link to comment
Share on other sites
8 answers to this question
Recommended Posts