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

I am having a tough time trying to figure out how to write a Javaprogram that sw

ID: 3614766 • Letter: I

Question

I am having a tough time trying to figure out how to write a Javaprogram that swaps the locations of the largest number and thesmallest number in an array of 10 randomly generated integersranging between 0-99. Here's what I've done:
public class SwapMaxMin {
public static void main (String[]args) {

int [] myScore = new int [10];
int i;
for (i=0; i < myScore.length; i++)
myScore [i] = (int)(Math.random()*100);
//print array
for (i=0; i < 1;i++)
System.out.println ("Before swapping:");
for (i=0; i < myScore.length; i++)

System.out.println ("myScore [" + i + "] = " + myScore [i]);
//int myMax, myMin;
int myMax = myScore [0];
int myMin= 100;
for (i=0; i < myScore.length; i++)
if (myScore [i]> myMax)


myMax = myScore[i];
System.out.println ("The largest number = "+ myMax);
// swapFirstTwoInArray (myScore[i]);
for (i=0; i < myScore.length; i++)
if (myScore [i]< myMin)
myMin = myScore[i];
System.out.println ("The smallest number = " +myMin);
System.out.println ("Before swapping:");

int temp = myMax;
myMax =myMin;
myMin= temp;

System.out.println("max number"+myMax);
System.out.println("min"+myMin);
for (i=0; i < myScore.length; i++){
System.out.println ("myScore [" + i + "] = " + myScore [i]);
}
System.out.println( "Done!" );
}
}
I did swap the myMax and myMin (you can see in the code),but in the
array, it just wouldn't swap.
public class SwapMaxMin {
public static void main (String[]args) {

int [] myScore = new int [10];
int i;
for (i=0; i < myScore.length; i++)
myScore [i] = (int)(Math.random()*100);
//print array
for (i=0; i < 1;i++)
System.out.println ("Before swapping:");
for (i=0; i < myScore.length; i++)

System.out.println ("myScore [" + i + "] = " + myScore [i]);
//int myMax, myMin;
int myMax = myScore [0];
int myMin= 100;
for (i=0; i < myScore.length; i++)
if (myScore [i]> myMax)


myMax = myScore[i];
System.out.println ("The largest number = "+ myMax);
// swapFirstTwoInArray (myScore[i]);
for (i=0; i < myScore.length; i++)
if (myScore [i]< myMin)
myMin = myScore[i];
System.out.println ("The smallest number = " +myMin);
System.out.println ("Before swapping:");

int temp = myMax;
myMax =myMin;
myMin= temp;

System.out.println("max number"+myMax);
System.out.println("min"+myMin);
for (i=0; i < myScore.length; i++){
System.out.println ("myScore [" + i + "] = " + myScore [i]);
}
System.out.println( "Done!" );
}
}
I did swap the myMax and myMin (you can see in the code),but in the
array, it just wouldn't swap.

Explanation / Answer

You must save the indices of the locations of the smallest andlargest elements. Your current code saves the actual values, and swapping themdoesn't affect the array. The following code modifies the array when swapping: int i; int small; int large; int temp; int [] myScore = new int[10]; // Randomise the array for (i = 0; i