Jump to content

Help with a cannonball class and main class?

Above7heInfluence

So my lesson was to make a class cannonball that figures the position of a cannonball using 2 methods. I got the one method to work but im having an issue with the exact formula method ( position = -.5 * gravity * (deltaT * deltaT) + velocity * deltaT). I just need help with getting the exact formula to work. It is suppose to update time as .01 but print every 100 times it is ran. The values should be close between the two methods but the exact formula is printing way off so i think something is wrong. Here is the code for the 2 files. Thank you.

 

/**   This class simulates a cannonball fired up in the air.*/public class Cannonball{   // private implementation   private double deltaT;   private double ivel;   private double position;   private double cvel;   private double g = 9.81;   private double position2;   private double ccvel;   private double deltaTT;   private double g2 = 9.81;   /**      Creates a Cannonball object at position 0.      @param ivel the initial velocity   */   public Cannonball(double ivel) {        cvel = ivel;       ccvel = ivel;   }   /**      Updates the position and velocity of this cannon ball       after a given time interval.      @param deltaT the time interval   */   public void move(double deltaT) {        position = position + getVelocity() * deltaT;       cvel = getVelocity() - g * deltaT;   }   /**      Gets the velocity of this cannon ball.      @return the velocity   */   public double getVelocity() {        return cvel;   }      /**      Gets the position of this cannon ball.      @return the (vertical) position   */   public double getPosition() {        return position;   }   public void movee(double deltaTT) {       position2 = -0.5 * g2 * (deltaTT * deltaTT) + ccvel * deltaTT;   }      /**      Gets the velocity of this cannon ball.      @return the velocity   */   public double getVelocity2() {        return ccvel;   }   /**      Gets the position of this cannon ball.      @return the (vertical) position   */   public double getPosition2() {        return position2;   }}

and the main class. 

 

/* * a tester for the cannonball class. *//** * * @author  */import java.util.Scanner;public class CannonballTester {    /**     * @param args the command line arguments     */    public static void main(String[] args) {      Scanner in = new Scanner(System.in);            System.out.println("Enter initial velocity of the cannonball: ");      int n = in.nextInt();         Cannonball ball = new Cannonball(n);           boolean done = false;      int i = 0;      while (!done)      {          ball.move(.01);          ball.movee(.01);          i++;           if (i == 100)            {                System.out.println("Current Position: " + ball.getPosition());                System.out.println("Position from exact formula: " + ball.getPosition2());                i = 0;            }          if (ball.getPosition() <= 0)              done = true;      }    }}
 

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

Could you give us a dump of the output?

Primary:

Intel i5 4670K (3.8 GHz) | ASRock Extreme 4 Z87 | 16GB Crucial Ballistix Tactical LP 2x8GB | Gigabyte GTX980ti | Mushkin Enhanced Chronos 240GB | Corsair RM 850W | Nanoxia Deep Silence 1| Ducky Shine 3 | Corsair m95 | 2x Monoprice 1440p IPS Displays | Altec Lansing VS2321 | Sennheiser HD558 | Antlion ModMic

HTPC:

Intel NUC i5 D54250WYK | 4GB Kingston 1600MHz DDR3L | 256GB Crucial M4 mSATA SSD | Logitech K400

NAS:

Thecus n4800 | WD White Label 8tb x4 in raid 5

Phones:

Oneplux 6t (Mint), Nexus 5x 8.1.0 (wifi only), Nexus 4 (wifi only)

Link to comment
Share on other sites

Link to post
Share on other sites

How do i do that? Just copy and paste it? 

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think its because you arent setting the velocity again in the "accurate" calculation.

 

In the first calc you are changing the value of the velocity to account for the force of gravity after calculating the position, the second calc does not have this.

 

The falling body equation is (.5)g(t^2), but you still have to account in the change of the initial velocity (the N value passed in as args). Over time the initial velocity has to change due to the force of gravity on it.

 

Just adding "ccvel = getVelocity2() - g * deltaT;" after calculating the second position should fix it.

Primary:

Intel i5 4670K (3.8 GHz) | ASRock Extreme 4 Z87 | 16GB Crucial Ballistix Tactical LP 2x8GB | Gigabyte GTX980ti | Mushkin Enhanced Chronos 240GB | Corsair RM 850W | Nanoxia Deep Silence 1| Ducky Shine 3 | Corsair m95 | 2x Monoprice 1440p IPS Displays | Altec Lansing VS2321 | Sennheiser HD558 | Antlion ModMic

HTPC:

Intel NUC i5 D54250WYK | 4GB Kingston 1600MHz DDR3L | 256GB Crucial M4 mSATA SSD | Logitech K400

NAS:

Thecus n4800 | WD White Label 8tb x4 in raid 5

Phones:

Oneplux 6t (Mint), Nexus 5x 8.1.0 (wifi only), Nexus 4 (wifi only)

Link to comment
Share on other sites

Link to post
Share on other sites

run:

Enter initial velocity of the cannonball: 

100

Current Position: 95.14404999999988

Position from exact formula: 0.9995095

Current Position: 180.47809999999953

Position from exact formula: 0.9995095

Current Position: 256.002149999999

Position from exact formula: 0.9995095

Current Position: 321.7161999999982

Position from exact formula: 0.9995095

Current Position: 377.62024999999716

Position from exact formula: 0.9995095

Current Position: 423.714299999996

Position from exact formula: 0.9995095

Current Position: 459.9983499999944

Position from exact formula: 0.9995095

Current Position: 486.4723999999931

Position from exact formula: 0.9995095

Current Position: 503.13644999999144

Position from exact formula: 0.9995095

Current Position: 509.99049999999016

Position from exact formula: 0.9995095

Current Position: 507.03454999998866

Position from exact formula: 0.9995095

Current Position: 494.2685999999873

Position from exact formula: 0.9995095

Current Position: 471.692649999986

Position from exact formula: 0.9995095

Current Position: 439.3066999999846

Position from exact formula: 0.9995095

Current Position: 397.11074999998306

Position from exact formula: 0.9995095

Current Position: 345.1047999999814

Position from exact formula: 0.9995095

Current Position: 283.28884999997956

Position from exact formula: 0.9995095

Current Position: 211.66289999997727

Position from exact formula: 0.9995095

Current Position: 130.22694999997486

Position from exact formula: 0.9995095

Current Position: 38.98099999997224

Position from exact formula: 0.9995095

BUILD SUCCESSFUL (total time: 2 seconds)

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
Share on other sites

Link to post
Share on other sites

ooooooh ok i think i see what is going on. you need a variable to total the delta T. Once you have that, then the function will be

   public void movee(double deltaTT) {

       VarToHoldDeltaT = VarToHoldDeltaT + deltaTT
       position2 = -0.5 * g2 * (VarToHoldDeltaT* VarToHoldDeltaT) + ccvel * VarToHoldDeltaT;
   }

 

The DeltaT is really the delta from 0, not from the iteration before. You need to sum it up from the start.

Primary:

Intel i5 4670K (3.8 GHz) | ASRock Extreme 4 Z87 | 16GB Crucial Ballistix Tactical LP 2x8GB | Gigabyte GTX980ti | Mushkin Enhanced Chronos 240GB | Corsair RM 850W | Nanoxia Deep Silence 1| Ducky Shine 3 | Corsair m95 | 2x Monoprice 1440p IPS Displays | Altec Lansing VS2321 | Sennheiser HD558 | Antlion ModMic

HTPC:

Intel NUC i5 D54250WYK | 4GB Kingston 1600MHz DDR3L | 256GB Crucial M4 mSATA SSD | Logitech K400

NAS:

Thecus n4800 | WD White Label 8tb x4 in raid 5

Phones:

Oneplux 6t (Mint), Nexus 5x 8.1.0 (wifi only), Nexus 4 (wifi only)

Link to comment
Share on other sites

Link to post
Share on other sites

This is what i came up with, but its untested:
 

/**


   This class simulates a cannonball fired up in the air.
*/
public class Cannonball
{
   // private implementation
   // Holds the value of time that has passed.
   private double deltaT;
   
   // Holds the current velocity
   private double velocity;
   
   // Holds the current position
   private double position;
   
   // Holds the gravity value
   private const double g = 9.81;

   /**
      Creates a Cannonball object at position 0.
      @param ivel the initial velocity
   */
   public Cannonball(double ivel) {
       velocity = ivel;
   }
      
   /**
      Gets the position of this cannon ball.
      @return the (vertical) position
   */
   public double getPosition() {
       return position;
   }

   /**
      Updates the position and velocity of this cannon ball
      after a given time interval.
      @param iDeltaT the time interval
   */
   public void move(double iDeltaT) {
       // Update Position
       position = position + velocity * iDeltaT;
       
       // Update velocity
       velocity = velocity - g * iDeltaT;
   }

   /**
      Updates the position and velocity of this cannon ball
      after a given time interval. Using a more accurate function.
      @param iDeltaT the time interval
   */
   public void moveAcc(double iDeltaT) {
       // Update the amount of time that has passed
       deltaT = deltaT + iDeltaT;
       
       // Update the position
       position = -0.5 * g2 * (deltaT * deltaT) + velocity * deltaT;
   }
}

 


 * a tester for the cannonball class.
 */


/**
 *
 * @author
 */
import java.util.Scanner;


public class CannonballTester {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      
      System.out.println("Enter initial velocity of the cannonball: ");
      int n = in.nextInt();
   
      // Inaccurate ball
      Cannonball ball = new Cannonball(n);
      
      // Accurate ball
      Cannonball accBall = new Cannonball(n);
     
      boolean done = false;
      int i = 0;
      while (!done)
      {
          ball.move(.01);
          accBall.moveAcc(.01);
          i++;
           if (i == 100)
            {
                System.out.println("Current Position: " + ball.getPosition());
                System.out.println("Position from exact formula: " + accBall.getPosition());
                i = 0;
            }
          if (ball.getPosition() <= 0 && accBall.getPosition() <= 0)
              done = true;
      }
    }
}

/*

 

 

I THINK this should work, but im not set to debug java on this machine, so careful using it.

Primary:

Intel i5 4670K (3.8 GHz) | ASRock Extreme 4 Z87 | 16GB Crucial Ballistix Tactical LP 2x8GB | Gigabyte GTX980ti | Mushkin Enhanced Chronos 240GB | Corsair RM 850W | Nanoxia Deep Silence 1| Ducky Shine 3 | Corsair m95 | 2x Monoprice 1440p IPS Displays | Altec Lansing VS2321 | Sennheiser HD558 | Antlion ModMic

HTPC:

Intel NUC i5 D54250WYK | 4GB Kingston 1600MHz DDR3L | 256GB Crucial M4 mSATA SSD | Logitech K400

NAS:

Thecus n4800 | WD White Label 8tb x4 in raid 5

Phones:

Oneplux 6t (Mint), Nexus 5x 8.1.0 (wifi only), Nexus 4 (wifi only)

Link to comment
Share on other sites

Link to post
Share on other sites

Please explain in detail the instance variables in the Cannonball class. It's very confusing for me, especially g and g2 when you only seem to need only g.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×