Given 4 Integer Arrays with size 4 like this:
[null, 100, 110, 120] Force
[130, null, 140, 150] Radius
[160, 170, null, 180] Mass 1
[190, 200, 210, null] Mass 2
You will have to find out and replace all nulls with their respective value using Newton's laws of motion.
F=Gm1m2/d2
You will apply the formula index by index meaning index F[0] array will be placed in the formula with index 0 from the other 3 arrays (r[0], mass1[0] and mass2[0]), the same with index 1, 2 and 3.
All values will be correct, there will be no negatives or insanely big values.
The output will be a Double type Array with the 4 values in order. Meaning that index 0 will be the Force, index 1 will be the radius, index 2 will be mass 1 and index 3 will be mass 2.
import java.lang.Math; import java.util.*; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) { double[] actual = Kata.newton(f, r, m1, m2); int n=0; for(int i=0;i<4;i++) { if(actual[i]==expected[i]) n++; } assertEquals(4, n); } double[] newtonSol(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){ double[] result = new double[4]; double g = 6.674*Math.pow(10,-11); result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2); result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]); result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]); result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]); return result; } @Test void testRandom() { for(int i=0;i<40;i++){ Integer[] f=new Integer[4]; Integer[] r=new Integer[4]; Integer[] m1=new Integer[4]; Integer[] m2=new Integer[4]; for(int d=0;d<4;d++){ f[d]=(int)(Math.random()*250)+10; r[d]=(int)(Math.random()*250)+10; m1[d]=(int)(Math.random()*250)+10; m2[d]=(int)(Math.random()*250)+10; } f[0]=null; r[1]=null; m1[2]=null; m2[3]=null; double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } } @Test void testFixed(){ Integer[] f={null, 100, 150, 120}; Integer[] r={100, null, 150, 120}; Integer[] m1={150, 100, null, 120}; Integer[] m2={120, 100, 150, null}; double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f= new Integer[] {null, 106, 150, 140}; r=new Integer[] {100, null, 65, 120}; m1=new Integer[] {150, 112, null, 120}; m2=new Integer[] {120, 448, 150, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f=new Integer[] {null, 106, 150, 240}; r=new Integer[] {100, null, 65, 120}; m1=new Integer[] {103, 112, null, 120}; m2=new Integer[] {120, 241, 111, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } }
- import java.lang.Math;
- import java.util.*;
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) {
- double[] actual = Kata.newton(f, r, m1, m2);
- int n=0;
- for(int i=0;i<4;i++) {
- if(actual[i]==expected[i]) n++;
- }
- assertEquals(4, n);
- }
- double[] newtonSol(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){
- double[] result = new double[4];
- double g = 6.674*Math.pow(10,-11);
- result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2);
- result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]);
- result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]);
- result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]);
- return result;
- }
- @Test
- void testRandom() {
- for(int i=0;i<40;i++){
- Integer[] f=new Integer[4];
- Integer[] r=new Integer[4];
- Integer[] m1=new Integer[4];
- Integer[] m2=new Integer[4];
- for(int d=0;d<4;d++){
- f[d]=(int)(Math.random()*250)+10;
- r[d]=(int)(Math.random()*250)+10;
- m1[d]=(int)(Math.random()*250)+10;
- m2[d]=(int)(Math.random()*250)+10;
- }
- f[0]=null;
- r[1]=null;
- m1[2]=null;
- m2[3]=null;
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- }
- @Test
- void testFixed(){
- Integer[] f={null, 100, 150, 120};
- Integer[] r={100, null, 150, 120};
- Integer[] m1={150, 100, null, 120};
- Integer[] m2={120, 100, 150, null};
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- f= new Integer[] {null, 106, 150, 140};
- r=new Integer[] {100, null, 65, 120};
- m1=new Integer[] {150, 112, null, 120};
- m2=new Integer[] {120, 448, 150, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- f=new Integer[] {null, 106, 150, 240};
- r=new Integer[] {100, null, 65, 120};
- m1=new Integer[] {103, 112, null, 120};
- m2=new Integer[] {120, 241, 111, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- }