Question: I need assistance in the processing of data in constructor of CompressedArray.java, for clarification TestCompressedArray.java is a file to test if the code of CompressedArray.java

I need assistance in the processing of data in constructor of CompressedArray.java, for clarification TestCompressedArray.java is a file to test if the code of CompressedArray.java is working correctly, so my code isn't passing test 2.

My code is outputting:

29.50 10.80 16.40 16.40 87.80 12.20 87.80 12.20 63.70 13.90 12.20 63.70 13.90 25.00 77.40

And its supposed to output (test 2 of TestCompressedArray.java):

29.50 10.80 16.40 87.80 12.20 63.70 13.90 25.00 77.40 97.40

My code seems to not create the 2d matrix/compressed array properly, can anyone help me:

import java.util.Arrays; public class CompressedArray { private int origArraySize; private double array[]; public CompressedArray (double[][] aArray) { int n = aArray.length; int total = (n * (n - 1)) / 2; int i, j, k = 0; origArraySize = total; array = new double[total]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i > j) array[k++] = aArray[i][j]; } } } public int getLength () { return origArraySize; } public double getElement(int i) { return array[i]; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final CompressedArray other = (CompressedArray) obj; if (this.origArraySize != other.origArraySize ) { return false; } if (!Arrays.equals(this.array, other.array)) { return false; } return true; } @Override public String toString() { String t = ""; int count = 1; int i = 0; while ((i + count) < array.length){ for (int j=0; j < count; j++) { t += String.format("%8.2f", array[i + j]); } i = count; count ++; t += " "; } System.out.println(t); return t; } }

public class TestCompressedArray {

public static void main(String[] args) {

double[][] origArray1 = new double[][] { new double[] {15.2, 43.6, 28.2, 35.0, 95.5}, new double[] {29.5, 88.3, 72.1, 44.4, 37.4}, new double[] {10.8, 16.4, 74.6, 91.7, 36.2}, new double[] {87.8, 12.2, 63.7, 19.6, 73.1}, new double[] {13.9, 25.0, 77.4, 97.4, 30.5}, }; CompressedArray arr1 = new CompressedArray(origArray1);

// --------------------------------------------------------- // Test 1 - getLength() on a CompressedArray object // --------------------------------------------------------- if (arr1.getLength() == 10) { System.out.println("Test 1 Passed"); } else { System.out.println("Test 1 Failed"); }

// --------------------------------------------------------- // Test 2 - toString() method on a CompressedArray // --------------------------------------------------------- String soln = " 29.50 10.80 16.40 87.80 12.20 63.70 13.90 25.00 77.40 97.40 ";

if (arr1.toString().equals(soln)) { System.out.println("Test 2 Passed"); } else { System.out.println("Test 2 Failed"); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!