Question

Create a class called Reverse that reverses an array of integers. The class should have two...

Create a class called Reverse that reverses an array of integers.

The class should have two method:

- public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped.

- private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time, with the startIndex incremented by one and the endIndex decremented by one.

The termination condition should occur when startIndex is equal to or greater than the endIndex, because this indicates all the elements in the array have been swapped.

Create a second class called ReverseTest that contains the main method, and tests the Reverse.reverse method. The method should be called twice, once with an array containing an odd number of elements, and once with an array containing an even number of elements. The test class does not need to ask for input from the user of the class, nor does it need to do any additional error checking.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Reverse {

    public static void reverse(int [] array) {
        reverseRecursive(array, 0, array.length-1);
    }

    private static void reverseRecursive(int [] array, int startIndex, int endIndex) {
        if(startIndex < endIndex) {
            int temp = array[startIndex];
            array[startIndex] = array[endIndex];
            array[endIndex] = temp;
            reverseRecursive(array, startIndex+1, endIndex-1);
        }
    }

}

class ReverseTest {

    private static void print(int[] array) {
        for(int i = 0; i < array.length; ++i) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr1 = {3, 9, 1, 4, 5};
        System.out.print("Original array: ");
        print(arr1);
        Reverse.reverse(arr1);
        System.out.print("Reversed array: ");
        print(arr1);
        System.out.println();

        int[] arr2 = {8, 2, 1, 6, 5, 0};
        System.out.print("Original array: ");
        print(arr2);
        Reverse.reverse(arr2);
        System.out.print("Reversed array: ");
        print(arr2);
        System.out.println();
    }

}

original array: 3 9145 Reversed array: 5 4 193 Original azray: 8 2165 Reversed array: 0 5 612 8

\color{blue}Hey,\;Please\;let\;me\;know\;if\;you\;need\;me\;to\;make\;any\;changes. \\ I\;would\;really\;appreciate\;an\;upvote.\;Thank\;you!

Add a comment
Know the answer?
Add Answer to:
Create a class called Reverse that reverses an array of integers. The class should have two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Create a new class called ReversibleArray. 2. In the class header, add the code <T>...

    1. Create a new class called ReversibleArray. 2. In the class header, add the code <T> immediately to the right of the class name. 3. Give this class two private instance variables: T[] array and int count 4. Create a constructor which takes in one parameter of type T[] and assign that parameter's value into this.array. Set count as the length of the array. 5. Add a toString() method that outputs the array values in the format: elem0, elem1, elem2,...

  • Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...

    Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...

  • Assignment #9 will be the construction of a program that reads in a sequence of integers...

    Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers. Then compute the minimum number, compute the largest number among the numbers that are divisible by 2, count even numbers, and compute the...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • Create a class called CompareArrays that determines if two specified integer arrays are equal. The class...

    Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. Create a second class called CompareArraysTest that contains...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Create a class called Play that has an InputReader as an instance variable. Be sure to...

    Create a class called Play that has an InputReader as an instance variable. Be sure to initialize it in the constructor. The class has two methods. Write a method with this signature: Write a method with this signature: public void stringPlay() The method prompts the user for a string, reads it in, and then displays the string as many times as the length of that string. The output string should be formatted with the first letter uppercase and the rest...

  • Public class Permutations { // Helper method for outputting an array. private static v...

    public class Permutations { // Helper method for outputting an array. private static void PrintArray(string[] array) { foreach (string element in array) { Console.Write($"{element} "); } Console.WriteLine(); } // Helper method for invoking Generate. private static void Generate(string[] array) { Generate(array.Length, array); } public static void Main(string[] args) { } } procedure generate(k : integer, A : array of any): if k = 1 then output(A) else // Generate permutations with kth unaltered // Initially k == length(A) generate(k -...

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT