1. Engineering
  2. Computer Science
  3. hi i am merging a 2d array to a 1d...

Question: hi i am merging a 2d array to a 1d...

Question details

Hi I am merging a 2d array to a 1d array and everything seems to be passing for the P and Cr tests but the D and HD are not passing  and I don't know what I am missing. I can't use inbuilt libraries or hardcode

/**

     * 

     * @return the array converted to a single-dimensional array

     * For example,

     * if data = {{10, 40}, {50}, {}, {70, 20, 90}, {}},

     * return {10, 40, 50, 70, 20, 90}

     */

   

    public int[] merge() {

   

    int [] array = new int[data.length * data[0].length];

    int k = 0;

    for (int i = 0; i < data.length; i++) {

    for(int j = 0; j < data[i].length; j++) {

    array[k++] = data[i][j];

    }

    }

return array;

    }

 

Test

Analytics aP, aCR, aD, aHD, aEmpty;

@BeforeEach

public void setUp() throws Exception {

currentMethodName = null;

aP = new Analytics(new int[][]{{30, 10, 40},

            {50, 20, 80},

            {10, 60, 30},

            {60, 60, 60}});

            

        aCR = new Analytics(new int[][]{{-30, 10, -40},

            {-50, 20, 0},

            {-10, 60, 30},

            {-60, 60, 60},

            {-20, -20, -20}});

 

        aD = new Analytics(new int[][]{{-30, 10, -40},

            {},

            {-10, 60},

            {-60},

            {-20, -20, -20, -20, -20},

            {-20, -20, -20, -20, -20},

            {-20, -20, -20, -20, -20},

            {-20, -20, -20, -20, -20},

            {-20, -20, -20, -20, -20},

            {-20, -20, -20, -20, -20},

            {-20, -20, -20, -20, -20}});

 

        aHD = new Analytics(new int[][]{{-30, 10, -40},

            {},

            {-10, 60},

            {-60},

            {-20, -20, -20, -20, -20},

            {-20, -20},

            {-20},

            {},

            {50},

            {10, 30, 20}});

@Test @Order(37) @Graded(description="testMergeP", marks=2.5)

public void testMergeP() {

assertEquals("[30, 10, 40, 50, 20, 80, 10, 60, 30, 60, 60, 60]", Arrays.toString(aP.merge()));

currentMethodName = new Throwable().getStackTrace()[0].getMethodName();

}

@Test @Order(38) @Graded(description="testMergeCR", marks=2.5)

public void testMergeCR() {

testMergeP();

assertEquals("[-30, 10, -40, -50, 20, 0, -10, 60, 30, -60, 60, 60, -20, -20, -20]", Arrays.toString(aCR.merge()));

currentMethodName = new Throwable().getStackTrace()[0].getMethodName();

}

@Test @Order(39) @Graded(description="testMergeD", marks=2.5)

public void testMergeD() {

testMergeCR();

assertEquals("[-30, 10, -40, -10, 60, -60, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20]", Arrays.toString(aD.merge()));

currentMethodName = new Throwable().getStackTrace()[0].getMethodName();

}

@Test @Order(40) @Graded(description="testMergeHD", marks=2.5)

public void testMergeHD() {

testMergeD();

assertEquals("[-30, 10, -40, -10, 60, -60, -20, -20, -20, -20, -20, -20, -20, -20, 50, 10, 30, 20]", Arrays.toString(aHD.merge()));

currentMethodName = new Throwable().getStackTrace()[0].getMethodName();

}

   

 

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution