Question: The task here is to swap the first two elements of an array. This should be familiar, but this time it's in an array. We're
The task here is to swap the first two elements of an array. This should be familiar, but this time it's in an array.
We're also introducing something that looks a little different and you will write your code inside a method other than main. This doesn't really change anything, and main has been set up so that you can still run it, and it works very similarly to the exercises you have been working on.
In the method swap(int[]), you receive an array of ints called numbers. The task is to swap the first two elements of this array. The array is then returned as the result of the method (but you don't have to handle this part, just the swap itself).
/** * Mastery Test Week 3 Exercise 1. Thread 1. * - Swap two numbers. * * @author (your name) */ import java.util.Arrays; public class Swap { public static int[] swap(int[] numbers) { // In the coming weeks, the lecturer will explain all of the code above. But for now here is an explanation: // 1. In the code above we have provided an integer array "numbers", containing two elements. // In the space provided below, write code to swap the values in "numbers[0]" and "numbers[1]". // Do NOT initialise "numbers[0]" or "numbers[1]". Just write the three lines that swap the values. // Add your code BELOW this line. Do NOT change anything ABOVE this comment line. // Add your code ABOVE this line. Do NOT change anything BELOW this comment line. return numbers; // Do NOT chage this line of code } // do NOT change anything below this line public static void main(String[] args) { int[] numbers = {1, 2}; System.out.println("initially the array was: " + Arrays.toString(numbers)); System.out.println("After calling Swap method, the array is: " +Arrays.toString(swap(numbers))); } }
need solution please
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
