Using class and methods for updating information...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alireza6485
    New Member
    • Jan 2009
    • 19

    Using class and methods for updating information...

    Hi,Please help me to answer this question.I tried my best and I am not sure if my code is good or useless.
    I am writing a program that calculates projectile motion.
    so the ball is thrown with initial speed ,angle in postion x=0 and v=0
    location and vertical velocity are changing while horizental velocity is constant.DELTA= 0.01 and G=9.8
    the formulas are :
    The formula for updating the y-position is y = y + vy * DELTA ;
    where vy is the vertical velocity, and we assume that at time zero y is 0.
    The vertical velocity is updated by vy = vy - g * DELTA ;
    where g = 9.81 m/sec2 is the acceleration due to gravity near the earth. There is no acceleration in the horizontal direction.
    Therefore, the velocity in the x-direction remains constant, and the horizontal x-position is updated by x = x + vx * DELTA.
    Given an initial speed v and angle , the horizontal and vertical speeds are vx = v * cos(q) ; vy = v * sin(q) ;

    I have to :
    Write a class Baseball with private instance variables x, y, vx, and vy. Make appropriate constructors so that these variables can be initialized when constructing a Baseball object. Give the class a static variable DELTA which you should set to 0.01, so that the position can be updated each 0.01 seconds. Also make a static variable for gravitational acceleration G. Give an instance method update() which updates the instance variables, as described above. Provide instance methods for accessing each of the four instance variables (e.g., public double getX()).

    IS my code right???(IF not please re-write and fix it for me):


    public class Baseball
    {
    private double x;
    private double y;
    private double vx;
    private double vy;
    private static double DELTA=0.01;
    private static double G=9.8;

    public void initialinfo(dou ble velocity,double teta)
    {
    x=0;
    y=0;
    vx=velocity*Mat h.cos(teta);
    vy=velocity*Mat h.sin(teta);
    }

    public void vyInfo()
    {
    double newvy=vy-(G*DELTA);
    vy=newvy;
    }

    public void yInfo()
    {
    double newY=y+(vy*DELT A);
    y=newY;
    }

    public void xInfo()
    {
    double newX=y+(vx*DELT A);
    x=newX;
    }

    public double getX()
    {
    return x;
    }


    public double getY()
    {
    return y;
    }

    }
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Originally posted by alireza6485
    IS my code right???
    The way to tell is to write another class with a main() method that

    (1) Creates a Baseball instance
    (2) Updates it lots of times
    (3) After each update prints out the ball's position.

    Then take the positions and graph them on a piece of paper. You expect to see a parabola. Or find a reasonably open space and watch the path of an object you throw: that's the shape your graph should have.

    Also check what you have written against each of the things that the question said to do. "Make appropriate constructors so that these variables can be initialized when constructing a Baseball object." Where is your constructor?

    Comment

    Working...