How to Build Your Own Solar System in Java

Save

Modeling a solar system in Java, besides being fun, is a good exercise to illustrate and practice thinking in terms of some basic concepts in Object Oriented Design (OOD), especially inheritance and polymorphism. You can learn OOD concepts with a simple build-your-own solar system model.

Things You'll Need

  • Computer
  • Java Software Development Kit (SDK)
  • Text editor or Java Integrated Development Environment (IDE)
  • Create your class files. Create a skeletal structure for your solar system by creating the basic class files.  Don't worry about what goes in them right now. Just create the follow Java files:

    GravityObject.java
    OrbitalSystem.java
    Star.java
    Planet.java

    You might add more to these later on if you like. For example, you could add a Moon class, and you could add planet and star types.

  • Create GravityObject abstract class. You'll want to create your GravityObject as an abstract class. Declaring a class to be abstract in Java indicates to the compiler that we want this class to serve as a model to be used by other classes, but the class should not be created itself. This makes sense for the solar system example: there is no such thing as a \"GravityObject\", however there are things like planets and stars, which are objects which have and are affected by gravity. You only write the programming for it one time. In Object Oriented Design, this trait is called Inheritance.

    Type this into your GravityObject file:

    public abstract class GravityObject {
    double xPosition;
    double yPosition
    GO
    double degreeInOrbit
    GO
    double distanceFromParent
    GO

    GravityObject() {
    this.distance = 0
    GO
    }

    GravityObject(double distance) {
    this.distance = distance
    GO
    }
    }

    This is a simple example, so you'll only use the x and y positions of the object, along with the distance from its parent and a degree variable. You could later create another abstract class, 3DGravityObject or RelativisticGravityObject, and have it inherit from this object. This will allow you to add the details for things that change.

  • Create the OrbitalSystem abstract class. This class will also be abstract, but will be more sophisticated than the GravityObject class.

    import java.util.ArrayList
    GO

    public abstract class OrbitalSystem extends GravityObject {
    private ArrayList children = new ArrayList(); // Objects within the system. They will orbit the parent.

    public void add(GravityObject child) { children.add(child); }

    public void tick() {
    for (int x = 0; x < children.size(); x++) {
    GravityObject current = children.get(x)
    GO
    current.degree += 1
    current.xPosition = this.xPosition + Math.cos(degree/180 Math.PI) current.distance
    GO
    current.yPosition = this.yPosition - Math.sin(degree/180 Math.PI) current.distance
    GO
    } (see references 2)

    }
    }

    The class extends the GravityObject class. The ArrayList holds all GravityObjects, and declared its variable private so that you can force other classes to use the add function, ensuring that only GravityObjects can be added to the array. This illustrates two other important OOD concepts besides inheritance. The first is data hiding: by sealing it away, you've ensured that other parts of the program cannot access it and put invalid information into it. The second is polymorphism, which allows us to refer to an object using not only its own names, but the names of any of its ancestors. This allows for a great deal of flexibility in writing code.

  • Write planet and star classes. Since most of the work has been done in the abstract OrbitalSystem and GravityObject classes, the Planet and Star classes will be simple.

    public class Star extends OrbitalSystem { }
    GO

    and

    public class Planet extends GravityObject { };

  • Write main class. Your main function should look like the following:

    public static int main(String[] args) {
    Star s = new Star(); // Create a new star.
    s.add(new Planet(20)); // Add a planet to the star's orbital system which orbits at a distance of 20 units.
    s.add(new Planet(66)); // Add another planet to the star's orbital system which orbits at a distance of 66 units.

    while (true) {
    s.tick()
    GO
    }

    }

    This will create a star and two orbiting planets, and will set them in motion.

References

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!