Class AstronomicalSystem

java.lang.Object
it.unimi.di.prog2.s18.AstronomicalSystem

public class AstronomicalSystem extends Object
An astronomical system.

An astronomical system is an ordered set of celestial bodies; it is capable of simulating the evolution (of position and velocity) of the celestial bodies contained within it, and of calculating its total energy.

The intended use of this class is:

  • create an astronomical system;
  • add a list of celestial bodies to it;
  • perform the simulation;
  • calculate the total energy.

The method add(CelestialBody) does not make a copy of the celestial body, therefore if the celestial body is modified after being added, the behavior of the astronomical system is undefined. The last two steps can be repeated to know the intermediate state of the simulation; conversely, adding celestial bodies after the start of the simulation makes the behavior of the astronomical system undefined.

  • Field Details

  • Constructor Details

    • AstronomicalSystem

      public AstronomicalSystem()
      Creates an empty system.
  • Method Details

    • add

      public boolean add(CelestialBody c)
      Adds a celestial body to the system.

      If the celestial body is already present in the system (i.e., there is already a celestial body with the same name), this request is silently ignored.

      Parameters:
      c - the celestial body.
      Returns:
      true if the celestial body was added, false otherwise.
      Throws:
      NullPointerException - if c is null.
    • step

      private void step()
      Performs a simulation step.

      For details see the overview of this package.

    • simulateSteps

      public void simulateSteps(int num)
      Simulates the evolution of the system for a given number of steps.
      Parameters:
      num - the number of steps to simulate.
      Throws:
      IllegalArgumentException - if num is not positive.
    • totalEnergy

      public long totalEnergy()
      Returns the total energy of the system.
      Returns:
      the total energy.
    • toString

      public String toString()
      Overrides:
      toString in class Object