98 954
Assignments Done
98.8%
Successfully Done
In May 2021

# Answer to Question #41946 in Java | JSP | JSF for sana khalil

Question #41946
Write a void method Shift that takes three parameters A, p, d.
A is an array, p is the number of positions and d is the direction (L = left, R = right). The method should shift the contents of A p positions in the d direction.
(B) To test your method, write a program that reads an array of 10 elements then calls Shift twice and prints the array after each call.
1
2014-05-05T10:49:47-0400
// Answer on Question#41946 - Programing - Javapublic class Shift {    /*     * shift the content of A p position in the d direction     */    static int[] shift(int A[], int p, String d) {        int temp[] = new int[A.length];        int j = 0;        if (d.equals("R")) {            for (int i = A.length - p; i < A.length; i++) {                temp[j++] = A[i];            }            for (int i = 0; i < A.length - p; i++) {                temp[j++] = A[i];            }        }        if (d.equals("L")) {            for (int i = p; i < A.length; i++) {                temp[j++] = A[i];            }            for (int i = 0; i < p; i++) {                temp[j++] = A[i];            }        }        return temp;    }    /*     * test the progrsm     */    public static void main(String args[]) {        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};        System.out.println("Original array: ");        for (int i = 0; i < arr.length; i++) {            System.out.print(arr[i] + " ");        }        System.out.println("\n");        int out[] = shift(arr, 10, "R");        System.out.println("after p=10 d=R:");        for (int i = 0; i < out.length; i++) {            System.out.print(out[i] + " ");        }        System.out.println("");        int out1[] = shift(arr, 8, "L");        System.out.println("\nafter p=8 d=L:");        for (int i = 0; i < out1.length; i++) {            System.out.print(out1[i] + " ");        }        System.out.println("");        int out2[] = shift(arr, 2, "L");        System.out.println("\nafter p=2 d=L:");        for (int i = 0; i < out2.length; i++) {            System.out.print(out2[i] + " ");        }        System.out.println("");        int out3[] = shift(arr, 4, "R");        System.out.println("\nafter p=4 d=R:");        for (int i = 0; i < out3.length; i++) {            System.out.print(out3[i] + " ");        }    }}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!