Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}.
import java.util.Scanner;
public class ModifyArray {
public swapArrayEnds(10, 20, 30, 40) {
for (i = 0; i < (arrVals.length / 2); ++i) {
tempValue = arrVals[i]; // Do swap //*please fix code//
arrVals[i] = arrVals[arrVals.length - 1 - i];
arrVals[arrVals.length - 1 - i] = tempValue;
Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}.
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int numElem = 4;
int[] sortArray = new int[numElem];
int i;
int userNum;
for (i = 0; i < sortArray.length; ++i) {
sortArray[i] = scnr.nextInt();
}
swapArrayEnds(sortArray);
for (i = 0; i < sortArray.length; ++i) {
System.out.print(sortArray[i]);
System.out.print(" ");
}
System.out.println("");
}
}
![CTU Class Homepage.
H
zy Section 4.4 - CS 226T: Java Progra X
✰ learn.zybooks.com/zybook/CS226-2204A-03-2204A/chapter/4/section/4
=zyBooks My library > CS 226T: Java Programming home > 4.4: Array parameters
Run
Thank you. Your reservation is co X
ART
Common student error
Your code used a for loop. Instead, swapping the first and last elements requires a temporary variable, not a loop.
Failed to compile
ModifyArray.java:5: error: invalid method declaration; return type required
public swapArrayEnds (10, 20, 30, 40) {
Print - Your Confirmed Reservatic X b Answered: 4.2.3: Method definiti X
ModifyArray.java:5: error: illegal start of type
public swapArrayEnds (10, 20, 30, 40) {
3 errors
ModifyArray.java:12: error: illegal start of expression
public static void main (String [] args) {
How was this section?
Note: Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often
don't recognize the problem until reaching a later line.
Provide feedback
zyBooks catalog
Activity summary for assignment: Unit 4 Interactive Assignment
Due: 10/31/2022, 3:59 PM -08
Feedback?
? Help/FAQ
70 %
70% submitted to
Stacey Queen
X
:
9:11 AM
8/30/2022](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F788fc729-a951-45c4-9909-47eea7bbf82f%2F068e5bb6-79ec-4ded-92a1-143614a71080%2Fpxc0v1_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)