Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Programming language is java. MDArray - Part a: Write a class called MDArray containing the following variables and methods: 1. Private data field: mdarray of
Programming language is java.
MDArray - Part a: Write a class called MDArray containing the following variables and methods: 1. Private data field: mdarray of type double[]0; 2. Public constructors: your class should have two constructors - one that takes a 2-D array of doubles and another one that takes a text file name as a String. For the latter one, the text file will contain a 2-D array where the first line is an int representing the number of rows in the array, and each line below that has an int representing the length of the row followed by the values that represent that row in the 2-D array. For example, if the contents of the text file is as follows: 3 24.1 8.9 59.5 2.0 7.3 2.1 8.9 3 1.3 5.2 3.4 then it represents a ragged 2-D array with 3 rows, where the first row has length 2, the second one 5 and the third one 3. 3. Public methods: boolean isRagged(): returns true if the invoking 2-D array is a ragged array; int getNumberOfRows(): returns the number of rows in the array: int getNumberOfCols(): returns the number of columns in the array. If the array is ragged, it should return the maximum length row in the array: double getValAt(int i, int j): returns the double value stored in row i, column j. If either i orj is out-of-bounds, you should return NaN and print a message to the indicating this problem; void sort(boolean byColumn): sorts the invoking two-dimensional array using the selection sort algorithm. You may use an auxiliary array here, however note that this method is void and thus you must modify the invoking array. Also notice that it should take as a parameter a boolean byColumn - this parameter will determine whether or not the array should be sorted by column (that is, if byColumn is true, then sot by column). For example, given a 2-D array below (left), we can sort it in two ways - by row (middle) or by column (right): 486 123 1 47 927 456 2 58 135 789 369 Note: Sorting by row should work on any 2-D array (ragged or not). However, sorting by column should only work for non-ragged arrays and should print a "cannot sort ragged arrays by column" message to the screen if the method is invoked by a ragged array. If this happens, you should simply print the message and not modify the array - there is no need to exit the program; int hamming(boolean byColumn): returns the hamming distance of the invoking array from its sorted state (by row or by column, depending on the passed in argument). The hamming distance of a 2-D array can be defined as follows: each number that is not where it should be (as in the sorted state) is worth one penalty point. For example, if the invoking matrix is: 486 297 135 then its hamming distance is 9 from its by-row sorted state and 8 from its by- column sorted state. Note: If the invoking array is ragged and the boolean byColumn is true, you should return the code -1 to indicate that there is no by-column hamming distance for ragged arrays; double[] max(): returns the row in a 2-D array that contains the maximum value in the entire 2-D array; String toString(): returns the string representation of the invoking array. Each value should be comma separated and each row should be in a new line when printed to the screen. You may have autonomy on however prettier you wish the print out to be; MDArrayDemo - Part b: In a class called MDArrayDemo, write a main method to test each of your constructors and methods from the MDArray class. Make at least five distinct MDArray objects (3 non-ragged and 2 ragged-use may use Math.random(), but at least one of them must be constructed using a file input)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