Square Numbers
Easy
22
1
86.4% Acceptance
Your task is to implement a Java method called squareNumbers
that takes an array of integers and returns a new array containing the squares of each number.
Method Signature
public static int[] squareNumbers(int[] numbers)
Instructions
- Implement the
squareNumbers
method in the providedMain
class. - Ensure that your method handles arrays of various lengths and produces accurate results.
Required Output
For a given array numbers = [a, b, c, ...]
, the method should return an array result = [a^2, b^2, c^2, ...]
.
Examples
Example 1
Input:
squareNumbers([3])
Expected Output:
[9]
Example 2
Input:
squareNumbers([1, 2, 3, 4, 5])
Expected Output:
[1, 4, 9, 16, 25]
Edge Cases
-
Verify that the method works correctly for an array with a single integer.
- Example:
Expected Output:squareNumbers([0])
[0]
- Example:
-
Confirm that the method functions accurately with an array containing 10 integer elements.
- Example:
Expected Output:squareNumbers([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
[100, 400, 900, 1600, 2500, 3600, 4900, 6400, 8100, 10000]
- Example:
-
Ensure the method produces correct results when given an array with 5 integer elements.
- Example:
Expected Output:squareNumbers([-2, -1, 0, 1, 2])
[4, 1, 0, 1, 4]
- Example:
Challenges
Challenge 1: Single Integer Input
- Description: Verify that the "squareNumbers" method works correctly when provided with an array containing a single integer.
- Test Input:
[3]
- Expected Output:
[9]
Challenge 2: Array with 10 Items
- Description: Ensure that the "squareNumbers" method functions accurately with an array containing 10 integer elements.
- Test Input:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- Expected Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Challenge 3: Array with 5 Items
- Description: Confirm that the "squareNumbers" method produces correct results when given an array with 5 integer elements.
- Test Input:
[0, -1, 2, -3, 4]
- Expected Output:
[0, 1, 4, 9, 16]