Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java program: Create an Arrays.java class in a folder called lab1 (do not use packages) Using Java, implement the method Arrays.swapLargestAndSmallest(int[] a) that swaps the
Java program:
- Create an Arrays.java class in a folder called lab1 (do not use packages)
- Using Java, implement the method Arrays.swapLargestAndSmallest(int[] a) that swaps the largest and smallest element in a. If there are multiple largest or smallest elements, pick the first one.
- Run the following code to test your program.
public class Tester { public static void main(String[] args) { int[] a = new int[] { 5, 4, 1, 9, 2, 6 }; Arrays.swapLargestAndSmallest(a); System.out.println(java.util.Arrays.toString(a)); System.out.println("Expected: [5, 4, 9, 1, 2, 6]"); a = new int[] {1, 4, 1, 9, 2, 6}; Arrays.swapLargestAndSmallest(a); System.out.println(java.util.Arrays.toString(a)); System.out.println("Expected: [9, 4, 1, 1, 2, 6]"); a = new int[] {1, 4, 2, 9, 9, 6}; Arrays.swapLargestAndSmallest(a); System.out.println(java.util.Arrays.toString(a)); System.out.println("Expected: [9, 4, 2, 1, 9, 6]"); a = new int[] {1}; Arrays.swapLargestAndSmallest(a); System.out.println(java.util.Arrays.toString(a)); System.out.println("Expected: [1]"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started