public void shiftNTimes(int[] array, int numShifts) { for (int i = 0; i < numShi
ID: 3584540 • Letter: P
Question
public void shiftNTimes(int[] array, int numShifts) { for (int i = 0; i < numShifts; i++) { for (int j = 0; j < array.length-1; j++) { int temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; @Test public void testshiftNTimes() { LoopsAndArrays la = new LoopsAndArrays(); int[] a1 = { 1, 2, 3, 4, 5, 6, 7 }; int[] a2 = { 1, 2, 3, 4, 5, 6, 7 }; int[] a3 = { 1, 2, 3 }; int[] a4 = { 4, 5, 6, 7, 1, 2, 3 }; int[] a5 = { 1, 2, 3, 4, 5, 6, 7 }; int[] a6 = { 3, 1, 2 }; la.shiftNTimes(a2, 0); assertEquals(a5,a2); la.shiftNTimes(a3, 5); assertEquals(a6, a3); la.shiftNTimes(a1, 3); assertEquals(a4, a1); why a5 does not equal a2Explanation / Answer
I ran program with the method and they are equal...(I added an print array before the shift and after the shift code snippit: int[] a1 = { 1, 2, 3, 4, 5, 6, 7 }; int[] a2 = { 1, 2, 3, 4, 5, 6, 7 }; int[] a3 = { 1, 2, 3 }; int[] a4 = { 4, 5, 6, 7, 1, 2, 3 }; int[] a5 = { 1, 2, 3, 4, 5, 6, 7 }; int[] a6 = { 3, 1, 2 }; System.out.println("Shifting a2..."); shiftNTimes(a2, 0); System.out.println("a5,a2 "+assertEquals(a5,a2)); System.out.println("Shifting a3..."); shiftNTimes(a3, 5); System.out.println("a6,a3 "+assertEquals(a6,a3)); System.out.println("Shifting a1..."); shiftNTimes(a1, 3); System.out.println("a4,a1 "+assertEquals(a4,a1)); and output: Shifting a2... 1 2 3 4 5 6 7 1 2 3 4 5 6 7 a5,a2 true Shifting a3... 1 2 3 3 1 2 a6,a3 true Shifting a1... 1 2 3 4 5 6 7 4 5 6 7 1 2 3 a4,a1 true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.