Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone adjust my code so that it passing the toString test in TestCompressedArray.java exercise, its format is off and I can't figure how to

Can anyone adjust my code so that it passing the toString test in TestCompressedArray.java exercise, its format is off and I can't figure how to output the compressed array its asking for:

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"); //System.out.println(arr1); }

double[][] origArray2 = new double[][] { new double[] {5.8, 10.4, -4.7, 13.7}, new double[] {10.4, 7.1, -19.3, 4.8}, new double[] {-4.7, -19.3, 5.1, 17.2}, new double[] {13.7, 4.8, 17.2, -6.7} };

double[][] origArray3 = new double[][] { new double[] {5.8, 12.3, -11.6, 21.3}, new double[] {10.4, 7.1, 0.2, 35.0}, new double[] {-4.7, -19.3, 5.1, 18.4}, new double[] {13.7, 4.8, 17.2, -6.7} }; double[][] origArray4 = new double[][] { new double[] {10.4, 7.1, 0.2, 35.0}, new double[] {-4.7, -19.3, 5.1, 18.4}, new double[] {13.7, 4.8, 17.2, -6.7}, new double[] {5.8, 12.3, -11.6, 21.3}, };

CompressedArray arr2 = new CompressedArray(origArray2); CompressedArray arr3 = new CompressedArray(origArray3); CompressedArray arr4 = new CompressedArray(origArray4);

// --------------------------------------------------------- // Test 3 - getLength() on more CompressedArray objects // --------------------------------------------------------- if (arr2.getLength() == 6 && arr3.getLength() == 6 && arr4.getLength() == 6) { System.out.println("Test 3 Passed"); } else { System.out.println("Test 3 Failed"); } // --------------------------------------------------------- // Test 4 - equals() between two CompressedArray objects // --------------------------------------------------------- if (!arr2.equals(arr4) && !arr3.equals(arr4)) { System.out.println("Test 4 Passed"); } else { System.out.println("Test 4 Failed"); } // --------------------------------------------------------- // Test 5 - equals() between two CompressedArray objects // ---------------------------------------------------------

if (arr2.equals(arr3)) { System.out.println("Test 5 Passed"); } else { System.out.println("Test 5 Failed"); }

} }

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 j) array[k++] = aArray[i][i]; } } } 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 = ""; for (int i = 0; i

} } image text in transcribed

TestCompressedArray.java X 1 City.java Program.java CompressedArray.java X TestProgram.java 1 import java.util.Arrays; 2 public class CompressedArray ( private int origArraySize; private double array(); 2 public class TestCompressedArray ( 44 public static void main(String[] args) { 60 7 8 10 11 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; ij) array[k++] = Array[i][i]; } 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}, }; 11 12 13 14 CompressedArray arr1 = new CompressedArray(origArray1); 16 // // Test 1 - getLength() on a CompressedArray object public int getLength() { return origArraySize; } 17 18 19 20 21 22 23 24 19 20 210 22 23 24 250 26 27 28 290 30 if (arri.getLength() == 10) { System.out.println("Test 1 Passed"); } else { System.out.println("Test 1 Failed"); } public double getElement(int i) { return array[i]; } // Test 2 - toString() method on a CompressedArray @Override public boolean equals(Object obj) { if (this == obj) { return true; String soln = " 29.50 10.80 16.40 87.80 12.20 63.70 13.90 25.00 26 27 28 29 30 31 32 33 34 35 36 37 77.40 97.40 "; 33 lt (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; if (arri.toString().equals(soln)) { System.out.println("Test 2 Passed"); } else { System.out.println("Test 2 Failed"); } 38 39 40 41 42 43 44 45 46 47 48 49 500 51 final CompressedArray other = (CompressedArray) obj; if (this.origArraysize != other.origArraySize ) { return false; lf (!Arrays.equals(this.array, other.array)) { 39 40 41 42 43 44 45 46 47 448 49 double[][] origArray2 = new double[] [] { new double[] {5.8, 10.4, -4.7, 13.7}, new double[] {10.4, 7.1, -19.3, 4.8}, new double[] {-4.7, -19.3, 5.1, 17.2}, new double[] {13.7, 4.8, 17.2, -6.7} }; return false; } return true; } double[][] origArray3 = new double[][] { new double[] {5.8, 12.3, -11.6, 21.3}, new double[] {10.4, 7.1, 0.2, 35.0}, new double[] {-4.7, -19.3, 5.1, 18.4}, new double[] {13.7, 4.8, 17.2, -6.7} }; 50 51 @Override public String toString() { String t = ""; for (int i = 0; i <>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago