/**
* 1. Write the method plusTwo().
*
* Your method will take 2 int arrays as parameters.
* Each will have a length of 2. Your method must
* create and return a new array with a length of 4,
* containing all their elements.
*
* Here are some examples:
* plusTwo({1, 2}, {3, 4}) returns {1, 2, 3, 4}
* plusTwo({4, 4}, {2, 2}) returns {4, 4, 2, 2}
* plusTwo({9, 2}, {3, 4}) returns {9, 2, 3, 4}
*
* @param a an array of int, length 2.
* @param b an array of int, length 2.
* @return an array of int as described above.
*/
// WRITE THE METHOD plusTwo() HERE
import java.util.Arrays; //TestCode.java public class TestCode { public static int[] plusTwo(int arr[][]){ int result[] = new int[4]; result[0] = arr[0][0]; result[1] = arr[0][1]; result[2] = arr[1][0]; result[3] = arr[1][1]; return result; } public static void main(String[] args) { int arr[][] = {{1, 2}, {3, 4}}; System.out.println(Arrays.toString(plusTwo(arr))); } }
[1, 2, 3, 4]
public static int[] plusTwo(int arr[][]){ int result[] = new int[4]; result[0] = arr[0][0]; result[1] = arr[0][1]; result[2] = arr[1][0]; result[3] = arr[1][1]; return result; }
Get Answers For Free
Most questions answered within 1 hours.