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

1. Make sure the methods you define below are called and tested in main(). 2. De

ID: 3671204 • Letter: 1

Question

1. Make sure the methods you define below are called and tested in main().

2. Define the following function

public static void arrayShiftRight(int[] input, int index)

a. This function shifts all elements in the input array to the right by one, starting with the position indicated by index.

For example, in the above code, we called arrayShiftRight(input, 1), to indicate we wanted to shift each element right after the first (ie, not including the B).

b. public static void arrayShiftLeft(int[] input, int startIndex)

This function is the reciprocal of arrayShiftRight(), so you should start by copying and pasting that function, then renaming it to arrayShiftLeft()

This function shifts all elements in the input array to the left by one, starting with the position indicated by index.

For example, lets say we’d like to change “Baits” to “Bits”, which is displayed pictorially below.

3. Define the following functions, using arrayShiftRight() and arrayShiftLeft()

a. public static void rotateRight(int[] input)

Given {1,2,3}, this function shifts all elements right and moves the last element back to the head of the list, so {3,1,2}

Use arrayShiftRight() to accomplish this

b.public static void rotateLeft(int[] input)

Given {1,2,3}, this function shifts all elements to the left and moves the first element to the end of the list, so {2,3,1}

Use arrayShiftLeft() to accomplish this

import java.util.Arrays; public class Exercise4 t public static void main(String[ ] args) int data (3,9,2,0,0,0 I/a six-element array with only three values so far int data2-1,5,3,2,9,6); //a six-element array filled with values. //arrayShiftLeft (data); //...do the same for data2 //print out each element in data here //print out each element in data2 here (see for loop above) //TODO: following the pattern above for arrayShiftLeft, for (int i = 0; i

Explanation / Answer

Answer:

import java.util.Arrays;

public class Exercise4 {
  
public static void main(String[] args) {
int[] data = {3,9,2,0,0,0}; //a six-element array with only three values so far
int[] data2 = {1,5,3,2,9,6}; //a six-element array filled with values
  
System.out.println("Original Arrays:");
printArray(data);
printArray(data2);
  
System.out.println("arrayShiftLeft:");
arrayShiftLeft(data,3);
arrayShiftLeft(data2,3);
//arrayShiftLeft(data2,1);
  
//print out each element in data here
printArray(data);
//print out each element in data2 here (see for loop above)
printArray(data2);

//TODO: following the pattern above for arrayShiftLeft,
//add code to main to call arrayShiftRight and then print out arrays
System.out.println("arrayShiftRight:");
arrayShiftRight(data,2);
arrayShiftRight(data2,2);
  
//print out each element in data here
printArray(data);
//print out each element in data2 here (see for loop above)
printArray(data2);
  
System.out.println("rotateLeft:");
rotateLeft(data);
rotateLeft(data2);
  
//print out each element in data here
printArray(data);
//print out each element in data2 here (see for loop above)
printArray(data2);
}
  
public static void printArray(int[] index){
for(int i= 0; i<index.length; i++)
System.out.print(index[i] + " ");
System.out.println();
}
  
public static void arrayShiftLeft(int[] index, int startIndex){
if (startIndex < 0 || startIndex >= index.length){
System.out.println("Error - value out of range");
return ;
}
int valueIndex = index[startIndex];
int first= index[0];
int i;

for(i= startIndex; i<index.length-1; i++)
index[i] = index[i+1];
  
if (startIndex > 0)
for(i= 0; i<startIndex-1; i++)
index[i] = index[i+1];
  
index[index.length-1]= first;
if (startIndex > 0)
index[startIndex-1]= valueIndex;
}
  
public static void arrayShiftRight(int[] index, int startIndex){   
if (startIndex < 0 || startIndex >= index.length){
System.out.println("Error - value out of range");
return ;
}
int valueIndex = index[startIndex];
int last= index[index.length-1];
int i;

for(i=startIndex; i>0; i--)
index[i] = index[i-1];
  
if (startIndex+1 < index.length)
for(i=index.length-1; i>startIndex; i--)
index[i] = index[i-1];
  
index[0]= last;
if (startIndex+1 < index.length)
index[startIndex+1]= valueIndex;
}
  
public static void rotateLeft(int[] index){
arrayShiftLeft(index,0);
}
  
public static void rotateRight(int[] index){
arrayShiftRight(index,0);
}
}