Class CelestialBody

java.lang.Object
it.unimi.di.prog2.s17.CelestialBody
All Implemented Interfaces:
Comparable<CelestialBody>
Direct Known Subclasses:
Planet, Star

public abstract class CelestialBody extends Object implements Comparable<CelestialBody>
A celestial body.

A celestial body is characterized by

  • a name (not empty or composed of only spaces);
  • a position and a velocity given by a Point;
  • an energy given by the product of the norm of the position and the norm of the velocity.

A celestial body can update its velocity based on the interaction with another celestial body; once its velocity is modified, it can update its position. For details, refer to the overview of this package.

Equality and ordering between celestial bodies are based solely on the name.

  • Field Details

    • name

      private final String name
      The name of the celestial body.
    • position

      private Point position
      The position of the celestial body.
  • Constructor Details

    • CelestialBody

      protected CelestialBody(String name, int x, int y, int z)
      Constructs a celestial body with the given name.
      Parameters:
      name - the name.
      x - the x coordinate of the initial position.
      y - the y coordinate of the initial position.
      z - the z coordinate of the initial position.
      Throws:
      NullPointerException - if the name is null.
      IllegalArgumentException - if the name is composed of only spaces, or empty.
  • Method Details

    • name

      public String name()
      Returns the name of this celestial body.
      Returns:
      the name.
    • position

      public Point position()
      Returns the position of this celestial body.
      Returns:
      the position (not null}).
    • position

      protected void position(Point position)
      Sets the position of this celestial body.
      Parameters:
      position - the new position.
      Throws:
      NullPointerException - if the position is null.
    • velocity

      public abstract Point velocity()
      Returns the velocity of this celestial body.
      Returns:
      the velocity (not null}).
    • energy

      public long energy()
      Returns the total energy of this celestial body.
      Returns:
      the energy.
    • updatePosition

      public abstract void updatePosition()
      Updates the position of this celestial body (usually following a change in its velocity).
    • updateVelocity

      public abstract void updateVelocity(CelestialBody c)
      Updates the velocity of this celestial body.

      The velocity must be updated based on the interaction with the celestial body given as an argument according to what is illustrated in the overview of this package; this method must not modify the celestial body given as an argument.

      Parameters:
      c - the celestial body to interact with.
      Throws:
      NullPointerException - if c is null.
    • compareTo

      public int compareTo(CelestialBody o)
      Specified by:
      compareTo in interface Comparable<CelestialBody>
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object