Question

/** * 1. Write the method plusTwo(). * * Your method will take 2 int arrays...

/**
* 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

Homework Answers

Answer #1
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;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java Lab Assignment Write a method named findMax. The method will take an array of doubles...
Java Lab Assignment Write a method named findMax. The method will take an array of doubles as an argument, and it will return the largest value in the array. (Note, you will have to create a new array of doubles) . method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Java Lab Assignment Write a method called convertToArray. The method will take a String as an...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an argument and it will return an array of chars. Each index of the String will be stored in each index of the array. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for * @return...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars as an argument and it will return a String. Each index of the array will be part of the each index of the String. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for *...
Write a function called matches that takes two int arrays and their respective sizes, and returns...
Write a function called matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ Im confused how to start this, any hints are great and...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
In C++ You are given two arrays each of which is sorted. Write a method called...
In C++ You are given two arrays each of which is sorted. Write a method called mergeIt that takes the two arrays and merges them into one array. For instance, if you had 5, 9, 11 and 4, 6, 7 then you need to merge it to 4,5,6,7,9,11.