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

Write array methods that carry out the following tasks for an array of integers

ID: 3810401 • Letter: W

Question

Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods {private int[] values; public ArrayMethods(int[] initial values) {values = initial Values;} public void swapFirstAndLast() {...} public void shiftRight() {...} ...} a. Swap the first and last elements in the array. b. Shift all elements to the right by one and move the last element into the first position. For example, 1 4 9 16 25 would be transformed into 25 1 4916. c. Replace all even elements with 0. d. Replace each element except the first and last by the larger of its two neighbors. e. Remove the middle element if the array length is odd, or the middle two elements if the length is even. f. Move all even elements to the front, otherwise preserving the order of the elements. g. Return the second-largest element in the array. h. Return true if the array is currently sorted in increasing order. i. Return true if the array contains two adjacent duplicate elements. j. Return true if the array contains duplicate elements (which need not be adjacent).

Explanation / Answer


/*The java class ArrayMethods contains methods swapFirstAndLast,
* shiftRight, replaceZero, relaceLarger,removeMiddle,
* moveEvenFront, secondLargest, isascendingOrder,
* containsAdjacentDuplicates and containsDuplicates*/

//ArrayMethods.java
public class ArrayMethods
{
   //private integer array variable
   private int[] values;  
   //constructor that sets intial values of array
   public ArrayMethods(int[] initialValues)
   {
       values=initialValues;
   }

   /*
   * a. The method swapFirstAndLast swaps first and last elements of array
   * */
   public void swapFirstAndLast()
   {
       int first=values[0];
       int last=values[values.length-1];

       values[0]=last;
       values[values.length-1]=first;      
   }

  
   /*
   * b. The method shiftRight that shifts the elements to the right
   * and move the last element into the first element.
   * */
   public void shiftRight()
   {
       int last=values[values.length-1];

       for (int i = values.length-2; i >=0 ; i--)
       {
           values[i+1]=values[i];
       }

       values[0]=last;
   }
   /*
   * c. The method replaceZero that sets zero values
   * into the array.
   * */
   public void replaceZero()
   {
       for (int i = 0; i < values.length; i++)
       {
           if(values[i]%2==0)
               values[i]=0;
       }
   }

   /*
   * d.The method replaceLarger that replaces the larger
   * of the two adjacent elements.
   * */
   public void relaceLarger()
   {
       for (int i = 1; i < values.length-1; i++)
       {
           if(values[i-1]>values[i+1])          
               values[i]=values[i-1];
           else
               values[i]=values[i+1];
       }
   }


   /*
   * e.The method removeMiddle that sets the middle
   * to zero if odd otherwise set zero to two middle
   * element for even size array.
   *
   * */
   public void removeMiddle()
   {
       int size=values.length;      
       //check if size is even
       if(size%2==0)
       {
           int mid=size/2;
           values[mid]=0;
           values[mid+1]=0;          
       }
       else
       {
           //otherwise
           int mid=size/2;
           values[mid]=0;
       }
   }

   /*
   * The method moveEvenFront that moves all even elements
   * to the front of the array.*/
   public void moveEvenFront()
   {
       int evenPosition=0;
       for (int i = 0; i < values.length; i++)
       {
           //check if element is even
           if(values[i]%2==0)
           {
               //The store the value in temp
               int temp=values[i];

               //Move the element to the front
               for (int j = i;
                       j >evenPosition; j--) {
                   values[j]=values[j-1];
               }
               //move the temp to the starting location
               values[evenPosition]=temp;
               //increment evenEnd by one
               evenPosition++;
           }
       }
   }

   /*
   * g. The method secondLargest returns the second largest element
   * in the array.
   * */
   public int secondLargest()
   {
       int big=values[0];
       int loc=0;
       //find the postion of first largest
       for(int i=1;i<values.length;i++)
       {
           if(big<values[i])
           {
               big=values[i];
               loc = i;
           }
       }

       //assume second
       int secondlargest=values[values.length-loc-1];
      
       for(int i=1;i<values.length;i++)
       {
           if(secondlargest <values[i] && loc != i)
               secondlargest =values[i];
       }
      
       //return second largest
       return secondlargest;
   }

  
  
