In: Computer Science
Kindly upvote if
this helped
They will be equal if:
- Both array have same length
- Both array have same elements at the same indexes.
- We will also check if elements are even (not a condition
for being equal)
I have implemented a method which checks both, it should be equal
and should only have even numbers
If you want separate, just create a seperate function to check for
even as I did in OR part of the implemented method.
Class to TEST:
- Package name I used is search
package search;
public class ArrTest {
public boolean compare(int []a , int []b) {
if(a.length !=b.length) {
return false;
}else {
for(int i=0;i<a.length;i++) {
if(a[i]!=b[i] || a[i]%2!=0 ) {
return false;
}
}
}
return true;
}
}
Junit class:
package search;
import static org.junit.Assert.*;
import org.junit.Test;
public class ArrayTest {
@Test
public void ArrayTest() {
ArrTest t = new ArrTest();
int []a = {1,2,3,4,5,6};
int []b = {1,2,3,4,5,6};
int []c = {2,4,6,8};
int []d = {2,4,6,8};
boolean areEqualAndEven = t.compare(a,b);
assertEquals(areEqualAndEven, true);
}
}
OUTPUT (a,b) -> failed as all elements in a and b are not
even.
Passed as elements in a and b are equal, same and
even