• 0

Help with Assignment - Java - Student Class


Question

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

CannonBall-Instructions-JavaDocAbstract.pdf

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

I think I see the answer.

What are you coding this in? Are you using an IDE? If so have you seen what sequence the code executes in?

Link to comment
Share on other sites

  • 0

 

I am using jGRASP to code this

1
 

 

 

When i coded in Java i used Eclipse; does jGRASP have debug? if yes then it's a great tool for seeing the flow of the logic and code.

Link to comment
Share on other sites

  • 0

Just from looking at your code, the shoot subroutine looks odd. I'm assuming you're trying to fill an array of points, then return it somehow. That can be done in two ways. Either define a class scope variable and a 'get' routine, or define and return it directly in 'shoot':

public ArrayList<Point> shoot ( double launchAngle, double initialVelocity ) {
	
	ArrayList<Point> trajectory = new ArrayList<Point>();
	deltasec = 0;
	
	setAngle ( launchAngle );
	setVelocity ( initialVelocity );
	
	while ( deltaSec < 60 ) {
		move ( deltaSec );
		trajectory.add ( getLocation () );
		deltaSec++;
	}
	
	return trajectory;
}
Although your assignment doesn't seem to want you to do it this way. From a cursory glance, it seems to want you to print a line of information and graphic in shoot after every tick (1/10 of a second), not store each movement in an array.

Edit: A small tip. Don't access a class' member variables directly. That breaks rules of encapsulation and creates a dependency on the internal structure of the Ball class.

Link to comment
Share on other sites

  • 0

This is fairly straightforward by deduction. xPos and yPos have a default value of 0. The only place where they are set a different value is in move() with this line:

xPos += xVel*deltaSec;
yPos += yVel*deltaSec;

The result of xVel * deltaSec is 0 if either operand is 0. We can see that deltaSec will not be 0 after the first iteration of your while loop, so it must be xVel and yVel that remain at 0 throughout. So now we need to figure out why xVel and yVel remain at 0.

 

The only place where you set xVel and yVel are in the xVel() and yVel() methods:

public void yVel(){
    yVel=v*Math.sin(alpha)*(deltaSec*gravity);
}


public void xVel(){
    xVel=v*Math.cos(alpha);
}

Are these methods called anywhere? No! Hence xVel and yVel remain at 0, hence xPos and yPos remain at 0.

 

This is very weird design and you got bitten by it. Instead of adding calls to xVel() and yVel(), just have setVel() do it:

public void setVel(double v){
    this.v=v;
    yVel=v*Math.sin(alpha)*(deltaSec*gravity);
    xVel=v*Math.cos(alpha);
}

after all, why would you ever want to set the velocity without also setting its x and y components? It creates that weird incoherent state where the x and y components do not match the velocity of the ball.

So now you can remove the pointless xVel() and yVel() methods. Or perhaps these were intended to be property getters and you meant to write:

public double yVel(){
    return yVel;
}

public double xVel(){
    return xVel;
}
Link to comment
Share on other sites

  • 0

it still does not work

1

There are a few problems with your code.

1) As Andre noted, you're not calling the relevant function(s) to calculate the vector velocity. Additionally, Java's Math.sin / cos functions accept radians, not degrees. The conversion function Math.toRadians should fix that.

2) You're not accounting for gravitational acceleration in your move routine.

3) According to the assignment, your shoot implementation should call the move subroutine for each tick (currently defined as 100 miliseconds). Your code appears to be arbitrarily counting to 60.

In truth though, that assignment is quite vague and ambiguous. I had trouble understanding the intent of some of it. The use of private static functions in the Cannonball class for example is puzzling to say the least. Here's how I'd implement it:

 

[snipped - please don't give full solutions to homework assignments - Andre S.]

Link to comment
Share on other sites

  • 0

Interestingly, precomputed quantities don't quite match stepped quantities. Probably a flaw in my trigonometric/kinematic math.

	private void setInitialVelocity ( double theta, double v0 ) {
		
		double sin = Math.sin ( Math.toRadians ( theta ) );
		
		/* v0x = v0 COS ? */
		/* cosine gives us the ratio of the adjacent (horizontal) to the hypotenuse. sohCAHtoa. v0x per second */
		xVel = v0 * Math.cos ( Math.toRadians ( theta ) );

		/* v0y = v0 SIN ?  */
		/* sine gives us the ratio of the opposite (vertical) to the hypotenuse. SOHcahtoa. v0y per second */
		yVel = v0 * sin;

		range 		= ( v0 * v0 ) / GRAVITY_METERSperSECONDsquared * Math.sin ( Math.toRadians ( 2 * theta ) );
		timeToMaxHeight = v0 * sin / GRAVITY_METERSperSECONDsquared;
		timeOfFlight 	= 2 * v0 * sin / GRAVITY_METERSperSECONDsquared;
	}
$  java ProjectileMotion
Enter a starting angle, 1-89 degrees: 45
Enter a firing velocity (0-20 meters/second): 20
range:40.77, tmax: 1.44, tflight: 2.88
sec: 0.10, x: 1.41, y: 1.41     |  *
sec: 0.20, x: 2.83, y: 2.73     |   *
sec: 0.30, x: 4.24, y: 3.95     |    *
sec: 0.40, x: 5.66, y: 5.07     |      *
sec: 0.50, x: 7.07, y: 6.09     |       *
sec: 0.60, x: 8.49, y: 7.01     |        *
sec: 0.70, x: 9.90, y: 7.84     |        *
sec: 0.80, x:11.31, y: 8.57     |         *
sec: 0.90, x:12.73, y: 9.20     |          *
sec: 1.00, x:14.14, y: 9.73     |          *
sec: 1.10, x:15.56, y:10.16     |           *
sec: 1.20, x:16.97, y:10.50     |           *
sec: 1.30, x:18.38, y:10.73     |           *
sec: 1.40, x:19.80, y:10.87     |           *
sec: 1.50, x:21.21, y:10.91     |           *
sec: 1.60, x:22.63, y:10.86     |           *
sec: 1.70, x:24.04, y:10.70     |           *
sec: 1.80, x:25.46, y:10.45     |           *
sec: 1.90, x:26.87, y:10.09     |           *
sec: 2.00, x:28.28, y: 9.65     |          *
sec: 2.10, x:29.70, y: 9.10     |          *
sec: 2.20, x:31.11, y: 8.45     |         *
sec: 2.30, x:32.53, y: 7.71     |        *
sec: 2.40, x:33.94, y: 6.87     |       *
sec: 2.50, x:35.36, y: 5.93     |      *
sec: 2.60, x:36.77, y: 4.89     |     *
sec: 2.70, x:38.18, y: 3.75     |    *
sec: 2.80, x:39.60, y: 2.52     |   *
sec: 2.90, x:41.01, y: 1.18     |  *
sec: 3.00, x:42.43, y:-0.25     | *
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.