Question

In: Computer Science

FOR JAVA (ZYBOOK) Write a method swapArrayEnds() that swaps the first and last elements of its...

FOR JAVA (ZYBOOK) 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}.

I can't modify/change any of the code. I can only add code to implement this where it says /* Your solution goes here */.

This is the code:

import java.util.Scanner;

public class ModifyArray {

/* Your solution goes here */

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("");
}
}

Solutions

Expert Solution

import java.util.Scanner;

public class ModifyArray {

    /* Your solution goes here */
    private static void swapArrayEnds(int[] sortArray) {
        // Storing first element to tmp
        int tmp = sortArray[0];
        // Replacing first element to last element
        sortArray[0] = sortArray[sortArray.length-1];
        // Replacing last element with value of variable tmp
        sortArray[sortArray.length-1] = tmp;
    }

    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("");
    }


}


Related Solutions

swapArrayEnds(int[] nums): swaps the first and last elements of its array parameter. Ex: {10, 20, 30,...
swapArrayEnds(int[] nums): swaps the first and last elements of its array parameter. Ex: {10, 20, 30, 40} becomes {40, 20, 30, 10}. removeTen(int[] nums): returns a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces at the end of the array should be set to 0. Ex: {1, 10, 10, 2} yields {1, 2, 0, 0}. You can make...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method...
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method has the following header(signature): public static void removeDuplicate(ArrayList<Integer> list) Write a test program (with main method) that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is what the input and output should look like:      Enter ten integers: 28 4 2 4 9 8 27 1 1 9      The distinct...
Write a program to swap the first and last elements of a linked list. (i) by...
Write a program to swap the first and last elements of a linked list. (i) by exchanging info part (ii) through pointers Need the program in java with no direct usage of packages use node and link.
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
Java Write a method, makeUserName, that is passed two Strings and an int: the first is...
Java Write a method, makeUserName, that is passed two Strings and an int: the first is a first name, the second is a last name, and the last is a random number. The method returns a user name that contains the last character of the first name, the first five characters of the last name (assume there are five or more characters in last name), and the random number. An example: Assume that “John”, “Smith”, 45, are passed when the...
An increasing-order integer array may contain duplicate elements. Write a Java method that does a binary...
An increasing-order integer array may contain duplicate elements. Write a Java method that does a binary search through the array for finding a specific target and return an array of two integer indices that specifies the close interval of indices at which the target is found or null if the target is not present. Use this test driver to test public class MultBS { int [] multBinarySearch(int [] x, int target) { // your codes go here } public static...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT