/*
* Name: Kristina Brend
* Login: cs11fiv
* Date: November, 14th, 2012
* File: ReverseRecurse.java
* Sources of Help: TA's in lab
*
* This is a program that reads user input and makes an array of values, the
* size is specified by the user and this program reverses the elements in the
* array through two different recursive methods.
*/
import java.util.*;
/*
*Class name: ReverseRecurse
*Purpose: This is class reverses an array based on user input
* */
public class ReverseRecurse
{
//Instance variables
int[] array;
int[] arrayCopy;
int[] arrayAdjusted;
/*
* Name: initArray
* Purpose: Scan's input from the user en uses the size to make an array of
* that size, and the numbers gets put into the array
* Parameters:
* Return: The array made from user input
*/
public int[] initArray()
{
//asks user for input
System.out.println ("Maximum number of integers you wish to enter?");
Scanner input = new Scanner (System.in);
int numOfInts =0;
//checks if there is an integer entered
if (input.hasNextInt())
{
numOfInts = input.nextInt();
//checks for < = 0 condition
while(numOfInts <= 0)
{
System.out.println("You must enter a value > 0, try again.");
numOfInts = input.nextInt();
}
}
//program stops with the system exit
else
{
// System.out.println("Empty Array");
System.exit (1);
}
// input.hasNextInt();
// input.nextInt();
//creates an array of the size the user decides
array = new int[numOfInts];
System.out.println("Enter up to " +numOfInts +" integers");
/* //stores user inputted values into the array
if (input.hasNextInt())
{
for (int index =0; index < numOfInts; index++)
{
array[index] = input.nextInt();
}
}
else if(not a next int)
{
numOfInts = index + 1
array.setSize(numofints)
}
else
{
// System.out.println("Empty Array");
//checks condition if non int is entered and makes an array of length 0
int indexx =0;
arrayCopy = new int[indexx];
System.arraycopy( array, 0, arrayCopy, 0 , indexx);
array = arrayCopy;
} */
int arraySize = 0;
while(arraySize < numOfInts && input.hasNextInt())
{
array[arraySize] = input.nextInt();
arraySize++;
}
if(arraySize != numOfInts)
{
arrayAdjusted = new int[arraySize];
System.arraycopy(array, 0, arrayAdjusted, 0, arrayAdjusted.length);
return arrayAdjusted;
}
else
{
int index = 0;
arrayCopy = new int[index];
System.arraycopy( array, 0, arrayCopy, 0 , index);
array[arraySize]
}
return array;
}
/*
* Name: printArray
* Purpose: Goes through the array and prints out each element on a line with
* space in between. Prints Empty array if theres no elements in the array.
* Parameters: Array of numbers, ints
* Return: void.
*/
public void printArray(int[] array)
{
if(array.length > 0)
{
for(int index = 0; index < array.length; index++)
{
System.out.print(array[index] + " ");
}
System.out.println("");
}
else
{
System.out.println("Empty Array");
}
}
/*
* Name: reverse
* Purpose: Uses the array, nad the lowest and the highest index to swap
* the first and last element in the array directly. Then we call the method
* again on the rest of the array.
* Parameters: int[] originalArray, int low, int high
* Return: void.
*/
public void reverse(int[] originalArray,int low, int high)
{
if(high == low)
{
originalArray[low] = originalArray[high];
}
else if((low + 1) == high)
{
int temp;
temp = originalArray[low];
originalArray[low] = originalArray[high];
originalArray[high] = temp;
}
else if(originalArray.length == 0)
{
}
else
{
int temp;
temp = originalArray[low];
originalArray[low] = originalArray[high];
originalArray[high] = temp;
reverse(originalArray, ++low, --high);
}
}
public int[] reverse(int[] originalArray){
//new array with swapped values created with same length as original
int newArraySwap[];
newArraySwap = new int[originalArray.length];
//new int array created that holds the remainder of the values
int newArrayHolder[];
//if the swapped array has length 1, it returns the original array
if (newArraySwap.length ==1)
{
return originalArray;
}
//if the new array has lenght 0, the array is returned
else if (newArraySwap.length == 0)
{
return newArraySwap;
}
//calls the method again till the array is properly swapped
else
{
newArraySwap[0] = originalArray[originalArray.length-1];
newArraySwap[originalArray.length-1] = originalArray[0];
int newArrayMiddle[];
newArrayMiddle = new int[originalArray.length -2];
System.arraycopy(originalArray, 1, newArrayMiddle, 0,
newArrayMiddle.length);
newArrayHolder = reverse(newArrayMiddle);
System.arraycopy( newArrayHolder, 0, newArraySwap, 1,
newArrayHolder.length);
}
return newArraySwap;
}
}