Question

In: Computer Science

Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...

  1. Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.

Solutions

Expert Solution

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


Related Solutions

write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write a method weave that takes two arrays of ints, a and b, and that returns...
Write a method weave that takes two arrays of ints, a and b, and that returns an array that contains the elements of a and b in the order a[0], b[0], a[1], b[1], etc. If one of the arrays a or b is longer than the other, just add the extra elements at the end of the array. In your solution, you can use only 3 arrays, namely the two arrays a, and b passed to the method and the...
Write a program that takes an integer as input and returns whether it is a prime...
Write a program that takes an integer as input and returns whether it is a prime number or not in C++. (Using if, loops, or/and bool only)
You must write tests for the following: all tests must be written in JUnit 5. Customer...
You must write tests for the following: all tests must be written in JUnit 5. Customer Class Checking that the customer number autoincrements with each new customer object created Order Class Checking that the order number autoincrements with each new order object created Adding a product that is already in the order Removing a product that is in the order - quantity less than the quantity in the order Removing a product that is in the order - quantity that...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a number of bunnies and return another integer for the total number of ears that the group of bunnies has. (Assume ear bunny has exactly 2 ears).. Write a method countX, that when given a string counts the number of lowercase 'x' chars in the string. countX("xxhixx") → 4 countX("xhixhix") → 3 countX("hi") → 0 Write a method copies that, when given a string and...
Write a method that takes two Sorted Arrays of different sizes and merges them into one...
Write a method that takes two Sorted Arrays of different sizes and merges them into one sorted array, and use the method to write a full recursive Merge Sort Algorithm.
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
// Java // This method takes an integer array as well as an integer (the starting...
// Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a character stack and converts all lower case letters // to upper case ones....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT