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 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.
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....
Write a very general sort method that can sort any type of data arrays/lists. For example,...
Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support...
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT