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

Write the method middle that takes an array a and returns a new array holding th

ID: 3848500 • Letter: W

Question

Write the method middle that takes an array a and returns a new array holding the middle third of a. You may assume that the length of a is a multiple of 3. For example middle (15, 4, 1, 6, 3, 2}} returns {1, 6}//returns the middle third of a public static int middle (int [] a) Write the method reversed that returns true if and only if the arrays a and b contain exactly the same elements, but in reversed order. For example reversed ({3, 1) {1, 3}) returns true, but reversed ({3, 1}, {2, 3}} and reversed ({3, 1}, {1, 1, 3} both return false.//returns true iff a and b contain the same elements, reversed public static boolean reversed (int [] a, int b)

Explanation / Answer

package sample1;

import java.util.Scanner;

import java.io.*;

public class test {

public static void main(String args[]) throws IOException
{
   int x[]={1,2,3,4,5,6,7,8,9};
   int res[]=middle(x);
   for(int i=0;i<res.length;i++){
       System.out.println(res[i]);
   }
   }
public static int[] middle(int a[]){
   int res[]=new int[a.length/3];
   int ind=0;
for(int i=a.length/3;i<a.length-(a.length/3);i++){
   res[ind]=a[i];
   ind++;
}
return res;
}
}

package sample1;

import java.util.Scanner;

import java.io.*;

public class test {

public static void main(String args[]) throws IOException
{
   int x[]={1,2,3,4};
   int y[]={4,3,2,1};
   int x1[]={1,2,3,4};
   int y1[]={4,3,2,4};
   System.out.println(reversed(x,y));
   System.out.println(reversed(x1,y1));
   }
public static boolean reversed(int a[],int b[]){
boolean flag=true;
   if(a.length==b.length){
     
   for(int i=0;i<a.length;i++){
         
       if(a[i]==b[b.length-1-i]){
             
       }
       else{
           flag=false;
       }
   }
     
}
   else{
       flag=false;
   }
   return flag;
}
}