In: Computer Science
Details:
Create a class called CompareArrays that determines if two specified integer arrays are equal.
The class should have one static methods:
NOTE: You are not allowed to use the Arrays.equals method for this programming assignment.
Create a second class called CompareArraysTest that contains the main method, and thoroughly tests the ComparyArrays.compare method. This test class does not need to ask users for input. Just create the needed arrays to ensure that you test the ComparyArrays.compare method well. The thoroughness of your testing in will impact your grade.
Source Code:
Output:
Code in text format (See above images of code for indentation):
import java.util.*;
/*class definition*/
class CompareArrays
{
/*static method to compare two arrays*/
public static boolean compare(int[] arrayOne, int[] arrayTwo)
{
boolean check=true;
/*if lengths are not equal then return false*/
if(arrayOne.length!=arrayTwo.length)
return false;
else
{
/*check for same size and same order*/
for(int i=0;i<arrayOne.length;i++)
{
/*if order is missed then return false and break*/
if(arrayOne[i]!=arrayTwo[i])
{
check=false;
break;
}
}
/*return check*/
return check;
}
}
}
/*class to test above method**/
public class CompareArraysTest
{
/*main method*/
public static void main(String[] args)
{
/*two arrays initialization*/
int[] arrayOne={12,3,3,4,5};
int[] arrayTwo={12,3,3,4,5};
/*method call*/
boolean check=CompareArrays.compare(arrayOne,arrayTwo);
/*print equal or not equal*/
if(check)
System.out.print("Equal");
else
System.out.print("NotEqual");
}
}
Note:
The above code doesn't read input from the user as mentioned in the question