Package it.unimi.di.prog2.s18
Class CelestialBody
java.lang.Object
it.unimi.di.prog2.s18.CelestialBody
- All Implemented Interfaces:
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 Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
CelestialBody
(String name, int x, int y, int z) Constructs a celestial body with the given name. -
Method Summary
Modifier and TypeMethodDescriptionint
long
energy()
Returns the total energy of this celestial body.boolean
int
hashCode()
name()
Returns the name of this celestial body.position()
Returns the position of this celestial body.protected void
Sets the position of this celestial body.abstract void
Updates the position of this celestial body (usually following a change in its velocity).abstract void
Updates the velocity of this celestial body.abstract Point
velocity()
Returns the velocity of this celestial body.
-
Field Details
-
name
The name of the celestial body. -
position
The position of the celestial body.
-
-
Constructor Details
-
CelestialBody
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 isnull
.IllegalArgumentException
- if the name is composed of only spaces, or empty.
-
-
Method Details
-
name
Returns the name of this celestial body.- Returns:
- the name.
-
position
Returns the position of this celestial body.- Returns:
- the position (not
null
}).
-
position
Sets the position of this celestial body.- Parameters:
position
- the new position.- Throws:
NullPointerException
- if the position isnull
.
-
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
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 isnull
.
-
compareTo
- Specified by:
compareTo
in interfaceComparable<CelestialBody>
-
equals
-
hashCode
public int hashCode()
-