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}.

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter9: Using Classes And Objects
Section: Chapter Questions
Problem 1CP: In previous chapters, you have created programs for the Greenville Idol competition. Now create a...
icon
Related questions
Question

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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT