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...
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...
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.
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To...
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To test this Triangle class, you write a JUnit test class named TriangleTest.java. This test class contains multiple test cases in the format of methods. The test runner executes TriangleTest to generate a JUnit test report. In this project, you do not need to write the code for this Triangle class. Instead, you will write JUnit test cases to make sure that the implementation of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
You are required to write 4 significant JUnit tests for the Account class outlined below taking...
You are required to write 4 significant JUnit tests for the Account class outlined below taking into account the specifications below. Your code must include 3 negative test cases. Specifications for the Account class: The constructor takes as arguments the name, ID and the initial balance. The withdrawn method allows any specified amount between $20 and $1000 to be withdrawn but the withdrawn amount cannot exceed the current balance. The withdraw method is expected to throw the AmtTooLow exception if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT