Find the Force of Gravity Between Two Objects with Java

The challenge

Your job is to find the gravitational force between two spherical objects (obj1 , obj2).

Gravitational attraction (Source: khanacademy.org)

Two arrays are give :

  • arr_val (value array), consists of 3 elements
    • 1st element : mass of obj 1
    • 2nd element : mass of obj 2
    • 3rd element : distance between their centers
  • arr_unit (unit array), consists of 3 elements
    • 1st element : unit for mass of obj 1
    • 2nd element : unit for mass of obj 2
    • 3rd element : unit for distance between their centers

Mass units are :

  • kilogram (kg)
  • gram (g)
  • milligram (mg)
  • microgram (μg)
  • pound (lb)

Distance units are :

  • meter (m)
  • centimeter (cm)
  • millimeter (mm)
  • micrometer (μm)
  • feet (ft)

Additional notes

value of G = 6.67 x 10-11N.kg–2.m2

1ft = 0.3048m

1lb = 0.453592kg

return value must be Newton for force (obviously)

Test cases

import static org.junit.Assert.assertEquals; import java.util.concurrent.ThreadLocalRandom; import org.junit.Test; public class TestCases { private static void test(double expected, double[] arrVal, String[] arrUnit) { assertEquals(expected, Solution.solution(arrVal, arrUnit), expected * 1e-5); } @Test public void fixedTests() { test(6.67e-12, new double[] {1000, 1000, 100}, new String[] {"g", "kg", "m"}); test(6.67e-9, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "m"}); test(0.0000667, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "cm"}); } private static final String[] WEIGHT_UNITS = {"kg", "g", "mg", "μg", "lb"}; private static final double[] WEIGHT_UNIT_CONVERSION_FACTORS = {1, 1e-3, 1e-6, 1e-9, 0.453592}; private static final String[] DISTANCE_UNITS = {"m", "cm", "mm", "μm", "ft"}; private static final double[] DISTANCE_UNIT_CONVERSION_FACTORS = {1, 1e-2, 1e-3, 1e-6, 0.3048}; @Test public void randomTests() { ThreadLocalRandom random = ThreadLocalRandom.current(); for (int i = 0; i < 100; i++) { double m1 = random.nextDouble(1, 1e5); double m2 = random.nextDouble(1, 1e5); double r = random.nextDouble(0.3, 1e7); double[] arrVal = new double[] {m1, m2, r}; double f = 6.67e-11 * m1 * m2 / r / r; int u1 = random.nextInt(WEIGHT_UNITS.length); int u2 = random.nextInt(WEIGHT_UNITS.length); int u3 = random.nextInt(DISTANCE_UNITS.length); arrVal[0] /= WEIGHT_UNIT_CONVERSION_FACTORS[u1]; arrVal[1] /= WEIGHT_UNIT_CONVERSION_FACTORS[u2]; arrVal[2] /= DISTANCE_UNIT_CONVERSION_FACTORS[u3]; String[] arrUnit = new String[] {WEIGHT_UNITS[u1], WEIGHT_UNITS[u2], DISTANCE_UNITS[u3]}; test(f, arrVal, arrUnit); } } }
Code language: Java (java)

The solution in Java

Option 1:

public class Solution { public static double solution(double[] arrVal, String[] arrUnit) { double newtons = 6.67e-11 * arrVal[0] * arrVal[1] / (arrVal[2] * arrVal[2]); for (String el : arrUnit) { newtons /= (el=="g") ? 1e3 : (el=="mg") ? 1e6 : (el=="μg") ? 1e9 : (el=="ft") ? 0.3048 * 0.3048 : 1; newtons *= (el=="lb") ? 0.453592 : (el=="cm") ? 1e4 : (el=="mm") ? 1e6 : (el=="μm") ? 1e12 : 1; } return newtons; } }
Code language: Java (java)

Option 2:

import java.util.*; public class Solution { public static double solution(double[] arrVal, String[] arrUnit) { Map<String, Double>massUnits = new HashMap<>(); massUnits.put("kg",1d); massUnits.put("g",1/1000d); massUnits.put("mg",1/1000000d); massUnits.put("μg",1/1000000000d); massUnits.put("lb",0.453592); Map<String, Double>distancenits = new HashMap<>(); distancenits.put("m",1d); distancenits.put("cm",1/100d); distancenits.put("mm",1/1000d); distancenits.put("μm",1/1000000d); distancenits.put("ft",0.3048); return 6.67e-11*(arrVal[0]*massUnits.get(arrUnit[0])*arrVal[1]*massUnits.get(arrUnit[1]))/(arrVal[2]*arrVal[2]*distancenits.get(arrUnit[2])*distancenits.get(arrUnit[2])); } }
Code language: Java (java)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments