Write a class called MDArray containing the following variables and methods: (JA
ID: 3771080 • Letter: W
Question
Write a class called MDArray containing the following variables and methods: (JAVA)
1. Private data field: mdarray of type double[][];
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.
3. Public methods:
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):
4 8 6 1 2 3 1 4 7
9 2 7 4 5 6 2 5 8
1 3 5 7 8 9 3 6 9
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:
4 8 6
2 9 7
1 3 5
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;
Explanation / Answer
Completed the following items in your problem
Item 1, Item2 (constructor withd double array), item 3 (selection sort) and provided display function
package assignment;
public class MDArray {
public static void main(String[] args) {
double[][] data = { {4.0,8.0,6.0} ,{9.0,2.0,7.0} , {1.0,3.0,5.0}};
MDArray arr = new MDArray(data);
System.out.println("Unsorted Array:");
arr.display();
System.out.println("Row Sorted Array:");
arr.selectionSort(false);
arr.display();
System.out.println("Column Sorted Array:");
arr.selectionSort( true);
arr.display();
double[][] data2 = { {8.0,6.0} ,{9.0,2.0,7.0} , {1.0,3.0,5.0}};
MDArray arr2 = new MDArray(data2);
System.out.println("Unsorted Array2:");
arr2.display();
System.out.println("Row Sorted Array:");
arr2.selectionSort( false);
arr2.display();
System.out.println("Column Sorted Array:");
arr2.selectionSort( true);
}
private double[][] data;
public MDArray(double[][] data) {
this.data = data;
}
// public MDArray(String fileName) {
// readData(fileName);
// }
//
// public void readData(String fileName) {
// //Implement the code here
// }
//byColumn = false, sorts by row, if true sorts by column
public void selectionSort( boolean byColumn) {
//by row
//auxillary array
//calculate length
int length = 0;
for(int i=0; i < data.length; i++) {
length += data[i].length;
}
double[] auxArr = new double[length];
int count = 0;
for(int i=0; i < data.length; i++)
for(int j = 0; j < data[i].length; j++)
auxArr[count++] = data[i][j];
if(!byColumn) {
auxArr = selectionSort(auxArr);
//now fill the original array
count = 0;
for(int i=0; i < data.length; i++)
for(int j = 0; j < data[i].length; j++)
data[i][j] = auxArr[count++];
} else {
//by column
if(isRaggedArray(data))
System.out.println("Cannot sort ragged array");
else {
auxArr = selectionSort(auxArr);
count = 0;
//for non-ragged arrays all rows have eqaul columns
for(int j = 0; j < data[0].length; j++)
for(int i=0; i < data.length; i++)
data[i][j] = auxArr[count++];
}
}
}
public void display() {
for(int i=0; i < data.length; i++) {
for(int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j]+" ");
}
System.out.println();
}
}
private static void swap(double[] a, int i, int j) {
double temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static double[] selectionSort(double[] list) {
for (int i = 0; i < list.length - 1; i++) {
// Find the index of the minimum value
int minPos = i;
for (int j = i + 1; j < list.length; j++) {
if (list[j] < list[minPos]) {
minPos = j;
}
}
swap(list, minPos, i);
}
return list;
}
private boolean isRaggedArray(double[][] data) {
for(int i=1; i < data.length; i++) {
if(data[0].length != data[i].length)
return true;
}
return false;
}
}
--------------------output-------------------
Unsorted Array:
4.0 8.0 6.0
9.0 2.0 7.0
1.0 3.0 5.0
Row Sorted Array:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
Column Sorted Array:
1.0 4.0 7.0
2.0 5.0 8.0
3.0 6.0 9.0
Unsorted Array2:
8.0 6.0
9.0 2.0 7.0
1.0 3.0 5.0
Row Sorted Array:
1.0 2.0
3.0 5.0 6.0
7.0 8.0 9.0
Column Sorted Array:
Cannot sort ragged array
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.