Ask Question
11 November, 16:40

Write a program whose input is two integers and whose output is the two integers swapped. Place the values in an array, where x is position 0 and y is position 1. Ex: If the input is: 3 8 then the output is: 8 3 Your program must define and call a method: public static void swapValues (int[] values)

+2
Answers (1)
  1. 11 November, 17:32
    0
    import java. util. Arrays;

    public class swap{

    public static void main (String []args) {

    int [] arr = {2,4};

    swapValues (arr);

    }

    public static void swapValues (int[] values) {

    int temp;

    System. out. println (Arrays. toString (values));

    temp=values[0];

    values[0]=values[1];

    values[1] = temp;

    System. out. println (Arrays. toString (values));

    }

    }

    Explanation:

    In the program above, we created the method swapValues that receives an array of integers as parameters. in the method definition, we created a temp variable that is used to swapp the element at index 0 and index 1. Java's Arrays. to string method is used to print the array before and after the swap.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program whose input is two integers and whose output is the two integers swapped. Place the values in an array, where x is position ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers