Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * Return a new array that has twice the capacity of a. The new array should

ID: 3628150 • Letter: #

Question

/**
* Return a new array that has twice the capacity of a. The new array should
* have 0's in the new spaces. For example, if a = {1,2,3}, the method
* should return {1,2,3,0,0,0}. Be sure your method works for arrays of any
* size.
*/
public static int[] doubleCapacity(int[] a) {
int [] c = new int[2*a.length];

for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int i = a.length; i < c.length; i++) {
c[i] = 0;
}
return c;
}


/**
* Test the doubleCapacity method. Careful! The == operator does not do what
* you think it does with arrays.
*/
public static boolean testDoubleCapacity(int[] a, int[] expected) {
int [] c = doubleCapacity (a);
if (c == expected) {
System.out.println ("passed!");
return true;
}
else {
System.out.println("expected " + expected + " not" + c);
return false;
}
}

public static void main(String[] args) {
testAverage(new int[]{0,3,6}, 3.0);
testAverage(new int[]{1,2,3,4,5,6,7,8,9,10}, 5.5);
testMin(new int[]{1,-5,6,4,-20}, -20);
testMin(new int[]{-100,-5,6,4,-20}, -100);
testMin(new int[]{0, 1, 2}, 0);
testDoubleCapacity(new int[]{1,2,3,4,5}, new int[]{1,2,3,4,5,0,0,0,0,0});
testDoubleCapacity(new int[]{1,2,3}, new int[]{1,2,3,0,0,0});
}
}

 

 

 

 

We can't use the java.util.Arrays package to do this.

 

 

It compiles, but returns this:
expected [I@55187eb3 not[I@3b26456a
expected [I@4d97507c not[I@92524b0

Why does it have all those symbols instead of the values of "expected" and "result"?

Explanation / Answer

please rate - thanks

the answer is in your Professors comment

/**
* Test the doubleCapacity method. Careful! The == operator does not do what
* you think it does with arrays.
*/

== does not compare each element of the array. you must do that yourself


public class doubleit
{
/**
* Return a new array that has twice the capacity of a. The new array should
* have 0's in the new spaces. For example, if a = {1,2,3}, the method
* should return {1,2,3,0,0,0}. Be sure your method works for arrays of any
* size.
*/
public static int[] doubleCapacity(int[] a) {
int [] c = new int[2*a.length];

for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int i = a.length; i < c.length; i++) {
c[i] = 0;
}
return c;
}


/**
* Test the doubleCapacity method. Careful! The == operator does not do what
* you think it does with arrays.
*/
public static boolean testDoubleCapacity(int[] a, int[] expected) {
int [] c = doubleCapacity (a);
int i,tot=0;
if(c.length==expected.length)
{
for(i=0;i<expected.length;i++)
     if(c[i]==expected[i])
          tot++;
if (tot == expected.length) {
      System.out.println ("passed!");
   return true;
   
   }
}

System.out.print("expected ");
for(i=0;i<expected.length;i++)
    System.out.print(expected[i]+" ");
System.out.print(" not " );
for(i=0;i<c.length;i++)
    System.out.print(c[i]+" ");
System.out.println();
return false;

}

public static void main(String[] args) {
//testAverage(new int[]{0,3,6}, 3.0);
//testAverage(new int[]{1,2,3,4,5,6,7,8,9,10}, 5.5);
//testMin(new int[]{1,-5,6,4,-20}, -20);
//testMin(new int[]{-100,-5,6,4,-20}, -100);
//testMin(new int[]{0, 1, 2}, 0);
testDoubleCapacity(new int[]{1,2,3,4,5}, new int[]{1,2,3,4,5,0,0,0,0,0});
testDoubleCapacity(new int[]{1,2,3}, new int[]{1,2,3,4,5,6});


}
}