Question: Four integers are read from input and stored into the array arrayToModify. Write a static method swapArrayPos ( ) that takes an integer array parameter

Four integers are read from input and stored into the array arrayToModify. Write a static method swapArrayPos() that takes an integer array parameter and swaps the second-to-last element with the last element of the array.
Ex: If the input is 40107545, then the output is:
Original array: 40107545 Changed array: 40104575
import java.util.Scanner;
public class ArrayParameterReferences {
/* Your code goes here */
public static void printArr(int[] arr){
int i;
for (i =0; i < arr.length; ++i){
System.out.print(arr[i]+"");
}
System.out.println();
}
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
final int NUM_ELEM =4;
int[] arrayToModify = new int[NUM_ELEM];
int i;
int userNum;
for (i =0; i < arrayToModify.length; ++i){
arrayToModify[i]= scnr.nextInt();
}
System.out.print("Original array: ");
printArr(arrayToModify);
swapArrayPos(arrayToModify);
System.out.print("Changed array: ");
printArr(arrayToModify);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!