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

I have homework assignemnt for a intro to Java programming class. The assignment

ID: 3751336 • Letter: I

Question

I have homework assignemnt for a intro to Java programming class.

The assignment asks this: The method arraySwap has three parameters: an array of integers and two indices. It swaps the value at the first index with the value at the second index. If the swap was successful, true is returned, otherwise false is returned. A swap fails if any of the two indices is less than 0 or larger than or equal to the array size.

This is the method I have created to do this:

public static boolean arraySwap(int[] inputNumbers, int[] index_01, int[] index_02)
{

int temp[] = new int[inputNumbers.length];
for (int i = 0; i < inputNumbers.length; i++)
temp[index_01[i]] = inputNumbers[i];
for (int i = 0; i < inputNumbers.length; i++)
{
inputNumbers[i] = temp[i];
index_02[i] = i;
return true;
}

return false;   
}

I do not know if I have incorrectly set the input indices or have not correctly set my method.

Any help is appreciated.

Explanation / Answer

public static boolean arraySwap(int[] inputNumbers, int[] index_01, int[] index_02) { if(index_01.length == index_02.length) { int temp, ind1, ind2; for(int i = 0; i = inputNumbers.length || ind2 < 0 || ind2 >= inputNumbers.length) { return false; // A swap fails if any of the two indices is less than 0 or larger than or equal to the array size. } temp = inputNumbers[ind1]; inputNumbers[ind1] = inputNumbers[ind2]; inputNumbers[ind2] = temp; } } return 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