Write a recursive method to reverse the values stored in an integer array. Place
ID: 646499 • Letter: W
Question
Write a recursive method to reverse the values stored in an integer array. Place the method where indicated in the following program. Run the program and paste a screenshot of the output.
package um.csc276.HW6_10;
// Import classes
import java.util.Random;
//==============================================================
// class HW6_10
//==============================================================
public class HW6_10
{
//--------------------------------------------------------------------
// randomizeArray
//--------------------------------------------------------------------
private static void randomizeArray (int[] arr, int upperLimit)
{
// Loop to generate and store in array random numbers from 1 to upperLimit
Random rand = new Random();
for (int i = 0; i < arr.length; i++)
arr[i] = rand.nextInt(upperLimit) + 1;
}
//--------------------------------------------------------------------
// printArray
//--------------------------------------------------------------------
private static void printArray (String heading, int[] arr)
{
// Loop to print array numbers
System.out.print(heading + ": [");
for (int i = 0; i < arr.length; i++)
System.out.printf("%3d", arr[i]);
System.out.println("]");
}
//----------------------------------------------------------------
// reverseArray
//----------------------------------------------------------------
private static int[] reverseArray (int[] arr)
{
YOUR CODE HERE
}
//----------------------------------------------------------------
// main
//----------------------------------------------------------------
public static void main (String[] args)
{
// Declare constants
final int ARRAY_SIZE = 8;
// Declare variables
int[] arr = new int[ARRAY_SIZE];
// Show application header
System.out.println("Welcome to Array Reverser");
System.out.println("--------------------------");
// Place random numbers in and print array
randomizeArray(arr, 99);
printArray (" Original array", arr);
// Reverse and print array
arr = reverseArray(arr);
printArray ("Reversed array", arr);
// Show application close
System.out.println(" End of Array Reverser");
}
}
[your program output here]
Explanation / Answer
import java.util.*;
public class Random
{
public static void reverseDisplay(int n)
{
int newNum = n/10;
if(newNum == 0)
System.out.print(n);
else
{
System.out.print(n%10);
reverseDisplay(newNum);
}
}
public static void main(String[] args)
{
reverseDisplay(54321);
System.out.println();
reverseDisplay(12345);
System.out.println();
reverseDisplay(91);
System.out.println();
reverseDisplay(19);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.