   /*
   * h. The method isascendingOrder that return true if
   * array is in ascending order.
   * */
   public boolean isascendingOrder()
   {
       boolean ascending=true;
      
       for (int i = 0; i < values.length-1; i++)
       {
           if(values[i]>values[i+1])
               ascending=false;
       }
       return ascending;
   }
  
  
   /*
   * i. The method containsAdjacentDuplicates that returns true
   * if two adjacent elements are same otherwise returns false.
   * */
   public boolean containsAdjacentDuplicates()
   {
       boolean contains=false;
      
       for (int i = 0; i < values.length-1; i++)
       {
           if(values[i]==values[i+1])
               contains=true;
       }
       return contains;
   }
   /*
   * j. The method containsDuplicates that returns true
   * if contains duplicate values otherwise returns false.
   * */
   public boolean containsDuplicates()
   {
       boolean contains=false;
      
       for (int i = 0; i < values.length; i++)
       {
           for (int j = 0; j < values.length; j++)
           {
               if(i==j)
                   continue;              
               else if(values[i]==values[j])
                   contains=true;
           }
       }
       return contains;
   }
}//end of class ArrayMethods

----------------- ----------------- ----------------- ----------------- -----------------


/*
* The java program TestArrayMethods that create an instance of
* ArratMethods and calls the methods with array and print results
* of each test to console. */
//TestArrayMethods.java
import java.util.Arrays;
public class TestArrayMethods
{
   public static void main(String[] args)
   {
      
       /*create an array ,elements that is used for all methods
          without change of elements making copy of elements for
          each method callig */
       int elements[]={1,4,9,16,25};
      
       //Copy array elements to temp array
       int temp[]=Arrays.copyOf(elements, elements.length);
      
       //Create an instance of ArrayMethods with temp array
       ArrayMethods am=new ArrayMethods(temp);
       System.out.println("array elements");
       //calling display method
       display(temp);
      
      
       //copy original array to temp
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling swapFirstAndLast method");
       //Create an instance of ArrayMethods with temp array
       am=new ArrayMethods(temp);
      
       //a.calling swapFirstAndLast
       am.swapFirstAndLast();
       //calling display method
       display(temp);
      
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling shiftRight method");      
       am=new ArrayMethods(temp);
      
       //b.calling shiftRight
       am.shiftRight();
       display(temp);
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling replaceZero method");
       am=new ArrayMethods(temp);
       //c.calling replaceZero
       am.replaceZero();
       display(temp);
      
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling replaceLarger method");
       am=new ArrayMethods(temp);
      
       //d.calling replaceLarger
       am.relaceLarger();
       display(temp);
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling moveEvenFront method");
       am=new ArrayMethods(temp);
       //e.calling removeMiddle
       am.removeMiddle();
       display(temp);
      
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling moveEvenFront method");
       am=new ArrayMethods(temp);
       //f.calling moveEvenFront
       am.moveEvenFront();
       display(temp);
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling secondLargest method");
       am=new ArrayMethods(temp);
       //g.calling secondLargest
       System.out.println(am.secondLargest());
       display(elements);
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling isascendingOrder method");
       am=new ArrayMethods(temp);      
       //h.calling isascendingOrder
       System.out.println(am.isascendingOrder());
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling containsAdjacentDuplicates method");      
       am=new ArrayMethods(temp);
      
       //i.calling containsAdjacentDuplicates
       System.out.println(am.containsAdjacentDuplicates());
      
       temp=Arrays.copyOf(elements, elements.length);
       System.out.println("Calling containsDuplicates method");
       am=new ArrayMethods(temp);
       //j.calling containsAdjacentDuplicates
       System.out.println(am.containsDuplicates());
      
   }//end of main method
  
   //Helper method to print elements in array,elements
   private static void display(int[] elements)
   {      
       for (int i = 0; i < elements.length; i++) {
           System.out.printf(",%d ",elements[i]);
       }
       System.out.println();
   }  
}

----------------- ----------------- ----------------- ----------------- -----------------

Sample Output:

array elements
,1 ,4 ,9 ,16 ,25
Calling swapFirstAndLast method
,25 ,4 ,9 ,16 ,1
Calling shiftRight method
,25 ,1 ,4 ,9 ,16
Calling replaceZero method
,1 ,0 ,9 ,0 ,25
Calling replaceLarger method
,1 ,9 ,16 ,25 ,25
Calling moveEvenFront method
,1 ,4 ,0 ,16 ,25
Calling moveEvenFront method
,4 ,16 ,1 ,9 ,25
Calling secondLargest method
16
,1 ,4 ,9 ,16 ,25
Calling isascendingOrder method
true
Calling containsAdjacentDuplicates method
false
Calling containsDuplicates method
false

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