Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA Math 140 Programming Assignment 2 Deadline: Feb 27, 2019, 11:59 pm Assignment: Write a Java class called AssignmentTwo that contains the five static methods
JAVA Math 140 Programming Assignment 2 Deadline: Feb 27, 2019, 11:59 pm Assignment: Write a Java class called AssignmentTwo that contains the five static methods defined below. Some of the methods are similar to worksheet problems covered in class Learning Outcome: Review how to work with two-dimensional arrays, three- dimensional arrays, and the System.arraycopy method. I will eventually post a main method to test your code. However, develop your own tests to make sure your methods generate the correct output Review the coding instructions at the end of this document. 1. public static int numElements(int[]0] twoDim) a. if twoDim is null then return -1 b. otherwise, return the number of integers stored in twoDim. c. twoDim may contain null rows d. if all rows of twoDim are null then return 0. e. do not use a nested loop to compute the number of elements; instead use the length property of each non-null rovw Example: twoDim (null, (9, 0, 8), nul, (0, 7, 6, 3, 4)); The method returns 8 since there are eight integers in the array. Math 140 Programming Assignment 2 Deadline: Feb 27, 2019, 11:59 pm 2. public static int numElements(int[]0] threeDim) a. if threeDim is null then return -1. b. otherwise, return the number of integers stored in threeDim. c. threeDim may contain two-dimensional arrays that are set to null. d. if every two-dimensional array stored in threeDim is null then return 0. e. do not use a nested loop to compute the number of elements; instead, call the numElements method described in part 1. Example: threeDim- ffnull 19, 0, 8), null, (0, 7, 6, 3,4)), null, (8, 9, 45, 8, 0), (6, 7, 2), (4, 8)) The method returns 24 since there are 24 integers in the array. 3. public static int[] twoDimensionalToOneDimensional(intUl] arr) if arr is null then return null . otherwise (i) copy the non-null rows of arr to a one-dimensional array and (i) return the one-dimensional array. . arr can contain null rows the length of the returned array should be equal to the number of integers stored in the non-null rows of arr; hint: use the result from method 1 to find the length of this array. copy the non-null rows in ascending order. In other words, first copy row O to the one-dimensional array, then row 1, 2, etc. if all the rows contained in arr are null then return an array whose length is zero. . use the System.arraycopy method to copy the non-null rows of arr to the one-dimensional array Example: Assume arr- ((1, 20, 50), (5, 8), null, (22, 100, 200, 1)); The method returns the following one-dimensional array (1, 20, 50, 5, 8, 22, 100, 200, 1)
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