Package it.unimi.di.prog2.e18


package it.unimi.di.prog2.e18
Codice relativo alla lezione 18 per maggiori dettagli si veda il diario del corso.

Questo esercizio è stato assegnato come tema d’esame nell’appello del 21 gennaio 2020 ed è ispirato a The N-Body Problem, il dodicesimo problema dell'Advent of Code del 2019.

Astronomical System

The purpose of the exercise is to design and implement a hierarchy of objects useful to represent and simulate the behavior of an astronomical system composed of some celestial bodies (such as planets and stars) subject to mutual gravitational interaction.

You will need to decide which classes (concrete or abstract) to implement. For each of them, you will need to describe in Javadoc format the choices related to the representation of the state (with particular reference to the representation invariant and the abstraction function) and the methods (with particular reference to pre-/post-conditions and side effects).

A celestial body is characterized by a name and a position, described by a three-dimensional point with integer coordinates; the norm of a three-dimensional point is the sum of the absolute values of its components (also known as the 1 norm). We will assume for simplicity that there are only two types of celestial bodies: stars and planets. Stars never change their position, unlike planets. Therefore, in addition to position, planets are characterized by their velocity, also described by a three-dimensional point. Each celestial body has an energy given by the product of the potential energy, corresponding to the norm of its position, and the kinetic energy, corresponding to the norm of its velocity (obviously this energy is zero in the case of fixed stars).

Celestial bodies are subject to mutual gravitational attraction which modifies their velocity and, indirectly, their position as follows:

  • first, each planet modifies its velocity based on the attraction towards all other celestial bodies: given the planet p and the celestial body c , independently for each of the three coordinates, the velocity of p changes by +1 or -1 depending on whether that coordinate is, respectively, less than or greater than that of c;

  • once the new velocity for all planets has been calculated, each planet modifies its position by adding the value of its velocity to that of its position (as if the planet were subject to uniform motion for one unit of time).

For example, if the system included only two planets and initially the x coordinate of Mars' position was 3 and that of Jupiter was 5, then the x coordinate of Mars' velocity would change by +1 (because 3 < 5) while that of Jupiter would change by -1 (because 5 > 3). Since initially the velocities are zero, after this update the x coordinate of Mars' velocity would be 1, while that of Jupiter would be -1 and updating the position of the two planets would bring the x coordinate of both positions to 4. Consequently, in the next update their two velocities (but not their positions) would remain unchanged.

An astronomical system is a collection of planets and fixed stars. It is characterized by a state that evolves in discrete time: at time 0 the state is given by the list of all celestial bodies that compose it, with the assigned position and zero velocity, then for each time step the two updates described above occur:

  • each planet (interacting with all celestial bodies in the system) updates its velocity,
  • after the new velocities have been calculated for all planets, each planet proceeds to update its position.

This determines the new state, consisting of the list of all celestial bodies with appropriately updated positions and velocities. The total energy of an astronomical system in a certain state is given by the sum of the energy of all the celestial bodies that compose it.

Test Class

Complete the AstronomicalSystemClient main method so that it reads from the input stream a sequence of quintuples corresponding to the various celestial bodies; each quintuple is given by:

  • a character that can be S or P to indicate, respectively, that the celestial body is a star or a planet,
  • a string (which does not contain spaces) indicating the name of the celestial body,
  • three integers indicating the initial coordinates of the celestial body.

These quintuples should be used to populate an astronomical system that must be evolved a number of steps equal to the integer indicated as a parameter on the command line. At the end of the evolution, the list of celestial bodies in the system (in alphabetical order by name and with data related to position and velocity) should be printed, and finally the total energy of the system should be printed.

Constraints

You can assume that the input has the specified format, that all numbers involved in the execution of the code are integers (and can be represented by variables of type int for position and velocity, and of type long for energies). Therefore, a plausible way to read the information provided in input is as follows:


 Scanner s = new Scanner(System.in);
 while (s.hasNext()) {
 char pOrS = s.next().charAt(0); // can be P or S
   String name = s.next()
   int x = s.nextInt();
   int y = s.nextInt();
   int z = s.nextInt();
   …
 }
 

Example

Running solution 1 and having


 P Mars -8 -10 0
 P Jupiter 5 5 10
 P Saturn 2 -7 3
 P Venus 9 -8 -3
 

in the input stream, the program outputs


 Planet, name: Jupiter, pos: (4, 2, 7), vel: (-1, -3, -3)
 Planet, name: Mars, pos: (-5, -7, 1), vel: (3, 3, 1)
 Planet, name: Saturn, pos: (3, -8, 2), vel: (1, -1, -1)
 Planet, name: Venus, pos: (6, -7, 0), vel: (-3, 1, 3)
 Total Energy: 312
 

Similarly, with the same input, running solution 100 outputs


 Planet, name: Jupiter, pos: (13, 16, -3), vel: (3, -11, -5)
 Planet, name: Mars, pos: (8, -12, -9), vel: (-7, 3, 0)
 Planet, name: Saturn, pos: (-29, -11, -1), vel: (-3, 7, 4)
 Planet, name: Venus, pos: (16, -13, 23), vel: (7, 1, 1)
 Total Energy: 1940
 

in the output stream. Similarly, running solution 21 and having


 S Sun 0 0 0
 P Jupiter 1 0 0
 P Mars 0 1 0
 P Saturn 0 0 1
 

in the input stream, the program outputs


 Planet, name: Jupiter, pos: (-2, 0, 0), vel: (-3, 2, 2)
 Planet, name: Mars, pos: (0, -2, 0), vel: (2, -3, 2)
 Planet, name: Saturn, pos: (0, 0, -2), vel: (2, 2, -3)
 Star, name: Sun, pos: (0, 0, 0)
 Total Energy: 42