MDArray - Part a: Write a class called MDArray containing the following variable
ID: 3560598 • Letter: M
Question
MDArray - Part a: Write a class called MDArray containing the following variables and methods:
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. For example, if the contents of the text file is as follows:
3
2 4.1 8.9
5 9.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:
Explanation / Answer
Below is your code.
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
class MDArray {
private double[][] mdarray;
public MDArray(double[][] mdarray)
{
this.mdarray = mdarray;
}
public MDArray(String fileName) throws FileNotFoundException
{
File file = new File(fileName);
Scanner obj = new Scanner(file);
mdarray = new double[Integer.parseInt(obj.nextLine())][];
int index = 0;
while (obj.hasNextLine())
{
String s[] = obj.nextLine().split(" ");
mdarray[index] = new double[s.length];
for (int i = 0; i < s.length; i++)
mdarray[index][i] = Double.parseDouble(s[i]);
index++;
}
}
public boolean isRagged()
{
for (int i = 1; i < mdarray.length; i++)
if (mdarray[i].length != mdarray[i - 1].length)
return true;
return false;
}
public int getNumberOfRows()
{
return mdarray.length;
}
public int getNumberOfCols()
{
if (mdarray == null)
return 0;
int maxLength = mdarray[0].length;
for (int i = 1; i < mdarray.length; i++)
if (mdarray[i].length > maxLength)
maxLength = mdarray[i].length;
return maxLength;
}
public double getValAt(int i, int j)
{
double val = -1;
try {
val = mdarray[i][j];
}
catch (Exception e)
{
System.out.println("Array Index out of bounds!");
return Double.NaN;
}
return val;
}
public void sort(boolean byColumn)
{
boolean isRagged = isRagged();
if (byColumn)
{
if (isRagged)
{
System.out.println("Cannot sort ragged arrays by column.");
}
else {
// sort by column
int index = 0;
ArrayList<Double> sorted = new ArrayList<Double>();
for (int col = 0; col < getNumberOfCols(); col++)
{
for (int row = 0; row < mdarray.length; row++)
{
sorted.add(mdarray[row][col]);
}
}
Collections.sort(sorted);
for (int col = 0; col < getNumberOfCols(); col++)
{
for (int row = 0; row < mdarray.length; row++)
{
mdarray[row][col] = sorted.get(index);
index++;
}
}
}
}
else {
// sort by row
int index = 0;
ArrayList<Double> sorted = new ArrayList<Double>();
for (int row = 0; row < mdarray.length; row++)
{
for (int j = 0; j < mdarray[row].length; j++)
sorted.add(mdarray[row][j]);
}
Collections.sort(sorted);
for (int row = 0; row < mdarray.length; row++)
{
for (int col = 0; col < mdarray[row].length; col++)
{
mdarray[row][col] = sorted.get(index);
index++;
}
}
}
}
public int hamming(boolean byColumn)
{
if (byColumn && isRagged())
return -1;
double orig[][] = new double[mdarray.length][];
for (int i = 0; i < mdarray.length; i++)
{
for (int j = 0; j < mdarray[i].length; j++)
{
if (orig[i] == null)
{
orig[i] = new double[mdarray[i].length];
}
orig[i][j] = mdarray[i][j];
}
}
sort(byColumn);
int hamming = 0;
// calculate hamming distance
for (int i = 0; i < mdarray.length; i++)
{
for (int j = 0; j < mdarray[i].length; j++)
{
if (orig[i][j] != mdarray[i][j])
hamming++;
}
}
mdarray = orig;
return hamming;
}
public double[] max()
{
int maxRow = 0;
double maxValue = Double.MIN_VALUE;
for (int i = 0; i < mdarray.length; i++)
{
for (int j = 0; j < mdarray[i].length; j++)
{
if (mdarray[i][j] > maxValue)
{
maxValue = mdarray[i][j];
maxRow = i;
}
}
}
return mdarray[maxRow];
}
public String toString()
{
String res = "";
for (int i = 0; i < mdarray.length; i++)
{
for (int j = 0; j < mdarray[i].length; j++)
{
res = res + mdarray[i][j] + " ";
}
res = res + " ";
}
return res;
}
}
public class MDArrayDemo {
public static void main(String[] args) throws FileNotFoundException {
MDArray array = new MDArray(new double[][] {
{ 4, 8, 6 }, { 9, 2, 7 }, { 1, 3, 5 }
});
MDArray array2 = new MDArray(new double[][] {
{ 4, 8, 6 }, { 9, 2, 7 }, { 1, 3, 5 }
});
MDArray array3 = new MDArray("mdarray.txt");
MDArray array4 = new MDArray(new double[][] {
{ 4, 8 }, { 9, 2, 7 }, { 5 }
});
MDArray array5 = new MDArray(new double[][] {
{ 4, 8, 6 }, { 9, 2, 7, 4, 5, 6 }, { 1, 3, 5 }
});
System.out.println("Original Array: ");
System.out.println(array);
System.out.println(" Sorted By Column: ");
array.sort(true);
System.out.println(array);
System.out.println(" Sorted By Row: ");
array.sort(false);
System.out.println(array);
System.out.println(" Hamming distance by column for array 4: " + array4.hamming(true));
System.out.println(" Hamming distance by row for array 5: " + array5.hamming(false));
System.out.println("Array read from file: ");
System.out.println(array3);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.