Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please just solve ( 9,10,11) and here is the solve of the first 8 public static String toString(int [] arr) { if(arr.length!=0 && arr !=

please just solve ( 9,10,11) and here is the solve of the first 8 public static String toString(int [] arr) { if(arr.length!=0 && arr != null){ String s = "["; int i=0; for(; i=0 && start= arr2.length){ int i=0, j,flag=1; for(; i=arr[max]){ max = i; } } return max; } return -1; } public static int getMult(int[] arr){ if(arr.length!=0 && arr != null){ int i, mult=1; for(i=0; i=0; i++){ revarr[arr.length-i-1] = arr[i]; } return revarr; }

and this is the testerr

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++; } }image text in transcribedimage text in transcribedimage text in transcribed

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 il arr) Examples: for array {3, 2, 1} = arr The method will return the string: "(3, 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) and entire contre la costa Examples: for the array {3, 2, 1} = arr The method will return the array: {3, 2, 1} for the array f} (i.e. an empty array with 0 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[] arr, int value) Example: for the array {3, 2, 1} = arr and the value o = value, after running the method arr 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[] 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 Il getReverse (int[] 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 I findMax (int o arr) 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} ray will be returned 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 tablel cannot be assumed to be equal to the dimensions of table2). Signature method: public static int [] [] sum Tables (int [] [] table1, int [] [] table2) Example: for the parameters int ( [] tablel = {{1, 4}, {16, 0}} int [] [] table2 = {{0, 6}, {4, 9}} The array is returned: {{9, 20}, {10, 1}}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions