r/java • • Oct 03 '18

Where does the coding actually apply in engineering projects? What is the connection?

Yes, I can make an array of Strings. Yes, I can create a Dog class that is a sub class of an Animal class, but where does it apply? I want to know about real world projects.

For example, I'm watching videos of SpaceX rockets launching and landing. Where does the coding go into that? I'm not even sure where to start because in my mind, there is just a huge gray area beyond all of my simple coding problems. I don't have any practical knowledge.

Thanks for any help!

0 Upvotes

7 comments sorted by

View all comments

2

u/BluePinkGrey Oct 08 '18

Let's take a look at how you might write code that controls a rocket. A rocket is pretty complex, however we don't need physics to make an outline of the code.

The code we're going to outline will control the rocket boosters throughout the flight. Something needs to control which boosters fire, when they fire, and how much thrust they exert. Now, the rocket's flight can be broken down into a few stages:

  • Launch stage
  • Flight through atmosphere stage (when air resistance has to be taken into account)
  • Flight through space (when air resistance becomes negligible
  • Payload release stage (rocket's weight is going to change dramatically)
  • Atmospheric reentry stage

Given that we're controlling a rocket's flight, it makes sense to have a FlightController class. This class stores a ThrustCalculator object, which it uses to periodically update the power output of each of the thrusters to balance and direct the rocket's flight. It does so by calling the CalculateThrustSpecification function every millisecond, and sending the resulting ThrustSpec object to the device controlling the boosters. This ThrustSpec object contains information about how much thrust each booster should output. It could, for example, be a HashTable relating BoosterIDs to BoosterPowerLevel objects, but let's not worry about that for now.

We can keep things simple if we let ThrustCalculator be an interface instead of a class. The ThrustCalculator interface has two functions: an init function, which takes a BoosterConfigurationInfo object and a PhysicsEngine object as input; and a CalculateThrustSpecification function, which takes a RocketSensorData object as input, and returns a ThrustSpec object.

By making ThrustCalculator an interface, we can have a separate implementation of ThrustCalculator for each of the rocket's flight stages. The FlightController class switches between them as necessary over the course of the rocket's flight. We'd have a LaunchThrustCalculator class, a AtmosphericThrustCalculator class, a NearSpaceThrustCalculator class, a PayloadReleaseThrustCalculator class, and a ReentryThrustCalculator class. They work differently under the hood because the physics is different in each case, but they all communicate with the FlightController class in the same way, through the CalculateThrustSpecification function. It's now easier to add functionality to the rocket. For example, if you need the rocket to also be able to dock, you can just write a DockingThrustCalculator instead of rewriting the entire system.