{"record":{"id":"fb88bde4f0d46dfc","repo":"TheAlgorithms/Java","slug":"mass-and-radius-must-be-positive","errorCode":null,"errorMessage":"Mass and radius must be positive.","messagePattern":"Mass and radius must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/Gravitation.java","lineNumber":62,"sourceCode":"\n        // Calculate the components of the force vector\n        double fx = forceMagnitude * (dx / distance);\n        double fy = forceMagnitude * (dy / distance);\n\n        return new double[] {fx, fy};\n    }\n\n    /**\n     * Calculates the speed required for a stable circular orbit.\n     *\n     * @param centralMass The mass of the central body (kg).\n     * @param radius The radius of the orbit (m).\n     * @return The orbital speed (m/s).\n     * @throws IllegalArgumentException if mass or radius are not positive.\n     */\n    public static double calculateCircularOrbitVelocity(double centralMass, double radius) {\n        if (centralMass <= 0 || radius <= 0) {\n            throw new IllegalArgumentException(\"Mass and radius must be positive.\");\n        }\n        return Math.sqrt(GRAVITATIONAL_CONSTANT * centralMass / radius);\n    }\n}\n","sourceCodeStart":44,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/Gravitation.java#L44-L67","documentation":"Thrown by Gravitation.calculateCircularOrbitVelocity when centralMass <= 0 or radius <= 0. The formula v = sqrt(G * M / r) divides by radius and requires positive mass for a physically meaningful gravitational source; non-positive values would cause division by zero or an undefined square root. This computes the speed for a stable circular orbit around a massive body.","triggerScenarios":"Calling calculateCircularOrbitVelocity(centralMass, radius) with centralMass = 0, a negative mass, radius = 0, or a negative radius.","commonSituations":"Unit-conversion errors producing zero (e.g. solar masses converted to kg with a wrong factor), reading a body from a catalogue where mass was absent and defaulted to 0, or a sign error in radius from a coordinate subtraction.","solutions":["Pass strictly positive centralMass (kg) and radius (m) — e.g. Sun mass 1.989e30 and Earth orbital radius 1.496e11.","Validate astrophysical inputs at the data-load boundary and reject zero/negative mass or radius before the call.","Double-check unit consistency (SI units kg and m are expected)."],"exampleFix":"// before\ndouble v = Gravitation.calculateCircularOrbitVelocity(0, 1.496e11);\n// after\ndouble v = Gravitation.calculateCircularOrbitVelocity(1.989e30, 1.496e11);","handlingStrategy":"validation","validationCode":"if (!(centralMass > 0 && radius > 0)) {\n    throw new IllegalArgumentException(\"centralMass and radius must be > 0\");\n}\ndouble v = Gravitation.calculateCircularOrbitVelocity(centralMass, radius);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate astrophysical inputs at the catalogue-load boundary.","Use named SI constants for canonical masses and radii.","Add a NaN guard when values originate from untrusted arithmetic."],"tags":["java","physics","parameter-validation","orbital-mechanics","gravity","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}