Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method named equals that accepts two arrays of integers as parameters an

ID: 3763750 • Letter: W

Question

Write a method named equals that accepts two arrays of integers as parameters and returns true if they contain exactly the same elements in the same order, and false otherwise. Note that the arrays might not be the same length; if the lengths differ, return false. Do not call Arrays.equals in your solution. For example, if the following arrays are declared: int[] a1 = {10, 20, 30, 40, 50, 60}; int[] a2 = {10, 20, 30, 40, 50, 60}; int[] a3 = {20, 3, 50, 10, 68}; The call equals(a1, a2) returns true but the call equals(a1, a3) returns false.(Java Language)

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Equal
{
   public static boolean equals(int[] a1,int[] a2)
   {
       if(a1.length!=a2.length)
           return false;
       else
       {
           for(int i=0;i<a1.length;i++)
               if(a1[i]!=a2[i])
                   return false;
       }
       return true;
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       int[] a1 = {10, 20, 30, 40, 50, 60};
       int[] a2 = {10, 20, 30, 40, 50, 60};
       int[] a3 = {20, 3, 50, 10, 68};
      
       if(equals(a1,a3)==true)
       System.out.println("a1 and a3 are equal");
       else
       System.out.println("a1 and a3 are not equal");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote