Class AstronomicalSystem
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final SortedSet
<CelestialBody> The set of celestial bodies. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(CelestialBody c) Adds a celestial body to the system.void
simulateSteps
(int num) Simulates the evolution of the system for a given number of steps.private void
step()
Performs a simulation step.toString()
long
Returns the total energy of the system.
-
Field Details
-
celestialBodies
The set of celestial bodies.
-
-
Constructor Details
-
AstronomicalSystem
public AstronomicalSystem()Creates an empty system.
-
-
Method Details
-
add
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
-