-2

is supposed to calculate the coordinates of a projectile launched with respect to time (steps of 100ms), with a linear equation, and it outputs linear numbers, but if i plot this equation with CalcMe.com (math tool) it makes a parabolic plot

InVel = Double.parseDouble(jTextField1.getText());
    g = Double.parseDouble(jTextField8.getText());

    y = 1;

    while(y >= -1) {
        t += 100;
        x = InVel * TimeUnit.MILLISECONDS.toSeconds(t) * Math.cos(45);
        y = InVel * TimeUnit.MILLISECONDS.toSeconds(t) * Math.sin(45) - (1 / 2) * g * Math.pow(TimeUnit.MILLISECONDS.toSeconds(t), 2);
        //System.out.print(Double.toString(x));
        //System.out.printf(" ");
        System.out.print(Double.toString(y));
        System.out.printf("%n");
    }

    jTextField6.setText(Double.toString(x));

the code is in java

g is constant (9.8) and invel is given by user so its constant too g is the gravity and invel the initial velocity of the projectile the equation is:x=invel*time*cos(45) and y=invel*time*sin(45)-(1/2)*g*t^2

anyone can help me?

arf20
  • 15
  • 1
  • 5
  • 1
    Your question title is confusing, you don't know why *what*, why is it working like you want, or why it does something else? Anyway `1 / 2` = 0 since it is integer division. – Pshemo Nov 30 '18 at 17:46
  • no, all variables are double so 0.5 – arf20 Nov 30 '18 at 17:49
  • 2
    `1` and `2` are integers, therefore `1/2` is 0 - it doesn't matter that the further calculation is in double – Thomas Kläger Nov 30 '18 at 17:50
  • The expression (1 / 2) is still all integers and so the result will be 0 like Pshemo stated. – DudeDoesThings Nov 30 '18 at 17:50
  • 1
    To clear up confusion you can declare double literals by either adding decimals like stated by Pshemo `1.0` or appending a ´d´ like so `1d`. Either will give you a double with value ´1´ – nitowa Nov 30 '18 at 18:31

1 Answers1

0

Your milisecond to second value conversion method TimeUnit.MILLISECONDS.toSeconds(t) is the main fact. Its returning long value which one you are wanted double. Please take a look on below code. Probably its your answer. Just replace hard-coded value with your jTextField

public static void main(String[] args) {
    double InVel = Double.parseDouble("10.555");
    double g = Double.parseDouble("9.8");

    double y = 1;
    double x=0;
    double t=0;
    while(y >= -1) {
        t += 100;
        double timeInSeconds =  (t / (double)1000) % (double)60;
        x = InVel * timeInSeconds * Math.cos(45);
        y = InVel * timeInSeconds * Math.sin(45) - ((double) 1 / (double) 2) * g * Math.pow(timeInSeconds, 2);
        //System.out.print(Double.toString(x));
        //System.out.printf(" ");
        System.out.println("X = " + x + " Y = " + Double.toString(y));
        System.out.printf("%n");
    }
}
flopcoder
  • 1,195
  • 12
  • 25