Search⌘ K
AI Features

Solution: Planetary Physics with Enums

Understand how to apply Java Enums for modeling planetary physics by defining constants with immutable fields, constructing instances, and creating methods to calculate surface gravity and weight. Discover how Enum methods can interact to encapsulate complex behavior within a clean, organized class design.

We'll cover the following...
Java 25
public class PlanetarySystem {
public enum Planet {
MERCURY(3.303e+23, 2.4397e6),
EARTH(5.976e+24, 6.37814e6),
JUPITER(1.9e+27, 7.1492e7);
private final double mass; // in kilograms
private final double radius; // in meters
// Universal Gravitational Constant
public static final double G = 6.67300E-11;
// Constructor
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
// Calculate surface gravity (g)
public double surfaceGravity() {
return (G * mass) / (radius * radius);
}
// Calculate weight (Force = mass * g)
public double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
}
public static void main(String[] args) {
double roverMass = 100.0; // kg
System.out.println("Rover Mass: " + roverMass + "kg");
System.out.println();
for (Planet p : Planet.values()) {
System.out.println("Weight on " + p + ": " + p.surfaceWeight(roverMass) + " N");
}
}
}
...