Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming /* Method that returns a char array in reverse order * */ publi

ID: 3573543 • Letter: J

Question

Java programming

/* Method that returns a char array in reverse order
*
*/

public class ReturnReverseArray
{
public static void main( String [] args )
{
    char [] array = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

    System.out.print( "The elements are " );
    for ( int i = 0; i < array.length; i++ )
       System.out.print( array[i] + " " );
    System.out.println( );

    char [] arrayReversed = arrayReverse( array );

    System.out.print( "The elements in reverse order are " );
    for ( int i = 0; i < arrayReversed.length; i++ )
           System.out.print( arrayReversed[i] + " " );
    System.out.println( );
}

// Insert your code here

}

Explanation / Answer

public class ReturnReverseArray {

   public static void main(String[] args) {

       char[] array = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

       System.out.print("The elements are ");

       for (int i = 0; i < array.length; i++)

           System.out.print(array[i] + " ");

       System.out.println();

       char[] arrayReversed = arrayReverse(array);

       System.out.print("The elements in reverse order are ");

       for (int i = 0; i < arrayReversed.length; i++)

           System.out.print(arrayReversed[i] + " ");

       System.out.println();

   }

   private static char[] arrayReverse(char[] array) {

       char[] reversed = new char[array.length];

       for (int i = 0; i < array.length; i++) {

           reversed[i] = array[array.length - 1 - i];

       }

       return reversed;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote