Skip to content

Instantly share code, notes, and snippets.

@songyang-dev
Last active April 17, 2023 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songyang-dev/574afa256fdf501c8a06d86d3b6885dd to your computer and use it in GitHub Desktop.
Save songyang-dev/574afa256fdf501c8a06d86d3b6885dd to your computer and use it in GitHub Desktop.
Dealing with units and constants in physics
class Acceleration {
public double metersPerSecondSquared;
public double kilometersPerSecondSquared;
public double newtonsPerKilogram;
public Acceleration(double metersPerSecondSquared) {
this.metersPerSecondSquared = metersPerSecondSquared;
this.kilometersPerSecondSquared = metersPerSecondSquared / 1000;
this.newtonsPerKilogram = metersPerSecondSquared;
}
public static void main(String[] args) {
Acceleration accelerationEarth = new Acceleration(9.8);
double timeSeconds = 10.0;
double distanceMeters = accelerationEarth.metersPerSecondSquared * timeSeconds * timeSeconds;
}
}
public class GravityConstants {
public static final Acceleration EARTH = new Acceleration(9.8);
public static final Acceleration MOON = new Acceleration(1.6);
public static final Acceleration SUN = new Acceleration(275);
public static void main(String[] args) {
// F = m g
double massBallKilogram = 1.0;
var earthGravityNewtons = massBallKilogram * GravityConstants.EARTH.newtonsPerKilogram;
}
}
public class IdealGasConstant {
public double litersAtmospherePerKelvinPerMole; // L⋅atm⋅K−1⋅mol−1
public double metersCubedPascalPerKelvinPerMole; //m3⋅Pa⋅K−1⋅mol−1
private static IdealGasConstant _instance = new IdealGasConstant();
private IdealGasConstant() {
this.litersAtmospherePerKelvinPerMole = 0.082057366080960;
this.metersCubedPascalPerKelvinPerMole = 8.31446261815324;
}
public static IdealGasConstant getInstance() {
return _instance;
}
public static void main(String[] args) {
IdealGasConstant idealGasConstant = IdealGasConstant.getInstance();
// P V = n R T
double numberOfParticlesMole = 1;
double temperatureKelvin = 273;
double pressurePascal = 100;
double volumeMetersCubed = numberOfParticlesMole * idealGasConstant.metersCubedPascalPerKelvinPerMole * temperatureKelvin / pressurePascal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment