java language please Write the definition of a method odds

java language please - Write the definition of a method , oddsMatchEvens, whose two parameters are arrays of integers of equal size. The size of each array is an even number. The method returns true if and only if the even-indexed elements of the first array equal the odd-indexed elements of the second, in sequence. That is if w is the first array and q the second array , w[0] equals q[1], and w[2] equals q[3], and so on.

Solution

/*This method has been tested in eclipse with set of test cases */

public static boolean oddsMatchEvens(int w[],int q[]){ // definition of a method , oddsMatchEvens,w and q //are two parameters of integer arrays
boolean result=false; //boolean variable,default value=false
int i=0,j=1; //i=0 for traversing w array,means even index and j=1 for //traversing q array, means odd index
while(i<w.length && j<q.length) //execute the loop till the size of the both array
{
if(w[i]==q[j]) //if the even-indexed elements of the first array equal the odd- //indexed elements of the second, in sequence
{
i=i+2; //skip the odd indexes of w array
j=j+2; //skip the even indexes of q array
result=true; //set the boolean variable true
}
else //if the condition is not satisfied
{
result=false; //set the boolean variable false
break; //break from the loop
}
}
return result; //return result of the function
}

//end of the method


Please let me know in case of any question,thanks.

*******************code without comment lines*********************

public static boolean oddsMatchEvens(int w[],int q[]){
boolean result=false;   
int i=0,j=1;
while(i<w.length && j<q.length)   
{
if(w[i]==q[j])
{
i=i+2;
j=j+2;
result=true;
}
else
{
result=false;   
break;   
}
}
return result;
}

java language please - Write the definition of a method , oddsMatchEvens, whose two parameters are arrays of integers of equal size. The size of each array is a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site