Quod Erat Demonstrandum

Blog about creative IT

Mechanic Objects in Java I

with 2 comments

The principles of object oriented allow us to design software more near to the reality than ever. A simple example would be a so called mechanic object in Java. I’ve created one and placed it into my qed42 framework. It’s really easy to use and I will explain to you how to create such abstract objects.

First of all you need to know what you want to do! What is your programming simulating or doing? I decided to take a simple physic model which is able to apply mechanical laws. Ok so first I need a simple software design because a mechanic object is actually an object that interacts with others.

Let us ask what a mechanic object really is. Well what is a mechanic object, or what can it else do and is there maybe an abstraction layer below the “mechanic” ? Well there is one, ok there must not be one but I like it because it’s a little bit Software Engineering. So the object I am talking about is the physical object. And we know that there are maybe a lot of phyiscal objects because it can be a mechanic object it can be a mechanic object with some special functions like gravity or relativistic formulas. This let us make the conclusion that the physical object here must be an interface. Well an interface in Java.

This is actually not everything because a mechanic object maybe has to inherit some functionality from other abstract objects like basic mechanics. We actuall know that we can calculate forces just like F = m * g or other funny things. Those formulas should be known in our mechanic object because we want to do a lot of things with the object but we don’t want to say things like: mechanicobject.getForce(); or something like this. We want it more abstract, just like mechanicobject.colide(otherobject); Let’s make some code! Starting with the interface:

// used to define the basics for a mechanical object
public interface PhysicalObject
{
     // Like a bounding box, but I don't use it right now
     public Vectr3D[] getBoundEdges();

     // Calculate the middle coordinates and return them
     // I don't use it right now, too
     public Vectr3D[] getMidCoord();

     // returns the name of the object
     public String getName();

     // returns the velocity in form of a unit
     public Unit<Double,String> getVelocity();

     // returns the weight in form of a unit
     public Unit<Double,String> getWeight();
}

I’m using a lot of selfmade things like Vectr3D or Unit. If you want to check what those things are doing just do it. Click on them to get to the code. If you don’t want to know all about it keep reading here, I will explain. Vectr3D defines a vector in three dimensions. It is able to store three double values, to make an toString()-output and to calculate it’s length.

Unit is used to save things like: 50 N (Newton) or 220 V (Volt). Just to have a nice toString()-output.

Now we have our interface defined but we have to start with the BasicMechanics class to get some more formulas working. It’s very easy because we just calculate some very very simple things. Just have look:

public class BasicMechanics extends Trackr<Unit<Double, String>> implements
                                           BasicPhysic<Double, String>
{
    public Unit<Double, String> getForce(double m)
    {
         return this.storeResult(new Unit<Double, String>(m * 9.807, "F"));
    }

    public Unit<Double, String> getForce(double m, double g)
    {
         return this.storeResult(new Unit<Double, String>(m * g, "F"));
    }

    public Unit<Double, String> getForceAccel(double m, double a)
    {
        return this.storeResult(new Unit<Double, String>(m * a, "F"));
    }

    public Unit<Double, String> getGravityForce(double m1, double m2, double r)
    {
         return this.storeResult(new Unit<Double, String>  (6.67420E-11 * (m1 * m2) /
         (SimpleOperation.quad(r)), ""));
    }

    public Unit<Double, String> getKineticEnergy(double m, double v)
    {
         return this.storeResult(new Unit<Double, String>(0.5 * m
				* SimpleOperation.quad(v), "J"));
    }

    public Unit<Double, String> getPotEnergy(double m, double g, double h)
    {
        return this.storeResult(new Unit<Double, String>(m * g * h, "J"));
    }

    public Double storeResultSimple(Double result)
    {
         if (this.getTrackActive())
         {
              this.getTrack().add(new Unit<Double,String>(result, "simple"));
              return result;
         }
         return result;
     }
}

Ok a lot of code but never mind the storeResult method and the extended class because it’s just a mechanism to store results in a list outside. If you want to make this by yourself just kick the storeResult out and don’t call storeResult just return the formula and throw the things like “extends” and “implements” out!

So you won’t find any usage at the moment in the mechanic object because I didn’t used anything from that at the moment. Mechanic object is still in creation mode. Before I show you what a mechanic object can do you have to know about the private datafields. Don’t wonder about things you don’t know like Collision.

public class MechanicObject extends BasicMechanics
                                       implements PhysicalObject
{
    // the bounding box
    private Vectr3D[] boundEdges;

    // the middle coordinates
    private Vectr3D midCoords;

    // the name
    private String name;

    // the initial velocity
    private Unit<Double, String> velocity;

    // the initial weight
    private Unit<Double, String> weight;

    // the velocity after a collision with another object
    private Unit<Double, String> velocityAfterCollision;

    // the collision with another object stored as an abstract data type
    private Collision<Double> collisionOne;

    ...
    ...

At this point I usually use to make my constructors but you can do it by yourself. Actually it’s not very hard to do it :-) If you have trouble, just ask me. Second thing I don’t want to paste here is the getters and setters for the private data fields. The only method I’ve done yet is the collision with another object. Let’s see how it works.

public void colide(MechanicObject mo, boolean rounded)
{
    Double thisVelocity = (this.weight.getNumber()*this.velocity.getNumber() +
          mo.getWeight().getNumber()*
          (2*mo.getVelocity().getNumber()-this.velocity.getNumber())/
          (this.weight.getNumber()+mo.getWeight().getNumber()));

    this.velocityAfterCollision = new Unit<Double,String>(thisVelocity,"m/s");

    Double moVelocity = (mo.getWeight().getNumber()*mo.getVelocity().getNumber() +
          this.weight.getNumber()*
          (2*this.velocity.getNumber()-mo.getVelocity().getNumber()))/
          (this.weight.getNumber()+mo.getWeight().getNumber());

    mo.velocityAfterCollision = new Unit<Double,String>(moVelocity,"m/s");

    this.collisionOne = new Collision<Double>(thisVelocity, moVelocity);

    System.out.println("Collision occured in: "+this.getClass().getName());

    System.out.println("--> Object 1: "+this.getName()+" m="+
          this.getWeight().getNumber()+""+this.getWeight().getUnit()+"
          v="+this.getVelocity().getNumber()+" "+this.getVelocity().getUnit()+
          " Object 2: "+mo.getName()+" m="+mo.getWeight().getNumber()+"
          "+mo.getWeight().getUnit()+" v="+mo.getVelocity().getNumber()+"
          "+mo.getVelocity().getUnit()+" <--");

    if(rounded)
        System.out.println("--> New Velocity of "+this.getName()+" is
                 "+Math.round(this.velocityAfterCollision.getNumber())+"
                 "+this.velocityAfterCollision.getUnit()+" | New Velocity of "
                 +mo.getName()+" is "+Math.round(mo.velocityAfterCollision.getNumber())+
                 " "+mo.velocityAfterCollision.getUnit());
    else
        System.out.println("--> New Velocity of "+this.getName()+" is "+
                 this.velocityAfterCollision.getNumber()+"
                 "+this.velocityAfterCollision.getUnit()+" | New Velocity of "
                 +mo.getName()+" is "+mo.velocityAfterCollision.getNumber()+"
                 "+mo.velocityAfterCollision.getUnit());
}

Yes it is a very complex output here but the output looks very very good if you start the program. If you want, you can exclude the whole output into the Collision class. Actually I didn’t show it here but if you are common with basic java techniques you should be able to exclude it there to keep this method only calculating. Just a hint for this: make the body not void but make it “public Collision collide(…)” to make the Collision thing or “public String collide(…)” if you want to make a return-method out of this. I prefer this one because it works! Later on I will show you more super things what my mechanic object can do. But here you have an example for how to use it and the output:

...
...

MechanicObject mo1 = new MechanicObject("mechanicObject1",
                                     new Unit<Double,String>(20.5,"m/s"),
                                     new Unit<Double,String>(20.5,"kg"));

MechanicObject mo2 = new MechanicObject("mechanicObject2",
                                     new Unit<Double,String>(80.5,"m/s"),
                                     new Unit<Double,String>(25.8,"kg"));

mo1.collide(mo2, false);  // get unrounded values
System.out.println();
mo1.collide(mo2, true);   // get rounded values    

...
...

And the output for this beautiful code:


Collision occured in: qed.phy.MechanicObject
--> Object 1: mechanicObject1 m=20.5 kg v=20.5 m/s Object 2: mechanicObject2 m=25.8 kg v=80.5 m/s New Velocity of mechanicObject1 is 498.5415766738661 m/s | New Velocity of mechanicObject2 is 27.368250539956808 m/s


Collision occured in: qed.phy.MechanicObject
--> Object 1: mechanicObject1 m=20.5 kg v=20.5 m/s Object 2: mechanicObject2 m=25.8 kg v=80.5 m/s New Velocity of mechanicObject1 is 499 m/s | New Velocity of mechanicObject2 is 27 m/s

And next time I show you why I want to inherit from that much classes!

Written by generalvlad86

July 3, 2009 at 2:02 pm

2 Responses

Subscribe to comments with RSS.

  1. wow, good work!
    That is a very good tutorial!!!!

    Michael Leung

    July 3, 2009 at 2:28 pm


Leave a Reply