Question
A) Write a method with the header public static void process(int[] vals) that takes a reference to an array of integers and modifies its internals
A) Write a method with the header
public static void process(int[] vals)
that takes a reference to an array of integers and modifies its internals as follows:
If an element is even, it should be doubled.
If an element is odd, it should be negated (i.e., multiplied by -1).
For example, if you add the following test code to your main method:
int[] a1 = {1, 2, 6, 5, -8, -10, -11}; ArrayMethods.process(a1); System.out.println(Arrays.toString(a1));
you should see the following output:
[-1, 4, 12, -5, -16, -20, 11]
Special case: If the parameter is null, the method should throw an IllegalArgumentException.
B) Write a method with the header
public static String[] extractBU(String[] words)
that takes a reference to an array of strings, and that creates and returns a new array that contains the strings from the original array whose first letter is either 'b', 'B', 'u' or 'U'.
For example:
String[] a2 = {"Build", "up", "your", "best", "self"}; String[] a3 = ArrayMethods.extractBU(a2); System.out.println(Arrays.toString(a3));
should display:
[Build, up, best]
Important:
You may not use any of the built-in methods for copying or extracting a portion of an array.
You must not modify the original array.
The new array (the one that you will return) should not have any extra unused elements. As a result, we recommend making an initial pass through the original array to determine how long the new array should be. Then you can create the new array and make a second pass through the original array to find and copy the appropriate strings.
Special case: If the parameter is null, the method should throw an IllegalArgumentException.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started