Reputation: 23
Edit: I apologise if this was quite badly written the first time around.
The issue I seem to be having is that the main method does not get()
me the object inside the ArrayList, rather it seems to be fetching a string. If I have use the getArr
method inside my main method, the ArrayList seems to behave fine, and get()
gives me the object stored.
If I use the get()
method inside the particle bunch class, the results seems to be a random combination of letters and numbers preceded by an @ sign.
If I use System.out.println(chargedBunch.getArr().get(1));
, the output is a string.
The current output:
PhysicsVector@7852e922
The average velocity is: PhysicsVector@4e25154f
mass 1.67E-27 Position: -0.7741333834277127 0.5957089157918354
-0.8629478031050868 Velocity: 0.01 0.0 0.0 Acceleration: 0.0 0.0 0.0
The main method:
public class ChargedParticleSimulationV2 {
public static void main(String[] args){
double pSpeed = 1.0 * 10e-3; // initial speed of particle in ms^-1
PhysicsVector pDirection = new PhysicsVector(1, 0, 0); // initial direction in cartesian co-ordinates
double magnetic_field_magnitude = 1 * 1e-7; // magnitude of magnetic field
PhysicsVector magnetic_field_direction = new PhysicsVector(0, 0, -1); // direction of magnetic field
double time = 0.0; // set simulation start time (t=0)
double timeStep = 1 * 10e-7; // determines how frequently the particle is incrememnted (***AFFECTS ACCURACY***)
PhysicsVector magnetic_field = PhysicsVector.scale(magnetic_field_magnitude, magnetic_field_direction.getUnitVector());
PhysicsVector electric_field = new PhysicsVector(0, 0, 0); // This program only explores the effects of a
// magnetic field, hence E=0
EMField field = new EMField(electric_field, magnetic_field);
//******everything works fine till here
ParticleBunch chargedBunch = new ParticleBunch(-1, 1, "proton", 100);
chargedBunch.setBunchVelocity(PhysicsVector.scale(pSpeed, pDirection));
System.out.println("The average velocity is: " + chargedBunch.getAvgVelocity().toString());
System.out.println(chargedBunch.getArr().get(1));
}
}
The PhysicsVector class:
public class PhysicsVector {
// Fix the dimension of the array representing the vectors
private static final int vectorSize = 3;
// In this case we have a three dimensional vector, the x component is [0] with y[1] and z [2]
private double[] vectorComponents = new double[vectorSize];
/**
* Default contructor that creates a PhysicsVector with zero magnitude
**/
public PhysicsVector() {
for (int i = 0; i < vectorComponents.length; i++) {
vectorComponents[i] = 0.;
}
}
public PhysicsVector(double x, double y, double z) {
vectorComponents[0] = x;
vectorComponents[1] = y;
vectorComponents[2] = z;
}
public void print() {
String text = this.returnString();
System.out.println(text);
}
public static PhysicsVector add(PhysicsVector v, PhysicsVector u) {
PhysicsVector sum = new PhysicsVector(v);
sum.increaseBy(u);
return sum;
}
public void increaseBy(PhysicsVector v) {
for (int i = 0; i < vectorComponents.length; i++) {
vectorComponents[i] += v.vectorComponents[i];
}
}
The EM Field class:
public class EMField {
protected PhysicsVector electric; // electric field strength
protected PhysicsVector magnetic; // magnetic flux density
/**
* Default constructor. Set data members to zero.
*
*/
public EMField() {
electric = new PhysicsVector();
magnetic = new PhysicsVector();
}
/**
* Constructor with two inputs - the electric field strength and magnetic flux density
*
* @param electricIn The electric field strength
* @param magneticIn The magnetic flux density
*/
public EMField(PhysicsVector electricIn, PhysicsVector magneticIn) {
electric = new PhysicsVector(electricIn);
magnetic = new PhysicsVector(magneticIn);
}
The Particle class:
public class Particle {
protected double mass; // the mass of the particle
protected PhysicsVector position, velocity, acceleration;
public Particle() {
mass = 0;
position = new PhysicsVector();
velocity = new PhysicsVector();
acceleration = new PhysicsVector();
}
The ChargedParticle Class:
public class ChargedParticle extends Particle {
private double charge; // charge of the particle
public ChargedParticle(String nameParticle) {
super();
charge = 0;
if (nameParticle.equalsIgnoreCase("Proton")) {
charge = 1.602192e-19;
mass = 1.67e-27;
}
else if (nameParticle.equalsIgnoreCase("Electron")) {
charge = -1.602192e-19;
mass = 9.11e-31;
}
}
The particle bunch class:
public class ParticleBunch {
protected ArrayList<ChargedParticle> chargedBunch;
public ParticleBunch(int min, int max, String particleName, int particleNumber) {
chargedBunch = new ArrayList<ChargedParticle>();
for (int particlei = 0; particlei < particleNumber; particlei++) {
ChargedParticle chargedParticle = new ChargedParticle(particleName);
PhysicsVector randOrigin = new PhysicsVector(randomWithinRange(min, max), randomWithinRange(min, max), randomWithinRange(min, max));
chargedParticle.setPosition(randOrigin);
chargedBunch.add(particlei, chargedParticle);
}
}
public static double randomWithinRange(double min, double max) {
double random = new Random().nextDouble();
double result = min + (random * (max - min));
return result;
}
public ArrayList<ChargedParticle> getArr() {
return chargedBunch;
}
public PhysicsVector getAvgVelocity() {
PhysicsVector avgVelocity = new PhysicsVector();
System.out.println(chargedBunch.get(1).getPosition().toString());
for (int particlei = 0; particlei < chargedBunch.size(); particlei++) {
avgVelocity.increaseBy(chargedBunch.get(particlei).getVelocity());
}
avgVelocity.scale(1 / chargedBunch.size());
return avgVelocity;
}
Upvotes: 0
Views: 111
Reputation: 6033
You are not comparing the same outputs :
System.out.println("Average Position: " + av.toString() ...
Here you call the toString()
method on the PhysicVector
object. If it is not explicitly defined, there is a default implementation in the Object
class that will return the classname and @.
In the other piece of code :
chargedBunch.getArr().get(1).getPosition().print();
You call a print()
method you have certainly defined which does some System.out.print()
stuff.
You have to defined a specific toString()
for PhysicVector
and use it when you want to print some logs.
Upvotes: 1