Problem Description: Two arrays are strictly identical if their corresponding el
ID: 3938208 • Letter: P
Question
Problem Description:
Two arrays are strictly identical if their corresponding elements
are equal. Write a program with class name StrictlyIdentical that
prompts the user to enter two lists of integers of size 5 and
displays whether the two are strictly identical.
Use following method header to pass two arrays and return a
Boolean.
public static boolean equals(int[] array1, int[] array2)
Method will return true if array1 and array2 are strictly
identical otherwise it will return false.
Here are two sample runs:
Sample 1:
Enter 5 elements of list1: 23 55 31 2 10
Enter 5 elements of list2: 23 55 31 2 10
Two lists are strictly identical.
Sample 2:
Enter 5 elements of list1: 23 55 31 2 10
Enter 5 elements of list2: 23 55 3 2 10
Two lists are not strictly identical.
Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
public class StrictlyIdentical {
public static boolean equals(int[] array1, int[] array2)
{
if (Arrays.equals(array1, array2))
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
int arr1[] = new int[5];
Scanner in = new Scanner(System.in);
System.out.print("Enter 5 elements of list1: ");
for(int i=0; i<5; i++)
{
arr1[i] = in.nextInt();
}
int arr2[] = new int[5];
System.out.print("Enter 5 elements of list2: ");
for(int j=0; j<5; j++)
{
arr2[j] = in.nextInt();
}
StrictlyIdentical ob=new StrictlyIdentical();
boolean result=ob.equals(arr1, arr2);
if(result)
{
System.out.println("Two lists are strictly identical.");
}
else
{
System.out.println("Two lists are not strictly identical.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.