Question
method 11 ,, using java thank you tester public class Tester { private static int numFailed = 0; private static int numSuccess = 0; public
method 11 ,, using java
thank you
tester
public class Tester {
private static int numFailed = 0;
private static int numSuccess = 0;
public static void main(String[] args) {
int[] arr = {1, -3, 5, 6, 0, -4};
String arrStr = ArrayUtils.toString(arr);
reportTest(arrStr.equals("[1, -3, 5, 6, 0, -4]"), 1);
int[] abs = ArrayUtils.absArray(arr);
arrStr = ArrayUtils.toString(arr);
String absStr = ArrayUtils.toString(abs);
reportTest(arrStr.equals("[1, -3, 5, 6, 0, -4]"), 2);
reportTest(absStr.equals("[1, 3, 5, 6, 0, 4]"), 3);
int[] arr2 = {1, -3, 5, 6, 0, -4};
ArrayUtils.fill(arr2, 0);
String arr2Str = ArrayUtils.toString(arr2);
reportTest(arr2Str.equals("[0, 0, 0, 0, 0, 0]"), 4);
int[] arr3 = {1, -3, 5, 6, 9, -4};
ArrayUtils.fill(arr3, 0, 1, 4);
String arr3Str = ArrayUtils.toString(arr3);
reportTest(arr3Str.equals("[1, 0, 0, 0, 9, -4]"), 5);
int[] arr4 = {1, 2, 3};
int[] arr5 = {1, 2, 3, 4};
reportTest(ArrayUtils.contains(arr4, arr5) == false, 6);
reportTest(ArrayUtils.contains(arr5, arr4) == true, 7);
int[] arr6 = {1, -3, 5, 6, 9, -4};
int[] arr6Reversed = ArrayUtils.getReverse(arr6);
String arr6ReversedStr = ArrayUtils.toString(arr6Reversed);
reportTest(arr6ReversedStr.equals("[-4, 9, 6, 5, -3, 1]"), 8);
int[] arr7 = {1, -3, 5, 6, 9, -4};
ArrayUtils.reverse(arr7);
String arr7Str = ArrayUtils.toString(arr7);
reportTest(arr7Str.equals("[-4, 9, 6, 5, -3, 1]"), 9);
int[] arr8 = {1, -3, 5, 6, 9, -4};
int maxPos = ArrayUtils.getMaxPosition(arr8);
reportTest(maxPos == 4, 10);
int[] arr9 = {1, 2, 3};
int mult = ArrayUtils.getMult(arr9);
reportTest(mult == 6, 11);
int[][] arr10 = {{1, 4, 2}, {16, 9 , 0}};
int[] res = ArrayUtils.findMax(arr10);
reportTest(res[0] == 1, 12);
reportTest(res[1] == 0, 13);
int[][] arr11 = {{1, 4},
{16, 0}};
int[][] arr12 = {{0, 6},
{4, 9}};
int[][] resSum = ArrayUtils.sumTables(arr11, arr12);
reportTest(resSum[0][0] == 1, 14);
reportTest(resSum[0][1] == 10, 15);
reportTest(resSum[1][0] == 20, 16);
reportTest(resSum[1][1] == 9, 17);
System.out.println("num. test failed: " + numFailed);
System.out.println("num. test passed: " + numSuccess);
System.out.println("Done!");
}
/**
* The method reports if the test failed or passed, according to the given condition.
* @param condition Condition to check. False value means that the test has failed.
* @param testNum Test number.
*/
private static void reportTest(boolean condition, int testNum) {
if(! condition) {
System.err.println("FAILED TEST " + testNum + ".");
numFailed++;
}
else {
System.out.println("PASSED TEST " + testNum + ".");
numSuccess++;
}
}
}
Exercise Material: Arrays, Two-Dimensional Arrays Question 1 (100%) Write a class called ArrayUtils that supports a collection of arrays. Realize the following static methods: 1. A method that takes an array of integers and returns a representation of its string. If arr is null, the method returns null. Signature method: public static String toString (int[] arr) Examples: for array {3, 2, 1} = arr The method will return the string: "13, 2, 1) for array (i.e. an empty array with O members The method "h" is returned. 2- A method that accepts an array of integers and returns a new array whose members are the absolute values of the array obtained. If arr is null, the method returns null. Signature method: public static int] absArray (int[] arr) Examples: for the array {3, 2, 1} = arr The method will return the array: {3, 2, 1} for the array 0 (i.e. an empty array with o Organs) will be returned an empty array. 3- A method that receives an array of integers and a val value and changes the array so that it puts the value val in each cell in the array. If arr is null or empty, the method does nothing. Signature of the method: public static void fill (int i arr, int value) Example: for the array {3, 2, 1} = arr and the value 0 = value, after running the method are the array will be: (0, 0, 0) 4. A method that gets an array of integers, a val value and two indices of beginning and end. The method changes the resulting array so that it puts the val value in all cells between start and end (including the beginning, not including the end). The method does not change the array arr if arr is null or empty or one of the indexes is invalid (valid index is an index within the boundaries of the array only). In addition, if the start is large or equal to the end the method does not change the array. Signature method: public static void fill (int i) arr, int value, int start, int end) Example: for array {7, 4, 3, 2, 1} = value = 0, start = 1, end = 3, arr After running the arr method the array will be: 7, 4, 0, 0, 1). 5- A method that accepts two arrays and returns true if all the members in the second array appear in the first array. Otherwise, false will be returned. If one or both arrays are null, false will be returned. Signature of the method public static boolean contains (int arri, int ( arr2) Note: If the array arr2 is empty, false will be returned. 6- A method which receives an array of integers and returns the index of the maximum member. If the resulting array array is null or the array is empty, 1- is returned. Signature of the method: public static int getMaxPosition (int arr) Note: If the maximum value appears in the array more than once, the index of its first appearance will be returned. Example: for array {2, 3, 1} 1 is returned, for array {3, 3, 1} 1 is returned. 7. A method which receives an array of integers and returns the product of all the members of the array. If the resulting array array is null or the array is empty. Integer MAX_VALUE (static Java language constant) will be returned. Signature of the method::. public static int getMult (int[] arr) 8. A method that receives an array of integers and returns a new array with the same members in reverse order. If the resulting array array is null null will be returned. If the array is empty, a new (non-null) array will be returned. Method signature: public static int getReverse (int i) arr) 9. A method which receives an array called arr of integers and modifies it so that after running the method it will contain the same members in reverse order. If the resulting array array is null, null will be returned. Please note, do not assign a new array to the solution. Signature of the method: public static void reverse (int ( arr) Note: If the array arr is empty, the method does not change it. 10. A method that gets an array called arr, a two-dimensional array of integers and looks for the maximum number that appears in ar. The function returns a one-dimensional array with two members indicating the position where the maximum number is the first member is the row and the second member is the column. If the resulting array array is null, or the array is empty (this means that there are no numbers at all in the array: the number of rows is o or the number of columns is o), null will be returned. Signature of the method: public static int [] findMax (int Darr) Note: If the maximum value appears in the array more than once, the indexes will be returned for its first appearance. Examples:. For the array {{0, 9, 16} {2, 4, 1}}, the maximum number is 16, which is in the second row (index 1) and the first column (index 0). Therefore the array will be returned: For the array {{16, 9, 16}, {2, 4, 1}}, the maximum number is 16, which is in the second row (index 1) and the first column (index 0) and also in the second row and the last column. Therefore the array will be returned: {0, 1} 11. A method which accepts two two-dimensional arrays and returns a new two-dimensional array which is their sum. If one of the parameters is null, or the arrays do not have the same dimensions, return null. Note: Assume that the number of columns is the same in all rows of tablel, and the same is true of table2 (however, the dimensions of tablet cannot be assumed to be equal to the dimensions of table2). Signature method: public static int 0 sumTables (int 0 0 tablet, int 0 0 table2) Example: for the parameters int 0 0 tablel = {{1, 4}, {16, 0}} int 0 0 table2 = {{0, 6}, {4,9}} The array is returned: {{9, 20}, {10, 1}} #: 49. 201,410Step 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