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 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...
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.
Write a program that asks the user to type in two integer values. Test these two...
Write a program that asks the user to type in two integer values. Test these two numbers to determine whether the first is evenly divisible by the second and then display the appropriate message to the terminal. Objective C
In Java I need a Flowchart and Code. Write the following method that tests whether the...
In Java I need a Flowchart and Code. Write the following method that tests whether the array has four consecutive numbers with the same value: public static boolean isConsecutiveFour(int[] values) Write a test program that prompts the user to enter a series of integers and displays it if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size—i.e., the number of values in the series.
Java Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and...
Java Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and determines if the vector v3 is the intersection or union of vectors v1 and v2. Example 1: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5} and v3 = {3, 5}, then:               intersect_or_union_fcn(v1, v2, v3) will print:                              v3 is the intersection of v1 and v2 Example 2: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5}...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT