Lesson7
Re: Lesson7
- package fallingparticlecalcapp;
- import org.opensourcephysics.controls.*;
- //beginning of class definition
- public class FallingParticleCalcApp extends AbstractCalculation{
- public void calculate(){
- //gets initial conditions
- double y0 = control.getDouble("Initial y");
- double v0 = control.getDouble("Initial v");
- //sets initial conditions
- Particle ball = new FallingParticle(y0,v0);
- //reads parameters and sets dt
- ball.dt = control.getDouble("dt");
- while(ball.y>0){
- ball.step();
- }
- control.println("final time ="+ball.t);
- //displays numerical results
- control.println("y = "+ball.y+" v ="+ball.v);
- //displays analytic position
- control.println("analytic y="+ball.analyticPosition());
- //displays analytic velocity
- control.println("analytic v="+ball.analyticVelocity());
- }
- public void reset(){
- control.setValue("Initial y", 10);
- control.setValue("Initial v",0);
- control.setValue("dt",0.01);
- }
- //creates a calculation control structure using this class
- public static void main(String[] args){
- CalculationControl.createApp(new FallingParticleCalcApp());
- }
- }
Re: Lesson7
Code: Select all
package fallingparticlecalcapp;
import org.opensourcephysics.controls.*;
//beginning of class definition
public class FallingParticleCalcApp extends AbstractCalculation{
public void calculate(){
//gets initial conditions
double y0 = control.getDouble("Initial y");
double v0 = control.getDouble("Initial v");
//sets initial conditions
Particle ball = new FallingParticle(y0,v0);
//reads parameters and sets dt
ball.dt = control.getDouble("dt");
while(ball.y>0){
ball.step();
}
control.println("final time ="+ball.t);
//displays numerical results
control.println("y = "+ball.y+" v ="+ball.v);
//displays analytic position
control.println("analytic y="+ball.analyticPosition());
//displays analytic velocity
control.println("analytic v="+ball.analyticVelocity());
}
public void reset(){
control.setValue("Initial y", 10);
control.setValue("Initial v",0);
control.setValue("dt",0.01);
}
//creates a calculation control structure using this class
public static void main(String[] args){
CalculationControl.createApp(new FallingParticleCalcApp());
}
}
Re: Lesson7
Add the file FallingParticle (java application) to the project.
Right-click the package "fallingparticlecalcapp"->New->Java Class
Right-click the package "fallingparticlecalcapp"->New->Java Class
- Attachments
-
- addClass.png (26.22 KiB) Viewed 2142 times
Re: Lesson7
- package fallingparticlecalcapp;
- public class FallingParticle extends Particle {
- final static double g = 9.8; // constant
- private double y0 = 0, v0 = 0; // initial position and velocity
- public FallingParticle(double y, double v) { // constructor
- System.out.println("A new FallingParticle object is created.");
- this.y = y; // instance value set equal to passed value
- this.v = v; // instance value set equal to passed value
- y0 = y; // no need to use "this" because there is only one y0
- v0 = v;
- }
- public void step() {
- y = y+v*dt; // Euler algorithm
- v = v-g*dt;
- t = t+dt;
- }
- public double analyticPosition() {
- return y0+v0*t-(g*t*t)/2.0;
- }
- public double analyticVelocity() {
- return v0-g*t;
- }
- }
Re: Lesson7
Code: Select all
package fallingparticlecalcapp;
public class FallingParticle extends Particle {
final static double g = 9.8; // constant
private double y0 = 0, v0 = 0; // initial position and velocity
public FallingParticle(double y, double v) { // constructor
System.out.println("A new FallingParticle object is created.");
this.y = y; // instance value set equal to passed value
this.v = v; // instance value set equal to passed value
y0 = y; // no need to use "this" because there is only one y0
v0 = v;
}
public void step() {
y = y+v*dt; // Euler algorithm
v = v-g*dt;
t = t+dt;
}
public double analyticPosition() {
return y0+v0*t-(g*t*t)/2.0;
}
public double analyticVelocity() {
return v0-g*t;
}
}
Re: Lesson7
- package fallingparticlecalcapp;
- abstract public class Particle {
- double y, v, t; // instance variables
- double dt; // time step
- public Particle() { // constructor
- System.out.println("A new Particle is created.");
- }
- abstract protected void step();
- abstract protected double analyticPosition();
- abstract protected double analyticVelocity();
- }
Re: Lesson7
Code: Select all
package fallingparticlecalcapp;
abstract public class Particle {
double y, v, t; // instance variables
double dt; // time step
public Particle() { // constructor
System.out.println("A new Particle is created.");
}
abstract protected void step();
abstract protected double analyticPosition();
abstract protected double analyticVelocity();
}
Re: Lesson7
The screenshot of FallingParticleCalcApp program.
- Attachments
-
- FallingParticleCalcApp.png (12.88 KiB) Viewed 2141 times