Define a method named swapValues that takes an array of four integers as a parameter, swaps array elements at indices 0 and 1, and swaps array elements at indices 2 and 3. Then write a main program that reads four integers from input and stores the integers in an array in positions 0 to 3. The main program should call function swapValues() to swap array's values and print the swapped values on a single line separated with spaces.

Respuesta :

The program is an illustration of arrays.

Arrays are used to hold multiple values.

The program in java, where comments are used to explain each line is as follows:

import java.util.*;

public class Main{

   //This defines the method

public static int[] swapValues(int[] arr) {

   //This swaps the first and second array elements

       int temp = arr[0];

       arr[0] = arr[1];   arr[1] = temp;

   //This swaps the third and fourth array elements

       temp = arr[2];

       arr[2] = arr[3];   arr[3] = temp;

   //This returns the swapped array to main

       return arr;

}

//The main method begins here

   public static void main(String[] args) {

       //This creates a Scanner object

       Scanner input = new Scanner(System.in);

 //This declares an array of 4 elements

 int[] intArray = new int[4];

 //This gets input for the array

 for(int i = 0; i<4;i++){

     intArray[i] = input.nextInt();

 }

 //This calls the swapValues method

 intArray=swapValues(intArray);

 //This prints the swapped array

 for (int i = 0; i < 4; i++){

 System.out.print( intArray[i]+ " ");     }

}  

}

At the end of the program, the elements are swapped and printed.

Read more about similar programs at:

https://brainly.com/question/14017034

Answer:

import java.util.Scanner;

public class LabProgram {

 

  public static void swapValues(int[] values) {

 int temporaryHolder;

 temporaryHolder = values[0];

     values[0] = values[1];

     values[1] = temporaryHolder;

      temporaryHolder = values[2];

     values[2] = values[3];

     values[3] = temporaryHolder;

     

}

 

  public static void main(String[] args) {

    Scanner scnr = new Scanner(System.in);

     int[] values = new int[4];

         values[0] = scnr.nextInt();

         values[1] = scnr.nextInt();

           values[2] = scnr.nextInt();

           values[3] = scnr.nextInt();

         swapValues(values);

         

        System.out.println(values[0] + " " + values[1] +  " " + values[2]  + " " + values[3]);

        scnr.close();

  }

}

Explanation:

this is the Zybooks version

ACCESS MORE
EDU ACCESS